my_webapp_ynh/scripts/upgrade
Antoine Lima 9c6143df27
Add NodeJS server as an alternative to PHP
This adds:
- Two entries in the manifest:
    - A choice for the NodeJS version
    - A port (provisionned in any case due to ynh limitations, but this should not matter)
- Services and configs:
    - Systemctl configs to run the NodeJS server
    - A watcher service and path to restart NodeJS upon file update
    - A custom NGinx config because it is incompatible with the default one
- Docs:
    - More info in the description and admin

The install/remove/backup/restore have been adapted and tested.
The upgrade script is updated but not tested
The change_url script does not change

It is not possible to have both PHP and NodeJS to keep the scripts simple.
2024-03-10 14:43:30 +01:00

212 lines
6.9 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="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 --app=$app --key=database --value=$database
ynh_app_setting_delete --app=$app --key=with_mysql
fi
# If admin_mail_html doesn't exist, create it
if [ -z "${admin_mail_html:-}" ]; then
admin_mail_html=1
ynh_app_setting_set --app=$app --key=admin_mail_html --value=$admin_mail_html
fi
# If fpm_footprint doesn't exist, create it
if [ -z "${fpm_footprint:-}" ]; then
fpm_footprint=low
ynh_app_setting_set --app=$app --key=fpm_footprint --value=$fpm_footprint
fi
# If fpm_free_footprint doesn't exist, create it
if [ -z "${fpm_free_footprint:-}" ]; then
fpm_free_footprint=0
ynh_app_setting_set --app=$app --key=fpm_free_footprint --value=$fpm_free_footprint
fi
# If fpm_usage doesn't exist, create it
if [ -z "${fpm_usage:-}" ]; then
fpm_usage=low
ynh_app_setting_set --app=$app --key=fpm_usage --value=$fpm_usage
fi
# If with_sftp doesn't exist, create it
if [ -z "${with_sftp:-}" ]; then
with_sftp=1
ynh_app_setting_set --app=$app --key=with_sftp --value=$with_sftp
fi
# If phpversion doesn't exist, create it. We assume it is the default system one.
if [ -z "$phpversion" ]; then
phpversion=$YNH_DEFAULT_PHP_VERSION
ynh_app_setting_set --app=$app --key=phpversion --value=$phpversion
fi
# If phpversion doesn't exist, create it. We assume it is the default system one.
if [ -z "$nodeversion" ]; then
nodeversion="none"
ynh_app_setting_set --app=$app --key=nodeversion --value=$nodeversion
fi
# Delete old user
if [ -n "$(ynh_app_setting_get --app=$app --key=user)" ]
then
ynh_systemd_action --service_name=php${phpversion}-fpm --action=stop
ynh_system_user_delete --username="$(ynh_app_setting_get --app=$app --key=user)"
ynh_app_setting_delete --app=$app --key=user
fi
# Ensure password is a setting even if empty, for the config panel
ynh_app_setting_set --app=$app --key=password --value="$password"
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_ON
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2
# 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 [ $phpversion != "none" ]
then
ynh_add_config --template="nginx-php.conf" --destination="$nginx_extra_conf_dir/php.conf"
YNH_PHP_VERSION="$phpversion"
fi
# Create a dedicated NGINX config
# Use a custom nginx config when using nodejs as it is incompatible with the html/php one
if [ $nodeversion == "none" ]
then
ynh_add_nginx_config
ynh_add_config --template="example-custom-nginx-config.conf" --destination="$nginx_extra_conf_dir/sample.conf"
else
# Add the config manually because yunohost does not support custom nginx confs
ynh_add_config --template="nginx-nodejs.conf" --destination="/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_systemd_action --service_name=nginx --action=reload
fi
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=2
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 [ $phpversion != "none" ]
then
ynh_script_progression --message="Upgrading PHP-FPM configuration..." --weight=2
# Create a dedicated PHP-FPM config
ynh_add_fpm_config --usage=$fpm_usage --footprint=$fpm_footprint --phpversion=$phpversion
fi
#=================================================
# NodeJS CONFIGURATION
#=================================================
if [ $nodeversion != "none" ]
then
ynh_script_progression --message="Updating NodeJS..." --weight=3
ynh_install_nodejs --nodejs_version=$nodeversion
ynh_use_nodejs
ynh_add_config --template="../sources/www/package.json" --destination="$install_dir/www/package.json"
ynh_add_config --template="../sources/www/index.js" --destination="$install_dir/www/index.js"
ynh_add_systemd_config --service="${app}-nodejs" --template="nodejs.service"
ynh_add_systemd_config --service="${app}-nodejs-watcher" --template="nodejs-watcher.service"
ynh_add_config --template="nodejs-watcher.path" --destination="/etc/systemd/system/${app}-nodejs-watcher.path"
systemctl enable "${app}-nodejs-watcher.path" --quiet
systemctl daemon-reload
yunohost service add "${app}-nodejs" --description="$app NodeJS Server" --log="/var/log/$app-nodejs.log"
ynh_add_config --template="nginx-nodejs.conf" --destination="$nginx_extra_conf_dir/nodejs.conf"
ynh_systemd_action --service_name="${app}-nodejs"
ynh_systemd_action --service_name="${app}-nodejs-watcher"
ynh_systemd_action --service_name="${app}-nodejs-watcher.path"
fi
#=================================================
# GENERIC FINALIZATION
#=================================================
# 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 --message="Upgrade of $app completed" --last