Add progression bar

This commit is contained in:
Maniack Crudelis 2019-01-30 19:18:18 +01:00
parent 5313245422
commit 51a015f726
7 changed files with 136 additions and 2 deletions

View file

@ -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 # Send an email to inform the administrator
# #
# usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type] # usage: ynh_send_readme_to_admin --app_message=app_message [--recipients=recipients] [--type=type]

View file

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

View file

@ -19,6 +19,7 @@ ynh_abort_if_errors
#================================================= #=================================================
# LOAD SETTINGS # LOAD SETTINGS
#================================================= #=================================================
ynh_script_progression --message="Load settings"
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -27,6 +28,7 @@ app=$YNH_APP_INSTANCE_NAME
#================================================= #=================================================
# BACKUP UNATTENDED-UPGRADES CONFIG # BACKUP UNATTENDED-UPGRADES CONFIG
#================================================= #=================================================
ynh_script_progression --message="Backup unattended-upgrades config"
ynh_backup "/etc/apt/apt.conf.d/50unattended-upgrades" ynh_backup "/etc/apt/apt.conf.d/50unattended-upgrades"
# Backup also the backup of the config file # Backup also the backup of the config file
@ -35,17 +37,26 @@ ynh_backup "/etc/apt/50unattended-upgrades.backup"
#================================================= #=================================================
# BACKUP APT PERIODIC # BACKUP APT PERIODIC
#================================================= #=================================================
ynh_script_progression --message="Backup APT periodic"
ynh_backup "/etc/apt/apt.conf.d/02periodic" ynh_backup "/etc/apt/apt.conf.d/02periodic"
#================================================= #=================================================
# BACKUP APTICRON CONFIG # BACKUP APTICRON CONFIG
#================================================= #=================================================
ynh_script_progression --message="Backup Apticron config"
ynh_backup "/etc/apticron/apticron.conf" ynh_backup "/etc/apticron/apticron.conf"
#================================================= #=================================================
# BACKUP THE CRON FILE # BACKUP THE CRON FILE
#================================================= #=================================================
ynh_script_progression --message="Backup cron file"
ynh_backup "/etc/cron.d/apticron" ynh_backup "/etc/cron.d/apticron"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Backup completed" --last

View file

@ -21,6 +21,7 @@ ynh_abort_if_errors
#================================================= #=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST # RETRIEVE ARGUMENTS FROM THE MANIFEST
#================================================= #=================================================
ynh_script_progression --message="Retrieve arguments from the manifest"
upgrade_level="$YNH_APP_ARG_UPGRADE_LEVEL" upgrade_level="$YNH_APP_ARG_UPGRADE_LEVEL"
ynh_update="$YNH_APP_ARG_YNH_UPDATE" ynh_update="$YNH_APP_ARG_YNH_UPDATE"
@ -34,6 +35,7 @@ app=$YNH_APP_INSTANCE_NAME
#================================================= #=================================================
# STORE SETTINGS FROM MANIFEST # STORE SETTINGS FROM MANIFEST
#================================================= #=================================================
ynh_script_progression --message="Store settings from manifest" --weight=3
ynh_app_setting_set $app upgrade_level "$upgrade_level" ynh_app_setting_set $app upgrade_level "$upgrade_level"
ynh_app_setting_set $app ynh_update $ynh_update ynh_app_setting_set $app ynh_update $ynh_update
@ -48,6 +50,7 @@ ynh_app_setting_set $app overwrite_periodic "1"
#================================================= #=================================================
# INSTALL DEPENDENCIES # INSTALL DEPENDENCIES
#================================================= #=================================================
ynh_script_progression --message="Install dependencies" --weight=10
ynh_install_app_dependencies $app_depencencies ynh_install_app_dependencies $app_depencencies
@ -56,6 +59,7 @@ ynh_install_app_dependencies $app_depencencies
#================================================= #=================================================
# CONFIGURE APTICRON # CONFIGURE APTICRON
#================================================= #=================================================
ynh_script_progression --message="Configure Apticron"
apticron_config="/etc/apticron/apticron.conf" apticron_config="/etc/apticron/apticron.conf"
ynh_replace_string "# CUSTOM_SUBJECT=.*" \ ynh_replace_string "# CUSTOM_SUBJECT=.*" \
@ -66,6 +70,7 @@ ynh_replace_string "# CUSTOM_NO_UPDATES_SUBJECT=.*" \
#================================================= #=================================================
# CONFIGURE UNATTENDED-UPGRADES # CONFIGURE UNATTENDED-UPGRADES
#================================================= #=================================================
ynh_script_progression --message="Configure Unattended-Upgrades"
unattended_upgrades_config="/etc/apt/apt.conf.d/50unattended-upgrades" unattended_upgrades_config="/etc/apt/apt.conf.d/50unattended-upgrades"
@ -115,6 +120,7 @@ fi
#================================================= #=================================================
# CONFIGURE APT PERIODIC FOR UNATTENDED # CONFIGURE APT PERIODIC FOR UNATTENDED
#================================================= #=================================================
ynh_script_progression --message="Configure APT Periodic for Unattended" --weight=2
cp "../conf/02periodic" "/etc/apt/apt.conf.d/02periodic" cp "../conf/02periodic" "/etc/apt/apt.conf.d/02periodic"
ynh_replace_string "__VERBOSITY__" "$unattended_verbosity" "/etc/apt/apt.conf.d/02periodic" ynh_replace_string "__VERBOSITY__" "$unattended_verbosity" "/etc/apt/apt.conf.d/02periodic"
@ -125,6 +131,7 @@ ynh_store_file_checksum "/etc/apt/apt.conf.d/02periodic"
#================================================= #=================================================
# MODIFY ORIGINAL APTICRON CRON FILE # MODIFY ORIGINAL APTICRON CRON FILE
#================================================= #=================================================
ynh_script_progression --message="Modify original Apticron cron file" --weight=3
apticron_cron="/etc/cron.d/apticron" apticron_cron="/etc/cron.d/apticron"
# Copy and comment the current cron # Copy and comment the current cron
@ -166,5 +173,9 @@ You can configure this app easily by using the experimental config-panel feature
You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions. You can also find some specific actions for this app by using the experimental action feature: $admin_panel/actions.
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/unattended_upgrades_ynh" 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/unattended_upgrades_ynh"
#=================================================
# END OF SCRIPT
#=================================================
ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="install" ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="install"
ynh_script_progression --message="Installation completed" --last

View file

@ -12,6 +12,7 @@ source /usr/share/yunohost/helpers
#================================================= #=================================================
# LOAD SETTINGS # LOAD SETTINGS
#================================================= #=================================================
ynh_script_progression --message="Load settings"
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -20,6 +21,7 @@ app=$YNH_APP_INSTANCE_NAME
#================================================= #=================================================
# REMOVE DEPENDENCIES # REMOVE DEPENDENCIES
#================================================= #=================================================
ynh_script_progression --message="Remove dependencies" --weight=5
ynh_remove_app_dependencies ynh_remove_app_dependencies
@ -28,11 +30,19 @@ ynh_remove_app_dependencies
#================================================= #=================================================
# REMOVE THE APT PERIODIC FILE # REMOVE THE APT PERIODIC FILE
#================================================= #=================================================
ynh_script_progression --message="Remove the APT Periodic file"
ynh_secure_remove "/etc/apt/apt.conf.d/02periodic" ynh_secure_remove "/etc/apt/apt.conf.d/02periodic"
#================================================= #=================================================
# DECONFIGURE UNATTENDED-UPGRADES # DECONFIGURE UNATTENDED-UPGRADES
#================================================= #=================================================
ynh_script_progression --message="Deconfigure unattended-upgrades"
mv "/etc/apt/50unattended-upgrades.backup" "/etc/apt/apt.conf.d/50unattended-upgrades" mv "/etc/apt/50unattended-upgrades.backup" "/etc/apt/apt.conf.d/50unattended-upgrades"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Deletion completed" --last

View file

@ -21,6 +21,7 @@ ynh_abort_if_errors
#================================================= #=================================================
# LOAD SETTINGS # LOAD SETTINGS
#================================================= #=================================================
ynh_script_progression --message="Load settings"
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -29,6 +30,7 @@ app=$YNH_APP_INSTANCE_NAME
#================================================= #=================================================
# REINSTALL DEPENDENCIES # REINSTALL DEPENDENCIES
#================================================= #=================================================
ynh_script_progression --message="Reinstall dependencies" --weight=10
ynh_install_app_dependencies $app_depencencies ynh_install_app_dependencies $app_depencencies
@ -80,3 +82,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/unattended_upgrades_ynh" 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/unattended_upgrades_ynh"
ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="restore" ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="restore"
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Restoration completed" --last

View file

@ -14,6 +14,7 @@ source _variables
#================================================= #=================================================
# LOAD SETTINGS # LOAD SETTINGS
#================================================= #=================================================
ynh_script_progression --message="Load settings"
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
@ -29,6 +30,7 @@ upgrade_type=$(ynh_check_app_version_changed)
#================================================= #=================================================
# ENSURE DOWNWARD COMPATIBILITY # ENSURE DOWNWARD COMPATIBILITY
#================================================= #=================================================
ynh_script_progression --message="Ensure downward compatibility"
# If overwrite_periodic doesn't exist, create it # If overwrite_periodic doesn't exist, create it
if [ -z "$overwrite_periodic" ]; then if [ -z "$overwrite_periodic" ]; then
@ -39,8 +41,7 @@ fi
#================================================= #=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#================================================= #=================================================
ynh_script_progression --message="Backup the app before upgrading" --weight=3
# It's impossible to make a backup here, because the backup name is going to have 32 characters. Which is not allowed...
# Backup the current version of the app # Backup the current version of the app
ynh_backup_before_upgrade ynh_backup_before_upgrade
@ -56,6 +57,7 @@ ynh_abort_if_errors
#================================================= #=================================================
# INSTALL DEPENDENCIES # INSTALL DEPENDENCIES
#================================================= #=================================================
ynh_script_progression --message="Upgrade dependencies" --weight=5
ynh_install_app_dependencies $app_depencencies ynh_install_app_dependencies $app_depencencies
@ -80,6 +82,7 @@ ynh_install_app_dependencies $app_depencencies
# Overwrite 02periodic config file only if it's allowed # Overwrite 02periodic config file only if it's allowed
if [ $overwrite_periodic -eq 1 ] if [ $overwrite_periodic -eq 1 ]
then then
ynh_script_progression --message="Upgrade APT Periodic for Unattended" --weight=3
ynh_backup_if_checksum_is_different "/etc/apt/apt.conf.d/02periodic" ynh_backup_if_checksum_is_different "/etc/apt/apt.conf.d/02periodic"
cp "../conf/02periodic" "/etc/apt/apt.conf.d/02periodic" cp "../conf/02periodic" "/etc/apt/apt.conf.d/02periodic"
@ -116,4 +119,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/unattended_upgrades_ynh" 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/unattended_upgrades_ynh"
#=================================================
# END OF SCRIPT
#=================================================
ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="upgrade" ynh_send_readme_to_admin --app_message="$message" --recipients="root" --type="upgrade"
ynh_script_progression --message="Upgrade completed" --last