1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/nextcloud_ynh.git synced 2024-09-03 19:55:57 +02:00
nextcloud_ynh/scripts/upgrade
2017-01-20 18:12:36 +01:00

244 lines
11 KiB
Bash
Executable file

#!/bin/bash
# Load common variables and helpers
source ./_common.sh
# Source app helpers
source /usr/share/yunohost/helpers
# Set app specific variables
app=$APPNAME
real_app=$YNH_APP_INSTANCE_NAME # real_app prendra la valeur nextcloud ou owncloud dans le cas d'une migration
dbname=$app
dbuser=$app
# Backup the current version of the app, restore it if the upgrade fails
backup_number=1
old_backup_number=2
if sudo yunohost backup list | grep -q $real_app-before-upgrade1 > /dev/null 2>&1; then # Vérifie l'existence d'une archive déjà numéroté à 1.
backup_number=2 # Et passe le numéro de l'archive à 2
old_backup_number=1
fi
ynh_app_setting_set $app backup_core_only 1 # Indique au backup/restore de ne pas sauvegarder le dossier de data, uniquement le core de nextcloud.
if [ "$(cat /var/www/$real_app/version.php | grep OC_VersionString | cut -d\' -f2)" \> "11.0.0" ]
then # Pas de backup avant la version suivant 11.0.0 (soit 11.0.1), car le backup incluera les dossiers data.
sudo yunohost backup create --ignore-hooks --apps $real_app --name $real_app-before-upgrade$backup_number # Créer un backup différent de celui existant.
if [ "$?" -eq 0 ]; then # Si le backup est un succès, supprime l'archive précédente.
if sudo yunohost backup list | grep -q $real_app-before-upgrade$old_backup_number > /dev/null 2>&1; then # Vérifie l'existence de l'ancienne archive avant de la supprimer, pour éviter une erreur.
sudo yunohost backup delete $real_app-before-upgrade$old_backup_number > /dev/null
fi
else # Si le backup a échoué
ynh_die "Backup failed, the upgrade process was aborted."
fi
fi
EXIT_PROPERLY () {
exit_code=$?
if [ "$exit_code" -eq 0 ]; then
ynh_app_setting_delete $real_app backup_core_only
exit 0 # Quitte sans erreur si le script se termine correctement.
fi
trap '' EXIT
set +eu
echo "Upgrade failed." >&2
if sudo yunohost backup list | grep -q $real_app-before-upgrade$backup_number > /dev/null 2>&1; then # Vérifie l'existence de l'archive avant de supprimer l'application et de restaurer
sudo yunohost app remove $real_app # Supprime l'application avant de la restaurer.
sudo yunohost backup restore --ignore-hooks $real_app-before-upgrade$backup_number --apps $real_app --force # Restore the backup if upgrade failed
ynh_die "The app was restored to the way it was before the failed upgrade."
fi
}
set -eu
trap EXIT_PROPERLY EXIT
# Migrate from ownCloud to Nextcloud
if [[ $YNH_APP_INSTANCE_NAME != $app ]]; then # Si le nom de l'app donné lors de la commande n'est pas nextcloud, vérifie si c'est owncloud pour lancer la migration.
[[ $YNH_APP_ID == owncloud ]] \
|| ynh_die "Incompatible application to migrate to Nextcloud"
# real_app=$YNH_APP_INSTANCE_NAME # real_app prend le nom de owncloud.
./upgrade.d/owncloud.sh # Prépare la migration de owncloud vers nextcloud.
else
real_app=$app
# handle old migrations from ownCloud
curr_dbname=$(sudo cat "/var/www/${app}/config/config.php" \
| grep dbname | sed "s|.*=> '\(.*\)'.*|\1|g") # Prend le nom de la bdd dans le fichier de config
if [[ $curr_dbname != $dbname ]]; then # Si le nom de la base de donnée n'est pas nextcloud, renomme la base de donnée.
curr_dbuser=$(sudo cat "/var/www/${app}/config/config.php" \
| grep dbuser | sed "s|.*=> '\(.*\)'.*|\1|g") # Prend le nom d'utilisateur de la bdd
dbpass=$(ynh_app_setting_get "$real_app" mysqlpwd)
# rename the MySQL database
rename_mysql_db "$curr_dbname" "$curr_dbuser" "$dbpass" "$dbname" "$dbuser"
sudo sed -ri "s#^(\s*'dbname' =>).*,#\1 '${dbname}',#" \
"/var/www/${app}/config/config.php"
sudo sed -ri "s#^(\s*'dbuser' =>).*,#\1 '${dbuser}',#" \
"/var/www/${app}/config/config.php"
fi
fi
# Retrieve app settings
domain=$(ynh_app_setting_get "$real_app" domain)
path=$(ynh_app_setting_get "$real_app" path)
path=${path%/}
dbpass=$(ynh_app_setting_get "$real_app" mysqlpwd)
user_home=$(ynh_app_setting_get "$real_app" user_home)
# 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 app's data directory
DATADIR="/home/yunohost.app/${app}/data"
[[ ! -d $DATADIR ]] && ynh_die \
"The data directory '$DATADIR' does not exist.\
The app is not correctly installed, you should remove it first."
# Upgrade dependencies
ynh_package_install_from_equivs ../conf/${DEPS_PKG_NAME}.control \
|| ynh_die "Unable to upgrade dependencies"
# FIXME: Delete current nginx configuration to be able to check if
# .well-known is already served. See https://dev.yunohost.org/issues/400
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf"
sudo rm -f "$nginx_conf"
sudo service nginx reload
# Copy and set nginx configuration
sed -i "s@#APP#@${app}@g" ../conf/nginx.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
# do not serve .well-known if it's already served on the domain
if is_url_handled "https://${domain}/.well-known/caldav" ; then
sed -ri '/^location = \/\.well\-known\/(caldav|carddav) \{/,/\}/d' \
../conf/nginx.conf
fi
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@#USER#@${app}@g" ../conf/php-fpm.conf
sed -i "s@#GROUP#@${app}@g" ../conf/php-fpm.conf
sed -i "s@#POOLNAME#@${app}@g" ../conf/php-fpm.conf
sed -i "s@#DESTDIR#@${DESTDIR}/@g" ../conf/php-fpm.conf
sudo cp ../conf/php-fpm.conf "$phpfpm_conf"
sudo chown root: $phpfpm_conf
sudo chmod 644 $phpfpm_conf
# Set system group in hooks
sed -i "s@#GROUP#@${app}@g" ../hooks/post_user_create
# occ helper for the current installation
_exec_occ() {
exec_occ "$DESTDIR" "$app" $@ # Appel de php occ avec les droits de l'user nextcloud. A noter que ce n'est là que la déclaration de la fonction qui sera appelée plus tard.
}
# Effectue les mises à majeures une à une. Le saut de mise à jour n'étant pas supporté.
major_version=${LAST_VERSION%%.*} # La version majeure correspond à la première partie du numéro de version.
actual_version=$(cat "$DESTDIR/version.php" | grep OC_VersionString | cut -d\' -f2) # Relève le numéro de version de l'instance nextcloud installée
actual_major_version=${actual_version%%.*}
_exec_occ -V # Affiche la version
while [ "$major_version" -ne "$actual_major_version" ]; do # Si la version majeure actuelle ne correspond à la dernière version majeure.
./upgrade.d/upgrade.$actual_major_version.sh # Exécute la mise à jour vers la version majeure suivante
actual_version=$(cat "$DESTDIR/version.php" | grep OC_VersionString | cut -d\' -f2) # Relève le nouveau numéro de version, après mise à jour.
actual_major_version=${actual_version%%.*}
_exec_occ -V # Affiche la version
done
if [ "$LAST_VERSION" != "$actual_version" ] # Si la version actuelle ne correspond à la dernière version, une mise à jour est nécessaire pour passer à la dernière version mineure.
then
./upgrade.d/upgrade.last.sh # Exécute la mise à jour vers la dernière version prise en charge.
fi
# Ensure that UpdateNotification app is disabled
_exec_occ app:disable updatenotification
# Enable plugins and set Nextcloud configuration
_exec_occ app:enable user_ldap
nc_conf="${DESTDIR}/config.json"
_exec_occ config:import "$nc_conf"
sudo rm -f "$nc_conf"
# Guess user_home value if empty
if [[ -z "${user_home:-}" ]]; then # user_home correspond au champs "Access the users home folder from Nextcloud?" du manifest
sudo cat "${DATADIR}/mount.json" >/dev/null 2>&1 \
&& user_home=1 \
|| user_home=0 # Test l'existence du fichier mount.json pour connaître la valeur de user_home, dans le cas où la valeur ne serait pas renseignée. (Mais ce fichier semble ne plus exister...)
ynh_app_setting_set "$real_app" user_home "$user_home"
fi
# Enable External Storage and create local mount to home folder as needed
if [[ ${user_home} -eq 1 ]]; then
_exec_occ app:enable files_external
_exec_occ files_external:list --output=json \
| grep -q '"storage":"\\\\OC\\\\Files\\\\Storage\\\\Local"' \
|| create_home_external_storage '_exec_occ'
fi
# Add dynamic logout URL to the config
# TODO: if changes are made to this section, replace it with new one.
_exec_occ config:system:get logout_url >/dev/null 2>&1 \
|| sudo su -c "echo \"
//-YunoHost-
// set logout_url according to main domain
\\\$main_domain = exec('cat /etc/yunohost/current_host');
\\\$CONFIG['logout_url'] = 'https://'.\\\$main_domain.'/yunohost/sso/?action=logout';
//-YunoHost-
\" >> ${DESTDIR}/config/config.php" -- $app
# Iterate over users to extend their home folder permissions - for the external
# storage plugin usage - and create relevant Nextcloud directories
for u in $(ynh_user_list); do
sudo mkdir -p "${DATADIR}/${u}"
sudo setfacl -m g:$app:rwx "/home/$u" || true
done
# Fix app ownerships & permissions
sudo find ${DESTDIR}/ -type f -print0 | sudo xargs -0 chmod 0644
sudo find ${DESTDIR}/ -type d -print0 | sudo xargs -0 chmod 0755
sudo find ${DATADIR}/ -type f -print0 | sudo xargs -0 chmod 0640
sudo find ${DATADIR}/ -type d -print0 | sudo xargs -0 chmod 0750
sudo chmod 640 "${DESTDIR}/config/config.php"
sudo chmod 755 /home/yunohost.app
# Set SSOwat rules
ynh_app_setting_set "$real_app" unprotected_uris "/"
ynh_app_setting_set "$real_app" skipped_regex \
"$(sed 's/[\.\-]/\%&/g' <<< $domain)/%.well%-known/.*"
# Reload services
sudo service php5-fpm restart || true
sudo service nginx reload || true
# Add cron job
cron_path="/etc/cron.d/$app"
sed -i "s@#USER#@${app}@g" ../conf/nextcloud.cron
sed -i "s@#DESTDIR#@${DESTDIR}@g" ../conf/nextcloud.cron
sudo cp ../conf/nextcloud.cron "$cron_path"
sudo chmod 644 "$cron_path"
_exec_occ background:cron
# Finish ownCloud migration
if [[ $real_app != $app ]]; then
echo "ownCloud has been successfully migrated to Nextcloud! \
A last scheduled operation will run in a couple of minutes to finish the \
migration in YunoHost side. Do not proceed any application operation while \
you don't see Nextcloud as installed." >&2
# install cron job and script for final migration step
script_path="/usr/local/sbin/owncloud-migration.sh"
sed -i "s@#APP#@${real_app}@g" ../conf/owncloud-migration.sh
sudo cp ../conf/owncloud-migration.sh "$script_path"
sudo chmod 755 "$script_path"
cron_path="/etc/cron.d/owncloud-migration"
echo "*/1 * * * * root $script_path" | sudo tee "$cron_path" >/dev/null
sudo chmod 644 "$cron_path"
fi
# Warn about possible disabled apps
echo "Note that if you've installed some third-parties Nextcloud applications, \
they are probably disabled and you'll have to manually activate them again." >&2