From 9df1697dea864f18a4e9b209b1a2786be5ccbbf2 Mon Sep 17 00:00:00 2001 From: Neko Nekowazarashi Date: Sun, 4 Jun 2023 14:41:03 +0700 Subject: [PATCH] penambahan skrip pengelolaan --- scripts/_common.sh | 17 +++++++ scripts/backup | 41 ++++++++++++++++ scripts/change_url | 71 +++++++++++++++++++++++++++ scripts/install | 107 +++++++++++++++++++++++++++++++++++++++++ scripts/remove | 33 +++++++++++++ scripts/restore | 50 +++++++++++++++++++ scripts/upgrade | 117 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 436 insertions(+) create mode 100644 scripts/_common.sh create mode 100755 scripts/backup create mode 100644 scripts/change_url create mode 100755 scripts/install create mode 100755 scripts/remove create mode 100755 scripts/restore create mode 100755 scripts/upgrade diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..944a65e --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +#================================================= +# COMMON VARIABLES +#================================================= + +#================================================= +# PERSONAL HELPERS +#================================================= + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +#================================================= +# FUTURE OFFICIAL HELPERS +#================================================= diff --git a/scripts/backup b/scripts/backup new file mode 100755 index 0000000..a151df9 --- /dev/null +++ b/scripts/backup @@ -0,0 +1,41 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source ../settings/scripts/_common.sh +source /usr/share/yunohost/helpers + +#================================================= +# DECLARE DATA AND CONF FILES TO BACKUP +#================================================= +ynh_print_info --message="Declaring files to be backed up..." + +#================================================= +# BACKUP THE APP MAIN DIR +#================================================= + +ynh_backup --src_path="$install_dir" + +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + +ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP SYSTEMD +#================================================= + +ynh_backup --src_path="/etc/systemd/system/$app.service" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." diff --git a/scripts/change_url b/scripts/change_url new file mode 100644 index 0000000..2ba57d8 --- /dev/null +++ b/scripts/change_url @@ -0,0 +1,71 @@ +#!/bin/bash + +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" + +#================================================= +# MODIFY URL IN NGINX CONF +#================================================= +ynh_script_progression --message="Updating NGINX web server configuration..." --weight=1 + +ynh_change_url_nginx_config + +#================================================= +# SPECIFIC MODIFICATIONS +#================================================= +# CONFIGURATION FOR SUBPATH +#================================================= + +if [ $change_path -eq 1 ] +then + if [ "$old_path" = "/" ]; then + old_st_path="" + else + old_st_path=$old_path + fi + + if [ "$new_path" = "/" ]; then + new_st_path="" + else + new_st_path=$new_path + fi + + ynh_replace_string -m "\"$old_st_path/?engine" -r "\"$new_st_path/?engine" -f "$install_dir/simplytranslate/templates/index.html" + ynh_replace_string -m "$old_st_path/prefs" -r "$new_st_path/prefs" -f "$install_dir/simplytranslate/templates/index.html" + ynh_replace_string -m "\"$old_st_path/\"" -r "\"$new_st_path/\"" -f "$install_dir/simplytranslate/templates/prefs.html" + ynh_replace_string -m "$old_st_path/prefs" -r "$new_st_path/prefs" -f "$install_dir/simplytranslate/templates/prefs.html" + ynh_replace_string -m "$old_st_path/api" -r "$new_st_path/api" -f "$install_dir/simplytranslate/main.py" + ynh_replace_string -m "$old_st_path/prefs" -r "$new_st_path/prefs" -f "$install_dir/simplytranslate/main.py" + ynh_replace_string -m "$old_st_path/switchlanguages" -r "$new_st_path/switchlanguages" -f "$install_dir/simplytranslate/main.py" + ynh_replace_string -m "f\"$old_st_path/?" -r "f\"$new_st_path/?" -f "$install_dir/simplytranslate/main.py" + ynh_replace_string -m "\"$old_st_path/\"" -r "\"$new_st_path/\"" -f "$install_dir/simplytranslate/main.py" +fi +#================================================= +# GENERIC FINALISATION +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Change of URL completed for $app" --last diff --git a/scripts/install b/scripts/install new file mode 100755 index 0000000..3a8f30d --- /dev/null +++ b/scripts/install @@ -0,0 +1,107 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# APP "BUILD" (DEPLOYING SOURCES, VENV, COMPILING ETC) +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= +ynh_script_progression --message="Setting up source files..." --weight=1 + +ynh_setup_source --dest_dir="$install_dir/simplytranslate" + +chown -R $app:www-data "$install_dir" + +#================================================= +# SYSTEM CONFIGURATION +#================================================= +ynh_script_progression --message="Adding system configurations related to $app..." --weight=1 + +ynh_add_nginx_config + +ynh_add_systemd_config + +yunohost service add $app --description="Web interface for SimplyTranslate" --log="/var/log/$app/$app.log" + +#================================================= +# APP INITIAL CONFIGURATION +#================================================= +# ADD A CONFIGURATION +#================================================= +ynh_script_progression --message="Adding a configuration file..." --weight=1 + +# some default config +ynh_app_setting_set -a $app -k libretranslate_enable -v false +ynh_app_setting_set -a $app -k libretranslate_instance -v "" +ynh_app_setting_set -a $app -k libretranslate_api -v "" +ynh_app_setting_set -a $app -k google_enable -v true +ynh_app_setting_set -a $app -k deepl_enable -v false +ynh_app_setting_set -a $app -k reverso_enable -v false +ynh_app_setting_set -a $app -k iciba_enable -v false + +# load the above config +libretranslate_enable=$(ynh_app_setting_get -a $app -k libretranslate_enable) +libretranslate_instance=$(ynh_app_setting_get -a $app -k libretranslate_instance) +libretranslate_api=$(ynh_app_setting_get -a $app -k libretranslate_api) +google_enable=$(ynh_app_setting_get -a $app -k google_enable) +deepl_enable=$(ynh_app_setting_get -a $app -k deepl_enable) +iciba_enable=$(ynh_app_setting_get -a $app -k iciba_enable) +reverso_enable=$(ynh_app_setting_get -a $app -k reverso_enable) + +ynh_add_config --template="config.conf.template" --destination="$install_dir/simplytranslate/config.conf" + +chmod 400 "$install_dir/simplytranslate/config.conf" +chown $app:$app "$install_dir/simplytranslate/config.conf" + +#================================================= +# CONFIGURATION FOR SUBPATH +#================================================= + +if [ "$path" = "/" ]; then + st_path="" +else + st_path=$path +fi + +ynh_replace_string -m "\"/?engine" -r "\"$st_path/?engine" -f "$install_dir/simplytranslate/templates/index.html" +ynh_replace_string -m "/prefs" -r "$st_path/prefs" -f "$install_dir/simplytranslate/templates/index.html" +ynh_replace_string -m "\"/\"" -r "\"$st_path/\"" -f "$install_dir/simplytranslate/templates/prefs.html" +ynh_replace_string -m "/prefs" -r "$st_path/prefs" -f "$install_dir/simplytranslate/templates/prefs.html" +ynh_replace_string -m "/api" -r "$st_path/api" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "/prefs" -r "$st_path/prefs" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "/switchlanguages" -r "$st_path/switchlanguages" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "f\"/?" -r "f\"$st_path/?" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "\"/\"" -r "\"$st_path/\"" -f "$install_dir/simplytranslate/main.py" + +#================================================= +# BUILD THE APP +#================================================= +ynh_script_progression --message="Building the app..." --weight=1 + +mkdir "$install_dir/venv" +python3 -m venv "$install_dir/venv" +"$install_dir/venv/bin/python3" -m pip install -r "$install_dir/simplytranslate/requirements.txt" + +chown -R $app:$app "$install_dir/venv" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + +#================================================= +# END OF SCRIPT +#================================================= +ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove new file mode 100755 index 0000000..ab8f44d --- /dev/null +++ b/scripts/remove @@ -0,0 +1,33 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# REMOVE SYSTEM CONFIGURATIONS +#================================================= +# REMOVE SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Removing system configurations related to $app..." --weight=1 + +if ynh_exec_warn_less yunohost service status $app >/dev/null +then + ynh_script_progression --message="Removing $app service integration..." --weight=1 + yunohost service remove $app +fi + +ynh_remove_systemd_config + +ynh_remove_nginx_config + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Removal of $app completed" --last diff --git a/scripts/restore b/scripts/restore new file mode 100755 index 0000000..877c52c --- /dev/null +++ b/scripts/restore @@ -0,0 +1,50 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source ../settings/scripts/_common.sh +source /usr/share/yunohost/helpers + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= +ynh_script_progression --message="Restoring the app main directory..." --weight=1 + +ynh_restore_file --origin_path="$install_dir" + +chown -R $app:www-data "$install_dir" + +#================================================= +# RESTORE SYSTEM CONFIGURATIONS +#================================================= +# RESTORE THE PHP-FPM CONFIGURATION +#================================================= +ynh_script_progression --message="Restoring system configurations related to $app..." --weight=1 + +ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" + +ynh_restore_file --origin_path="/etc/systemd/system/$app.service" +systemctl enable $app.service --quiet + +yunohost service add $app --description="Web interface for SimplyTranslate" --log="/var/log/$app/$app.log" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM OR THE APP SERVICE +#================================================= +ynh_script_progression --message="Reloading NGINX web server and $app's service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Restoration completed for $app" --last diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100755 index 0000000..3c53376 --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,117 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +upgrade_type=$(ynh_check_app_version_changed) + +#================================================= +# IMPORT SOME CONFIG +#================================================= + +libretranslate_enable=$(ynh_app_setting_get -a $app -k libretranslate_enable) +libretranslate_instance=$(ynh_app_setting_get -a $app -k libretranslate_instance) +libretranslate_api=$(ynh_app_setting_get -a $app -k libretranslate_api) +google_enable=$(ynh_app_setting_get -a $app -k google_enable) +deepl_enable=$(ynh_app_setting_get -a $app -k deepl_enable) +iciba_enable=$(ynh_app_setting_get -a $app -k iciba_enable) +reverso_enable=$(ynh_app_setting_get -a $app -k reverso_enable) +port=$(ynh_app_setting_get -a $app -k port) + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" + +#================================================= +# "REBUILD" THE APP (DEPLOY NEW SOURCES, RERUN NPM BUILD...) +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +if [ "$upgrade_type" == "UPGRADE_APP" ] +then + ynh_script_progression --message="Upgrading source files..." --weight=1 + + ynh_setup_source --dest_dir="$install_dir/simplytranslate" +fi + +chown -R $app:www-data "$install_dir" + +#================================================= +# CONFIGURATION FOR SUBPATH +#================================================= + +if [ "$path" = "/" ]; then + st_path="" +else + st_path=$path +fi + +ynh_replace_string -m "\"/?engine" -r "\"$st_path/?engine" -f "$install_dir/simplytranslate/templates/index.html" +ynh_replace_string -m "/prefs" -r "$st_path/prefs" -f "$install_dir/simplytranslate/templates/index.html" +ynh_replace_string -m "\"/\"" -r "\"$st_path/\"" -f "$install_dir/simplytranslate/templates/prefs.html" +ynh_replace_string -m "/prefs" -r "$st_path/prefs" -f "$install_dir/simplytranslate/templates/prefs.html" +ynh_replace_string -m "/api" -r "$st_path/api" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "/prefs" -r "$st_path/prefs" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "/switchlanguages" -r "$st_path/switchlanguages" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "f\"/?" -r "f\"$st_path/?" -f "$install_dir/simplytranslate/main.py" +ynh_replace_string -m "\"/\"" -r "\"$st_path/\"" -f "$install_dir/simplytranslate/main.py" + +#================================================= +# REAPPLY SYSTEM CONFIGURATIONS +#================================================= +ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=1 + +ynh_add_nginx_config + +ynh_add_systemd_config + +yunohost service add $app --description="Web interface for SimplyTranslate" --log="/var/log/$app/$app.log" + +#================================================= +# RECONFIGURE THE APP (UPDATE CONF, APPLY MIGRATIONS...) +#================================================= +# UPDATE A CONFIG FILE +#================================================= +ynh_script_progression --message="Updating a configuration file..." --weight=1 + +ynh_add_config --template="config.conf.template" --destination="$install_dir/simplytranslate/config.conf" + +chmod 400 "$install_dir/simplytranslate/config.conf" +chown $app:$app "$install_dir/simplytranslate/config.conf" + +#================================================= +# REBUILDING THE APP +#================================================= +ynh_script_progression --message="Rebuilding the app..." --weight=1 + +ynh_secure_remove "$install_dir/venv" +mkdir "$install_dir/venv" +python3 -m venv "$install_dir/venv" +"$install_dir/venv/bin/python3" -m pip install -r "$install_dir/simplytranslate/requirements.txt" + +chown -R $app:$app "$install_dir/venv" + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Upgrade of $app completed" --last