2018-05-26 10:27:01 +02:00
2016-06-20 23:43:51 +02:00
#!/bin/bash
2018-05-26 10:27:01 +02:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
2016-06-20 23:43:51 +02:00
# Retrieve arguments
2019-05-15 18:42:44 +02:00
app=$YNH_APP_INSTANCE_NAME
2021-08-22 08:58:18 +02:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
redirect_type=$(ynh_app_setting_get --app=$app --key=redirect_type)
redirect_path=$(ynh_app_setting_get --app=$app --key=redirect_path)
2016-06-20 23:43:51 +02:00
2018-05-26 10:27:01 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# Fix is_public as a boolean value
2016-11-23 21:49:09 +01:00
# Default value for redirect_type if upgrading from https://github.com/scith/redirect_ynh
if [ -z "$redirect_type" ];
then
redirect_type="proxy"
2016-11-23 21:53:14 +01:00
ynh_app_setting_set $app 'redirect_type' $redirect_type
2016-11-23 21:49:09 +01:00
fi
2019-05-15 18:35:57 +02:00
# Migrate away from old stuff with 'is_public' and old redirect type names
is_public=$(ynh_app_setting_get "$app" is_public)
if [ -n "$is_public" ]
then
if [ "$is_public" = "Yes" ]; then
is_public=1
elif [ "$is_public" = "No" ]; then
is_public=0
fi
if [ "$is_public" = "0" ] && [ "$redirect_type" != "proxy" ]; then
2021-08-22 08:58:18 +02:00
echo "WARNING: You previously had a 'supposedly' private 301 or 302 redirection... but it was found that it was public all along and it is not easy to create such a private redirection. Your 301 or 302 redirection will be re-flagged as public..." >&2
2019-05-15 18:35:57 +02:00
is_public=1
fi
if [ "$redirect_type" == "proxy" ] && [ "$is_public" = "1" ]
then
redirect_type="public_proxy"
elif [ "$redirect_type" == "proxy" ] && [ "$is_public" = "0" ]
then
redirect_type="private_proxy"
elif [ "$redirect_type" == "visible_302" ]
then
redirect_type="public_302"
elif [ "$redirect_type" == "visible_301" ]
then
redirect_type="public_301"
fi
ynh_app_setting_set $app 'redirect_type' $redirect_type
ynh_app_setting_set $app 'is_public'
fi
2021-08-22 09:01:11 +02:00
# Migrate legacy permissions to new system
if ynh_legacy_permissions_exists
then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
2019-05-15 18:35:57 +02:00
2018-05-26 10:27:01 +02:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
2016-06-20 23:43:51 +02:00
# Validate redirect path
url_regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
2019-04-29 20:49:40 +02:00
[[ ! $redirect_path =~ $url_regex ]] && ynh_die "Invalid destination: $redirect_path" 1
2016-06-20 23:43:51 +02:00
2019-05-15 18:42:44 +02:00
#=================================================
# CONFIGURE NGINX
#=================================================
2016-06-20 23:43:51 +02:00
# Nginx configuration
2019-05-09 23:08:09 +02:00
for FILE in $(ls ../conf/nginx-*.conf)
do
ynh_replace_string "YNH_LOCATION" "$path_url" $FILE
done
2019-05-15 18:35:57 +02:00
if [ "$redirect_type" = "public_302" ];
2016-06-20 23:43:51 +02:00
then
2019-04-29 20:49:40 +02:00
ynh_replace_string "YNH_REDIRECT_PATH" "$redirect_path" ../conf/nginx-visible-302.conf
2018-05-26 10:01:42 +02:00
cp ../conf/nginx-visible-302.conf /etc/nginx/conf.d/$domain.d/$app.conf
2019-05-15 18:35:57 +02:00
elif [ "$redirect_type" = "public_301" ];
2016-06-20 23:43:51 +02:00
then
2019-04-29 20:49:40 +02:00
ynh_replace_string "YNH_REDIRECT_PATH" "$redirect_path" ../conf/nginx-visible-301.conf
2018-05-26 10:01:42 +02:00
cp ../conf/nginx-visible-301.conf /etc/nginx/conf.d/$domain.d/$app.conf
2019-05-15 18:35:57 +02:00
elif [ "$redirect_type" = "public_proxy" ] || [ "$redirect_type" = "private_proxy" ];
2016-06-20 23:43:51 +02:00
then
2019-04-29 20:49:40 +02:00
ynh_replace_string "YNH_REDIRECT_PATH" "$redirect_path" ../conf/nginx-proxy.conf
2018-05-26 10:01:42 +02:00
cp ../conf/nginx-proxy.conf /etc/nginx/conf.d/$domain.d/$app.conf
2016-06-20 23:43:51 +02:00
fi
2018-05-26 10:27:01 +02:00
#=================================================
2019-05-15 18:42:44 +02:00
# CONFIGURE SSOWAT
2018-05-26 10:27:01 +02:00
#=================================================
2016-06-20 23:43:51 +02:00
# Make app public if necessary
2019-05-15 18:35:57 +02:00
if [ "$redirect_type" != "private_proxy" ]
2016-06-20 23:43:51 +02:00
then
2018-05-26 10:27:01 +02:00
# unprotected_uris allows SSO credentials to be passed anyway.
2021-01-23 15:21:17 +01:00
ynh_permission_update --permission="main" --add="visitors"
2016-06-20 23:43:51 +02:00
fi
2021-01-23 15:21:17 +01:00
#=================================================
# 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="Upgrade of $app completed" --last