Browse Source

Add progression bar

Maniack Crudelis 6 years ago
parent
commit
c626cb05d0

+ 78 - 0
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]

+ 8 - 0
scripts/actions/clean_backups

@@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers
 #=================================================
 # RETRIEVE ARGUMENTS
 #=================================================
+ynh_script_progression --message="Retrieve arguments from the manifest"
 
 app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID}
 
@@ -30,6 +31,7 @@ final_path=$(ynh_app_setting_get $app final_path)
 #=================================================
 # CLEAN ALL BACKUP FILES
 #=================================================
+ynh_script_progression --message="Clean all backup files" --weight=9
 
 # Get the backup directory from the config file
 backup_dir="$(grep "^backup_dir=" "$final_path/Backup_list.conf" | cut -d= -f2)"
@@ -42,3 +44,9 @@ do
 		ynh_secure_remove "$directory"
 	fi
 done 3<<< $(find "$backup_dir" -maxdepth 1 -mindepth 1 -type d)
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Execution completed" --last

+ 8 - 0
scripts/actions/force_backup

@@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers
 #=================================================
 # RETRIEVE ARGUMENTS
 #=================================================
+ynh_script_progression --message="Retrieve arguments from the manifest"
 
 app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID}
 
@@ -30,5 +31,12 @@ final_path=$(ynh_app_setting_get $app final_path)
 #=================================================
 # FORCE A NEW BACKUP
 #=================================================
+ynh_script_progression --message="Force a new backup" --weight=9
 
 ynh_exec_warn nice -n10 $final_path/archivist.sh
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Execution completed" --last

+ 8 - 0
scripts/actions/reset_default_config

@@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers
 #=================================================
 # RETRIEVE ARGUMENTS
 #=================================================
+ynh_script_progression --message="Retrieve arguments from the manifest"
 
 app=${YNH_APP_INSTANCE_NAME:-$YNH_APP_ID}
 final_path=$(ynh_app_setting_get $app final_path)
@@ -34,6 +35,7 @@ fi
 #=================================================
 # RESET THE CONFIG FILE
 #=================================================
+ynh_script_progression --message="Reset the config file $file"
 
 # Verify the checksum and backup the file if it's different
 ynh_backup_if_checksum_is_different "$config_file"
@@ -80,3 +82,9 @@ fi
 
 # Calculate and store the config file checksum into the app settings
 ynh_store_file_checksum "$config_file"
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Execution completed" --last

+ 10 - 0
scripts/backup

@@ -19,6 +19,7 @@ ynh_abort_if_errors
 #=================================================
 # LOAD SETTINGS
 #=================================================
+ynh_script_progression --message="Load settings"
 
 app=$YNH_APP_INSTANCE_NAME
 
@@ -29,6 +30,7 @@ final_path=$(ynh_app_setting_get $app final_path)
 #=================================================
 # BACKUP THE APP MAIN DIR
 #=================================================
+ynh_script_progression --message="Backup the app main dir"
 
 ynh_backup "$final_path"
 
@@ -37,11 +39,19 @@ ynh_backup "$final_path"
 #=================================================
 # BACKUP LOGROTATE
 #=================================================
+ynh_script_progression --message="Backup logrotate configuration"
 
 ynh_backup "/etc/logrotate.d/$app"
 
 #=================================================
 # 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

+ 14 - 0
scripts/install

@@ -21,6 +21,7 @@ ynh_abort_if_errors
 #=================================================
 # RETRIEVE ARGUMENTS FROM THE MANIFEST
 #=================================================
+ynh_script_progression --message="Retrieve arguments from the manifest"
 
 encrypt=$YNH_APP_ARG_ENCRYPT
 ynh_print_OFF; encryption_pwd=$YNH_APP_ARG_ENCRYPTION_PWD; ynh_print_ON
@@ -33,6 +34,7 @@ app=$YNH_APP_INSTANCE_NAME
 #=================================================
 # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
 #=================================================
+ynh_script_progression --message="Check if the app can be installed"
 
 final_path=/opt/yunohost/$app
 test ! -e "$final_path" || ynh_die "This path already contains a folder"
@@ -46,6 +48,7 @@ fi
 #=================================================
 # STORE SETTINGS FROM MANIFEST
 #=================================================
+ynh_script_progression --message="Store settings from manifest" --weight=3
 
 ynh_app_setting_set $app frequency "$frequency"
 ynh_app_setting_set $app encrypt "$encrypt"
@@ -58,6 +61,7 @@ ynh_app_setting_set $app overwrite_cron "1"
 #=================================================
 # INSTALL DEPENDENCIES
 #=================================================
+ynh_script_progression --message="Install dependencies" --weight=15
 
 # Valid the fucking debconf message
 # To find this, install the package, install also debconf-utils
@@ -68,6 +72,7 @@ ynh_install_app_dependencies $app_depencencies
 #=================================================
 # 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, uncompress and patch the source from app.src
@@ -86,6 +91,7 @@ mkdir -p "$backup_dir"
 #=================================================
 # CONFIGURE ARCHIVIST
 #=================================================
+ynh_script_progression --message="Configure Archivist" --weight=2
 
 config_file="$final_path/Backup_list.conf"
 cp "$final_path/Backup_list.conf.default" "$config_file"
@@ -140,6 +146,7 @@ fi
 #=================================================
 # SET THE CRON FILE
 #=================================================
+ynh_script_progression --message="Set the cron file"
 
 cp ../conf/cron /etc/cron.d/$app
 ynh_replace_string "__FINALPATH__" "$final_path" /etc/cron.d/$app
@@ -177,6 +184,7 @@ chown -R root: $final_path
 #=================================================
 # SETUP LOGROTATE
 #=================================================
+ynh_script_progression --message="Configure logrotate"
 
 mkdir -p /var/log/$app
 # Use logrotate to manage application logfile(s)
@@ -221,3 +229,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="root" --type="install"
 ynh_print_ON
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Installation completed" --last

+ 12 - 0
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
 
@@ -22,6 +23,7 @@ final_path=$(ynh_app_setting_get $app final_path)
 #=================================================
 # REMOVE DEPENDENCIES
 #=================================================
+ynh_script_progression --message="Remove dependencies" --weight=7
 
 # Remove metapackage and its dependencies
 ynh_remove_app_dependencies
@@ -29,6 +31,7 @@ ynh_remove_app_dependencies
 #=================================================
 # REMOVE APP MAIN DIR
 #=================================================
+ynh_script_progression --message="Remove app main directory"
 
 # Remove the app directory securely
 ynh_secure_remove "$final_path"
@@ -36,6 +39,7 @@ ynh_secure_remove "$final_path"
 #=================================================
 # REMOVE LOGROTATE CONFIGURATION
 #=================================================
+ynh_script_progression --message="Remove logrotate configuration"
 
 # Remove the app-specific logrotate config
 ynh_remove_logrotate
@@ -49,5 +53,13 @@ ynh_remove_logrotate
 # Remove a cron file
 ynh_secure_remove "/etc/cron.d/$app"
 
+ynh_script_progression --message="Remove backup directory" --weight=5
+
 # Remove the backup directory
 ynh_secure_remove "/home/yunohost.app/${app}/backup"
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Deletion completed" --last

+ 9 - 0
scripts/restore

@@ -21,6 +21,7 @@ ynh_abort_if_errors
 #=================================================
 # LOAD SETTINGS
 #=================================================
+ynh_script_progression --message="Load settings"
 
 app=$YNH_APP_INSTANCE_NAME
 
@@ -38,6 +39,7 @@ test ! -d $final_path \
 #=================================================
 # RESTORE THE APP MAIN DIR
 #=================================================
+ynh_script_progression --message="Restore the app main directory"
 
 ynh_restore_file "$final_path"
 
@@ -56,6 +58,7 @@ fi
 #=================================================
 # REINSTALL DEPENDENCIES
 #=================================================
+ynh_script_progression --message="Reinstall dependencies" --weight=17
 
 # Valid the fucking debconf message
 # To find this, install the package, install also debconf-utils
@@ -100,3 +103,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/archivist_ynh"
 
 ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="restore"
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Restoration completed" --last

+ 13 - 0
scripts/upgrade

@@ -14,6 +14,7 @@ source _variables
 #=================================================
 # LOAD SETTINGS
 #=================================================
+ynh_script_progression --message="Load settings" --weight=3
 
 app=$YNH_APP_INSTANCE_NAME
 
@@ -33,6 +34,7 @@ upgrade_type=$(ynh_check_app_version_changed)
 #=================================================
 # ENSURE DOWNWARD COMPATIBILITY
 #=================================================
+ynh_script_progression --message="Ensure downward compatibility"
 
 # If encrypt doesn't exist, create it
 if [ -z "$encrypt" ]; then
@@ -66,6 +68,7 @@ fi
 #=================================================
 # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
 #=================================================
+ynh_script_progression --message="Backup the app before upgrading" --weight=2
 
 # Backup the current version of the app
 ynh_backup_before_upgrade
@@ -84,6 +87,7 @@ ynh_abort_if_errors
 
 if [ "$upgrade_type" == "UPGRADE_APP" ]
 then
+	ynh_script_progression --message="Download, check and unpack source" --weight=2
 	# Download, check integrity, uncompress and patch the source from app.src
 	ynh_setup_source "$final_path"
 fi
@@ -91,6 +95,7 @@ fi
 #=================================================
 # UPGRADE DEPENDENCIES
 #=================================================
+ynh_script_progression --message="Upgrade dependencies" --weight=8
 
 ynh_install_app_dependencies $app_depencencies
 
@@ -109,6 +114,7 @@ fi
 #=================================================
 # UPDATE THE CRON FILE
 #=================================================
+ynh_script_progression --message="Update the cron file"
 
 # Overwrite the cron file only if it's allowed
 if [ $overwrite_cron -eq 1 ]
@@ -144,6 +150,7 @@ fi
 #=================================================
 # SETUP LOGROTATE
 #=================================================
+ynh_script_progression --message="Reconfigure logrotate"
 
 # Use logrotate to manage app-specific logfile(s)
 ynh_use_logrotate --non-append
@@ -175,3 +182,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/archivist_ynh"
 
 ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="upgrade"
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Upgrade completed" --last