From 1ff00da69ae4de6f227dac8552d4e3535fc49099 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Fri, 2 Jun 2017 18:46:29 +0200 Subject: [PATCH 1/8] [enh] Add a change_url script --- scripts/change_url | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 scripts/change_url diff --git a/scripts/change_url b/scripts/change_url new file mode 100644 index 0000000..0f3e4c4 --- /dev/null +++ b/scripts/change_url @@ -0,0 +1,80 @@ +#!/bin/bash + +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +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 + +#================================================= +# CHECK THE SYNTAX OF THE PATHS +#================================================= + +test -n "$old_path" || old_path="/" +test -n "$new_path" || new_path="/" +new_path=$(ynh_normalize_url_path $new_path) +old_path=$(ynh_normalize_url_path $old_path) + +#================================================= +# 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 +#================================================= +# MODIFY URL IN NGINX CONF +#================================================= + +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 + ynh_replace_string "location $old_path" "location $new_path" "$nginx_conf_path" +fi + +# Change the domain for nginx +if [ $change_domain -eq 1 ] +then + sudo mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf +fi + +#================================================= +# SPECIFIC MODIFICATIONS +#================================================= +# ... +#================================================= + +#================================================= +# GENERIC FINALISATION +#================================================= +# RELOAD NGINX +#================================================= + +sudo systemctl reload nginx From f61108fc8bceada57489e22a8b8a0af36836b03f Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Mon, 5 Jun 2017 13:06:12 +0200 Subject: [PATCH 2/8] _common.sh --- scripts/change_url | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/change_url b/scripts/change_url index 0f3e4c4..428b71a 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -6,6 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= +source _common.sh source /usr/share/yunohost/helpers #================================================= From 64bbd30989c89ed5fd00027475cae3d273dd8aa0 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Wed, 23 Aug 2017 17:11:20 +0200 Subject: [PATCH 3/8] Set required version to 2.7.2 and remove sudo prefixes --- manifest.json | 2 +- scripts/change_url | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/manifest.json b/manifest.json index a1f7b55..2273eb2 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,7 @@ "url": "http://example.com" }, "requirements": { - "yunohost": ">= 2.6.4" + "yunohost": ">= 2.7.2" }, "multi_instance": true, "services": [ diff --git a/scripts/change_url b/scripts/change_url index 428b71a..2f65629 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -63,7 +63,7 @@ fi # Change the domain for nginx if [ $change_domain -eq 1 ] then - sudo mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf + mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf fi #================================================= @@ -78,4 +78,4 @@ fi # RELOAD NGINX #================================================= -sudo systemctl reload nginx +systemctl reload nginx From 54c393cd99be85301a3505516048e7ad9b67bf09 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Wed, 23 Aug 2017 17:25:20 +0200 Subject: [PATCH 4/8] Add more generic cases --- scripts/change_url | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/change_url b/scripts/change_url index 2f65629..d3d583e 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -57,7 +57,11 @@ 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 - ynh_replace_string "location $old_path" "location $new_path" "$nginx_conf_path" + # Replace locations starting with old_path + # Look for every location possible patterns (see https://nginx.org/en/docs/http/ngx_http_core_module.html#location) + sudo sed --in-place "s@location\( \(=\|~\|~\*\|\^~\)\)\? $old_path@location\1 $new_path@" "$nginx_conf_path" + # Replace path in "return" directives + sudo sed --in-place "s@return \([[:digit:]]\{3\}\) $old_path@return \1 $new_path@" "$nginx_conf_path" fi # Change the domain for nginx From 22bc3cc21f3c3260b2005235a3ef35839d5716f8 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Sun, 3 Sep 2017 16:39:05 +0200 Subject: [PATCH 5/8] Update nginx conf file checksum --- scripts/change_url | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/change_url b/scripts/change_url index d3d583e..49964f7 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -57,17 +57,20 @@ 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 + ynh_backup_if_checksum_is_different "$nginx_conf_path" # Replace locations starting with old_path # Look for every location possible patterns (see https://nginx.org/en/docs/http/ngx_http_core_module.html#location) sudo sed --in-place "s@location\( \(=\|~\|~\*\|\^~\)\)\? $old_path@location\1 $new_path@" "$nginx_conf_path" # Replace path in "return" directives sudo sed --in-place "s@return \([[:digit:]]\{3\}\) $old_path@return \1 $new_path@" "$nginx_conf_path" + ynh_store_file_checksum "$nginx_conf_path" fi # Change the domain for nginx if [ $change_domain -eq 1 ] then mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf + ynh_store_file_checksum "$nginx_conf_path" fi #================================================= From fbac88a28d262803a77061da24b2ad5226d7bdf7 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Wed, 6 Sep 2017 08:20:12 +0200 Subject: [PATCH 6/8] Put ynh_replace_string back (works with regexps) --- scripts/change_url | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/change_url b/scripts/change_url index 49964f7..deed315 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -60,9 +60,9 @@ then ynh_backup_if_checksum_is_different "$nginx_conf_path" # Replace locations starting with old_path # Look for every location possible patterns (see https://nginx.org/en/docs/http/ngx_http_core_module.html#location) - sudo sed --in-place "s@location\( \(=\|~\|~\*\|\^~\)\)\? $old_path@location\1 $new_path@" "$nginx_conf_path" + ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $old_path" "location\1 $new_path" "$nginx_conf_path" # Replace path in "return" directives - sudo sed --in-place "s@return \([[:digit:]]\{3\}\) $old_path@return \1 $new_path@" "$nginx_conf_path" + ynh_replace_string "return \([[:digit:]]\{3\}\) $old_path" "return \1 $new_path" "$nginx_conf_path" ynh_store_file_checksum "$nginx_conf_path" fi From 3cef7eb3f71d8f60dfabe7bcbfa4e8c2dc5b6d64 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Wed, 6 Sep 2017 08:21:30 +0200 Subject: [PATCH 7/8] Handle nginx conf file checksums (additional helper) --- scripts/_common.sh | 12 ++++++++++++ scripts/change_url | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 0f53f77..4cd6e10 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -110,3 +110,15 @@ ynh_remove_systemd_config () { ynh_secure_remove "$finalsystemdconf" fi } + +# ============= FUTURE YUNOHOST HELPER ============= +# Delete a file checksum from the app settings +# +# $app should be defined when calling this helper +# +# usage: ynh_remove_file_checksum file +# | arg: file - The file for which the checksum will be deleted +ynh_delete_file_checksum () { + local checksum_setting_name=checksum_${1//[\/ ]/_} # Replace all '/' and ' ' by '_' + ynh_app_setting_delete $app $checksum_setting_name +} \ No newline at end of file diff --git a/scripts/change_url b/scripts/change_url index deed315..51b3b36 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -57,20 +57,25 @@ 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 "$nginx_conf_path" # Replace locations starting with old_path # Look for every location possible patterns (see https://nginx.org/en/docs/http/ngx_http_core_module.html#location) ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $old_path" "location\1 $new_path" "$nginx_conf_path" # Replace path in "return" directives ynh_replace_string "return \([[:digit:]]\{3\}\) $old_path" "return \1 $new_path" "$nginx_conf_path" + # Calculate and store the nginx config file checksum ynh_store_file_checksum "$nginx_conf_path" 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 "$nginx_conf_path" mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf - ynh_store_file_checksum "$nginx_conf_path" + # Store file checksum for the new config file location + ynh_store_file_checksum "/etc/nginx/conf.d/$new_domain.d/$app.conf" fi #================================================= From 39b0e3bb13e450ebd28fd50e5068672d445e447a Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Wed, 6 Sep 2017 08:22:15 +0200 Subject: [PATCH 8/8] Remove YunoHost 2.7.2 helpers from _common.sh --- scripts/_common.sh | 111 --------------------------------------------- 1 file changed, 111 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 4cd6e10..bb04a03 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,116 +1,5 @@ #!/bin/bash -# ============================================================================= -# YUNOHOST 2.7 FORTHCOMING HELPERS -# ============================================================================= - -# Create a dedicated nginx config -# -# usage: ynh_add_nginx_config -ynh_add_nginx_config () { - finalnginxconf="/etc/nginx/conf.d/$domain.d/$app.conf" - ynh_backup_if_checksum_is_different "$finalnginxconf" - sudo cp ../conf/nginx.conf "$finalnginxconf" - - # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable. - # Substitute in a nginx config file only if the variable is not empty - if test -n "${path_url:-}"; then - ynh_replace_string "__PATH__" "$path_url" "$finalnginxconf" - fi - if test -n "${domain:-}"; then - ynh_replace_string "__DOMAIN__" "$domain" "$finalnginxconf" - fi - if test -n "${port:-}"; then - ynh_replace_string "__PORT__" "$port" "$finalnginxconf" - fi - if test -n "${app:-}"; then - ynh_replace_string "__NAME__" "$app" "$finalnginxconf" - fi - if test -n "${final_path:-}"; then - ynh_replace_string "__FINALPATH__" "$final_path" "$finalnginxconf" - fi - ynh_store_file_checksum "$finalnginxconf" - - sudo systemctl reload nginx -} - -# Remove the dedicated nginx config -# -# usage: ynh_remove_nginx_config -ynh_remove_nginx_config () { - ynh_secure_remove "/etc/nginx/conf.d/$domain.d/$app.conf" - sudo systemctl reload nginx -} - -# Create a dedicated php-fpm config -# -# usage: ynh_add_fpm_config -ynh_add_fpm_config () { - finalphpconf="/etc/php5/fpm/pool.d/$app.conf" - ynh_backup_if_checksum_is_different "$finalphpconf" - sudo cp ../conf/php-fpm.conf "$finalphpconf" - ynh_replace_string "__NAMETOCHANGE__" "$app" "$finalphpconf" - ynh_replace_string "__FINALPATH__" "$final_path" "$finalphpconf" - ynh_replace_string "__USER__" "$app" "$finalphpconf" - sudo chown root: "$finalphpconf" - ynh_store_file_checksum "$finalphpconf" - - if [ -e "../conf/php-fpm.ini" ] - then - finalphpini="/etc/php5/fpm/conf.d/20-$app.ini" - ynh_backup_if_checksum_is_different "$finalphpini" - sudo cp ../conf/php-fpm.ini "$finalphpini" - sudo chown root: "$finalphpini" - ynh_store_file_checksum "$finalphpini" - fi - - sudo systemctl reload php5-fpm -} - -# Remove the dedicated php-fpm config -# -# usage: ynh_remove_fpm_config -ynh_remove_fpm_config () { - ynh_secure_remove "/etc/php5/fpm/pool.d/$app.conf" - ynh_secure_remove "/etc/php5/fpm/conf.d/20-$app.ini" 2>&1 - sudo systemctl reload php5-fpm -} - -# Create a dedicated systemd config -# -# usage: ynh_add_systemd_config -ynh_add_systemd_config () { - finalsystemdconf="/etc/systemd/system/$app.service" - ynh_backup_if_checksum_is_different "$finalsystemdconf" - sudo cp ../conf/systemd.service "$finalsystemdconf" - - # To avoid a break by set -u, use a void substitution ${var:-}. If the variable is not set, it's simply set with an empty variable. - # Substitute in a nginx config file only if the variable is not empty - if test -n "${final_path:-}"; then - ynh_replace_string "__FINALPATH__" "$final_path" "$finalsystemdconf" - fi - if test -n "${app:-}"; then - ynh_replace_string "__APP__" "$app" "$finalsystemdconf" - fi - ynh_store_file_checksum "$finalsystemdconf" - - sudo chown root: "$finalsystemdconf" - sudo systemctl enable $app - sudo systemctl daemon-reload -} - -# Remove the dedicated systemd config -# -# usage: ynh_remove_systemd_config -ynh_remove_systemd_config () { - finalsystemdconf="/etc/systemd/system/$app.service" - if [ -e "$finalsystemdconf" ]; then - sudo systemctl stop $app - sudo systemctl disable $app - ynh_secure_remove "$finalsystemdconf" - fi -} - # ============= FUTURE YUNOHOST HELPER ============= # Delete a file checksum from the app settings #