From 9e53f1cf6fce11cf7ef2aa15c3f82be9ea9b98d3 Mon Sep 17 00:00:00 2001 From: Gofannon Date: Thu, 14 Jun 2018 18:58:40 +0200 Subject: [PATCH] [enh] refactor 'restore' script to best practices --- scripts/restore | 149 +++++++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 52 deletions(-) diff --git a/scripts/restore b/scripts/restore index 2ee9014..8b5a099 100755 --- a/scripts/restore +++ b/scripts/restore @@ -1,69 +1,114 @@ #!/bin/bash -# This restore script is adapted to Yunohost >=2.4 -# Exit on command errors and treat unset variables as an error -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# The parameter $2 is the id of the app instance ex: ynhexample__2 -app=$YNH_APP_INSTANCE_NAME - -if [ ! -e .fonctions ]; then - # Get file fonction if not been to the current directory - sudo cp ../settings/scripts/.fonctions ./.fonctions - sudo chmod a+rx .fonctions +if [ ! -e _common.sh ]; then + # Get the _common.sh file if it's not in the current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh fi -# Loads the generic functions usually used in the script -source .fonctions -# Source app helpers +source _common.sh source /usr/share/yunohost/helpers -# Get old parameter of the app +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + domain=$(ynh_app_setting_get $app domain) -path=$(ynh_app_setting_get $app path) -is_public=$(ynh_app_setting_get $app is_public) +path_url=$(ynh_app_setting_get $app path) +final_path=$(ynh_app_setting_get $app final_path) -# Check domain/path availability -sudo yunohost app checkurl "${domain}${path}" -a "$app" \ - || ynh_die "Path not available: ${domain}${path}" +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= -# Check $final_path -final_path="/var/www/${app}" -if [ -d $final_path ]; then - ynh_die "There is already a directory: $final_path" -fi +ynh_webpath_available $domain $path_url \ + || ynh_die "Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die "There is already a directory: $final_path " -# Check configuration files nginx -nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" -if [ -f $nginx_conf ]; then - ynh_die "The NGINX configuration already exists at '${nginx_conf}'. You should safely delete it before restoring this app." -fi -# Check configuration files php-fpm -phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf" -if [ -f $phpfpm_conf ]; then - ynh_die "The PHP FPM configuration already exists at '${phpfpm_conf}'. You should safely delete it before restoring this app." -fi +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= -phpfpm_ini="/etc/php5/fpm/conf.d/20-${app}.ini" -if [ -f $phpfpm_ini ]; then - ynh_die "The PHP FPM INI configuration already exists at '${phpfpm_ini}'. You should safely delete it before restoring this app." -fi +ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" -# Create system user dedicace for this app +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= + +ynh_restore_file "$final_path" + +#================================================= +# RECREATE THE DEDICATED USER +#================================================= + +# Create the dedicated user (if not existing) ynh_system_user_create $app - # Restore sources & data -sudo cp -a ./sources "${final_path}" +#================================================= +# RESTORE USER RIGHTS +#================================================= -# Set permissions -sudo chown -R $app: "${final_path}" +# Restore permissions on app files +chown -R root: $final_path -# Restore nginx configuration files -sudo cp -a ./nginx.conf "${nginx_conf}" -# Restore php-fpm configuration files -sudo cp -a ./php-fpm.conf "${phpfpm_conf}" -sudo cp -a ./php-fpm.ini "${phpfpm_ini}" +# Restore permissions same as from the 'install' script +# except for conf, data, some data subfolders, and lib/plugin, where www-data must have write permissions +sudo chown -R $app:root $final_path/{conf,data,data/attic,data/cache,data/index,data/locks,data/media*,data/meta,data/pages,data/tmp,lib/plugins,lib/tpl} +sudo chmod -R 700 $final_path/conf +sudo chmod -R 700 $final_path/data +sudo chmod -R 755 $final_path/lib/plugins +sudo chmod 755 $final_path/lib/tpl/{dokuwiki,dokuwiki/images} -# Reload services -sudo systemctl reload php5-fpm -sudo systemctl reload nginx -sudo yunohost app ssowatconf +#================================================= +# RESTORE THE PHP-FPM CONFIGURATION +#================================================= + +ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf" +ynh_restore_file "/etc/php5/fpm/conf.d/20-$app.ini" + +#================================================= +# SPECIFIC RESTORATION +#================================================= +# ADVERTISE SERVICE IN ADMIN PANEL +#================================================= + +yunohost service add $app --log "/var/log/$app/$app.log" + +#================================================= +# RESTORE SYSTEMD +#================================================= + +ynh_restore_file "/etc/systemd/system/$app.service" +systemctl enable $app.service + +#================================================= +# RESTORE THE LOGROTATE CONFIGURATION +#================================================= + +ynh_restore_file "/etc/logrotate.d/$app" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= + +systemctl reload php5-fpm +systemctl reload nginx