my_webapp_ynh/scripts/upgrade
Éric Gaspar 052a5cf96c cleaning
2024-06-23 21:55:41 +02:00

153 lines
4.4 KiB
Bash

#!/bin/bash
source _common.sh
source /usr/share/yunohost/helpers
ynh_app_setting_set_default --key=php_upload_max_filesize --value=500M
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression "Ensuring downward compatibility..."
# If database doesn't exist, create it and remove with_mysql setting
if [ -z "${database:-}" ]; then
if [ $with_mysql -eq 1 ]; then
database="mysql"
else
database="none"
fi
ynh_app_setting_set --key=database --value=$database
ynh_app_setting_delete --key=with_mysql
fi
# If admin_mail_html doesn't exist, create it
# FIXME: maybe replace with: ynh_app_setting_set_default --key=admin_mail_html --value=1
if [ -z "${admin_mail_html:-}" ]; then
admin_mail_html=1
ynh_app_setting_set --key=admin_mail_html --value=$admin_mail_html
fi
# If with_sftp doesn't exist, create it
# FIXME: maybe replace with: ynh_app_setting_set_default --key=with_sftp --value=1
if [ -z "${with_sftp:-}" ]; then
with_sftp=1
ynh_app_setting_set --key=with_sftp --value=$with_sftp
fi
# If php_version doesn't exist, create it. We assume it is the default system one.
if [ -z "$php_version" ]; then
php_version=$YNH_DEFAULT_PHP_VERSION
ynh_app_setting_set --key=php_version --value=$php_version
fi
# If custom_error_file doesn't exist, create it.
# FIXME: maybe replace with: ynh_app_setting_set_default --key=custom_error_file --value=1
if [ -z "${custom_error_file:-}" ]; then
custom_error_file=1
ynh_app_setting_set --key=custom_error_file --value=$custom_error_file
fi
# Delete old user
if [ -n "$(ynh_app_setting_get --key=user)" ]
then
ynh_systemctl --service=php${php_version}-fpm --action=stop
ynh_system_user_delete --username="$(ynh_app_setting_get --key=user)"
ynh_app_setting_delete --key=user
fi
# Ensure password is a setting even if empty, for the config panel
ynh_app_setting_set --key=password --value="$password"
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_ON
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression "Upgrading NGINX web server configuration..."
# Prepare nginx.conf
nginx_extra_conf_dir="/etc/nginx/conf.d/$domain.d/$app.d"
mkdir -p "$nginx_extra_conf_dir"
# Prepare nginx extra conf
if [ $php_version != "none" ]
then
ynh_config_add --template="nginx-php.conf" --destination="$nginx_extra_conf_dir/php.conf"
YNH_PHP_VERSION="$php_version"
fi
# Add the config error code
if [ $custom_error_file -eq 1 ]
then
ynh_config_add --template="nginx-code-error.conf" --destination="$nginx_extra_conf_dir/error-code.conf"
fi
# Create a dedicated NGINX config
ynh_config_add_nginx
ynh_config_add --template="example-custom-nginx-config.conf" --destination="$nginx_extra_conf_dir/sample.conf"
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression "Making sure dedicated system user exists..."
if [ $with_sftp -eq 1 ]
then
groups="sftp.app"
else
groups=""
fi
ynh_system_user_create --username=$app --home_dir="$install_dir" --groups="$groups"
if [ -n "$password" ]
then
# Add the password to this user
chpasswd <<< "${app}:${password}"
fi
# Change the user group for previous my_webapp install script
groupadd -f "$app"
usermod -g "$app" "$app"
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
if [ $php_version != "none" ]
then
ynh_script_progression "Upgrading PHP-FPM configuration..."
ynh_config_add_phpfpm
fi
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
chown -R $app:www-data "$install_dir"
# Home directory of the user needs to be owned by root to allow
# SFTP connections
chown root:root "$install_dir"
setfacl -m g:$app:r-x "$install_dir"
setfacl -m g:www-data:r-x "$install_dir"
chmod 750 "$install_dir"
#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_OFF
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression "Upgrade of $app completed"