From 9085643892836479b8c33bba6f5054512e4d441f Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Wed, 30 Jan 2019 16:15:24 +0100 Subject: [PATCH] Add progression bar --- scripts/_common.sh | 78 ++++++++++++++++++++++++++++++++++ scripts/actions/public_private | 15 +++++++ scripts/backup | 13 ++++++ scripts/change_url | 14 ++++++ scripts/install | 19 +++++++++ scripts/remove | 13 ++++++ scripts/restore | 14 ++++++ scripts/upgrade | 19 ++++++++- 8 files changed, 184 insertions(+), 1 deletion(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index caf8bc7..278d8c7 100755 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -629,6 +629,84 @@ ynh_clean_check_starting () { #================================================= +# Print a message as INFO and show progression during an app script +# +# usage: ynh_script_progression --message=message [--weight=weight] [--time] +# | arg: -m, --message= - The text to print +# | arg: -w, --weight= - The weight for this progression. This value is 1 by default. Use a bigger value for a longer part of the script. +# | arg: -t, --time= - Print the execution time since the last call to this helper. Especially usefull to define weights. +# | arg: -l, --last= - Use for the last call of the helper, to fill te progression bar. +increment_progression=0 +previous_weight=0 +# Define base_time when the file is sourced +base_time=$(date +%s) +ynh_script_progression () { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [m]=message= [w]=weight= [t]=time [l]=last ) + local message + local weight + local time + local last + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + weight=${weight:-1} + time=${time:-0} + last=${last:-0} + + # Get execution time since the last $base_time + local exec_time=$(( $(date +%s) - $base_time )) + base_time=$(date +%s) + + # Get the number of occurrences of 'ynh_script_progression' in the script. Except those are commented. + local helper_calls="$(grep --count "^[^#]*ynh_script_progression" $0)" + # Get the number of call with a weight value + local weight_calls=$(grep --perl-regexp --count "^[^#]*ynh_script_progression.*(--weight|-w )" $0) + + # Get the weight of each occurrences of 'ynh_script_progression' in the script using --weight + local weight_valuesA="$(grep --perl-regexp "^[^#]*ynh_script_progression.*--weight" $0 | sed 's/.*--weight[= ]\([[:digit:]].*\)/\1/g')" + # Get the weight of each occurrences of 'ynh_script_progression' in the script using -w + local weight_valuesB="$(grep --perl-regexp "^[^#]*ynh_script_progression.*-w " $0 | sed 's/.*-w[= ]\([[:digit:]].*\)/\1/g')" + # Each value will be on a different line. + # Remove each 'end of line' and replace it by a '+' to sum the values. + local weight_values=$(( $(echo "$weight_valuesA" | tr '\n' '+') + $(echo "$weight_valuesB" | tr '\n' '+') 0 )) + + # max_progression is a total number of calls to this helper. + # Less the number of calls with a weight value. + # Plus the total of weight values + local max_progression=$(( $helper_calls - $weight_calls + $weight_values )) + + # Increment each execution of ynh_script_progression in this script by the weight of the previous call. + increment_progression=$(( $increment_progression + $previous_weight )) + # Store the weight of the current call in $previous_weight for next call + previous_weight=$weight + + # Set the scale of the progression bar + local scale=20 + # progress_string(1,2) should have the size of the scale. + local progress_string1="####################" + local progress_string0="...................." + + # Reduce $increment_progression to the size of the scale + if [ $last -eq 0 ] + then + local effective_progression=$(( $increment_progression * $scale / $max_progression )) + # If last is specified, fill immediately the progression_bar + else + local effective_progression=$scale + fi + + # Build $progression_bar from progress_string(1,2) according to $effective_progression + local progression_bar="${progress_string1:0:$effective_progression}${progress_string0:0:$(( $scale - $effective_progression ))}" + + local print_exec_time="" + if [ $time -eq 1 ] + then + print_exec_time=" [$(date +%Hh%Mm,%Ss --date="0 + $exec_time sec")]" + fi + + ynh_print_info "[$progression_bar] > ${message}${print_exec_time}" +} + # Send an email to inform the administrator # # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type] diff --git a/scripts/actions/public_private b/scripts/actions/public_private index f685d1e..6fcc6c9 100755 --- a/scripts/actions/public_private +++ b/scripts/actions/public_private @@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers #================================================= # RETRIEVE ARGUMENTS #================================================= +ynh_script_progression --message="Retrieve arguments from the manifest" # Get is_public is_public=${YNH_ACTION_IS_PUBLIC} @@ -38,6 +39,12 @@ fi #================================================= # MOVE TO PUBLIC OR PRIVATE #================================================= +if [ $is_public -eq 0 ]; then + public_private="private" +else + public_private="public" +fi +ynh_script_progression --message="Move the application to $public_private" --weight=3 if [ $is_public -eq 0 ] then @@ -49,6 +56,7 @@ else ynh_app_setting_set $app unprotected_uris "/" fi +ynh_script_progression --message="Reconfigure SSOwat" # Regen ssowat configuration yunohost app ssowatconf @@ -58,5 +66,12 @@ ynh_app_setting_set $app is_public $is_public #================================================= # RELOAD NGINX #================================================= +ynh_script_progression --message="Reload nginx" ynh_systemd_action --action=reload --service_name=nginx + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Execution completed" --last diff --git a/scripts/backup b/scripts/backup index 6f7b599..dd134f7 100644 --- a/scripts/backup +++ b/scripts/backup @@ -19,6 +19,7 @@ ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" --weight=2 app=$YNH_APP_INSTANCE_NAME @@ -32,6 +33,7 @@ db_pwd=$(ynh_app_setting_get "$app" mysqlpwd) #================================================= # BACKUP THE APP MAIN DIR #================================================= +ynh_script_progression --message="Backup the app main dir" CHECK_SIZE "$final_path" ynh_backup "$final_path" @@ -39,18 +41,21 @@ ynh_backup "$final_path" #================================================= # BACKUP NGINX CONFIGURATION #================================================= +ynh_script_progression --message="Backup nginx configuration" ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # BACKUP PHP-FPM CONFIGURATION #================================================= +ynh_script_progression --message="Backup php-fpm configuration" ynh_backup "/etc/php5/fpm/pool.d/$app.conf" #================================================= # BACKUP THE MYSQL DATABASE #================================================= +ynh_script_progression --message="Backup the mysql database" --weight=2 ynh_mysql_dump_db "$db_name" > db.sql CHECK_SIZE "db.sql" @@ -58,6 +63,7 @@ CHECK_SIZE "db.sql" #================================================= # BACKUP FAIL2BAN CONFIGURATION #================================================= +ynh_script_progression --message="Backup fail2ban configuration" ynh_backup "/etc/fail2ban/jail.d/$app.conf" ynh_backup "/etc/fail2ban/filter.d/$app.conf" @@ -67,5 +73,12 @@ ynh_backup "/etc/fail2ban/filter.d/$app.conf" #================================================= # BACKUP THE CRON FILE #================================================= +ynh_script_progression --message="Backup cron file" ynh_backup "/etc/cron.d/$app" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Backup completed" --last diff --git a/scripts/change_url b/scripts/change_url index 75d1c69..7e047ee 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers #================================================= # RETRIEVE ARGUMENTS #================================================= +ynh_script_progression --message="Retrieve arguments from the manifest" old_domain=$YNH_APP_OLD_DOMAIN old_path=$YNH_APP_OLD_PATH @@ -24,12 +25,14 @@ app=$YNH_APP_INSTANCE_NAME #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" --weight=2 final_path=$(ynh_app_setting_get $app final_path) #================================================= # CHECK THE SYNTAX OF THE PATHS #================================================= +ynh_script_progression --message="Check the syntax of the paths" test -n "$old_path" || old_path="/" test -n "$new_path" || new_path="/" @@ -39,6 +42,7 @@ old_path=$(ynh_normalize_url_path $old_path) #================================================= # ACTIVATE MAINTENANCE MODE #================================================= +ynh_script_progression --message="Activate maintenance mode" path_url=$old_path domain=$old_domain @@ -72,6 +76,7 @@ ynh_abort_if_errors #================================================= # MODIFY URL IN NGINX CONF #================================================= +ynh_script_progression --message="Modify url in nginx configuration" --weight=2 nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf @@ -121,6 +126,7 @@ ynh_replace_string "https://$old_domain${old_path}" "https://$domain_path" "/et #================================================= # UPDATE THE DATABASE #================================================= +ynh_script_progression --message="Update the database" ynh_mysql_execute_as_root "UPDATE leed_configuration SET value='$domain_path/' WHERE value LIKE '%${old_domain}%'" $app @@ -129,13 +135,21 @@ ynh_mysql_execute_as_root "UPDATE leed_configuration SET value='$domain_path/' W #================================================= # RELOAD NGINX #================================================= +ynh_script_progression --message="Reload nginx" ynh_systemd_action --action=reload --service_name=nginx #================================================= # DEACTIVE MAINTENANCE MODE #================================================= +ynh_script_progression --message="Deactive maintenance mode" path_url=$old_path domain=$old_domain ynh_maintenance_mode_OFF + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Change of url completed" --last diff --git a/scripts/install b/scripts/install index 52ad340..89820a0 100644 --- a/scripts/install +++ b/scripts/install @@ -19,6 +19,7 @@ ynh_abort_if_errors #================================================= # RETRIEVE ARGUMENTS FROM THE MANIFEST #================================================= +ynh_script_progression --message="Retrieve arguments from the manifest" domain=$YNH_APP_ARG_DOMAIN path_url=$YNH_APP_ARG_PATH @@ -32,6 +33,7 @@ app=$YNH_APP_INSTANCE_NAME #================================================= # CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS #================================================= +ynh_script_progression --message="Check if the app can be installed" --weight=2 final_path=/var/www/$app test ! -e "$final_path" || ynh_die "This path already contains a folder" @@ -45,6 +47,7 @@ ynh_webpath_register $app $domain $path_url #================================================= # STORE SETTINGS FROM MANIFEST #================================================= +ynh_script_progression --message="Store settings from manifest" --weight=2 ynh_app_setting_set $app admin $admin ynh_app_setting_set $app language $language @@ -57,6 +60,7 @@ ynh_app_setting_set $app overwrite_phpfpm "1" #================================================= # CREATE A MYSQL DATABASE #================================================= +ynh_script_progression --message="Create a mysql database" db_name=$(ynh_sanitize_dbid $app) ynh_app_setting_set $app db_name $db_name @@ -65,6 +69,7 @@ ynh_mysql_setup_db $db_name $db_name #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= +ynh_script_progression --message="Download, check and unpack source" --weight=3 ynh_app_setting_set $app final_path $final_path # Download, check integrity and uncompress the source from app.src @@ -73,6 +78,7 @@ ynh_setup_source "$final_path" #================================================= # NGINX CONFIGURATION #================================================= +ynh_script_progression --message="Configure nginx" --weight=2 # Create a dedicated nginx config ynh_add_nginx_config @@ -80,6 +86,7 @@ ynh_add_nginx_config #================================================= # CREATE DEDICATED USER #================================================= +ynh_script_progression --message="Create a dedicated user" --weight=2 # Create a dedicated system user ynh_system_user_create $app @@ -87,6 +94,7 @@ ynh_system_user_create $app #================================================= # PHP-FPM CONFIGURATION #================================================= +ynh_script_progression --message="Configure php-fpm" --weight=2 # Create a dedicated php-fpm config ynh_add_fpm_config @@ -96,6 +104,7 @@ ynh_add_fpm_config #================================================= # SETTING UP WITH CURL #================================================= +ynh_script_progression --message="Install Leed with Curl" --weight=5 # Set right permissions for curl install chown -R $app: $final_path @@ -122,6 +131,7 @@ code_sync=$(mysql -h localhost -u $db_name -p$db_pwd -s $db_name -e 'SELECT valu #================================================= # SETUP CRON FILE FOR SYNCHRONISATION #================================================= +ynh_script_progression --message="Setup a cron file" ynh_replace_string "__ADMIN__" "$admin" ../conf/cron_leed ynh_replace_string "__DOMAIN__" "$domain" ../conf/cron_leed @@ -144,6 +154,7 @@ chown -R $app $final_path/cache $final_path/plugins $final_path/updates #================================================= # SETUP FAIL2BAN #================================================= +ynh_script_progression --message="Configure fail2ban" --weight=9 # Create a dedicated fail2ban config ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="PHP message: Leed: wrong login for .* client: " --max_retry=5 @@ -151,6 +162,7 @@ ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failreg #================================================= # SETUP SSOWAT #================================================= +ynh_script_progression --message="Setup SSOwat" --weight=2 # Make app private if necessary ynh_app_setting_set $app is_public "$is_public" @@ -165,6 +177,7 @@ fi #================================================= # RELOAD NGINX #================================================= +ynh_script_progression --message="Reload nginx" --weight=3 ynh_systemd_action --action=reload --service_name=nginx @@ -185,3 +198,9 @@ If you're facing an issue or want to improve this app, please open a new issue i ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="install" ynh_print_ON + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Installation completed" --last diff --git a/scripts/remove b/scripts/remove index 4690bbc..d8a4759 100644 --- a/scripts/remove +++ b/scripts/remove @@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" --weight=2 app=$YNH_APP_INSTANCE_NAME @@ -23,6 +24,7 @@ db_name=$(ynh_app_setting_get $app db_name) #================================================= # REMOVE THE MYSQL DATABASE #================================================= +ynh_script_progression --message="Remove the mysql database" # Remove a database if it exists, along with the associated user ynh_mysql_remove_db $db_name $db_name @@ -30,6 +32,7 @@ ynh_mysql_remove_db $db_name $db_name #================================================= # REMOVE APP MAIN DIR #================================================= +ynh_script_progression --message="Remove app main directory" # Remove the app directory securely ynh_secure_remove "/var/www/$app" @@ -37,6 +40,7 @@ ynh_secure_remove "/var/www/$app" #================================================= # REMOVE NGINX CONFIGURATION #================================================= +ynh_script_progression --message="Remove nginx configuration" --weight=2 # Remove the dedicated nginx config ynh_remove_nginx_config @@ -44,6 +48,7 @@ ynh_remove_nginx_config #================================================= # REMOVE PHP-FPM CONFIGURATION #================================================= +ynh_script_progression --message="Remove php-fpm configuration" # Remove the dedicated php-fpm config ynh_remove_fpm_config @@ -51,6 +56,7 @@ ynh_remove_fpm_config #================================================= # REMOVE FAIL2BAN CONFIGURATION #================================================= +ynh_script_progression --message="Remove fail2ban configuration" --weight=5 # Remove the dedicated fail2ban config ynh_remove_fail2ban_config @@ -68,6 +74,13 @@ ynh_secure_remove "/etc/cron.d/$app" #================================================= # REMOVE DEDICATED USER #================================================= +ynh_script_progression --message="Remove the dedicated user" --weight=2 # Delete dedicated system user ynh_system_user_delete $app + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Deletion completed" --last diff --git a/scripts/restore b/scripts/restore index 0a5057d..f79b11a 100644 --- a/scripts/restore +++ b/scripts/restore @@ -19,6 +19,7 @@ ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" --weight=2 app=$YNH_APP_INSTANCE_NAME @@ -40,6 +41,7 @@ test ! -d $final_path \ #================================================= # ACTIVATE MAINTENANCE MODE #================================================= +ynh_script_progression --message="Activate maintenance mode" --weight=2 ynh_maintenance_mode_ON @@ -54,12 +56,14 @@ ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # RESTORE THE APP MAIN DIR #================================================= +ynh_script_progression --message="Restore the app main directory" ynh_restore_file "$final_path" #================================================= # RESTORE THE MYSQL DATABASE #================================================= +ynh_script_progression --message="Restore the mysql database" --weight=3 db_pwd=$(ynh_app_setting_get $app mysqlpwd) ynh_mysql_setup_db $db_name $db_name $db_pwd @@ -68,6 +72,7 @@ ynh_mysql_connect_as $db_name $db_pwd $db_name < ./db.sql #================================================= # RECREATE THE DEDICATED USER #================================================= +ynh_script_progression --message="Recreate the dedicated user" --weight=2 # Create the dedicated user (if not existing) ynh_system_user_create $app @@ -81,6 +86,7 @@ ynh_restore_file "/etc/php5/fpm/pool.d/$app.conf" #================================================= # RESTORE FAIL2BAN CONFIGURATION #================================================= +ynh_script_progression --message="Restore the fail2ban configuration" --weight=7 ynh_restore_file "/etc/fail2ban/jail.d/$app.conf" ynh_restore_file "/etc/fail2ban/filter.d/$app.conf" @@ -105,6 +111,7 @@ chown -R $app $final_path/cache $final_path/plugins $final_path/updates #================================================= # RELOAD NGINX AND PHP-FPM #================================================= +ynh_script_progression --message="Reload nginx and php-fpm" ynh_systemd_action --action=reload --service_name=php5-fpm ynh_systemd_action --action=reload --service_name=nginx @@ -112,6 +119,7 @@ ynh_systemd_action --action=reload --service_name=nginx #================================================= # DEACTIVE MAINTENANCE MODE #================================================= +ynh_script_progression --message="Deactive maintenance mode" --weight=2 ynh_maintenance_mode_OFF @@ -128,3 +136,9 @@ You can also find some specific actions for this app by using the experimental a If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/leed_ynh" ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="restore" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Restoration completed" --last diff --git a/scripts/upgrade b/scripts/upgrade index 86e56ea..ab11324 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" --weight=3 app=$YNH_APP_INSTANCE_NAME @@ -34,6 +35,7 @@ upgrade_type=$(ynh_check_app_version_changed) #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= +ynh_script_progression --message="Ensure downward compatibility" # If final_path doesn't exist, create it if [ -z "$final_path" ]; then @@ -83,6 +85,7 @@ fi #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= +ynh_script_progression --message="Backup the app before upgrading" --weight=4 # Backup the current version of the app ynh_backup_before_upgrade @@ -103,6 +106,7 @@ path_url=$(ynh_normalize_url_path $path_url) #================================================= # ACTIVATE MAINTENANCE MODE #================================================= +ynh_script_progression --message="Activate maintenance mode" ynh_maintenance_mode_ON @@ -114,6 +118,7 @@ ynh_maintenance_mode_ON if [ "$upgrade_type" == "UPGRADE_APP" ] then + ynh_script_progression --message="Download, check and unpack source" --weight=3 # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source "$final_path" fi @@ -125,6 +130,7 @@ fi # Overwrite the nginx configuration only if it's allowed if [ $overwrite_nginx -eq 1 ] then + ynh_script_progression --message="Reconfigure nginx" --weight=2 # Create a dedicated nginx config ynh_add_nginx_config fi @@ -132,6 +138,7 @@ fi #================================================= # CREATE DEDICATED USER #================================================= +ynh_script_progression --message="Create a dedicated user" # Create a dedicated user (if not existing) ynh_system_user_create $app @@ -143,6 +150,7 @@ ynh_system_user_create $app # Overwrite the php-fpm configuration only if it's allowed if [ $overwrite_phpfpm -eq 1 ] then + ynh_script_progression --message="Reconfigure php-fpm" --weight=2 # Create a dedicated php-fpm config ynh_add_fpm_config fi @@ -181,6 +189,7 @@ chown -R $app $final_path/cache $final_path/plugins $final_path/updates if [ "$upgrade_type" == "UPGRADE_APP" ] then + ynh_script_progression --message="Upgrade Leed with curl" --weight=4 # Clear leed cache ynh_secure_remove $final_path/cache/* # Set the app as temporarily public for curl call @@ -196,6 +205,7 @@ fi #================================================= # UPGRADE FAIL2BAN #================================================= +ynh_script_progression --message="Reconfigure fail2ban" --weight=8 # Create a dedicated fail2ban config ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failregex="PHP message: Leed: wrong login for .* client: " --max_retry=5 @@ -203,6 +213,7 @@ ynh_add_fail2ban_config --logpath="/var/log/nginx/${domain}-error.log" --failreg #================================================= # SETUP SSOWAT #================================================= +ynh_script_progression --message="Reconfigure SSOwat" # Make app private if necessary if [ $is_public -eq 0 ]; @@ -216,12 +227,14 @@ fi #================================================= # RELOAD NGINX #================================================= +ynh_script_progression --message="Reload nginx" --weight=2 ynh_systemd_action --action=reload --service_name=nginx #================================================= # DEACTIVE MAINTENANCE MODE #================================================= +ynh_script_progression --message="Deactive maintenance mode" ynh_maintenance_mode_OFF @@ -237,4 +250,8 @@ You can also find some specific actions for this app by using the experimental a If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/leed_ynh" -ynh_send_readme_to_admin --app_message="$message" --recipients="$admin" --type="upgrade" +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Upgrade completed" --last