From 189ab2eb3aac525bfc07b8a3697a74a11b425eed Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Sun, 3 Dec 2017 18:15:11 +0100 Subject: [PATCH] Use newer helpers and minor linter fixes --- manifest.json | 2 +- scripts/_common.sh | 199 --------------------------------------------- scripts/backup | 10 ++- scripts/restore | 10 ++- scripts/upgrade | 6 +- 5 files changed, 18 insertions(+), 209 deletions(-) diff --git a/manifest.json b/manifest.json index d9541df..f1e00f9 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,7 @@ }, "multi_instance": true, "requirements": { - "yunohost": ">= 2.4.0" + "yunohost": ">= 2.7.2" }, "services": [ "nginx", diff --git a/scripts/_common.sh b/scripts/_common.sh index 9b16683..f983e69 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -104,210 +104,11 @@ CHECK_FINALPATH () { # Check if destination directory already exists test ! -e "$final_path" || ynh_die "This path already contains a folder" } - -BACKUP_FAIL_UPGRADE () { - WARNING echo "Upgrade failed." - app_bck=${app//_/-} # Replace all '_' by '-' - if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$backup_number; then # Check if existing archive before removing app and restoring - sudo yunohost app remove $app # Remove app before restoring it - sudo yunohost backup restore --ignore-hooks $app_bck-pre-upgrade$backup_number --apps $app --force # Restore the backup if upgrade failed - ynh_die "The app was restored to the way it was before the failed upgrade." - fi -} - -BACKUP_BEFORE_UPGRADE () { # Backup the current version of the app, restore it if the upgrade fails - backup_number=1 - old_backup_number=2 - app_bck=${app//_/-} # Replace all '_' by '-' - if sudo yunohost backup list | grep -q $app_bck-pre-upgrade1; then # Check for existing archive numbered 1 - backup_number=2 # And change archive number to 2 - old_backup_number=1 - fi - - sudo yunohost backup create --ignore-hooks --apps $app --name $app_bck-pre-upgrade$backup_number # Create a backup different from the existing one - if [ "$?" -eq 0 ]; then # If backup succfessful, delete former archive - if sudo yunohost backup list | grep -q $app_bck-pre-upgrade$old_backup_number; then # Check for existing archive before removing it - QUIET sudo yunohost backup delete $app_bck-pre-upgrade$old_backup_number - fi - else # If backup failed - ynh_die "Backup failed, the upgrade process was aborted." - fi -} - #================================================= # FUTURE YUNOHOST HELPERS - TO BE REMOVED LATER #================================================= # Normalize the url path syntax -# Handle the slash at the beginning of path and its absence at ending -# Return a normalized url path -# -# example: url_path=$(ynh_normalize_url_path $url_path) -# ynh_normalize_url_path example -> /example -# ynh_normalize_url_path /example -> /example -# ynh_normalize_url_path /example/ -> /example -# ynh_normalize_url_path / -> / -# -# usage: ynh_normalize_url_path path_to_normalize -# | arg: url_path_to_normalize - URL path to normalize before using it -ynh_normalize_url_path () { - path_url=$1 - test -n "$path_url" || ynh_die "ynh_normalize_url_path expect a URL path as first argument and received nothing." - if [ "${path_url:0:1}" != "/" ]; then # If the first character is not a / - path_url="/$path_url" # Add / at begin of path variable - fi - if [ "${path_url:${#path_url}-1}" == "/" ] && [ ${#path_url} -gt 1 ]; then # If the last character is a / and that not the only character. - path_url="${path_url:0:${#path_url}-1}" # Delete the last character - fi - echo $path_url -} - -# Manage a fail of the script -# -# Print a warning to inform that the script was failed -# Execute the ynh_clean_setup function if used in the app script -# -# usage of ynh_clean_setup function -# This function provide a way to clean some residual of installation that not managed by remove script. -# To use it, simply add in your script: -# ynh_clean_setup () { -# instructions... -# } -# This function is optionnal. -# -# Usage: ynh_exit_properly is used only by the helper ynh_abort_if_errors. -# You must not use it directly. -ynh_exit_properly () { - exit_code=$? - if [ "$exit_code" -eq 0 ]; then - exit 0 # Exit without error if the script ended correctly - fi - - trap '' EXIT # Ignore new exit signals - set +eu # Do not exit anymore if a command fail or if a variable is empty - - echo -e "!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" >&2 - - if type -t ynh_clean_setup > /dev/null; then # Check if the function exist in the app script. - ynh_clean_setup # Call the function to do specific cleaning for the app. - fi - - ynh_die # Exit with error status -} - -# Exit if an error occurs during the execution of the script. -# -# Stop immediatly the execution if an error occured or if a empty variable is used. -# The execution of the script is derivate to ynh_exit_properly function before exit. -# -# Usage: ynh_abort_if_errors -ynh_abort_if_errors () { - set -eu # Exit if a command fail, and if a variable is used unset. - trap ynh_exit_properly EXIT # Capturing exit signals on shell script -} - -# Substitute/replace a string by another in a file -# -# usage: ynh_replace_string match_string replace_string target_file -# | arg: match_string - String to be searched and replaced in the file -# | arg: replace_string - String that will replace matches -# | arg: target_file - File in which the string will be replaced. -ynh_replace_string () { - delimit=@ - match_string=${1//${delimit}/"\\${delimit}"} # Escape the delimiter if it's in the string. - replace_string=${2//${delimit}/"\\${delimit}"} - workfile=$3 - - sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile" -} - -# Remove a file or a directory securely -# -# usage: ynh_secure_remove path_to_remove -# | arg: path_to_remove - File or directory to remove -ynh_secure_remove () { - path_to_remove=$1 - forbidden_path=" \ - /var/www \ - /home/yunohost.app" - - if [[ "$forbidden_path" =~ "$path_to_remove" \ - # Match all paths or subpaths in $forbidden_path - || "$path_to_remove" =~ ^/[[:alnum:]]+$ \ - # Match all first level paths from / (Like /var, /root, etc...) - || "${path_to_remove:${#path_to_remove}-1}" = "/" ]] - # Match if the path finishes by /. Because it seems there is an empty variable - then - echo "Avoid deleting $path_to_remove." >&2 - else - if [ -e "$path_to_remove" ] - then - sudo rm -R "$path_to_remove" - else - echo "$path_to_remove wasn't deleted because it doesn't exist." >&2 - fi - fi -} - -# Create a system user -# -# usage: ynh_system_user_create user_name [home_dir] -# | arg: user_name - Name of the system user that will be create -# | arg: home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home -ynh_system_user_create () { - if ! ynh_system_user_exists "$1" # Check if the user exists on the system - then # If the user doesn't exist - if [ $# -ge 2 ]; then # If a home dir is mentioned - user_home_dir="-d $2" - else - user_home_dir="--no-create-home" - fi - sudo useradd $user_home_dir --system --user-group $1 --shell /usr/sbin/nologin || ynh_die "Unable to create $1 system account" - fi -} - -# Delete a system user -# -# usage: ynh_system_user_delete user_name -# | arg: user_name - Name of the system user that will be create -ynh_system_user_delete () { - if ynh_system_user_exists "$1" # Check if the user exists on the system - then - echo "Remove the user $1" >&2 - sudo userdel $1 - else - echo "The user $1 was not found" >&2 - fi -} - -# Remove a file or a directory securely -# -# usage: ynh_secure_remove path_to_remove -# | arg: path_to_remove - File or directory to remove -ynh_secure_remove () { - path_to_remove=$1 - forbidden_path=" \ - /var/www \ - /home/yunohost.app" - - if [[ "$forbidden_path" =~ "$path_to_remove" \ - # Match all paths or subpaths in $forbidden_path - || "$path_to_remove" =~ ^/[[:alnum:]]+$ \ - # Match all first level paths from / (Like /var, /root, etc...) - || "${path_to_remove:${#path_to_remove}-1}" = "/" ]] - # Match if the path finishes by /. Because it seems there is an empty variable - then - echo "Avoid deleting $path_to_remove." >&2 - else - if [ -e "$path_to_remove" ] - then - sudo rm -R "$path_to_remove" - else - echo "$path_to_remove wasn't deleted because it doesn't exist." >&2 - fi - fi -} - # Delete a file checksum from the app settings # # $app should be defined when calling this helper diff --git a/scripts/backup b/scripts/backup index 08fc9c7..02b2865 100644 --- a/scripts/backup +++ b/scripts/backup @@ -1,8 +1,5 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu - #================================================= # IMPORT GENERIC HELPERS #================================================= @@ -15,6 +12,13 @@ fi source _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 + #================================================= # LOAD SETTINGS #================================================= diff --git a/scripts/restore b/scripts/restore index 14a1dbc..b7baa5c 100644 --- a/scripts/restore +++ b/scripts/restore @@ -1,8 +1,5 @@ #!/bin/bash -# Exit on command errors and treat unset variables as an error -set -eu - #================================================= # IMPORT GENERIC HELPERS #================================================= @@ -15,6 +12,13 @@ fi source _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 + #================================================= # LOAD SETTINGS #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 257b8bd..520e310 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -33,11 +33,11 @@ final_path=$(ynh_app_setting_get "$app" final_path) # MANAGE SCRIPT FAILURE #================================================= -BACKUP_BEFORE_UPGRADE # Backup the current version of the app +ynh_backup_before_upgrade # Backup the current version of the app ynh_clean_setup () { - BACKUP_FAIL_UPGRADE + ynh_restore_upgradebackup # restore it if the upgrade fails } -ynh_abort_if_errors # Stop script if an error is detected +ynh_abort_if_errors # Active trap to stop script execution if an error occurs #=================================================