From 570d57458bef7b6242a7161d46d44d8b555418cb Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Holcroft Date: Sat, 21 Oct 2017 22:52:21 +0200 Subject: [PATCH] Add comments and respect same order as YunoHost/example_ynh --- conf/config.php | 2 +- conf/php-fpm.conf | 8 ++-- scripts/backup | 41 +++++++++++++--- scripts/install | 118 ++++++++++++++++++++++++++++++++++------------ scripts/remove | 50 ++++++++++++++------ scripts/restore | 74 +++++++++++++++++++++++------ scripts/upgrade | 95 +++++++++++++++++++++++++++---------- 7 files changed, 293 insertions(+), 95 deletions(-) diff --git a/conf/config.php b/conf/config.php index 211db4c..5269a5a 100644 --- a/conf/config.php +++ b/conf/config.php @@ -232,4 +232,4 @@ define('HTTP_PROXY_PASSWORD', ''); define('HTTP_VERIFY_SSL_CERTIFICATE', false); // TOTP (2FA) issuer name -define('TOTP_ISSUER', 'Kanboard'); \ No newline at end of file +define('TOTP_ISSUER', 'Kanboard'); diff --git a/conf/php-fpm.conf b/conf/php-fpm.conf index 8b69b95..ad5e9c8 100644 --- a/conf/php-fpm.conf +++ b/conf/php-fpm.conf @@ -1,6 +1,6 @@ -[#POOLNAME#] +[__NAMETOCHANGE__] ; The address on which to accept FastCGI requests. -listen = /var/run/php5-fpm-#POOLNAME#.sock +listen = /var/run/php5-fpm-__NAMETOCHANGE__.sock ; Set permissions for unix socket, if one is used. listen.owner = www-data @@ -47,7 +47,7 @@ request_terminate_timeout = 1d request_slowlog_timeout = 5s ; The log file for slow requests. -slowlog = /var/log/nginx/#POOLNAME#.slow.log +slowlog = /var/log/nginx/__NAMETOCHANGE__.slow.log ; Set open file descriptor rlimit. rlimit_files = 4096 @@ -56,7 +56,7 @@ rlimit_files = 4096 rlimit_core = 0 ; Chdir to this directory at the start. -chdir = #DESTDIR# +chdir = __FINALPATH__ ; Redirect worker stdout and stderr into main error log. catch_workers_output = yes diff --git a/scripts/backup b/scripts/backup index 1466e9d..4ff3af0 100644 --- a/scripts/backup +++ b/scripts/backup @@ -1,18 +1,30 @@ #!/bin/bash -# Source local helpers +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + 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 app helpers +source _common.sh source /usr/share/yunohost/helpers -# Abort script if errors +#================================================= +# 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 # Set app specific variables @@ -22,12 +34,29 @@ dbname=$app domain=$(ynh_app_setting_get "$app" domain) final_path=$(ynh_app_setting_get "$app" final_path) +#================================================= +# STANDARD BACKUP STEPS +#================================================= +# BACKUP THE APP MAIN DIR +#================================================= + # Copy the app source files ynh_backup "$final_path" -# Copy the conf files +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + ynh_backup "/etc/nginx/conf.d/${domain}.d/${app}.conf" + +#================================================= +# BACKUP THE PHP-FPM CONFIGURATION +#================================================= + ynh_backup "/etc/php5/fpm/pool.d/${app}.conf" -# Dump the database +#================================================= +# BACKUP THE MYSQL DATABASE +#================================================= + ynh_mysql_dump_db "$dbname" > ./db.sql diff --git a/scripts/install b/scripts/install index a6e9707..32240c5 100644 --- a/scripts/install +++ b/scripts/install @@ -1,14 +1,25 @@ #!/bin/bash -# Source local helpers -source ./_common.sh +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Source app helpers +source _common.sh source /usr/share/yunohost/helpers -# Abort script if errors +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script ynh_abort_if_errors +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= + # Retrieve arguments domain=$YNH_APP_ARG_DOMAIN path_url=$YNH_APP_ARG_PATH @@ -17,6 +28,14 @@ is_public=$YNH_APP_ARG_IS_PUBLIC app=$YNH_APP_INSTANCE_NAME +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= + +# Check destination directory +final_path="/var/www/$app" +test ! -e "$final_path" || ynh_die "This path already contains a folder" + # Normalize the url path syntax path_url=$(ynh_normalize_url_path "$path_url") @@ -25,16 +44,24 @@ ynh_webpath_available "$domain" "$path_url" # Register (book) web path ynh_webpath_register "$app" "$domain" "$path_url" -ynh_app_setting_set "$app" admin_user "$admin" - -# Retrieve admin email -email=$(ynh_user_get_info "$admin" mail) - -# Check destination directory -final_path="/var/www/$app" -test ! -e "$final_path" || ynh_die "This path already contains a folder" +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_app_setting_set "$app" domain "$domain" ynh_app_setting_set "$app" final_path "$final_path" +ynh_app_setting_set "$app" adminusername "$admin" +ynh_app_setting_set "$app" is_public "$is_public" + + +#================================================= +# STANDARD MODIFICATIONS +#================================================= + + +#================================================= +# CREATE A MYSQL DATABASE +#================================================ # Generate random password dbpass=$(ynh_string_random) @@ -44,11 +71,41 @@ dbuser=$app # Initialize database and store mysql password for upgrade ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" ynh_app_setting_set "$app" mysqlpwd "$dbpass" -ynh_app_setting_set "$app" adminusername "$admin" -ynh_app_setting_set "$app" is_public "$is_public" +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +# Download, check integrity, uncompress and patch the source from app.src ynh_setup_source "$final_path" +#================================================= +# NGINX CONFIGURATION +#================================================= + +ynh_add_nginx_config + +#================================================= +# CREATE DEDICATED USER +#================================================= + + + +#================================================= +# PHP-FPM CONFIGURATION +#================================================= + +ynh_add_fpm_config + +#================================================= +# SPECIFIC SETUP +#================================================= +# Create config.php +#================================================= + +# Retrieve admin email +email=$(ynh_user_get_info "$admin" mail) + # Copy and edit config.php config_php="${final_path}/config.php" @@ -59,27 +116,27 @@ ynh_replace_string "yuno_admin" "$admin" "$config_php" ynh_replace_string "yuno_email" "$email" "$config_php" ynh_replace_string "yuno_domain" "$domain" "$config_php" +#================================================= +# Database initialization +#================================================= + +# Init database +ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < "${final_path}/app/Schema/Sql/mysql.sql" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================ + # Set permissions to kanboard and data directory chown -R root:root "$final_path" chown -R www-data "$final_path"/{data,plugins} -# Copy and set php-fpm configuration -phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf" - -cp ../conf/php-fpm.conf "$phpfpm_conf" -ynh_replace_string "#POOLNAME#" "$app" "$phpfpm_conf" -ynh_replace_string "#DESTDIR#" "$final_path" "$phpfpm_conf" - -chown root: "$phpfpm_conf" -chmod 644 "$phpfpm_conf" - #================================================= -# NGINX CONFIGURATION +# SETUP SSOWAT #================================================= -# Create a dedicated nginx config -ynh_add_nginx_config - if [[ "$path_url" == "/" ]] then # ynh panel is only comptable with non-root installation @@ -98,8 +155,9 @@ then ynh_replace_string "define('DISABLE_LOGOUT'.*$" "define('DISABLE_LOGOUT', false);" "$config_php" fi -# Init database -ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < "${final_path}/app/Schema/Sql/mysql.sql" +#================================================= +# RELOAD NGINX & PHP5-FPM +#================================================= # Reload services service php5-fpm restart diff --git a/scripts/remove b/scripts/remove index 59500e1..a03e600 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,28 +1,48 @@ #!/bin/bash -# Source local helpers -source ./_common.sh +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# 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) +final_path=$(ynh_app_setting_get "$app" final_path) + dbname=$app dbuser=$app -# Drop MySQL database and user -ynh_mysql_drop_db "$dbname" -ynh_mysql_drop_user "$dbuser" +#================================================= +# REMOVE THE MYSQL DATABASE +#================================================= -# Retrieve domain from app settings -domain=$(ynh_app_setting_get "$app" domain) +# Remove a database if it exists, along with the associated user +ynh_mysql_remove_db "$dbuser" "$dbname" -# Delete app directory and configurations -ynh_secure_remove "/var/www/${app}" -ynh_secure_remove "/etc/php5/fpm/pool.d/${app}.conf" -ynh_secure_remove "/etc/nginx/conf.d/${domain}.d/${app}.conf" +#================================================= +# REMOVE APP MAIN DIR +#================================================= -# Reload services -service php5-fpm restart -service nginx reload +ynh_secure_remove "$final_path" + +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= + +# Remove the dedicated nginx config +ynh_remove_nginx_config + +#================================================= +# REMOVE PHP-FPM CONFIGURATION +#================================================= + +ynh_remove_fpm_config diff --git a/scripts/restore b/scripts/restore index 55e1031..0688853 100644 --- a/scripts/restore +++ b/scripts/restore @@ -1,14 +1,31 @@ #!/bin/bash -# Source local helpers -source ./_common.sh +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Source app helpers +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 source /usr/share/yunohost/helpers -# Abort script if errors +#================================================= +# 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 # Set app specific variables @@ -21,27 +38,56 @@ path_url=$(ynh_app_setting_get "$app" path) dbpass=$(ynh_app_setting_get "$app" mysqlpwd) final_path=$(ynh_app_setting_get "$app" final_path) -# Check web path availability -ynh_webpath_available "$domain" "$path_url" +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= -# Check destination directory -test ! -e "$final_path" || ynh_die "This path already contains a folder" +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 " + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= + +ynh_restore_file "/etc/nginx/conf.d/${domain}.d/${app}.conf" + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= # Restore the app files ynh_restore_file "$final_path" -# Create and restore the database -ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" +#================================================= +# RESTORE THE MYSQL DATABASE +#================================================= + +ynh_mysql_setup_db "$dbuser" "$dbname" "$dbpass" ynh_mysql_connect_as "$dbuser" "$dbpass" "$dbname" < ./db.sql -# Restore permissions +#================================================= +# RESTORE USER RIGHTS +#================================================= + chown -R root:root "$final_path" chown -R www-data "$final_path"/{data,plugins} -# Restore configuration 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" -# Reload services +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= + service php5-fpm restart service nginx reload diff --git a/scripts/upgrade b/scripts/upgrade index dc2e9ce..124dc53 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,22 +1,30 @@ #!/bin/bash -# Source local helpers -source ./_common.sh +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= -# Source app helpers +source _common.sh source /usr/share/yunohost/helpers -# Abort script if errors +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= + +# Exit if an error occurs during the execution of the script ynh_abort_if_errors +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= + app=$YNH_APP_INSTANCE_NAME # Set app specific variables dbuser=$app -# Source app helpers -source /usr/share/yunohost/helpers - # Retrieve settings domain=$(ynh_app_setting_get "$app" domain) path_url=$(ynh_app_setting_get "$app" path) @@ -32,8 +40,18 @@ then # Old version doesnt have is_public settings ynh_app_setting_set "$app" is_public "$is_public" fi -# flush php sessions before upgrade -ynh_secure_remove /var/lib/php5/session/* +#================================================= +# CHECK THE PATH +#================================================= + +# Normalize the URL path syntax +path_url=$(ynh_normalize_url_path $path_url) + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= # Move old app dir mv "$final_path" "$final_path.old" @@ -51,6 +69,30 @@ fi # delete temp directory ynh_secure_remove "$final_path.old" +#================================================= +# NGINX CONFIGURATION +#================================================= + +# Create a dedicated nginx config +ynh_add_nginx_config + + +# flush php sessions before upgrade +ynh_secure_remove /var/lib/php5/session/* + +#================================================= +# PHP-FPM CONFIGURATION +#================================================= + +# Create a dedicated php-fpm config +ynh_add_fpm_config + +#================================================= +# SPECIFIC UPGRADE +#================================================= +# Create config.php +#================================================= + # Copy and edit config.php config_php="$final_path/config.php" cp ../conf/config.php "$config_php" @@ -61,26 +103,26 @@ ynh_replace_string "yuno_admin" "$admin" "$config_php" ynh_replace_string "yuno_email" "$email" "$config_php" ynh_replace_string "yuno_domain" "$domain" "$config_php" -# Set permissions to kanboard and data directory -chown -R root:root "$final_path" -chown -R www-data "$final_path"/{data,plugins} +#================================================= +# Database initialization +#================================================= # Launch database migration "$final_path"/cli db:migrate -# Copy and set php-fpm configuration -phpfpm_conf="/etc/php5/fpm/pool.d/${app}.conf" -cp ../conf/php-fpm.conf "$phpfpm_conf" +#================================================= +# GENERIC FINALIZATION +#================================================= +# SECURE FILES AND DIRECTORIES +#================================================= -ynh_replace_string "#USER#" "$app" "$phpfpm_conf" -ynh_replace_string "#GROUP#" "$app" "$phpfpm_conf" -ynh_replace_string "#POOLNAME#" "$app" "$phpfpm_conf" -ynh_replace_string "#DESTDIR#" "$final_path" "$phpfpm_conf" +# Set permissions to kanboard and data directory +chown -R root:root "$final_path" +chown -R www-data "$final_path"/{data,plugins} -chown root: "$phpfpm_conf" -chmod 644 "$phpfpm_conf" - -ynh_add_nginx_config +#================================================= +# SETUP SSOWAT +#================================================= if [[ "$path_url" == "/" ]] then @@ -100,6 +142,9 @@ then ynh_replace_string "define('DISABLE_LOGOUT'.*$" "define('DISABLE_LOGOUT', false);" "$config_php" fi -# Reload services +#================================================= +# RELOAD NGINX +#================================================= + service php5-fpm restart -service nginx reload +systemctl reload nginx