From 437d1c023bd27bc7f5444fbfc07b5e928f5928f5 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Thu, 11 Aug 2022 13:20:52 +0200 Subject: [PATCH] Fix: restore, remove change_url scripts --- scripts/_common.sh | 30 ++++++++++++++++++++++++++++++ scripts/change_url | 9 +++++++++ scripts/remove | 6 ++++++ scripts/restore | 15 +++++++++------ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index ef0c347..5023f2d 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -15,7 +15,37 @@ pkg_dependencies="" # PERSONAL HELPERS #================================================= +# Substitute/replace a string (or expression) by another in a file on a +# +# usage: ynh_replace_string_occurrence --occurrence=occurrence --match_string=match_string --replace_string=replace_string --target_file=target_file +# | arg: -o, --occurrence= - Replace the nth occurrence in the file +# | arg: -m, --match_string= - String to be searched and replaced in the file +# | arg: -r, --replace_string= - String that will replace matches +# | arg: -f, --target_file= - File in which the string will be replaced. +# +# As this helper is based on sed command, regular expressions and references to +# sub-expressions can be used (see sed manual page for more information) +# +ynh_replace_string_occurrence() { + # Declare an array to define the options of this helper. + local legacy_args=omrf + local -A args_array=([o]=occurrence [m]=match_string= [r]=replace_string= [f]=target_file=) + local occurrence + local match_string + local replace_string + local target_file + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + set +o xtrace # set +x + local delimit=@ + # Escape the delimiter if it's in the string. + match_string=${match_string//${delimit}/"\\${delimit}"} + replace_string=${replace_string//${delimit}/"\\${delimit}"} + + set -o xtrace # set -x + sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}${occurrence}" "$target_file" +} #================================================ # EXPERIMENTAL HELPERS diff --git a/scripts/change_url b/scripts/change_url index 422835d..7dd9ab8 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -36,6 +36,8 @@ port_files=$(ynh_app_setting_get --app=$app --key=port_files) port_syncing_server=$(ynh_app_setting_get --app=$app --key=port_syncing_server) port_syncing_server_worker=$(ynh_app_setting_get --app=$app --key=port_syncing_server_worker) +config_api_gateway="$final_path/live/api-gateway.env" + #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= @@ -104,6 +106,13 @@ fi #================================================= # SPECIFIC MODIFICATIONS #================================================= +# MODIFY CONFIG +#================================================= +ynh_script_progression --message="Modify configuring..." --weight=1 + +ynh_replace_string --match_string="^FILES_SERVER_URL.*$" --replace_string="FILES_SERVER_URL=https://$new_domain$new_path/files" --target_file="$config_api_gateway" +ynh_store_file_checksum --file="$config_api_gateway" + #================================================= # SETUP FAIL2BAN #================================================= diff --git a/scripts/remove b/scripts/remove index 38c82de..969a31c 100755 --- a/scripts/remove +++ b/scripts/remove @@ -153,6 +153,12 @@ ynh_remove_fail2ban_config #================================================= # SPECIFIC REMOVE #================================================= +# REMOVE CRON CONFIGURATION +#================================================= +ynh_script_progression --message="Removing cron configuration..." --weight=1 + +ynh_secure_remove --file="/etc/cron.d/$app" + #================================================= # REMOVE SWAP #================================================= diff --git a/scripts/restore b/scripts/restore index 67ec45f..4522096 100755 --- a/scripts/restore +++ b/scripts/restore @@ -47,6 +47,7 @@ config_auth_worker="$final_path/live/auth-worker.env" config_files="$final_path/live/files.env" config_syncing_server="$final_path/live/syncing-server.env" config_syncing_server_worker="$final_path/live/syncing-server-worker.env" +config_nginx="/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # CHECK IF THE APP CAN BE RESTORED @@ -165,22 +166,24 @@ ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=re ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_worker" # Syncing_Server Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server" --target_file="$config_syncing_server" -ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server" --target_file="$config_api_gateway" -ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server" --target_file="$config_auth" -ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server" --target_file="$config_auth_worker" +ynh_replace_string --match_string="^SYNCING_SERVER_JS_URL.*$" --replace_string="SYNCING_SERVER_JS_URL=http://localhost:$port_syncing_server" --target_file="$config_api_gateway" +ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=http://localhost:$port_syncing_server" --target_file="$config_auth" +ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=http://localhost:$port_syncing_server" --target_file="$config_auth_worker" # Syncing_Server_Worker Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_worker" --target_file="$config_syncing_server_worker" # Auth Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_auth" --target_file="$config_auth" -ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_api_gateway" -ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_syncing_server" -ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_syncing_server_worker" +ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_api_gateway" +ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_syncing_server" +ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_syncing_server_worker" # Auth_Worker Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_auth_worker" --target_file="$config_auth_worker" # API-Gateway Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_api_gateway" --target_file="$config_api_gateway" +ynh_replace_string_occurrence --occurrence="1" --match_string="^proxy_pass.*$" --replace_string="proxy_pass http://127.0.0.1:$port_api_gateway/;" --target_file="$config_nginx" # Files Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_files" --target_file="$config_files" +ynh_replace_string_occurrence --occurrence="2" --match_string="^proxy_pass.*$" --replace_string="proxy_pass http://127.0.0.1:$port_files/;" --target_file="$config_nginx" #================================================= # RESTORE SYSTEMD