1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/bozon_ynh.git synced 2024-09-03 18:16:09 +02:00

Refractoring

This commit is contained in:
ewilly 2020-01-05 19:49:24 +01:00
parent 00146b3582
commit 8bda6b3513
7 changed files with 493 additions and 232 deletions

View file

@ -1,19 +1,24 @@
# #!/bin/bash
# Common variables & functions
#
# 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) # Check if directory/file already exists (path in argument)
myynh_check_path () { myynh_check_path () {
[ -z "$1" ] && ynh_die "No argument supplied" [ -z "$1" ] && ynh_die --message="No argument supplied"
[ ! -e "$1" ] || ynh_die "$1 already exists" [ ! -e "$1" ] || ynh_die --message="$1 already exists"
} }
# Create directory only if not already exists (path in argument) # Create directory only if not already exists (path in argument)
myynh_create_dir () { myynh_create_dir () {
[ -z "$1" ] && ynh_die "No argument supplied" [ -z "$1" ] && ynh_die --message="No argument supplied"
[ -d "$1" ] || mkdir -p "$1" [ -d "$1" ] || mkdir -p "$1"
} }
@ -25,14 +30,14 @@ myynh_check_disk_space () {
if [ $free_space -le $backup_size ]; then if [ $free_space -le $backup_size ]; then
WARNING echo "Not enough backup disk space for: $1" WARNING echo "Not enough backup disk space for: $1"
WARNING echo "Space available: $(HUMAN_SIZE $free_space)" 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 fi
} }
# Clean & copy files needed to final folder # Clean & copy files needed to final folder
myynh_clean_source () { myynh_clean_source () {
find "$TMPDIR" -type f -name ".htaccess" | xargs rm find "$tmpdir" -type f -name ".htaccess" | xargs rm
[ -e "$TMPDIR/.gitignore" ] && rm -r "$TMPDIR/.gitignore" [ -e "$tmpdir/.gitignore" ] && rm -r "$tmpdir/.gitignore"
} }
myynh_set_permissions () { myynh_set_permissions () {
@ -47,7 +52,7 @@ myynh_set_permissions () {
} }
#Convert --data to --data-urlencode before ynh_local_curl #Convert --data to --data-urlencode before ynh_local_curl
urlencode() { myynh_urlencode() {
local data local data
if [[ $# != 1 ]]; then if [[ $# != 1 ]]; then
echo "Usage: $0 string-to-urlencode" echo "Usage: $0 string-to-urlencode"
@ -61,3 +66,11 @@ urlencode() {
echo "${data##/?}" echo "${data##/?}"
return 0 return 0
} }
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
#=================================================
# FUTURE OFFICIAL HELPERS
#=================================================

View file

@ -3,42 +3,63 @@
# yunohost backup create -n "bozon-test" --apps bozon # yunohost backup create -n "bozon-test" --apps bozon
# yunohost backup delete bozon-test # yunohost backup delete bozon-test
set -eu #=================================================
if [ ! -e _common.sh ]; then # GENERIC START
# Get the _common.sh file if it's not in the current directory #=================================================
cp ../settings/scripts/_common.sh ./_common.sh # IMPORT GENERIC HELPERS
chmod a+rx _common.sh #=================================================
fi #Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source _common.sh source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers 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 ynh_abort_if_errors
# retrieve arguments #=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME 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" # STANDARD BACKUP STEPS
data_path="/home/yunohost.app/$app" #=================================================
nginx_conf="/etc/nginx/conf.d/$domain.d/$app.conf" # BACKUP THE APP MAIN DIR
phpfpm_conf="/etc/php5/fpm/pool.d/$app.conf" #=================================================
ynh_script_progression --message="Backing up the main app directory..." --time --weight=1
# backup source & conf files
if [ -e "$final_path" ]; then if [ -e "$final_path" ]; then
myynh_check_disk_space "$final_path" myynh_check_disk_space "$final_path"
ynh_backup "$final_path" "source" ynh_backup --src_path="$final_path"
fi 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 if [ -e "$data_path" ]; then
backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) backup_core_only=$(ynh_app_setting_get "$app" backup_core_only)
if [ $backup_core_only -eq 0 ]; then if [ $backup_core_only -eq 0 ]; then
myynh_check_disk_space "$data_path" myynh_check_disk_space "$data_path"
ynh_backup "$data_path" "data" 1 ynh_backup --src_path="$data_path" --is_big
else else
echo "Data dir will not be saved, because backup_core_only is set to true." >&2 echo "Data dir will not be saved, because backup_core_only is set to true." >&2
fi fi

View file

@ -1,63 +1,103 @@
#!/bin/bash #!/bin/bash
set -eu #=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
ynh_abort_if_errors # RETRIEVE ARGUMENTS
#=================================================
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain_old=$YNH_APP_OLD_DOMAIN old_domain=$YNH_APP_OLD_DOMAIN
domain_new=$YNH_APP_NEW_DOMAIN new_domain=$YNH_APP_NEW_DOMAIN
path_url_old=$YNH_APP_OLD_PATH old_path=$YNH_APP_OLD_PATH
path_url_new=$YNH_APP_NEW_PATH new_path=$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")
# definie useful vars # definie useful vars
nginx_conf_old="/etc/nginx/conf.d/$domain_old.d/$app.conf" old_nginx_conf="/etc/nginx/conf.d/$old_domain.d/$app.conf"
nginx_conf_new="/etc/nginx/conf.d/$domain_new.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_domain=0
change_path_url=0 if [ "$old_domain" != "$new_domain" ]; then
change_domain=1
# check what should be changed
[ "$domain_old" != "$domain_new" ] && change_domain=1
[ "$path_url_old" != "$path_url_new" ] && change_path_url=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 fi
else change_path=0
#$path_url_new = / => $path_url_old != / if [ "$old_path" != "$new_path" ]; then
ynh_replace_string "rewrite ^$path_url_old$ $path_url_old/ permanent;" "#sub_path_only rewrite ^__PATH__$ __PATH__/ permanent;" "$nginx_conf_old" change_path=1
ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $path_url_old/" "location\1 $path_url_new" "$nginx_conf_old"
fi
ynh_store_file_checksum "$nginx_conf_old"
fi fi
# change the domain for nginx #=================================================
if [ $change_domain -eq 1 ] # STANDARD MODIFICATIONS
then #=================================================
ynh_delete_file_checksum "$nginx_conf_old" # MODIFY URL IN NGINX CONF
mv $nginx_conf_old "$nginx_conf_new" #=================================================
ynh_store_file_checksum "$nginx_conf_new" ynh_script_progression --message="Updating nginx web server configuration..." --time --weight=1
# 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
# Store file checksum for the new config file location
ynh_store_file_checksum --file="$old_nginx_conf"
fi fi
systemctl reload nginx # 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
#=================================================
# 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

View file

@ -1,13 +1,23 @@
#!/bin/bash #!/bin/bash
set -eu #=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers 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 ynh_abort_if_errors
# retrieve arguments #=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$YNH_APP_ARG_DOMAIN domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH path_url=$YNH_APP_ARG_PATH
@ -19,72 +29,119 @@ backup_core_only=$YNH_APP_ARG_BACKUP_CORE_ONLY
# definie useful vars # definie useful vars
final_path="/var/www/$app" final_path="/var/www/$app"
data_path="/home/yunohost.app/$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") # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
ynh_webpath_available "$domain" "$path_url" #=================================================
ynh_webpath_register "$app" "$domain" "$path_url" ynh_script_progression --message="Validating installation parameters..."
myynh_check_path "$final_path" ## 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" # CREATE DEDICATED USER
ynh_app_setting_set "$app" path "$path_url" #=================================================
ynh_app_setting_set "$app" is_public $is_public ynh_script_progression --message="Configuring system user..."
ynh_app_setting_set "$app" admin_user "$admin" ynh_system_user_create --username="$app"
ynh_app_setting_set "$app" backup_core_only $backup_core_only
# create a dedicated system user #=================================================
ynh_system_user_create "$app" # DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
# download & unpack bozon ynh_script_progression --message="Setting up source files..." --weight=3
TMPDIR=$(mktemp -d) ## download source
ynh_setup_source "$TMPDIR" tmpdir=$(mktemp -d)
ynh_setup_source --dest_dir="$tmpdir"
# clean & copy files needed to final folder ## clean & copy files needed to final folder
myynh_clean_source myynh_clean_source
mv "$TMPDIR" "$final_path" mv "$tmpdir" "$final_path"
## create private & data folders
# create private & data folders
myynh_create_dir "$final_path/private" myynh_create_dir "$final_path/private"
myynh_create_dir "$data_path/uploads" myynh_create_dir "$data_path/uploads"
myynh_create_dir "$data_path/thumbs" myynh_create_dir "$data_path/thumbs"
ln -s "$data_path/uploads" "$final_path/uploads" ln -s "$data_path/uploads" "$final_path/uploads"
ln -s "$data_path/thumbs" "$final_path/thumbs" ln -s "$data_path/thumbs" "$final_path/thumbs"
## set permissions
# set permissions
myynh_set_permissions myynh_set_permissions
# configure nginx settings #=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring nginx web server..."
if [ "$path_url" != "/" ]; then 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 fi
ynh_add_nginx_config ynh_add_nginx_config
# copy and set php-fpm configuration #=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_script_progression --message="Configuring php-fpm..."
ynh_add_fpm_config 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 yunohost app ssowatconf
## reload Nginx
# fill the superadmin creation form (helper ynh_local_curl doesn't work due to --data vs --data-urlencode ?) 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_url="/index.php?p=login"
admin=$(urlencode $admin) admin=$(myynh_urlencode $admin)
password=$(urlencode $password) ynh_print_OFF
password=$(myynh_urlencode $password)
ynh_local_curl $admin_url "creation=1" "login=$admin" "pass=$password" "confirm=$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 ] # GENERIC FINALIZATION
then #=================================================
# 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) # 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') 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') pathluaregex=$([ "$path_url" == "/" ] || echo "$path_url" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g')
regexList="${domainluaregex}${pathluaregex}/index%.php$","${domainluaregex}${pathluaregex}/index%.php%?p=.*$" 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 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

View file

@ -2,48 +2,74 @@
# to test the functionnality : # to test the functionnality :
# yunohost app remove bozon # yunohost app remove bozon
set -u #=================================================
if [ ! -e _common.sh ]; then # GENERIC START
# Get the _common.sh file if it's not in the current directory #=================================================
cp ../settings/scripts/_common.sh ./_common.sh # IMPORT GENERIC HELPERS
chmod a+rx _common.sh #=================================================
fi
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
set -u # LOAD SETTINGS
#=================================================
# retrieve arguments ynh_script_progression --message="Loading installation settings..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME 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" # BACKUP OF APP AND DATAS
data_path="/home/yunohost.app/$app" #=================================================
ynh_script_progression --message="Backing up app and datas..." --time --weight=3
# create a full backup (core+datas) if app installed
if [ $(yunohost app list -i -f "$app" | wc -l) -gt 1 ]; then 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//_/-} app_bck=${app//_/-}
yunohost backup create --apps "$app" --name "${app_bck}_$(date '+%Y%m%d-%H%M%S')" yunohost backup create --apps "$app" --name "${app_bck}_$(date '+%Y%m%d-%H%M%S')"
echo "BoZon fully backuped." >&2 echo "BoZon fully backuped." >&2
fi fi
# remove metapackage and its dependencies #=================================================
# STANDARD REMOVE
#=================================================
# REMOVE DEPENDENCIES
#=================================================
ynh_script_progression --message="Removing dependencies..." --time --weight=1
ynh_remove_app_dependencies 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 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 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

View file

@ -4,49 +4,78 @@
# yunohost app remove bozon # yunohost app remove bozon
# yunohost backup restore "bozon-test" # yunohost backup restore "bozon-test"
set -eu #=================================================
if [ ! -e _common.sh ]; then # GENERIC START
# Fetch helpers file if not in current directory #=================================================
cp ../settings/scripts/_common.sh ./_common.sh # IMPORT GENERIC HELPERS
chmod a+rx _common.sh #=================================================
fi #Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source _common.sh source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers 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 ynh_abort_if_errors
# retrieve arguments #=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get "$app" domain) domain=$(ynh_app_setting_get --app="$app" --key=domain)
path_url=$(ynh_app_setting_get "$app" path) path_url=$(ynh_app_setting_get --app="$app" --key=path)
backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) 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" # CHECK IF THE APP CAN BE RESTORED
data_path="/home/yunohost.app/$app" #=================================================
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" # STANDARD RESTORATION STEPS
myynh_check_path "$final_path" #=================================================
# 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" # RESTORE THE PHP-FPM CONFIGURATION
ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf" #=================================================
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 [ ! -d "$data_path" ]; then
if [ $backup_core_only -eq 0 ]; then if [ $backup_core_only -eq 0 ]; then
ynh_restore_file "$data_path" ynh_restore_file --origin_path="$data_path"
else else
myynh_create_dir "$data_path/uploads" myynh_create_dir "$data_path/uploads"
myynh_create_dir "$data_path/thumbs" myynh_create_dir "$data_path/thumbs"
@ -55,9 +84,22 @@ else
echo "$data_path already exists and will not be overwritten" >&2 echo "$data_path already exists and will not be overwritten" >&2
fi fi
# set permissions #=================================================
# RESTORE USER RIGHTS
#=================================================
myynh_set_permissions myynh_set_permissions
# restart services #=================================================
systemctl reload php5-fpm # GENERIC FINALIZATION
systemctl reload nginx #=================================================
# 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

View file

@ -1,76 +1,138 @@
#!/bin/bash #!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
set -eu set -eu
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
ynh_abort_if_errors # LOAD SETTINGS
#=================================================
# retrieve arguments ynh_script_progression --message="Loading installation settings..."
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get "$app" domain) domain=$(ynh_app_setting_get --app="$app" --key=domain)
path_url=$(ynh_app_setting_get "$app" path) path_url=$(ynh_app_setting_get --app="$app" --key=path)
is_public=$(ynh_app_setting_get "$app" is_public) is_public=$(ynh_app_setting_get --app="$app" --key=is_public)
admin_user=$(ynh_app_setting_get "$app" admin_user) admin_user=$(ynh_app_setting_get --app="$app" --key=admin_user)
backup_core_only=$(ynh_app_setting_get "$app" backup_core_only) 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 #=================================================
# 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" 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" data_path="/home/yunohost.app/$app"
ynh_app_setting_set --app="$app" --key=data_path --value="$data_path"
fi
# use prior backup and restore on error only if backup feature exists on installed instance #=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# 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 if [ -f "/etc/yunohost/apps/$app/scripts/backup" ]; then
ynh_backup_before_upgrade # Backup the current version of the app ynh_backup_before_upgrade # Backup the current version of the app
ynh_clean_setup () { ynh_clean_setup () {
ynh_restore_upgradebackup ynh_restore_upgradebackup
} }
fi fi
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
# add required packages #=================================================
ynh_install_app_dependencies "$PKG_DEPENDENCIES" # STANDARD UPGRADE STEPS
#=================================================
# create a dedicated system user # DOWNLOAD, CHECK AND UNPACK SOURCE
ynh_system_user_create "$app" #=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]; then
# download & unpack bozon # download & unpack bozon
TMPDIR=$(mktemp -d) ynh_script_progression --message="Setting up source files..." --weight=2
ynh_setup_source "$TMPDIR" ## download source
tmpdir=$(mktemp -d)
# clean & copy files needed to final folder ynh_setup_source --dest_dir="$tmpdir"
## clean & copy files needed to final folder
myynh_clean_source myynh_clean_source
[ -e "$TMPDIR/config.php" ] && rm "$TMPDIR/config.php" [ -e "$tmpdir/config.php" ] && rm "$tmpdir/config.php"
cp -a "$TMPDIR/." "$final_path" cp -a "$tmpdir/." "$final_path"
rm -R "$TMPDIR" rm -R "$tmpdir"
## set permissions
# set permissions
myynh_set_permissions myynh_set_permissions
fi
# configure nginx settings #=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading nginx web server configuration..."
if [ "$path_url" != "/" ]; then 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 fi
ynh_add_nginx_config 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 ynh_add_fpm_config
# if app is private, remove url to SSOWat conf from skipped_uris #=================================================
if [ $is_public -eq 0 ] # SETUP SSOWAT
then #=================================================
ynh_app_setting_delete "$app" unprotected_regex # in old script unprotected_regex was used in place of protected_regex 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) # 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') 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') pathluaregex=$([ "$path_url" == "/" ] || echo "$path_url" | sed -e 's/[]().%+*?[^$[]/\%&/g' | sed -e 's/\-/\%&/g')
regexList="${domainluaregex}${pathluaregex}/index%.php$","${domainluaregex}${pathluaregex}/index%.php%?p=.*$" 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 else
ynh_app_setting_set "$app" unprotected_uris "/" ynh_app_setting_set --app="$app" --key=unprotected_uris --value="/"
fi 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 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 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 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