1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/element_ynh.git synced 2024-09-03 18:36:08 +02:00

Refactor change url and use new helper ynh_nginx_config

This commit is contained in:
Josué Tille 2018-05-09 16:09:26 +02:00
parent ef78e50ef4
commit f807de03fc
4 changed files with 80 additions and 67 deletions

View file

@ -11,14 +11,6 @@ final_path="/var/www/$app"
# DEFINE ALL COMMON FONCTIONS
#=================================================
config_nginx() {
if [ "$path_url" != "/" ]
then
ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf"
fi
ynh_add_nginx_config
}
config_riot() {
cp ../conf/config.json $final_path/config.json
ynh_replace_string __DEFAULT_SERVER__ $default_home_server $final_path/config.json
@ -75,4 +67,66 @@ install_source() {
set_permission() {
chown www-data:$app -R $final_path
chmod u=rX,g=rX,o= -R $final_path
}
}
# Create a dedicated nginx config
#
# usage: ynh_add_nginx_config "list of others variables to replace"
#
# | arg: list of others variables to replace separeted by a space
# | for example : 'path_2 port_2 ...'
#
# This will use a template in ../conf/nginx.conf
# __PATH__ by $path_url
# __DOMAIN__ by $domain
# __PORT__ by $port
# __NAME__ by $app
# __FINALPATH__ by $final_path
#
# And dynamic variables (from the last example) :
# __PATH_2__ by $path_2
# __PORT_2__ by $port_2
#
ynh_add_nginx_config () {
local finalnginxconf="/etc/nginx/conf.d/$domain.d/$app.conf"
local others_var=${1:-}
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
# path_url_slash_less is path_url, or a blank value if path_url is only '/'
local path_url_slash_less=${path_url%/}
ynh_replace_string "__PATH__/" "$path_url_slash_less/" "$finalnginxconf"
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
# Replace all other variable given as arguments
for v in $others_var
do
ynh_replace_string "__${v^^}__" "${!v}" "$finalnginxconf"
done
if [ "${path_url:-}" != "/" ]
then
ynh_replace_string "^#sub_path_only" "" "$finalnginxconf"
fi
ynh_store_file_checksum "$finalnginxconf"
sudo systemctl reload nginx
}

View file

@ -15,65 +15,24 @@ source ./_common.sh
# Retrive 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
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
path_url=$(ynh_normalize_url_path ${YNH_APP_NEW_PATH:-'/'})
domain=$YNH_APP_NEW_DOMAIN
#=================================================
# 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 ]
# Update nginx config
if [ "$old_domain" != "$domain" ]
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)
old_file_path="/etc/nginx/conf.d/$old_domain.d/$app.conf"
new_file_path="/etc/nginx/conf.d/$domain.d/$app.conf"
mv "$old_file_path" "$new_file_path"
# Move from sub path to root
if [ "$new_path" == "/" ]
then
ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $old_path/\?" "location\1 /" "$nginx_conf_path"
ynh_replace_string "\(^.*rewrite.*\^$old_path.* permanent;\)" "#sub_path_only\1" "$nginx_conf_path"
ynh_replace_string "\(rewrite *\^\)$old_path\$ $old_path/*" "\1$new_path$ $new_path" "$nginx_conf_path"
# Move to a sub path
else
ynh_replace_string "location\( \(=\|~\|~\*\|\^~\)\)\? $old_path/\?" "location\1 $new_path/" "$nginx_conf_path"
ynh_replace_string "^#sub_path_only" "" "$nginx_conf_path"
ynh_replace_string "\(rewrite *\^\)$old_path\$ $old_path/*" "\1$new_path$ $new_path/" "$nginx_conf_path"
fi
# Calculate and store the nginx config file checksum
ynh_store_file_checksum "$nginx_conf_path"
# Change the checksum setting name
checksum_setting_old_name=checksum_${old_file_path//[\/ ]/_}
checksum_setting_new_name=checksum_${new_file_path//[\/ ]/_}
checksum_value=$(ynh_app_setting_get $app $checksum_setting_old_name)
ynh_app_setting_set $app $checksum_setting_new_name $checksum_value
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
# Reload services
sudo systemctl reload nginx.service
ynh_add_nginx_config

View file

@ -48,7 +48,7 @@ ynh_add_fpm_config
config_riot
# Config nginx
config_nginx
ynh_add_nginx_config
#=================================================
# GENERIC FINALIZATION

View file

@ -42,7 +42,7 @@ ynh_add_fpm_config
config_riot
# Update nginx config
config_nginx
ynh_add_nginx_config
#=================================================
# GENERIC FINALIZATION