1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/snserver_ynh.git synced 2024-09-03 20:26:22 +02:00
snserver_ynh/scripts/upgrade
2024-05-20 20:53:12 +02:00

255 lines
11 KiB
Bash
Executable file

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_add_swap
source /usr/share/yunohost/helpers
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping systemd services..." --weight=1
ynh_systemd_action \
--service_name="$app-api-gateway" \
--action="stop" \
--log_path="/var/log/$app/api-gateway.log"
ynh_systemd_action \
--service_name="$app-auth" \
--action="stop" \
--log_path="/var/log/$app/auth.log"
ynh_systemd_action \
--service_name="$app-auth-worker" \
--action="stop" \
--log_path="/var/log/$app/auth-worker.log"
ynh_systemd_action \
--service_name="$app-files" \
--action="stop" \
--log_path="/var/log/$app/files.log"
ynh_systemd_action \
--service_name="$app-syncing-server" \
--action="stop" \
--log_path="/var/log/$app/syncing-server.log"
ynh_systemd_action \
--service_name="$app-syncing-server-worker" \
--action="stop" \
--log_path="/var/log/$app/syncing-server-worker.log"
ynh_systemd_action \
--service_name="$app-workspace" \
--action="stop" \
--log_path="/var/log/$app/workspace.log"
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
# Create non existing settings
if [ -z "${jwt_secret:-}" ]; then
jwt_secret=$(ynh_string_random --length=48 | base64)
ynh_app_setting_set --app="$app" --key=jwt_secret --value="$jwt_secret"
fi
if [ -z "${legacy_jwt_secret:-}" ]; then
legacy_jwt_secret=$(ynh_string_random --length=48 | base64)
ynh_app_setting_set --app="$app" --key=legacy_jwt_secret --value="$legacy_jwt_secret"
fi
if [ -z "${auth_jwt_secret:-}" ]; then
auth_jwt_secret=$(ynh_string_random --length=48 | base64)
ynh_app_setting_set --app="$app" --key=auth_jwt_secret --value="$auth_jwt_secret"
fi
if [ -z "${pseudo_key_params_key:-}" ]; then
pseudo_key_params_key=$(ynh_string_random --length=48 | base64)
ynh_app_setting_set --app="$app" --key=pseudo_key_params_key --value="$pseudo_key_params_key"
fi
if [ -z "${encryption_server_key:-}" ]; then
encryption_server_key=$(hexdump -n 32 -e '4/4 "%08X"' /dev/random) # 32bytes hex key is required
ynh_app_setting_set --app="$app" --key=encryption_server_key --value="$encryption_server_key"
fi
if [ -z "${valet_token_secret:-}" ]; then
valet_token_secret=$(ynh_string_random --length=48 | base64)
ynh_app_setting_set --app="$app" --key=valet_token_secret --value="$valet_token_secret"
fi
if [ -z "${disable_user_registration:-}" ]; then
disable_user_registration=false
ynh_app_setting_set --app="$app" --key=DISABLE_USER_REGISTRATION --value="$disable_user_registration"
fi
if [ -z "${files_size:-}" ]; then
files_size=100
ynh_app_setting_set --app="$app" --key=FILES_SIZE --value="$files_size"
fi
# Delete legacy settings
if [ -n "${api_gateway_version_installed+x}" ]; then
ynh_app_setting_delete --app="$app" --key=api_gateway_version_installed
fi
if [ -n "${auth_version_installed+x}" ]; then
ynh_app_setting_delete --app="$app" --key=auth_version_installe
fi
if [ -n "${syncing_server_version_installed+x}" ]; then
ynh_app_setting_delete --app="$app" --key=syncing_server_version_installed
fi
if [ -n "${install_dir_www+x}" ]; then
ynh_app_setting_delete --app="$app" --key=install_dir_www
fi
if [ -n "${install_dir_extensions+x}" ]; then
ynh_app_setting_delete --app="$app" --key=install_dir_extensions
fi
# If old service exsits; remove it
if ynh_exec_warn_less yunohost service status "$app-syncing-server-js" >/dev/null; then
ynh_script_progression --message="Removing old service..." --weight=1
yunohost service remove "$app-syncing-server-js"
ynh_remove_systemd_config --service="$app-syncing-server-js"
ynh_reset_systemd
fi
if ynh_exec_warn_less yunohost service status "$app-syncing-server-js-worker" >/dev/null; then
ynh_script_progression --message="Removing old service..." --weight=1
yunohost service remove "$app-syncing-server-js-worker"
ynh_remove_systemd_config --service="$app-syncing-server-js-worker"
ynh_reset_systemd
fi
#=================================================
# UPGRADE NODEJS
#=================================================
ynh_script_progression --message="Upgrading NodeJS..." --weight=1
ynh_install_nodejs --nodejs_version="$nodejs_version"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_script_progression --message="Upgrading source files..." --weight=1
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$install_dir/live" --full_replace=1
cp "$YNH_APP_BASEDIR/sources/extra_files/cron.sh" "$install_dir/cron.sh"
chmod -R o-rwx "$install_dir"
chown -R "$app:$app" "$install_dir"
#=================================================
# ADD A CONFIGURATION
#=================================================
ynh_script_progression --message="Adding configuration files..." --weight=2
ynh_add_config --template="env_api-gateway.env.sample" --destination="$config_api_gateway"
ynh_add_config --template="env_auth.env.sample" --destination="$config_auth"
ynh_add_config --template="env_auth-worker.env.sample" --destination="$config_auth_worker"
ynh_add_config --template="env_files.env.sample" --destination="$config_files"
ynh_add_config --template="env_syncing-server.env.sample" --destination="$config_syncing_server"
ynh_add_config --template="env_syncing-server-worker.env.sample" --destination="$config_syncing_server_worker"
ynh_add_config --template="env_workspace.env.sample" --destination="$config_workspace"
ynh_add_config --template="cron.env" --destination="$install_dir/cron.env"
#=================================================
# INSTALLING Standard Notes - Syncing Server
#=================================================
ynh_script_progression --message="Building Standard Notes - Syncing Server..." --weight=93
ynh_use_nodejs
pushd "$install_dir/live"
ynh_exec_warn_less ynh_exec_as "$app" env NODE_ENV="production" NODE_OPTIONS="--max-old-space-size=$node_max_old_space_size" "$ynh_node_load_PATH" yarn install --immutable
ynh_exec_warn_less ynh_exec_as "$app" env NODE_ENV="production" NODE_OPTIONS="--max-old-space-size=$node_max_old_space_size" "$ynh_node_load_PATH" yarn build
popd
#=================================================
# REAPPLY SYSTEM CONFIGURATIONS
#=================================================
ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=1
# Create a dedicated NGINX config
ynh_add_nginx_config
# Create a dedicated systemd config
ynh_add_systemd_config --service="$app-api-gateway" --template="systemd_api-gateway.service"
ynh_add_systemd_config --service="$app-auth" --template="systemd_auth.service"
ynh_add_systemd_config --service="$app-auth-worker" --template="systemd_auth-worker.service"
ynh_add_systemd_config --service="$app-files" --template="systemd_files.service"
ynh_add_systemd_config --service="$app-syncing-server" --template="systemd_syncing-server.service"
ynh_add_systemd_config --service="$app-syncing-server-worker" --template="systemd_syncing-server-worker.service"
ynh_add_systemd_config --service="$app-workspace" --template="systemd_workspace.service"
yunohost service add "$app-api-gateway" --description="Standard Notes - API Gateway" --log="/var/log/$app/api-gateway.log"
yunohost service add "$app-auth" --description="Standard Notes - Auth" --log="/var/log/$app/auth.log"
yunohost service add "$app-auth-worker" --description="Standard Notes - Auth - Worker" --log="/var/log/$app/auth-worker.log"
yunohost service add "$app-files" --description="Standard Notes - Files" --log="/var/log/$app/files.log"
yunohost service add "$app-syncing-server" --description="Standard Notes - Syncing Server" --log="/var/log/$app/syncing-server.log"
yunohost service add "$app-syncing-server-worker" --description="Standard Notes - Syncing Server - Worker" --log="/var/log/$app/syncing-server-worker.log"
yunohost service add "$app-workspace" --description="Standard Notes - Workspace" --log="/var/log/$app/workspace.log"
# Use logrotate to manage application logfile(s)
ynh_use_logrotate --logfile="/var/log/$app/api-gateway.log"
ynh_use_logrotate --logfile="/var/log/$app/auth.log"
ynh_use_logrotate --logfile="/var/log/$app/auth-worker.log"
ynh_use_logrotate --logfile="/var/log/$app/files.log"
ynh_use_logrotate --logfile="/var/log/$app/syncing-server.log"
ynh_use_logrotate --logfile="/var/log/$app/syncing-server-worker.log"
ynh_use_logrotate --logfile="/var/log/$app/workspace.log"
# Create a dedicated Fail2Ban config
ynh_add_fail2ban_config --use_template
ynh_add_config --template="cron" --destination="/etc/cron.d/$app"
chown root: "/etc/cron.d/$app"
chmod 640 "/etc/cron.d/$app"
if [ "${PACKAGE_CHECK_EXEC:-0}" -eq 0 ]; then
ynh_add_swap --size="$swap_needed"
fi
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting systemd services..." --weight=1
# Start a systemd service
ynh_systemd_action \
--service_name="$app-api-gateway" \
--action="start" \
--log_path="/var/log/$app/api-gateway.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-auth" \
--action="start" \
--log_path="/var/log/$app/auth.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-auth-worker" \
--action="start" \
--log_path="/var/log/$app/auth-worker.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-files" \
--action="start" \
--log_path="/var/log/$app/files.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-syncing-server" \
--action="start" \
--log_path="/var/log/$app/syncing-server.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-syncing-server-worker" \
--action="start" \
--log_path="/var/log/$app/syncing-server-worker.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-workspace" \
--action="start" \
--log_path="/var/log/$app/workspace.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last