From c964ac3b9ff8ce66c3c698521d84d33e441e91de Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Wed, 30 Jan 2019 16:21:42 +0100 Subject: [PATCH] Add progression bar --- scripts/_common.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/install | 13 +++++++- scripts/remove | 6 +++- scripts/upgrade | 9 ++++++ 4 files changed, 104 insertions(+), 2 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index caf8bc7..278d8c7 100644 --- 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/install b/scripts/install index a359dde..e8e7818 100755 --- 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" --weight=2 ssh_user=$YNH_APP_ARG_SSH_USER # Default value is null @@ -40,6 +41,7 @@ fi #================================================= # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS #================================================= +ynh_script_progression --message="Check if the app can be installed" --weight=2 # Correct user name # An unix user name can contains only :alnum: and . - _ @@ -61,6 +63,7 @@ ynh_print_ON #================================================= # STORE SETTINGS FROM MANIFEST #================================================= +ynh_script_progression --message="Store settings from manifest" ynh_app_setting_set $app ssh_user $ssh_user ynh_app_setting_set $app size $size @@ -70,6 +73,7 @@ ynh_app_setting_set $app size $size #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= +ynh_script_progression --message="Download, check and unpack source" --weight=2 ynh_app_setting_set $app final_path $final_path # Download, check integrity, uncompress and patch the source from app.src @@ -94,6 +98,7 @@ mkdir -p "$user_dir" #================================================= # INSTALL QUOTAS SYSTEM #================================================= +ynh_script_progression --message="Install quotas system" --weight=3 # https://github.com/maniackcrudelis/ssh_chroot/blob/master/unix_quotas/unix_quotas.sh quotas_install @@ -101,6 +106,7 @@ quotas_install #================================================= # CONFIGURE FSTAB TO SUPPORT QUOTAS #================================================= +ynh_script_progression --message="Configure fstab to support quotas" --weight=3 # https://github.com/maniackcrudelis/ssh_chroot/blob/master/unix_quotas/unix_quotas.sh # Set fstab @@ -113,6 +119,7 @@ quotas_activate "$quotas_mount_point" #================================================= # CREATE THE USER WITH CHROOT_MANAGER #================================================= +ynh_script_progression --message="Create the user with Chroot_manager" --weight=5 add_password="" ynh_print_OFF @@ -175,4 +182,8 @@ message="A new chrooted directory has been created. To use it, connect to 'ssh $ 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/ssh_chroot_dir_ynh" -ynh_send_readme_to_admin --app_message="$message" --recipients="root" +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Installation completed" --last diff --git a/scripts/remove b/scripts/remove index a533781..670c3f6 100755 --- a/scripts/remove +++ b/scripts/remove @@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" app=$YNH_APP_INSTANCE_NAME @@ -30,6 +31,7 @@ source "$final_path/unix_quotas/unix_quotas.sh" #================================================= # REMOVE QUOTAS OPTIONS IN FSTAB #================================================= +ynh_script_progression --message="Remove quotas options in fstab" --weight=3 user_dir="/home/yunohost.app/ssh_chroot_directories/$ssh_user" @@ -43,6 +45,7 @@ quotas_deactivate "$quotas_mount_point" #================================================= # REMOVE THE USER WITH CHROOT_MANAGER #================================================= +ynh_script_progression --message="Remove the user with Chroot_manager" --weight=3 $final_path/chroot_manager.sh deluser --name $ssh_user @@ -51,6 +54,7 @@ $final_path/chroot_manager.sh deluser --name $ssh_user #================================================= # REMOVE APP MAIN DIR #================================================= +ynh_script_progression --message="Remove app main directory" # Remove the app directory securely ynh_secure_remove "$final_path" @@ -60,4 +64,4 @@ ynh_secure_remove "$final_path" #================================================= ynh_print_info " -The directory /home/yunohost.app/ssh_chroot_directories hasn't been removed." >&2 +The directory /home/yunohost.app/ssh_chroot_directories hasn't been removed." diff --git a/scripts/upgrade b/scripts/upgrade index c25b15d..b1a6775 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -19,6 +19,7 @@ ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= +ynh_script_progression --message="Load settings" app=$YNH_APP_INSTANCE_NAME @@ -39,6 +40,7 @@ upgrade_type=$(ynh_check_app_version_changed) 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 @@ -55,6 +57,7 @@ source "$final_path/ssh_chroot/ssh_chroot.sh" #================================================= # UPDATE THE CHROOT DIRECTORY #================================================= +ynh_script_progression --message="Update the chroot directory" --weight=3 user_dir="/home/yunohost.app/ssh_chroot_directories/$ssh_user" @@ -83,3 +86,9 @@ ln -sf $final_path/chroot_manager.sh /home/yunohost.app/ssh_chroot_directories/c # Set permissions to app files chown -R root: $final_path + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Upgrade completed" --last