mirror of
https://github.com/YunoHost-Apps/snserver_ynh.git
synced 2024-09-03 20:26:22 +02:00
261 lines
12 KiB
Bash
Executable file
261 lines
12 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
|
|
source ../settings/scripts/_common.sh
|
|
source ../settings/scripts/ynh_add_swap
|
|
source ../settings/scripts/ynh_install_ruby__2
|
|
source ../settings/scripts/ynh_redis
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# MANAGE SCRIPT FAILURE
|
|
#=================================================
|
|
|
|
ynh_clean_setup () {
|
|
#### Remove this function if there's nothing to clean before calling the remove script.
|
|
true
|
|
}
|
|
# Exit if an error occurs during the execution of the script
|
|
ynh_abort_if_errors
|
|
|
|
#=================================================
|
|
# LOAD SETTINGS
|
|
#=================================================
|
|
ynh_script_progression --message="Loading settings..." --weight=2
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
domain=$(ynh_app_setting_get --app=$app --key=domain)
|
|
path_url=$(ynh_app_setting_get --app=$app --key=path)
|
|
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
|
|
final_path_www=$(ynh_app_setting_get --app=$app --key=final_path_www)
|
|
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
|
|
db_user=$db_name
|
|
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
|
|
|
|
redis_db=$(ynh_redis_get_free_db)
|
|
ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db"
|
|
|
|
config_syncing_server_js="$final_path/live/syncing-server-js.env"
|
|
config_syncing_server_js_worker="$final_path/live/syncing-server-js-worker.env"
|
|
config_auth="$final_path/live/auth.env"
|
|
config_auth_worker="$final_path/live/auth-worker.env"
|
|
config_api_gateway="$final_path/live/api-gateway.env"
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE RESTORED
|
|
#=================================================
|
|
ynh_script_progression --message="Validating restoration parameters..." --weight=2
|
|
|
|
ynh_webpath_available --domain=$domain --path_url=$path_url || ynh_die --message="Path not available: ${domain}${path_url}"
|
|
test ! -d $final_path || ynh_die --message="There is already a directory: $final_path "
|
|
test ! -d $final_path_www || ynh_die --message="There is already a directory: $final_path_www "
|
|
|
|
#=================================================
|
|
# STANDARD RESTORATION STEPS
|
|
#=================================================
|
|
# FIND AND OPEN A PORT
|
|
#=================================================
|
|
ynh_script_progression --message="Finding an available port..." --weight=1
|
|
|
|
# Find an available port
|
|
port_syncing_server_js=$(ynh_find_port --port=3000)
|
|
port_syncing_server_js_worker=$(ynh_find_port --port=$((port_syncing_server_js+1)))
|
|
port_auth=$(ynh_find_port --port=$((port_syncing_server_js_worker+1)))
|
|
port_auth_worker=$(ynh_find_port --port=$((port_auth+1)))
|
|
port_api_gateway=$(ynh_find_port --port=$((port_auth_worker+1)))
|
|
ynh_app_setting_set --app=$app --key=port_syncing_server_js --value=$port_syncing_server_js
|
|
ynh_app_setting_set --app=$app --key=port_syncing_server_js_worker --value=$port_syncing_server_js_worker
|
|
ynh_app_setting_set --app=$app --key=port_auth --value=$port_auth
|
|
ynh_app_setting_set --app=$app --key=port_auth_worker --value=$port_auth_worker
|
|
ynh_app_setting_set --app=$app --key=port_api_gateway --value=$port_api_gateway
|
|
|
|
#=================================================
|
|
# RESTORE THE NGINX CONFIGURATION
|
|
#=================================================
|
|
|
|
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
|
|
|
|
#=================================================
|
|
# RESTORE THE APP MAIN DIR
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring the app main directory..." --weight=1
|
|
|
|
ynh_restore_file --origin_path="$final_path"
|
|
ynh_restore_file --origin_path="$final_path_www"
|
|
|
|
#=================================================
|
|
# RECREATE THE DEDICATED USER
|
|
#=================================================
|
|
ynh_script_progression --message="Recreating the dedicated system user..." --weight=1
|
|
|
|
# Create the dedicated user (if not existing)
|
|
ynh_system_user_create --username=$app --home_dir=$final_path
|
|
|
|
#=================================================
|
|
# RESTORE USER RIGHTS
|
|
#=================================================
|
|
|
|
# Restore permissions on app files
|
|
chmod 750 "$final_path"
|
|
chmod -R o-rwx "$final_path"
|
|
chown -R $app:$app "$final_path"
|
|
|
|
chmod 750 "$final_path_www"
|
|
chmod -R o-rwx "$final_path_www"
|
|
chown -R "www-data":"www-data" "$final_path_www"
|
|
|
|
mkdir -p "/var/log/$app"
|
|
chown -R $app: "/var/log/$app"
|
|
|
|
#=================================================
|
|
# SPECIFIC RESTORATION
|
|
#=================================================
|
|
#=================================================
|
|
# ADD SWAP
|
|
#=================================================
|
|
ynh_script_progression --message="Adding swap..."
|
|
|
|
ynh_add_swap --size=$swap_needed
|
|
|
|
#=================================================
|
|
# REINSTALL DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Reinstalling dependencies..." --weight=17
|
|
|
|
# Define and install dependencies
|
|
ynh_install_app_dependencies $pkg_dependencies
|
|
ynh_install_nodejs --nodejs_version=$NODEJS_VERSION
|
|
ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg"
|
|
|
|
#=================================================
|
|
# RESTORE THE MYSQL DATABASE
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring the MySQL database..." --weight=2
|
|
|
|
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd
|
|
ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql
|
|
|
|
#=================================================
|
|
# MODIFY CONFIG
|
|
#=================================================
|
|
ynh_script_progression --message="Modify config files..." --weight=2
|
|
|
|
# Redis Port
|
|
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_js"
|
|
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_js_worker"
|
|
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_auth"
|
|
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_auth_worker"
|
|
# Syncing_Server_JS Port
|
|
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_js" --target_file="$config_syncing_server_js"
|
|
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server_js" --target_file="$config_auth"
|
|
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server_js" --target_file="$config_auth_worker"
|
|
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server_js" --target_file="$config_api_gateway"
|
|
# Syncing_Server_JS_Worker Port
|
|
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_js_worker" --target_file="$config_syncing_server_js_worker"
|
|
# Auth Port
|
|
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_auth" --target_file="$config_auth"
|
|
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_syncing_server_js"
|
|
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_syncing_server_js_worker"
|
|
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_api_gateway"
|
|
# Auth_Worker Port
|
|
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_auth_worker" --target_file="$config_auth_worker"
|
|
# API-Gateway Port
|
|
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_api_gateway" --target_file="$config_api_gateway"
|
|
|
|
|
|
|
|
#=================================================
|
|
# RESTORE SYSTEMD
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring the systemd configuration..." --weight=1
|
|
|
|
ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server-js.service"
|
|
ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server-js-worker.service"
|
|
ynh_restore_file --origin_path="/etc/systemd/system/$app-auth.service"
|
|
ynh_restore_file --origin_path="/etc/systemd/system/$app-auth-worker.service"
|
|
ynh_restore_file --origin_path="/etc/systemd/system/$app-api-gateway.service"
|
|
systemctl enable $app-syncing-server-js.service --quiet
|
|
systemctl enable $app-syncing-server-js-worker.service --quiet
|
|
systemctl enable $app-auth.service --quiet
|
|
systemctl enable $app-auth-worker.service --quiet
|
|
systemctl enable $app-api-gateway.service --quiet
|
|
|
|
#=================================================
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
#=================================================
|
|
ynh_script_progression --message="Integrate $app service in Yunohost..." --weight=1
|
|
|
|
yunohost service add "$app-syncing-server-js" --description="Standard Notes - Syncing Server" --log="/var/log/$app/syncing-server-js.log"
|
|
yunohost service add "$app-syncing-server-js-worker" --description="Standard Notes - Syncing Server - Worker" --log="/var/log/$app/syncing-server-js-worker.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-api-gateway" --description="Standard Notes - API Gateway" --log="/var/log/$app/api-gateway.log"
|
|
|
|
#=================================================
|
|
# START SYSTEMD SERVICE
|
|
#=================================================
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=1
|
|
|
|
ynh_systemd_action \
|
|
--service_name="$app-syncing-server-js" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/syncing-server-js.log" \
|
|
--line_match='{"message":"Server started on port '$port_syncing_server_js'","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-syncing-server-js-worker" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/syncing-server-js-worker.log" \
|
|
--line_match='{"message":"Starting worker...","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-auth" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/auth.log" \
|
|
--line_match='{"message":"Server started on port '$port_auth'","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-auth-worker" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/auth-worker.log" \
|
|
--line_match='{"message":"Starting worker...","level":"info"}'
|
|
ynh_systemd_action \
|
|
--service_name="$app-api-gateway" \
|
|
--action="start" \
|
|
--log_path="/var/log/$app/api-gateway.log" \
|
|
--line_match='{"message":"Server started on port '$port_api_gateway'","level":"info"}'
|
|
|
|
#=================================================
|
|
# RESTORE THE LOGROTATE CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=1
|
|
|
|
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
|
|
|
|
#=================================================
|
|
# RESTORE FAIL2BAN CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Restoring the fail2ban configuration..." --weight=1
|
|
|
|
ynh_restore_file "/etc/fail2ban/jail.d/$app.conf"
|
|
ynh_restore_file "/etc/fail2ban/filter.d/$app.conf"
|
|
ynh_systemd_action --action=restart --service_name=fail2ban
|
|
|
|
#=================================================
|
|
# GENERIC FINALIZATION
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading nginx web server..." --weight=2
|
|
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Restoration completed for $app" --last
|