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

[enh] refactor 'restore' script to best practices

This commit is contained in:
Gofannon 2018-06-14 18:58:40 +02:00
parent bdf0f77c19
commit 9e53f1cf6f

View file

@ -1,69 +1,114 @@
#!/bin/bash #!/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 if [ ! -e _common.sh ]; then
app=$YNH_APP_INSTANCE_NAME # Get the _common.sh file if it's not in the current directory
cp ../settings/scripts/_common.sh ./_common.sh
if [ ! -e .fonctions ]; then chmod a+rx _common.sh
# Get file fonction if not been to the current directory
sudo cp ../settings/scripts/.fonctions ./.fonctions
sudo chmod a+rx .fonctions
fi fi
# Loads the generic functions usually used in the script source _common.sh
source .fonctions
# Source app helpers
source /usr/share/yunohost/helpers 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) domain=$(ynh_app_setting_get $app domain)
path=$(ynh_app_setting_get $app path) path_url=$(ynh_app_setting_get $app path)
is_public=$(ynh_app_setting_get $app is_public) final_path=$(ynh_app_setting_get $app final_path)
# Check domain/path availability #=================================================
sudo yunohost app checkurl "${domain}${path}" -a "$app" \ # CHECK IF THE APP CAN BE RESTORED
|| ynh_die "Path not available: ${domain}${path}" #=================================================
# Check $final_path ynh_webpath_available $domain $path_url \
final_path="/var/www/${app}" || ynh_die "Path not available: ${domain}${path_url}"
if [ -d $final_path ]; then test ! -d $final_path \
ynh_die "There is already a directory: $final_path" || ynh_die "There is already a directory: $final_path "
fi
# Check configuration files nginx #=================================================
nginx_conf="/etc/nginx/conf.d/${domain}.d/${app}.conf" # STANDARD RESTORATION STEPS
if [ -f $nginx_conf ]; then #=================================================
ynh_die "The NGINX configuration already exists at '${nginx_conf}'. You should safely delete it before restoring this app." # RESTORE THE NGINX CONFIGURATION
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
phpfpm_ini="/etc/php5/fpm/conf.d/20-${app}.ini" ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf"
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
# 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 ynh_system_user_create $app
# Restore sources & data #=================================================
sudo cp -a ./sources "${final_path}" # RESTORE USER RIGHTS
#=================================================
# Set permissions # Restore permissions on app files
sudo chown -R $app: "${final_path}" chown -R root: $final_path
# Restore nginx configuration files # Restore permissions same as from the 'install' script
sudo cp -a ./nginx.conf "${nginx_conf}" # except for conf, data, some data subfolders, and lib/plugin, where www-data must have write permissions
# Restore php-fpm configuration files 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 cp -a ./php-fpm.conf "${phpfpm_conf}" sudo chmod -R 700 $final_path/conf
sudo cp -a ./php-fpm.ini "${phpfpm_ini}" 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 # RESTORE THE PHP-FPM CONFIGURATION
sudo systemctl reload nginx #=================================================
sudo yunohost app ssowatconf
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