#!/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 /usr/share/yunohost/helpers #================================================= # MANAGE SCRIPT FAILURE #================================================= ynh_clean_setup () { true } # Exit if an error occurs during the execution of the script ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= ynh_script_progression --message="Loading installation 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) db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_user=$db_name datadir=$(ynh_app_setting_get --app=$app --key=datadir) redis_db=$(ynh_redis_get_free_db) ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db" config_api_gateway="$final_path/live/api-gateway.env" config_auth="$final_path/live/auth.env" config_auth_worker="$final_path/live/auth-worker.env" config_files="$final_path/live/files.env" config_syncing_server="$final_path/live/syncing-server.env" config_syncing_server_worker="$final_path/live/syncing-server-worker.env" config_workspace="$final_path/live/workspace.env" config_nginx="/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # CHECK IF THE APP CAN BE RESTORED #================================================= ynh_script_progression --message="Validating restoration parameters..." --weight=2 test ! -d $final_path \ || ynh_die --message="There is already a directory: $final_path " #================================================= # STANDARD RESTORATION STEPS #================================================= # FIND AND OPEN A PORT #================================================= ynh_script_progression --message="Finding an available port..." --weight=1 # Find an available port port_api_gateway=$(ynh_find_port --port=3000) port_auth=$(ynh_find_port --port=$((port_api_gateway+1))) port_auth_worker=$(ynh_find_port --port=$((port_auth+1))) port_files=$(ynh_find_port --port=$((port_auth_worker+1))) port_syncing_server=$(ynh_find_port --port=$((port_files+1))) port_syncing_server_worker=$(ynh_find_port --port=$((port_syncing_server+1))) port_workspace=$(ynh_find_port --port=$((port_syncing_server_worker+1))) ynh_app_setting_set --app=$app --key=port_api_gateway --value=$port_api_gateway 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_files --value=$port_files ynh_app_setting_set --app=$app --key=port_syncing_server --value=$port_syncing_server ynh_app_setting_set --app=$app --key=port_syncing_server_worker --value=$port_syncing_server_worker ynh_app_setting_set --app=$app --key=port_workspace --value=$port_workspace #================================================= # 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 THE APP MAIN DIR #================================================= ynh_script_progression --message="Restoring the app main directory..." --weight=1 ynh_restore_file --origin_path="$final_path" chmod 750 "$final_path" chmod -R o-rwx "$final_path" chown -R $app:$app "$final_path" #================================================= # RESTORE THE DATA DIRECTORY #================================================= ynh_script_progression --message="Restoring the data directory..." --weight=1 ynh_restore_file --origin_path="$datadir" --not_mandatory mkdir -p "$datadir/uploads" chmod 750 "$datadir" chmod -R o-rwx "$datadir" chown -R $app:$app "$datadir" #================================================= # RESTORE FAIL2BAN CONFIGURATION #================================================= ynh_script_progression --message="Restoring the Fail2Ban configuration..." --weight=1 ynh_restore_file --origin_path="/etc/fail2ban/jail.d/$app.conf" ynh_restore_file --origin_path="/etc/fail2ban/filter.d/$app.conf" ynh_systemd_action --action=restart --service_name=fail2ban #================================================= # SPECIFIC RESTORATION #================================================= # 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 NGINX CONFIGURATION #================================================= ynh_script_progression --message="Restoring the NGINX web server configuration..." --weight=1 ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # RESTORE THE MYSQL DATABASE #================================================= ynh_script_progression --message="Restoring the MySQL database..." --weight=2 db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) 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_api_gateway" 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" ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_files" ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server" ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_worker" ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_workspace" # Syncing_Server Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server" --target_file="$config_syncing_server" ynh_replace_string --match_string="^SYNCING_SERVER_JS_URL.*$" --replace_string="SYNCING_SERVER_JS_URL=http://localhost:$port_syncing_server" --target_file="$config_api_gateway" ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=http://localhost:$port_syncing_server" --target_file="$config_auth" ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=http://localhost:$port_syncing_server" --target_file="$config_auth_worker" # Syncing_Server_Worker Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_worker" --target_file="$config_syncing_server_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=http://localhost:$port_auth" --target_file="$config_api_gateway" ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_syncing_server" ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_syncing_server_worker" # 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" ynh_replace_string_on_line --line="2" --match_string="proxy_pass.*$" --replace_string="proxy_pass http://127.0.0.1:$port_api_gateway/;" --target_file="$config_nginx" # Files Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_files" --target_file="$config_files" ynh_replace_string_on_line --line="17" --match_string="proxy_pass.*$" --replace_string="proxy_pass http://127.0.0.1:$port_files/;" --target_file="$config_nginx" # Workspace Port ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_workspace" --target_file="$config_workspace" ynh_replace_string --match_string="^WORKSPACE_SERVER_URL.*$" --replace_string="WORKSPACE_SERVER_URL=http://localhost:$port_workspace" --target_file="$config_api_gateway" #================================================= # RESTORE VARIOUS FILES #================================================= ynh_script_progression --message="Restoring various files..." --weight=1 ynh_restore_file --origin_path="/etc/cron.d/$app" #================================================= # ADD SWAP #================================================= ynh_script_progression --message="Adding swap..." if [ ${PACKAGE_CHECK_EXEC:-0} -eq 0 ]; then ynh_add_swap --size=$swap_needed fi #================================================= # RESTORE SYSTEMD #================================================= ynh_script_progression --message="Restoring the systemd configuration..." --weight=1 ynh_restore_file --origin_path="/etc/systemd/system/$app-api-gateway.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-files.service" ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server.service" ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server-worker.service" ynh_restore_file --origin_path="/etc/systemd/system/$app-workspace.service" systemctl enable $app-api-gateway.service --quiet systemctl enable $app-auth.service --quiet systemctl enable $app-auth-worker.service --quiet systemctl enable $app-files.service --quiet systemctl enable $app-syncing-server.service --quiet systemctl enable $app-syncing-server-worker.service --quiet systemctl enable $app-workspace.service --quiet #================================================= # RESTORE THE LOGROTATE CONFIGURATION #================================================= ynh_script_progression --message="Restoring the logrotate configuration..." --weight=1 mkdir -p "/var/log/$app" chown -R $app: "/var/log/$app" ynh_restore_file --origin_path="/etc/logrotate.d/$app" #================================================= # INTEGRATE SERVICE IN YUNOHOST #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 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" #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression --message="Starting a systemd service..." --weight=1 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.*$' #================================================= # 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