From 65361e305d3719128474f952d88cce5e063878d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Fri, 29 Mar 2024 13:08:31 +0100 Subject: [PATCH] Fix scripts/actions --- actions.toml | 8 +-- manifest.toml | 1 - scripts/actions/clean_backups | 33 +---------- scripts/actions/force_backup | 29 +--------- scripts/actions/reset_default_app | 82 ++++------------------------ scripts/actions/reset_default_config | 70 ++++++++---------------- 6 files changed, 40 insertions(+), 183 deletions(-) diff --git a/actions.toml b/actions.toml index 651d257..73b2529 100644 --- a/actions.toml +++ b/actions.toml @@ -1,6 +1,6 @@ [force_backup] name = "Create a new backup" -command = "/bin/bash scripts/actions/force_backup" +command = "scripts/actions/force_backup" # user = "root" # optional # cwd = "/" # optional # accepted_return_codes = [0, 1, 2, 3] # optional @@ -9,7 +9,7 @@ description = "Run Archivist to create a new backup." [clean_backups] name = "Clean all previous backup files" -command = "/bin/bash scripts/actions/clean_backups" +command = "scripts/actions/clean_backups" # user = "root" # optional # cwd = "/" # optional # accepted_return_codes = [0, 1, 2, 3] # optional @@ -18,7 +18,7 @@ description = "Remove all previous backup files made by Archivist." [reset_default_config] name = "Reset the config file and restore a default one." -command = "/bin/bash scripts/actions/reset_default_config \"Backup_list.conf\"" +command = "scripts/actions/reset_default_config \"Backup_list.conf\"" # user = "root" # optional # cwd = "/" # optional # accepted_return_codes = [0, 1, 2, 3] # optional @@ -27,7 +27,7 @@ description = "Reset the config file Backup_list.conf." [reset_default_app] name = "Reset the app with a default configuration." -command = "/bin/bash scripts/actions/reset_default_app" +command = "scripts/actions/reset_default_app" # user = "root" # optional # cwd = "/" # optional # accepted_return_codes = [0, 1, 2, 3] # optional diff --git a/manifest.toml b/manifest.toml index affdd1b..9b61cdc 100644 --- a/manifest.toml +++ b/manifest.toml @@ -14,7 +14,6 @@ maintainers = [] [upstream] license = "GPL-3.0" code = "https://github.com/maniackcrudelis/archivist" -website = "https://github.com/maniackcrudelis/archivist" [integration] yunohost = ">= 11.2.9" diff --git a/scripts/actions/clean_backups b/scripts/actions/clean_backups index c13256d..70af0dd 100755 --- a/scripts/actions/clean_backups +++ b/scripts/actions/clean_backups @@ -1,7 +1,5 @@ #!/bin/bash -#================================================= -# GENERIC STARTING #================================================= # IMPORT GENERIC HELPERS #================================================= @@ -9,31 +7,6 @@ source scripts/_common.sh source /usr/share/yunohost/helpers -#================================================= -# MANAGE SCRIPT FAILURE -#================================================= - -# Exit if an error occurs during the execution of the script -ynh_abort_if_errors - -#================================================= -# RETRIEVE ARGUMENTS -#================================================= - -app=$YNH_APP_INSTANCE_NAME - -final_path=$(ynh_app_setting_get --app=$app --key=final_path) - -#================================================= -# CHECK IF ARGUMENTS ARE CORRECT -#================================================= - -#================================================= -# CHECK IF AN ACTION HAS TO BE DONE -#================================================= - -#================================================= -# SPECIFIC ACTION #================================================= # CLEAN ALL BACKUP FILES #================================================= @@ -42,10 +15,8 @@ ynh_script_progression --message="Cleaning 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)" -while read directory <&3 -do - if [ -n "$directory" ] - then +while read -r directory <&3; do + if [ -n "$directory" ]; then ynh_print_info --message="Clean backup directory $directory" ynh_secure_remove --file="$directory" fi diff --git a/scripts/actions/force_backup b/scripts/actions/force_backup index fdbb7ed..94e88f7 100755 --- a/scripts/actions/force_backup +++ b/scripts/actions/force_backup @@ -1,7 +1,5 @@ #!/bin/bash -#================================================= -# GENERIC STARTING #================================================= # IMPORT GENERIC HELPERS #================================================= @@ -9,37 +7,12 @@ source scripts/_common.sh source /usr/share/yunohost/helpers -#================================================= -# MANAGE SCRIPT FAILURE -#================================================= - -# Exit if an error occurs during the execution of the script -ynh_abort_if_errors - -#================================================= -# RETRIEVE ARGUMENTS -#================================================= - -app=$YNH_APP_INSTANCE_NAME - -final_path=$(ynh_app_setting_get --app=$app --key=final_path) - -#================================================= -# CHECK IF ARGUMENTS ARE CORRECT -#================================================= - -#================================================= -# CHECK IF AN ACTION HAS TO BE DONE -#================================================= - -#================================================= -# SPECIFIC ACTION #================================================= # FORCE A NEW BACKUP #================================================= ynh_script_progression --message="Forcing a new backup..." --weight=9 -ynh_exec_warn nice -n10 $final_path/archivist.sh +ynh_exec_warn nice -n10 "$install_dir/archivist.sh" #================================================= # END OF SCRIPT diff --git a/scripts/actions/reset_default_app b/scripts/actions/reset_default_app index 0317278..4f325eb 100755 --- a/scripts/actions/reset_default_app +++ b/scripts/actions/reset_default_app @@ -1,7 +1,5 @@ #!/bin/bash -#================================================= -# GENERIC STARTING #================================================= # IMPORT GENERIC HELPERS #================================================= @@ -10,25 +8,7 @@ source scripts/_common.sh source /usr/share/yunohost/helpers -#================================================= -# MANAGE SCRIPT FAILURE -#================================================= - -ynh_clean_setup () { -# Clean installation remaining that are not handle by the remove script. - ynh_clean_check_starting -} -# Exit if an error occurs during the execution of the script -ynh_abort_if_errors - -#================================================= -# RETRIEVE ARGUMENTS -#================================================= - -app=$YNH_APP_INSTANCE_NAME - -final_path=$(ynh_app_setting_get --app=$app --key=final_path) -frequency="$(ynh_app_setting_get --app=$app --key=frequency)" +_set_frequencies #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -37,68 +17,28 @@ frequency="$(ynh_app_setting_get --app=$app --key=frequency)" ynh_script_progression --message="Resetting source files..." --time --weight=1 # Download, check integrity, uncompress and patch the source from app.src -(cd scripts; YNH_CWD=$PWD ynh_setup_source --dest_dir="$final_path") +(cd scripts; YNH_CWD=$PWD ynh_setup_source --dest_dir="$install_dir" --full_replace=1 --keep="passkey Backup_list.conf") -#================================================= -# RECREATE DIRECTORY -#================================================= - -backup_dir="/home/yunohost.app/${app}/backup" -mkdir -p "$backup_dir" - -#================================================= -# UPDATE THE CRON FILE -#================================================= -ynh_script_progression --message="Updating the cron file..." - -# Verify the checksum and backup the file if it's different -ynh_backup_if_checksum_is_different --file="/etc/cron.d/$app" - -(cd scripts; cp ../conf/cron /etc/cron.d/$app) -ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file=/etc/cron.d/$app -ynh_replace_string --match_string="__APP__" --replace_string=$app --target_file=/etc/cron.d/$app -if [ "$frequency" = "Daily" ]; then - cron_freq="0 2 * * *" - run_freq="every day" -elif [ "$frequency" = "Each 3 days" ]; then - cron_freq="0 2 */3 * *" - run_freq="each 3 days" -elif [ "$frequency" = "Weekly" ]; then - cron_freq="0 2 * * 0" - run_freq="once a week on sunday" -elif [ "$frequency" = "Biweekly" ]; then - cron_freq="0 2 * * 0/2" - run_freq="one sunday out of two" -else # Monthly - cron_freq="0 2 1 * *" - run_freq="once a month on the first sunday" -fi -ynh_replace_string --match_string="__FREQUENCY__" --replace_string="$cron_freq" --target_file=/etc/cron.d/$app - -# Recalculate and store the config file checksum into the app settings -ynh_store_file_checksum --file="/etc/cron.d/$app" +# Set permissions on app files +chown -R "root:root" "$install_dir" #================================================= # RECONFIGURE ARCHIVIST #================================================= ynh_script_progression --message="Reconfiguring archivist..." --time --weight=1 -yunohost app action run $app reset_default_config +yunohost app action run "$app" reset_default_config #================================================= -# SETUP LOGROTATE +# SYSTEM CONFIGURATION #================================================= -ynh_script_progression --message="Resetting logrotate configuration..." --time --weight=1 +ynh_script_progression --message="Resetting system configurations related to $app..." --weight=1 -# Use logrotate to manage app-specific logfile(s) -ynh_use_logrotate --non-append +# Use logrotate to manage application logfile(s) +ynh_use_logrotate -#================================================= -# SECURE FILES AND DIRECTORIES -#================================================= - -# Set permissions on app files -chown -R root: $final_path +# Add Cron configuration file +ynh_add_config --template="archivist.cron" --destination="/etc/cron.d/$app" #================================================= # END OF SCRIPT diff --git a/scripts/actions/reset_default_config b/scripts/actions/reset_default_config index 9647d3c..bc0bac2 100755 --- a/scripts/actions/reset_default_config +++ b/scripts/actions/reset_default_config @@ -1,7 +1,5 @@ #!/bin/bash -#================================================= -# GENERIC STARTING #================================================= # IMPORT GENERIC HELPERS #================================================= @@ -9,24 +7,6 @@ source scripts/_common.sh source /usr/share/yunohost/helpers -#================================================= -# MANAGE SCRIPT FAILURE -#================================================= - -# Exit if an error occurs during the execution of the script -ynh_abort_if_errors - -#================================================= -# RETRIEVE ARGUMENTS -#================================================= - -app=$YNH_APP_INSTANCE_NAME - -final_path=$(ynh_app_setting_get --app=$app --key=final_path) -encrypt=$(ynh_app_setting_get --app=$app --key=encrypt) -core_backup=$(ynh_app_setting_get --app=$app --key=core_backup) -apps_backup=$(ynh_app_setting_get --app=$app --key=apps_backup) - #================================================= # SORT OUT THE CONFIG FILE TO HANDLE #================================================= @@ -34,11 +14,9 @@ apps_backup=$(ynh_app_setting_get --app=$app --key=apps_backup) file="$1" if [ "$file" = "Backup_list.conf" ]; then - config_file="$final_path/Backup_list.conf" + config_file="$install_dir/Backup_list.conf" fi -#================================================= -# SPECIFIC ACTION #================================================= # RESET THE CONFIG FILE #================================================= @@ -47,43 +25,39 @@ ynh_script_progression --message="Reseting the config file $file" # Verify the checksum and backup the file if it's different ynh_backup_if_checksum_is_different --file="$config_file" -if [ "$file" = "Backup_list.conf" ] -then - # Get the default file and overwrite the current config - cp "$final_path/Backup_list.conf.default" "$config_file" - - # Recreate the default config - backup_dir="/home/yunohost.app/${app}/backup" - enc_backup_dir="/home/yunohost.app/${app}/encrypted_backup" - ynh_replace_string --match_string="^backup_dir=.*" --replace_string="backup_dir=$backup_dir" --target_file="$config_file" - ynh_replace_string --match_string="^enc_backup_dir=.*" --replace_string="enc_backup_dir=$enc_backup_dir" --target_file="$config_file" - - if [ $encrypt -eq 1 ] - then +if [ "$file" = "Backup_list.conf" ]; then + if [ "$encrypt" -eq 1 ]; then encrypt=true - passkey="$final_path/passkey" + passkey="$install_dir/passkey" + echo "$encryption_pwd" > "$passkey" + chmod 400 "$passkey" else encrypt=false passkey=na fi - ynh_replace_string --match_string="^encrypt=.*" --replace_string="encrypt=$encrypt" --target_file="$config_file" - ynh_replace_string --match_string="^cryptpass=.*" --replace_string="cryptpass=$passkey" --target_file="$config_file" - if [ $core_backup -eq 1 ] - then + if [ "$core_backup" -eq 1 ]; then core_backup=true else core_backup=false fi - ynh_replace_string --match_string="^ynh_core_backup=.*" --replace_string="ynh_core_backup=$core_backup" --target_file="$config_file" - if [ $apps_backup -eq 1 ] - then + config_file="$install_dir/Backup_list.conf" + cp "$install_dir/Backup_list.conf.default" "$config_file" + + ynh_replace_string --target_file="$config_file" --match_string="^backup_dir=.*" --replace_string="backup_dir=$data_dir/backup" + ynh_replace_string --target_file="$config_file" --match_string="^enc_backup_dir=.*" --replace_string="enc_backup_dir=$data_dir/encrypted_backup" + + ynh_replace_string --target_file="$config_file" --match_string="^encrypt=.*" --replace_string="encrypt=$encrypt" + ynh_replace_string --target_file="$config_file" --match_string="^cryptpass=.*" --replace_string="cryptpass=$passkey" + + ynh_replace_string --target_file="$config_file" --match_string="^ynh_core_backup=.*" --replace_string="ynh_core_backup=$core_backup" + + if [ $apps_backup -eq 1 ]; then # Add all current applications to the backup - while read backup_app - do - ynh_replace_string --match_string="^ynh_app_backup=$" --replace_string="ynh_app_backup=$backup_app\n&" --target_file="$config_file" - done <<< "$(yunohost app list -i | grep id: | sed 's/.*id: //')" + while read -r backup_app; do + ynh_replace_string --target_file="$config_file" --match_string="^ynh_app_backup=$" --replace_string="ynh_app_backup=$backup_app\n&" + done <<< "$(yunohost app list | grep 'id:' | sed 's/.*id: //')" fi fi