From 8bda6b3513d9d29adc90d5a815bdc748d1a95590 Mon Sep 17 00:00:00 2001 From: ewilly Date: Sun, 5 Jan 2020 19:49:24 +0100 Subject: [PATCH] Refractoring --- scripts/_common.sh | 37 +++++++---- scripts/backup | 65 ++++++++++++------- scripts/change_url | 134 ++++++++++++++++++++++++-------------- scripts/install | 147 +++++++++++++++++++++++++++++------------- scripts/remove | 78 +++++++++++++++-------- scripts/restore | 108 +++++++++++++++++++++---------- scripts/upgrade | 156 +++++++++++++++++++++++++++++++-------------- 7 files changed, 493 insertions(+), 232 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 1744eeb..6d854b5 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,19 +1,24 @@ -# -# Common variables & functions -# +#!/bin/bash -# Package dependencies -PKG_DEPENDENCIES="php-zip php-curl php-gd" +#================================================= +# COMMON VARIABLES +#================================================= +# dependencies used by the app +pkg_dependencies="php-zip php-curl php-gd" + +#================================================= +# PERSONAL HELPERS +#================================================= # Check if directory/file already exists (path in argument) myynh_check_path () { - [ -z "$1" ] && ynh_die "No argument supplied" - [ ! -e "$1" ] || ynh_die "$1 already exists" + [ -z "$1" ] && ynh_die --message="No argument supplied" + [ ! -e "$1" ] || ynh_die --message="$1 already exists" } # Create directory only if not already exists (path in argument) myynh_create_dir () { - [ -z "$1" ] && ynh_die "No argument supplied" + [ -z "$1" ] && ynh_die --message="No argument supplied" [ -d "$1" ] || mkdir -p "$1" } @@ -25,14 +30,14 @@ myynh_check_disk_space () { if [ $free_space -le $backup_size ]; then WARNING echo "Not enough backup disk space for: $1" WARNING echo "Space available: $(HUMAN_SIZE $free_space)" - ynh_die "Space needed: $(HUMAN_SIZE $backup_size)" + ynh_die --message="Space needed: $(HUMAN_SIZE $backup_size)" fi } # Clean & copy files needed to final folder myynh_clean_source () { - find "$TMPDIR" -type f -name ".htaccess" | xargs rm - [ -e "$TMPDIR/.gitignore" ] && rm -r "$TMPDIR/.gitignore" + find "$tmpdir" -type f -name ".htaccess" | xargs rm + [ -e "$tmpdir/.gitignore" ] && rm -r "$tmpdir/.gitignore" } myynh_set_permissions () { @@ -47,7 +52,7 @@ myynh_set_permissions () { } #Convert --data to --data-urlencode before ynh_local_curl -urlencode() { +myynh_urlencode() { local data if [[ $# != 1 ]]; then echo "Usage: $0 string-to-urlencode" @@ -61,3 +66,11 @@ urlencode() { echo "${data##/?}" return 0 } + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +#================================================= +# FUTURE OFFICIAL HELPERS +#================================================= diff --git a/scripts/backup b/scripts/backup index 5e5c76c..47df790 100644 --- a/scripts/backup +++ b/scripts/backup @@ -3,42 +3,63 @@ # yunohost backup create -n "bozon-test" --apps bozon # yunohost backup delete bozon-test -set -eu -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 -source _common.sh +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= +#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= +# Exit if an error occurs during the execution of the script ynh_abort_if_errors -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 app=$YNH_APP_INSTANCE_NAME -domain=$(ynh_app_setting_get "$app" domain) +domain=$(ynh_app_setting_get --app="$app" --key=domain) +final_path=$(ynh_app_setting_get --app="$app" --key=final_path) +data_path=$(ynh_app_setting_get --app="$app" --key=data_path) -# definie useful vars -final_path="/var/www/$app" -data_path="/home/yunohost.app/$app" -nginx_conf="/etc/nginx/conf.d/$domain.d/$app.conf" -phpfpm_conf="/etc/php5/fpm/pool.d/$app.conf" - -# backup source & conf files +#================================================= +# STANDARD BACKUP STEPS +#================================================= +# BACKUP THE APP MAIN DIR +#================================================= +ynh_script_progression --message="Backing up the main app directory..." --time --weight=1 if [ -e "$final_path" ]; then myynh_check_disk_space "$final_path" - ynh_backup "$final_path" "source" + ynh_backup --src_path="$final_path" fi -[ -e "$nginx_conf" ] && ynh_backup "$nginx_conf" "nginx.conf" -[ -e "$phpfpm_conf" ] && ynh_backup "$phpfpm_conf" "php-fpm.conf" +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Backing up nginx web server configuration..." --time --weight=1 +ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" -# backup data +#================================================= +# BACKUP THE PHP-FPM CONFIGURATION +#================================================= +ynh_script_progression --message="Backing up php-fpm configuration..." --time --weight=1 +ynh_backup --src_path="/etc/php/7.0/fpm/pool.d/$app.conf" + +##================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP THE DATA DIRECTORY +#================================================= +ynh_script_progression --message="Backing up data directory..." if [ -e "$data_path" ]; then backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) if [ $backup_core_only -eq 0 ]; then myynh_check_disk_space "$data_path" - ynh_backup "$data_path" "data" 1 + ynh_backup --src_path="$data_path" --is_big else echo "Data dir will not be saved, because backup_core_only is set to true." >&2 fi diff --git a/scripts/change_url b/scripts/change_url index dbaaba1..2c633e0 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -1,63 +1,103 @@ #!/bin/bash -set -eu +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# manage script failure -ynh_abort_if_errors - -# retrieve arguments +#================================================= +# RETRIEVE ARGUMENTS +#================================================= app=$YNH_APP_INSTANCE_NAME -domain_old=$YNH_APP_OLD_DOMAIN -domain_new=$YNH_APP_NEW_DOMAIN -path_url_old=$YNH_APP_OLD_PATH -path_url_new=$YNH_APP_NEW_PATH - -# check the syntax -[ -n "$path_url_old" ] || path_url_old="/" -path_url_old=$(ynh_normalize_url_path "$path_url_old") -[ -n "$path_url_new" ] || path_url_new="/" -path_url_new=$(ynh_normalize_url_path "$path_url_new") +old_domain=$YNH_APP_OLD_DOMAIN +new_domain=$YNH_APP_NEW_DOMAIN +old_path=$YNH_APP_OLD_PATH +new_path=$YNH_APP_NEW_PATH # definie useful vars -nginx_conf_old="/etc/nginx/conf.d/$domain_old.d/$app.conf" -nginx_conf_new="/etc/nginx/conf.d/$domain_new.d/$app.conf" +old_nginx_conf="/etc/nginx/conf.d/$old_domain.d/$app.conf" +new_nginx_conf="/etc/nginx/conf.d/$new_domain.d/$app.conf" +new_path=$(ynh_normalize_url_path --path_url="$new_path") + +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 +# Needed for helper "ynh_add_nginx_config" +final_path=$(ynh_app_setting_get --app=$app --key=final_path) + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before changing its url (may take a while)..." --time --weight=1 +# backup the current version of the app +ynh_backup_before_upgrade +ynh_clean_setup () { + # remove the new domain config file, the remove script won't do it as it doesn't know yet its location. + ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf" + # 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 WHICH PARTS SHOULD BE CHANGED +#================================================= change_domain=0 -change_path_url=0 +if [ "$old_domain" != "$new_domain" ]; then + change_domain=1 +fi +change_path=0 +if [ "$old_path" != "$new_path" ]; then + change_path=1 +fi -# check what should be changed -[ "$domain_old" != "$domain_new" ] && change_domain=1 -[ "$path_url_old" != "$path_url_new" ] && change_path_url=1 +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# MODIFY URL IN NGINX CONF +#================================================= +ynh_script_progression --message="Updating nginx web server configuration..." --time --weight=1 -# change the path in the nginx config file -if [ $change_path_url -eq 1 ] -then - ynh_backup_if_checksum_is_different "$nginx_conf_old" - if [ "$path_url_new" != "/" ]; then - if [ "^$path_url_old" != "/" ]; then - #$path_url_new != / & $path_url_old != / - ynh_replace_string "rewrite ^$path_url_old$ $path_url_old/ permanent;" "rewrite ^$path_url_new$ $path_url_new/ permanent;" "$nginx_conf_old" - ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $path_url_old" "location\1 $path_url_new" "$nginx_conf_old" - else - #$path_url_new != / & $path_url_old = / - ynh_replace_string "#sub_path_only rewrite ^__PATH__$ __PATH__/ permanent;" "rewrite ^$path_url_old$ $path_url_old/ permanent;" "$nginx_conf_old" - ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $path_url_old" "location\1 $path_url_new/" "$nginx_conf_old" - fi - else - #$path_url_new = / => $path_url_old != / - ynh_replace_string "rewrite ^$path_url_old$ $path_url_old/ permanent;" "#sub_path_only rewrite ^__PATH__$ __PATH__/ permanent;" "$nginx_conf_old" - ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $path_url_old/" "location\1 $path_url_new" "$nginx_conf_old" +# Change the path in the nginx config file +if [ $change_path -eq 1 ]; then + # Make a backup of the original nginx config file if modified + ynh_backup_if_checksum_is_different --file="$old_nginx_conf" + # Set global variables for nginx helper + domain="$old_domain" + path_url="$new_path" + # Create a dedicated nginx config + ynh_add_nginx_config + if [ "$path_url" != "/" ]; then + ynh_replace_string --match_string="^#sub_path_only" --replace_string="" --target_file="$old_nginx_conf" fi - ynh_store_file_checksum "$nginx_conf_old" + # Store file checksum for the new config file location + ynh_store_file_checksum --file="$old_nginx_conf" fi -# change the domain for nginx -if [ $change_domain -eq 1 ] -then - ynh_delete_file_checksum "$nginx_conf_old" - mv $nginx_conf_old "$nginx_conf_new" - ynh_store_file_checksum "$nginx_conf_new" +# Change the domain for nginx +if [ $change_domain -eq 1 ]; then + # Delete file checksum for the old conf file location + ynh_delete_file_checksum --file="$old_nginx_conf" + mv "$old_nginx_conf" "$new_nginx_conf" + # Store file checksum for the new config file location + ynh_store_file_checksum --file="$new_nginx_conf" fi -systemctl reload nginx +#================================================= +# GENERIC FINALISATION +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading nginx web server..." --time --weight=1 +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Change of URL completed for $app" --time --last diff --git a/scripts/install b/scripts/install index 4809509..ce78784 100644 --- a/scripts/install +++ b/scripts/install @@ -1,13 +1,23 @@ #!/bin/bash -set -eu +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= +# Exit if an error occurs during the execution of the script ynh_abort_if_errors -# retrieve arguments +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= app=$YNH_APP_INSTANCE_NAME domain=$YNH_APP_ARG_DOMAIN path_url=$YNH_APP_ARG_PATH @@ -19,72 +29,119 @@ backup_core_only=$YNH_APP_ARG_BACKUP_CORE_ONLY # definie useful vars final_path="/var/www/$app" data_path="/home/yunohost.app/$app" +path_url=$(ynh_normalize_url_path --path_url="$path_url") -# check domain/path availability -path_url=$(ynh_normalize_url_path "$path_url") -ynh_webpath_available "$domain" "$path_url" -ynh_webpath_register "$app" "$domain" "$path_url" -myynh_check_path "$final_path" +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= +ynh_script_progression --message="Validating installation parameters..." +## check domain/path availability +test ! -e "$final_path" || ynh_die --message="This path already contains a folder" +## register (book) web path +ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url" +## check that admin user is an existing account +ynh_user_exists --username="$admin" -# check that admin user is an existing account -ynh_user_exists "$admin" +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_script_progression --message="Storing installation settings..." +ynh_app_setting_set --app="$app" --key=domain --value="$domain" +ynh_app_setting_set --app="$app" --key=path --value="$path_url" +ynh_app_setting_set --app="$app" --key=is_public --value=$is_public +ynh_app_setting_set --app="$app" --key=admin_user --value="$admin" +ynh_app_setting_set --app="$app" --key=backup_core_only --value=$backup_core_only +ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" +ynh_app_setting_set --app="$app" --key=data_path --value="$data_path" -# add required packages -ynh_install_app_dependencies "$PKG_DEPENDENCIES" +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# INSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Installing dependencies..." --weight=3 +ynh_install_app_dependencies "$pkg_dependencies" -# save app settings -ynh_app_setting_set "$app" domain "$domain" -ynh_app_setting_set "$app" path "$path_url" -ynh_app_setting_set "$app" is_public $is_public -ynh_app_setting_set "$app" admin_user "$admin" -ynh_app_setting_set "$app" backup_core_only $backup_core_only +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Configuring system user..." +ynh_system_user_create --username="$app" -# create a dedicated system user -ynh_system_user_create "$app" - -# download & unpack bozon -TMPDIR=$(mktemp -d) -ynh_setup_source "$TMPDIR" - -# clean & copy files needed to final folder +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= +ynh_script_progression --message="Setting up source files..." --weight=3 +## download source +tmpdir=$(mktemp -d) +ynh_setup_source --dest_dir="$tmpdir" +## clean & copy files needed to final folder myynh_clean_source -mv "$TMPDIR" "$final_path" - -# create private & data folders +mv "$tmpdir" "$final_path" +## create private & data folders myynh_create_dir "$final_path/private" myynh_create_dir "$data_path/uploads" myynh_create_dir "$data_path/thumbs" ln -s "$data_path/uploads" "$final_path/uploads" ln -s "$data_path/thumbs" "$final_path/thumbs" - -# set permissions +## set permissions myynh_set_permissions -# configure nginx settings +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Configuring nginx web server..." if [ "$path_url" != "/" ]; then - ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf" + ynh_replace_string --match_string="^#sub_path_only" --replace_string="" --target_file="../conf/nginx.conf" fi ynh_add_nginx_config -# copy and set php-fpm configuration +#================================================= +# PHP-FPM CONFIGURATION +#================================================= +ynh_script_progression --message="Configuring php-fpm..." ynh_add_fpm_config -# set temporary public access for curl call -ynh_app_setting_set "$app" unprotected_uris "/" +#================================================= +# SETUP APPLICATION WITH CURL +#================================================= +# create admin user in bozon +ynh_script_progression --message="Configuring admin user in BoZon..." --weight=2 +## set the app as temporarily public for curl call +ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/" +## reload SSOwat config yunohost app ssowatconf - -# fill the superadmin creation form (helper ynh_local_curl doesn't work due to --data vs --data-urlencode ?) +## reload Nginx +ynh_systemd_action --service_name=nginx --action=reload +## fill the superadmin creation form (helper ynh_local_curl doesn't work due to --data vs --data-urlencode ?) admin_url="/index.php?p=login" -admin=$(urlencode $admin) -password=$(urlencode $password) -ynh_local_curl $admin_url "creation=1" "login=$admin" "pass=$password" "confirm=$password" +admin=$(myynh_urlencode $admin) +ynh_print_OFF + password=$(myynh_urlencode $password) + ynh_local_curl $admin_url "creation=1" "login=$admin" "pass=$password" "confirm=$password" +ynh_print_ON -# if app is private, remove url to SSOWat conf from skipped_uris -if [ $is_public -eq 0 ] -then +#================================================= +# GENERIC FINALIZATION +#================================================= +# SETUP SSOWAT +#================================================= +ynh_script_progression --message="Configuring SSOwat..." +if [ $is_public -eq 0 ]; then # escape magic chars in vars (lua magic chars are ().%+-*?[^$ according to https://www.lua.org/pil/20.2.html) domainluaregex=$(echo "$domain" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g') pathluaregex=$([ "$path_url" == "/" ] || echo "$path_url" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g') regexList="${domainluaregex}${pathluaregex}/index%.php$","${domainluaregex}${pathluaregex}/index%.php%?p=.*$" - ynh_app_setting_set "$app" protected_regex "$regexList" + ynh_app_setting_set --app="$app" --key=protected_regex --value="$regexList" fi + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading nginx web server..." +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove index 4c2fbf5..2a3f4d2 100644 --- a/scripts/remove +++ b/scripts/remove @@ -2,48 +2,74 @@ # to test the functionnality : # yunohost app remove bozon -set -u -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 +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= source _common.sh source /usr/share/yunohost/helpers -# manage script failure -set -u - -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 app=$YNH_APP_INSTANCE_NAME -domain=$(ynh_app_setting_get "$app" domain) +domain=$(ynh_app_setting_get --app="$app" --key=domain) +final_path=$(ynh_app_setting_get --app="$app" --key=final_path) +data_path=$(ynh_app_setting_get --app="$app" --key=data_path) -# definie useful vars -final_path="/var/www/$app" -data_path="/home/yunohost.app/$app" - -# create a full backup (core+datas) if app installed +#================================================= +# BACKUP OF APP AND DATAS +#================================================= +ynh_script_progression --message="Backing up app and datas..." --time --weight=3 if [ $(yunohost app list -i -f "$app" | wc -l) -gt 1 ]; then - ynh_app_setting_set "$app" backup_core_only 0 + ynh_app_setting_set --app="$app" --key=backup_core_only --value=0 app_bck=${app//_/-} yunohost backup create --apps "$app" --name "${app_bck}_$(date '+%Y%m%d-%H%M%S')" echo "BoZon fully backuped." >&2 fi -# remove metapackage and its dependencies +#================================================= +# STANDARD REMOVE +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." --time --weight=1 ynh_remove_app_dependencies -# remove the app directory securely -ynh_secure_remove "$final_path" +#================================================= +# REMOVE APP MAIN DIR +#================================================= +ynh_script_progression --message="Removing app main directory..." --time --weight=1 +ynh_secure_remove --file="$final_path" -# remove the dedicated nginx config +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Removing nginx web server configuration..." --time --weight=1 ynh_remove_nginx_config -# remove the dedicated php-fpm config +#================================================= +# REMOVE PHP-FPM CONFIGURATION +#================================================= +ynh_script_progression --message="Removing php-fpm configuration..." --time --weight=1 ynh_remove_fpm_config -# remove a directory securely -ynh_secure_remove "$data_path" +#================================================= +# SPECIFIC REMOVE +#================================================= +ynh_secure_remove --file="$data_path" -# delete a system user -ynh_system_user_delete "$app" +#================================================= +# GENERIC FINALIZATION +#================================================= +# REMOVE DEDICATED USER +#================================================= +ynh_script_progression --message="Removing the dedicated system user..." --time --weight=1 +ynh_system_user_delete --username="$app" + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Removal of $app completed" --time --last diff --git a/scripts/restore b/scripts/restore index e11760e..cda5821 100644 --- a/scripts/restore +++ b/scripts/restore @@ -4,49 +4,78 @@ # yunohost app remove bozon # yunohost backup restore "bozon-test" -set -eu -if [ ! -e _common.sh ]; then - # Fetch helpers file if not in current directory - cp ../settings/scripts/_common.sh ./_common.sh - chmod a+rx _common.sh -fi -source _common.sh +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= +#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= +# Exit if an error occurs during the execution of the script ynh_abort_if_errors -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 app=$YNH_APP_INSTANCE_NAME -domain=$(ynh_app_setting_get "$app" domain) -path_url=$(ynh_app_setting_get "$app" path) -backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) +domain=$(ynh_app_setting_get --app="$app" --key=domain) +path_url=$(ynh_app_setting_get --app="$app" --key=path) +backup_core_only=$(ynh_app_setting_get --app="$app" --key=backup_core_only) +final_path=$(ynh_app_setting_get --app="$app" --key=final_path) +data_path=$(ynh_app_setting_get --app="$app" --key=data_path) -# definie useful vars -final_path="/var/www/$app" -data_path="/home/yunohost.app/$app" +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= +ynh_script_progression --message="Validating restoration parameters..." --time --weight=1 +ynh_webpath_available --domain="$domain" --path_url="$path_url" || ynh_die --message="Path not available: ${domain}${path_url}" +test ! -d $final_path || ynh_die --message="There is already a directory: $final_path" -# check domain/path availability -ynh_webpath_available "$domain" "$path_url" -myynh_check_path "$final_path" +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= +ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" -# add required packages -ynh_install_app_dependencies "$PKG_DEPENDENCIES" +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= +ynh_script_progression --message="Restoring the app main directory..." --time --weight=1 +ynh_restore_file --origin_path="$final_path" -# create a dedicated system user -ynh_system_user_create "$app" +#================================================= +# RECREATE THE DEDICATED USER +#================================================= +ynh_script_progression --message="Recreating the dedicated system user..." --time --weight=1 +ynh_system_user_create --username="$app" -# restore sconf files -ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" -ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf" +#================================================= +# RESTORE THE PHP-FPM CONFIGURATION +#================================================= +ynh_restore_file --origin_path="/etc/php/7.0/fpm/pool.d/$app.conf" -# restore source -ynh_restore_file "$final_path" +#================================================= +# SPECIFIC RESTORATION +#================================================= +# REINSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Reinstalling dependencies..." --time --weight=1 +ynh_install_app_dependencies $pkg_dependencies -# restore data only if there is no data +#================================================= +# RESTORE THE DATA DIR +#================================================= +ynh_script_progression --message="Restoring the data directory..." --time --weight=2 if [ ! -d "$data_path" ]; then if [ $backup_core_only -eq 0 ]; then - ynh_restore_file "$data_path" + ynh_restore_file --origin_path="$data_path" else myynh_create_dir "$data_path/uploads" myynh_create_dir "$data_path/thumbs" @@ -55,9 +84,22 @@ else echo "$data_path already exists and will not be overwritten" >&2 fi -# set permissions +#================================================= +# RESTORE USER RIGHTS +#================================================= myynh_set_permissions -# restart services -systemctl reload php5-fpm -systemctl reload nginx +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= +ynh_script_progression --message="Reloading nginx web server and php-fpm..." --time --weight=1 +ynh_systemd_action --service_name=php7.0-fpm --action=reload +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Restoration completed for $app" --time --last + diff --git a/scripts/upgrade b/scripts/upgrade index 48a4ece..ad4a7cc 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,76 +1,138 @@ #!/bin/bash +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= set -eu source _common.sh source /usr/share/yunohost/helpers -# manage script failure -ynh_abort_if_errors - -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." app=$YNH_APP_INSTANCE_NAME -domain=$(ynh_app_setting_get "$app" domain) -path_url=$(ynh_app_setting_get "$app" path) -is_public=$(ynh_app_setting_get "$app" is_public) -admin_user=$(ynh_app_setting_get "$app" admin_user) -backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) +domain=$(ynh_app_setting_get --app="$app" --key=domain) +path_url=$(ynh_app_setting_get --app="$app" --key=path) +is_public=$(ynh_app_setting_get --app="$app" --key=is_public) +admin_user=$(ynh_app_setting_get --app="$app" --key=admin_user) +backup_core_only=$(ynh_app_setting_get --app="$app" --key=backup_core_only) +final_path=$(ynh_app_setting_get --app="$app" --key=final_path) +data_path=$(ynh_app_setting_get --app="$app" --key=data_path) -# definie useful vars -final_path="/var/www/$app" -data_path="/home/yunohost.app/$app" - -# use prior backup and restore on error only if backup feature exists on installed instance -if [ -f "/etc/yunohost/apps/$app/scripts/backup" ] ; then - ynh_backup_before_upgrade # Backup the current version of the app - ynh_clean_setup () { - ynh_restore_upgradebackup - } +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +ynh_script_progression --message="Ensuring downward compatibility..." --time --weight=1 +# If final_path doesn't exist, create it +if [ -z "$final_path" ]; then + final_path="/var/www/$app" + ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" +fi +# If data_path doesn't exist, create it +if [ -z "$data_path" ]; then + data_path="/home/yunohost.app/$app" + ynh_app_setting_set --app="$app" --key=data_path --value="$data_path" fi -# add required packages -ynh_install_app_dependencies "$PKG_DEPENDENCIES" +#================================================= +# CHECK VERSION +#================================================= +upgrade_type=$(ynh_check_app_version_changed) -# create a dedicated system user -ynh_system_user_create "$app" +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=3 +if [ -f "/etc/yunohost/apps/$app/scripts/backup" ]; then + ynh_backup_before_upgrade # Backup the current version of the app + ynh_clean_setup () { + ynh_restore_upgradebackup + } +fi +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors -# download & unpack bozon -TMPDIR=$(mktemp -d) -ynh_setup_source "$TMPDIR" +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= +if [ "$upgrade_type" == "UPGRADE_APP" ]; then + # download & unpack bozon + ynh_script_progression --message="Setting up source files..." --weight=2 + ## download source + tmpdir=$(mktemp -d) + ynh_setup_source --dest_dir="$tmpdir" + ## clean & copy files needed to final folder + myynh_clean_source + [ -e "$tmpdir/config.php" ] && rm "$tmpdir/config.php" + cp -a "$tmpdir/." "$final_path" + rm -R "$tmpdir" + ## set permissions + myynh_set_permissions +fi -# clean & copy files needed to final folder -myynh_clean_source -[ -e "$TMPDIR/config.php" ] && rm "$TMPDIR/config.php" -cp -a "$TMPDIR/." "$final_path" -rm -R "$TMPDIR" - -# set permissions -myynh_set_permissions - -# configure nginx settings +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading nginx web server configuration..." if [ "$path_url" != "/" ]; then - ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf" + ynh_replace_string --match_string="^#sub_path_only" --replace_string="" --target_file="../conf/nginx.conf" fi ynh_add_nginx_config -# copy and set php-fpm configuration +#================================================= +# UPGRADE DEPENDENCIES +#================================================= +ynh_script_progression --message="Upgrading dependencies..." --time --weight=1 +ynh_install_app_dependencies $pkg_dependencies + +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Making sure dedicated system user exists..." +ynh_system_user_create --username="$app" + +#================================================= +# PHP-FPM CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading php-fpm configuration..." ynh_add_fpm_config -# if app is private, remove url to SSOWat conf from skipped_uris -if [ $is_public -eq 0 ] -then - ynh_app_setting_delete "$app" unprotected_regex # in old script unprotected_regex was used in place of protected_regex +#================================================= +# SETUP SSOWAT +#================================================= +ynh_script_progression --message="Upgrading SSOwat configuration..." +if [ $is_public -eq 0 ]; then # escape magic chars in vars (lua magic chars are ().%+-*?[^$ according to https://www.lua.org/pil/20.2.html) domainluaregex=$(echo "$domain" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g') pathluaregex=$([ "$path_url" == "/" ] || echo "$path_url" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g') regexList="${domainluaregex}${pathluaregex}/index%.php$","${domainluaregex}${pathluaregex}/index%.php%?p=.*$" - ynh_app_setting_set "$app" protected_regex "$regexList" + ynh_app_setting_set --app="$app" --key=protected_regex --value="$regexList" else - ynh_app_setting_set "$app" unprotected_uris "/" + ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/" fi -# Purge php sessions stored in /var/lib/phpx/sessions +#================================================= +# CLEAN PHP SESSIONS STORED IN /var/lib/phpx/sessions +#================================================= +ynh_script_progression --message="Cleaning php sessions stored..." if [ -d "/usr/lib/php5" ]; then - [ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean + [ -x /usr/lib/php5/sessionclean ] && /usr/lib/php5/sessionclean elif [ -d "/usr/lib/php" ]; then - [ -x /usr/lib/php/sessionclean ] && /usr/lib/php/sessionclean + [ -x /usr/lib/php/sessionclean ] && /usr/lib/php/sessionclean fi + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading nginx web server..." +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Upgrade of $app completed" --last