From abce695bdc018c77526ab3d9536fe88974a9ca47 Mon Sep 17 00:00:00 2001 From: frju365 Date: Sun, 11 Feb 2018 20:16:43 +0100 Subject: [PATCH] Big Refactoring --- scripts/upgrade | 174 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 125 insertions(+), 49 deletions(-) diff --git a/scripts/upgrade b/scripts/upgrade index d6011cb..4847e6e 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,14 +1,20 @@ #!/bin/bash -set -eu -source .fonctions # Charge les fonctions génériques habituellement utilisées dans le script +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Récupère les infos de l'application. -app=$YNH_APP_INSTANCE_NAME - -# Source app helpers +source _common.sh source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= +app=$YNH_APP_INSTANCE_NAME + domain=$(ynh_app_setting_get $app domain) path_url=$(ynh_app_setting_get $app path_url) final_path=$(ynh_app_setting_get $app final_path) @@ -18,50 +24,120 @@ admin_email=$(ynh_app_setting_get $app admin_email) db_pass=$(ynh_app_setting_get $app mysqlpwd) db_user=$(ynh_app_setting_get $app db_user) -CHECK_PATH # Vérifie et corrige la syntaxe du path. +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= -# Remove trailing "/" for next commands - -path_url=$(ynh_normalize_url_path $path_url) # Vérifie et corrige la syntaxe du path. - -# We download the sources and check the md5sum -SFILE=`sudo cat ../sources/source_file`; -sudo wget -nv -i ../sources/source_url -O ${SFILE}.tar.gz -sudo md5sum -c ../sources/source_md5 --status || (echo "Corrupt source" >&2 && false) -sudo tar xvf ${SFILE}.tar.gz -C ../sources/ -sudo cp -a ../sources/abantecart-src-master/. $final_path - -sudo rm -rf $final_path/tests -sudo rm -rf $final_path/install.txt -sudo mv $final_path/public_html/* $final_path/ -sudo rm -rf $final_path/public_html - -# file owned by www-data before checking permissions -sudo chown www-data:www-data $final_path -R - -# set database configuration -sed -i "s@YNH_WWW_PATH@$path_url@g" ../conf/nginx.conf - -# Modify Nginx configuration file and copy it to Nginx conf directory -sed -i "s@YNH_WWW_PATH@$path_url@g" ../conf/nginx.conf -sed -i "s@YNH_WWW_ALIAS@$final_path/@g" ../conf/nginx.conf -sudo sed -i "s@NAMETOCHANGE@$app@g" ../conf/nginx.conf -nginxconf=/etc/nginx/conf.d/$domain.d/$app.conf -sudo cp ../conf/nginx.conf $nginxconf -sudo chown root: $nginxconf -sudo chmod 644 $nginxconf - -sudo sed -i "s@NAMETOCHANGE@$app@g" ../conf/php-fpm.conf -sudo sed -i "s@FOLDERTOCHANGE@$final_path@g" ../conf/php-fpm.conf -finalphpconf=/etc/php5/fpm/pool.d/$app.conf - -ynh_app_setting_set "$app" is_public "$is_public" -if [ "$is_public" = "Yes" ]; -then - ynh_app_setting_set "$app" unprotected_uris "/" +# Fix is_public as a boolean value +if [ "$is_public" = "Yes" ]; then + ynh_app_setting_set $app is_public 1 + is_public=1 +elif [ "$is_public" = "No" ]; then + ynh_app_setting_set $app is_public 0 + is_public=0 fi -sudo service php5-fpm reload -sudo service nginx reload -sudo yunohost app ssowatconf +# If db_name doesn't exist, create it +if [ -z $db_name ]; then + db_name=$(ynh_sanitize_dbid $app) + ynh_app_setting_set $app db_name $db_name +fi +# If final_path doesn't exist, create it +if [ -z $final_path ]; then + final_path=/var/www/$app + ynh_app_setting_set $app final_path $final_path +fi + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= + +# Backup the current version of the app +ynh_backup_before_upgrade +ynh_clean_setup () { + # restore it if the upgrade fails + ynh_restore_upgradebackup +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# CHECK THE PATH +#================================================= + +# Normalize the URL path syntax +path_url=$(ynh_normalize_url_path $path_url) + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +# Download, check integrity, uncompress and patch the source from app.src +ynh_setup_source "$final_path" + + +ynh_secure_remove $final_path/tests +ynh_secure_remove $final_path/install.txt +mv $final_path/public_html/* $final_path/ +ynh_secure_remove $final_path/public_html + +#================================================= +# NGINX CONFIGURATION +#================================================= + +# Create a dedicated nginx config +ynh_add_nginx_config + +#================================================= +# CREATE DEDICATED USER +#================================================= + +# Create a system user +ynh_system_user_create $app + +#================================================= +# PHP-FPM CONFIGURATION +#================================================= + +# Create a dedicated php-fpm config +ynh_add_fpm_config + +#================================================= +# SETUP LOGROTATE +#================================================= + +# Use logrotate to manage app-specific logfile(s) +ynh_use_logrotate --non-append + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= + +# Set right permissions for curl installation +chown -R $app:$app $final_path + +#================================================= +# SETUP SSOWAT +#================================================= + +if [ $is_public -eq 0 ] +then # Remove the public access + ynh_app_setting_delete $app skipped_uris +fi +# Make app public if necessary +if [ $is_public -eq 1 ] +then + # unprotected_uris allows SSO credentials to be passed anyway + ynh_app_setting_set $app unprotected_uris "/" +fi + +#================================================= +# RELOAD NGINX +#================================================= + +systemctl reload nginx