1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/agendav_ynh.git synced 2024-09-03 20:36:12 +02:00
agendav_ynh/scripts/upgrade

128 lines
4.4 KiB
Bash

#!/bin/bash
set -eu
# Source common variables and helpers
source ./_common.sh
# Set app specific variables
app="$YNH_APP_INSTANCE_NAME"
dbname=$app
dbuser=$app
# Retrieve arguments
domain=$(ynh_app_setting_get "$app" domain)
path=$(ynh_app_setting_get "$app" path)
path=${path%/}
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
encryptkey=$(ynh_app_setting_get "$app" encryptkey)
language=$(ynh_app_setting_get "$app" language)
# Set and store language
if [[ -z "$language" ]]; then
# retrieve language the old way
lang=${LANG/.*/}
[[ ${LANGUAGES[$lang]+_} ]] || lang="en"
# retrieve and store the relevant language
language=${LANGUAGES[$lang]}
ynh_app_setting_set "$app" language "$language"
fi
# Define LOGDIR (create it later when user is created)
LOGDIR=/var/log/$app
# Check destination directory
DESTDIR="/var/www/$app"
[[ ! -d $DESTDIR ]] && ynh_die \
"The destination directory '$DESTDIR' does not exist.\
The app is not correctly installed, you should remove it first."
# Check whether Baïkal or Radicale is installed
if sudo yunohost app list --installed -f baikal | grep -q id ; then
caldav_app="baikal"
caldav_baseurl="/cal.php/"
elif sudo yunohost app list --installed -f radicale | grep -q id ; then
caldav_app="radicale"
caldav_baseurl="/"
else
ynh_die "You must install Baïkal or Radicale before"
fi
# Install dependencies
ynh_package_is_installed "php5-cli" \
|| ynh_package_install "php5-cli"
# Create tmp directory and fetch app inside
TMPDIR=$(mktemp -d)
extract_agendav "$TMPDIR"
# Copy and set AgenDAV configuration
conf_path="${TMPDIR}/web/config/settings.php"
cp ../conf/settings.php "$conf_path"
sed -i "s/{DBUSER}/${dbuser}/g" "$conf_path"
sed -i "s/{DBPASS}/${dbpass}/g" "$conf_path"
sed -i "s/{DBNAME}/${dbname}/g" "$conf_path"
sed -i "s/{ENCRYPTKEY}/${encryptkey}/g" "$conf_path"
sed -i "s@{LOGDIR}@${LOGDIR}@g" "$conf_path"
sed -i "s@{TIMEZONE}@$(cat /etc/timezone)@g" "$conf_path"
sed -i "s@{LANGUAGE}@${language}@g" "$conf_path"
# CalDAV config
caldav_domain=$(ynh_app_setting_get "$caldav_app" domain)
caldav_path=$(ynh_app_setting_get "$caldav_app" path)
caldav_url="https://${caldav_domain}${caldav_path%/}"
sed -i "s@{CALDAV_BASEURL}@${caldav_url}${caldav_baseurl}@g" "$conf_path"
sed -i "s@{CALDAV_DOMAIN}@${caldav_domain}@g" "$conf_path"
# Replace files and set permissions
sudo rm -rf "$DESTDIR"
sudo mv "$TMPDIR" "$DESTDIR"
if ! id -u $app > /dev/null 2>&1 ; then
sudo useradd -c "$app system account" \
-d /var/www/$app --system --user-group $app --shell /usr/sbin/nologin \
|| ynh_die "Unable to create $app system account"
fi
# Protect source code against modifications
sudo find "${DESTDIR}" -type f -exec chown root:root {} \; -exec chmod 644 {} \;
sudo find "${DESTDIR}" -type d -exec chown root:root {} \; -exec chmod 755 {} \;
# Only agendav user should write here
sudo chown -hR $app: "${DESTDIR}/web/var/cache/"{profiler,twig}
sudo chmod -R 750 "${DESTDIR}/web/var/cache/"{profiler,twig}
# The agendav user should read here, but does not need to write
# Other users should not be able to read as it stores passwords.
sudo find "${DESTDIR}/web/config" -type f -exec chown root:$app {} \; -exec chmod 640 {} \;
sudo find "${DESTDIR}/web/config" -type d -exec chown root:$app {} \; -exec chmod 750 {} \;
# Create log directory
sudo install -m 750 -o $app -g adm -d "$LOGDIR"
# Run database migrations
(cd "$DESTDIR" && sudo sudo -u $app \
php agendavcli migrations:migrate --no-interaction) \
|| ynh_die "Unable to run AgenDAV database migration"
# Copy and set nginx configuration
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
sed -i "s@{PATH}@${path}@g" ../conf/nginx.conf
sed -i "s@{LOCATION}@${path:-/}@g" ../conf/nginx.conf
sed -i "s@{DESTDIR}@${DESTDIR}@g" ../conf/nginx.conf
sed -i "s@{POOLNAME}@${app}@g" ../conf/nginx.conf
# comment redirection in case of an installation at root
[[ -n "$path" ]] || sed -i '$s/^/#/' ../conf/nginx.conf
sudo cp ../conf/nginx.conf "$nginx_conf"
# Copy and set php-fpm configuration
phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf"
sed -i "s@{POOLNAME}@${app}@g" ../conf/php-fpm.conf
sed -i "s@{DESTDIR}@${DESTDIR}/@g" ../conf/php-fpm.conf
sed -i "s@{USER}@${app}@g" ../conf/php-fpm.conf
sed -i "s@{GROUP}@${app}@g" ../conf/php-fpm.conf
sudo cp ../conf/php-fpm.conf "$phpfpm_conf"
# Reload services
sudo service php5-fpm restart || true
sudo service nginx reload || true