From 3a10c355a06d004881fe48da37adce8630794821 Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Wed, 19 May 2021 19:58:58 +0200
Subject: [PATCH 1/7] Implement change_url

---
 check_process      |   2 +-
 scripts/change_url | 164 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 165 insertions(+), 1 deletion(-)
 create mode 100644 scripts/change_url

diff --git a/check_process b/check_process
index de19364..9a0f2e2 100644
--- a/check_process
+++ b/check_process
@@ -26,7 +26,7 @@
 		multi_instance=1
 		incorrect_path=0
 		port_already_use=0
-		change_url=0
+		change_url=1
 ;;; Options
 Email=cda@rootkey.co.uk
 Notification=all
diff --git a/scripts/change_url b/scripts/change_url
new file mode 100644
index 0000000..0f438f1
--- /dev/null
+++ b/scripts/change_url
@@ -0,0 +1,164 @@
+#!/bin/bash
+
+#=================================================
+# GENERIC STARTING
+#=================================================
+# IMPORT GENERIC HELPERS
+#=================================================
+
+source _common.sh
+source /usr/share/yunohost/helpers
+
+#=================================================
+# RETRIEVE ARGUMENTS
+#=================================================
+
+old_domain=$YNH_APP_OLD_DOMAIN
+old_path=$YNH_APP_OLD_PATH
+
+new_domain=$YNH_APP_NEW_DOMAIN
+new_path=$YNH_APP_NEW_PATH
+
+app=$YNH_APP_INSTANCE_NAME
+
+#=================================================
+# LOAD SETTINGS
+#=================================================
+ynh_script_progression --message="Loading installation settings..." --weight=1
+
+# Needed for helper "ynh_add_nginx_config"
+final_path=$(ynh_app_setting_get --app=$app --key=final_path)
+port=$(ynh_app_setting_get --app=$app --key=port)
+key=$(ynh_app_setting_get --app=$app --key=key)
+redis_db=$(ynh_app_setting_get --app=$app --key=redis_db)
+port=$(ynh_app_setting_get --app=$app --key=port)
+db_name=$(ynh_app_setting_get --app=$app --key=db_name)
+db_user=$db_name
+db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
+
+#=================================================
+# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
+#=================================================
+ynh_script_progression --message="Backing up the app before changing its URL (may take a while)..." --weight=1
+
+# Backup the current version of the app
+ynh_backup_before_upgrade
+ynh_clean_setup () {
+	# Remove the new domain config file, the remove script won't do it as it doesn't know yet its location.
+	ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf"
+
+	# Restore it if the upgrade fails
+	ynh_restore_upgradebackup
+}
+# Exit if an error occurs during the execution of the script
+ynh_abort_if_errors
+
+#=================================================
+# CHECK WHICH PARTS SHOULD BE CHANGED
+#=================================================
+
+change_domain=0
+if [ "$old_domain" != "$new_domain" ]
+then
+	change_domain=1
+fi
+
+change_path=0
+if [ "$old_path" != "$new_path" ]
+then
+	change_path=1
+fi
+
+#=================================================
+# STANDARD MODIFICATIONS
+#=================================================
+# STOP SYSTEMD SERVICE
+#=================================================
+ynh_script_progression --message="Stopping a systemd service..." --weight=1
+
+ynh_systemd_action --service_name="$app-beat" --action=stop --log_path="/var/log/$app/$app.log"
+ynh_systemd_action --service_name="$app-server" --action=stop --log_path="/var/log/$app/$app.log"
+ynh_systemd_action --service_name="$app-worker" --action=stop --log_path="/var/log/$app/$app.log"
+
+#=================================================
+# MODIFY URL IN NGINX CONF
+#=================================================
+ynh_script_progression --message="Updating NGINX web server configuration..." --weight=1
+
+nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf
+
+# Change the path in the NGINX config file
+if [ $change_path -eq 1 ]
+then
+	# Make a backup of the original NGINX config file if modified
+	ynh_backup_if_checksum_is_different --file="$nginx_conf_path"
+	# Set global variables for NGINX helper
+	domain="$old_domain"
+	path_url="$new_path"
+	# Create a dedicated NGINX config
+	ynh_add_nginx_config
+fi
+
+# Change the domain for NGINX
+if [ $change_domain -eq 1 ]
+then
+	# Delete file checksum for the old conf file location
+	ynh_delete_file_checksum --file="$nginx_conf_path"
+	mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf
+	# Store file checksum for the new config file location
+	ynh_store_file_checksum --file="/etc/nginx/conf.d/$new_domain.d/$app.conf"
+fi
+
+#=================================================
+# MODIFY THE CONFIG FILE
+#=================================================
+
+configfile="$final_path/code/config/.env"
+
+cp ../conf/env.prod "$configfile"
+
+ynh_replace_string --match_string="__REDIS_DB__"  --replace_string="$redis_db"   --target_file="$configfile"
+ynh_replace_string --match_string="__PORT__"      --replace_string="$port"       --target_file="$configfile"
+ynh_replace_string --match_string="__DOMAIN__"    --replace_string="$new_domain" --target_file="$configfile"
+ynh_replace_string --match_string="__DB_USER__"   --replace_string="$db_name"    --target_file="$configfile"
+ynh_replace_string --match_string="__DB_PWD__"    --replace_string="$db_pwd"     --target_file="$configfile"
+ynh_replace_string --match_string="__DB_NAME__"   --replace_string="$db_name"    --target_file="$configfile"
+ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$configfile"
+ynh_replace_string --match_string="__KEY__"       --replace_string="$key"        --target_file="$configfile"
+
+
+#=================================================
+# MODIFY THE FEDERATION 
+#=================================================
+
+$final_path/code/virtualenv/bin/python $final_path/code/api/manage.py fix_federation_ids https://$old_domain https://$new_domain --no-dry-run --no-input
+
+#=================================================
+# START SYSTEMD SERVICE
+#=================================================
+ynh_script_progression --message="Starting a systemd service..."
+
+ynh_systemd_action --service_name="$app-beat" --action="start" --log_path="/var/log/$app/$app.log"
+ynh_systemd_action --service_name="$app-server" --action="start" --log_path="/var/log/$app/$app.log"
+ynh_systemd_action --service_name="$app-worker" --action="start" --log_path="/var/log/$app/$app.log"
+
+#=================================================
+# SETUP FAIL2BAN
+#=================================================
+ynh_script_progression --message="Configuring Fail2Ban..."
+
+# Create a dedicated Fail2Ban config
+ynh_add_fail2ban_config --logpath="/var/log/nginx/$new_domain-access.log" --failregex="<HOST>.* \"POST /api/v1/token/ HTTP/1.1\" 400 68.*$" --max_retry=5
+
+#=================================================
+# RELOAD NGINX
+#=================================================
+ynh_script_progression --message="Reloading NGINX web server..." --weight=1
+
+ynh_systemd_action --service_name=nginx --action=reload
+
+#=================================================
+# END OF SCRIPT
+#=================================================
+
+ynh_script_progression --message="Change of URL completed for $app" --last
\ No newline at end of file

From 3780bdd493ea8357e5ad8fdcd00c11cca8738f2f Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Wed, 19 May 2021 20:40:37 +0200
Subject: [PATCH 2/7] Update change_url

---
 scripts/change_url | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/scripts/change_url b/scripts/change_url
index 0f438f1..25d3aa0 100644
--- a/scripts/change_url
+++ b/scripts/change_url
@@ -112,20 +112,10 @@ fi
 #=================================================
 # MODIFY THE CONFIG FILE
 #=================================================
+ynh_script_progression --message="Modifying a config file..."
 
-configfile="$final_path/code/config/.env"
-
-cp ../conf/env.prod "$configfile"
-
-ynh_replace_string --match_string="__REDIS_DB__"  --replace_string="$redis_db"   --target_file="$configfile"
-ynh_replace_string --match_string="__PORT__"      --replace_string="$port"       --target_file="$configfile"
-ynh_replace_string --match_string="__DOMAIN__"    --replace_string="$new_domain" --target_file="$configfile"
-ynh_replace_string --match_string="__DB_USER__"   --replace_string="$db_name"    --target_file="$configfile"
-ynh_replace_string --match_string="__DB_PWD__"    --replace_string="$db_pwd"     --target_file="$configfile"
-ynh_replace_string --match_string="__DB_NAME__"   --replace_string="$db_name"    --target_file="$configfile"
-ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$configfile"
-ynh_replace_string --match_string="__KEY__"       --replace_string="$key"        --target_file="$configfile"
-
+domain=$new_domain
+ynh_add_config --template="../conf/env.prod" --destination="$final_path/code/config/.env"
 
 #=================================================
 # MODIFY THE FEDERATION 

From 35e9875dec857212d9c957ac372b3fbf7b0838ab Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Sun, 23 May 2021 11:18:23 +0200
Subject: [PATCH 3/7] typo

---
 scripts/change_url | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/change_url b/scripts/change_url
index 25d3aa0..5a34b69 100644
--- a/scripts/change_url
+++ b/scripts/change_url
@@ -37,7 +37,7 @@ db_user=$db_name
 db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
 
 #=================================================
-# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
+# BACKUP BEFORE CHANGE URL THEN ACTIVE TRAP
 #=================================================
 ynh_script_progression --message="Backing up the app before changing its URL (may take a while)..." --weight=1
 

From a4176be0919e9355a3cb162c8cbc0f4485dc8954 Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Thu, 10 Jun 2021 22:04:39 +0200
Subject: [PATCH 4/7] Fix config

---
 scripts/change_url | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/change_url b/scripts/change_url
index 5a34b69..5a5cc57 100644
--- a/scripts/change_url
+++ b/scripts/change_url
@@ -115,7 +115,7 @@ fi
 ynh_script_progression --message="Modifying a config file..."
 
 domain=$new_domain
-ynh_add_config --template="../conf/env.prod" --destination="$final_path/code/config/.env"
+ynh_add_config --template="../conf/env.prod" --destination="$final_path/config/.env"
 
 #=================================================
 # MODIFY THE FEDERATION 

From fdaf6f08d06e3cbd2aa853442805dd33678aba29 Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Fri, 11 Jun 2021 23:33:52 +0200
Subject: [PATCH 5/7] Missing arguments

---
 scripts/change_url | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/scripts/change_url b/scripts/change_url
index 5a5cc57..e11487d 100644
--- a/scripts/change_url
+++ b/scripts/change_url
@@ -35,6 +35,9 @@ port=$(ynh_app_setting_get --app=$app --key=port)
 db_name=$(ynh_app_setting_get --app=$app --key=db_name)
 db_user=$db_name
 db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
+datadir=$(ynh_app_setting_get --app=$app --key=datadir)
+redis_db=$(ynh_app_setting_get --app=$app --key=redis_db)
+key=$(ynh_app_setting_get --app=$app --key=key)
 
 #=================================================
 # BACKUP BEFORE CHANGE URL THEN ACTIVE TRAP

From 242047bd20e33ab52af6dd3177574ddf2791ff5f Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Fri, 25 Jun 2021 20:46:55 +0200
Subject: [PATCH 6/7] Few fixes

---
 scripts/change_url | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/scripts/change_url b/scripts/change_url
index e11487d..a0fb40a 100644
--- a/scripts/change_url
+++ b/scripts/change_url
@@ -124,7 +124,8 @@ ynh_add_config --template="../conf/env.prod" --destination="$final_path/config/.
 # MODIFY THE FEDERATION 
 #=================================================
 
-$final_path/code/virtualenv/bin/python $final_path/code/api/manage.py fix_federation_ids https://$old_domain https://$new_domain --no-dry-run --no-input
+source $final_path/virtualenv/bin/activate
+python3 $final_path/api/manage.py fix_federation_ids https://$old_domain https://$new_domain --no-dry-run --no-input
 
 #=================================================
 # START SYSTEMD SERVICE

From 58de65c19945258a3cf98fd3fcff8d1cde028c00 Mon Sep 17 00:00:00 2001
From: yalh76 <yalh@yahoo.com>
Date: Mon, 28 Jun 2021 21:13:08 +0200
Subject: [PATCH 7/7] Update change_url

---
 scripts/change_url | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/change_url b/scripts/change_url
index a0fb40a..f9e6de3 100644
--- a/scripts/change_url
+++ b/scripts/change_url
@@ -155,4 +155,4 @@ ynh_systemd_action --service_name=nginx --action=reload
 # END OF SCRIPT
 #=================================================
 
-ynh_script_progression --message="Change of URL completed for $app" --last
\ No newline at end of file
+ynh_script_progression --message="Change of URL completed for $app" --last