From 4d78443b1d901a42635df441d60f8a974a814793 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Sun, 5 May 2019 20:24:46 +0200 Subject: [PATCH 1/2] Add change_url script --- check_process | 2 +- scripts/_common.sh | 35 ++++++++++++++ scripts/change_url | 114 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 scripts/_common.sh create mode 100644 scripts/change_url diff --git a/check_process b/check_process index d1c8d52..411ac7a 100644 --- a/check_process +++ b/check_process @@ -16,7 +16,7 @@ multi_instance=0 incorrect_path=1 port_already_use=0 - change_url=0 + change_url=1 ;;; Levels Level 1=auto Level 2=auto diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..0a018dc --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +# Check if an URL is already handled +# usage: is_url_handled URL +is_url_handled() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [u]=url= ) + local url + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Try to get the url with curl, and keep the http code and an eventual redirection url. + local curl_output="$(curl --insecure --silent --output /dev/null \ + --write-out '%{http_code};%{redirect_url}' "$url")" + + # Cut the output and keep only the first part to keep the http code + local http_code="${curl_output%%;*}" + # Do the same thing but keep the second part, the redirection url + local redirection="${curl_output#*;}" + + # Return 1 if the url isn't handled. + # Which means either curl got a 404 (or the admin) or the sso. + # A handled url should redirect to a publicly accessible url. + # Return 1 if the url has returned 404 + if [ "$http_code" = "404" ] || [[ $redirection =~ "/yunohost/admin" ]]; then + return 1 + # Return 1 if the url is redirected to the SSO + elif [[ $redirection =~ "/yunohost/sso" ]]; then + return 1 + fi +} diff --git a/scripts/change_url b/scripts/change_url new file mode 100644 index 0000000..4cb7436 --- /dev/null +++ b/scripts/change_url @@ -0,0 +1,114 @@ +#!/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=2 + +# Needed for helper "ynh_add_nginx_config" +final_path=$(ynh_app_setting_get --app=$app --key=final_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 + +#================================================= +# CHECK IF THE APP CAN BE MOVED WITH THESE ARGS +#================================================= + +if [ $change_domain -eq 1 ] +then + # Check if .well-known is available for the new domain. + if is_url_handled "https://$new_domain/.well-known/caldav" || is_url_handled "https://$new_domain/.well-known/carddav" + then + ynh_die --message="Another app already uses the domain $new_domain to serve a caldav/carddav feature. Please use another domain." + fi +fi + +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# MODIFY URL IN NGINX CONF +#================================================= +ynh_script_progression --message="Updating nginx web server configuration..." --weight=2 + +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 + +#================================================= +# SPECIFIC MODIFICATIONS +#================================================= +# UPDATE CONFIGURATION +#================================================= +ynh_script_progression --message="Updating baikal configuration..." + +ynh_replace_string --match_string="_BASEURI\", \"$old_path" --replace_string="_BASEURI\", \"$new_path" --target_file="${final_path}/Specific/config.system.php" + +#================================================= +# GENERIC FINALISATION +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading nginx web server..." + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Change of URL completed for $app" --last From b68fbc83416d5fd6eec7aaaed26d0496bcf5e11d Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Mon, 6 May 2019 14:11:30 +0200 Subject: [PATCH 2/2] Require ynh 3.5 --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 2e9cf5f..c491438 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,7 @@ "email": "julien.malik@paraiso.me" }, "requirements": { - "yunohost": ">= 3.2.0" + "yunohost": ">= 3.5.0" }, "multi_instance": false, "services": [