From 0d2bc5924b96b3dee6973c72305709b794861c81 Mon Sep 17 00:00:00 2001 From: dkoukoul Date: Thu, 21 Dec 2023 16:33:57 +0200 Subject: [PATCH 1/2] added scripts --- scripts/backup | 49 +++++++++++++++++++++ scripts/restore | 57 +++++++++++++++++++++++++ scripts/upgrade | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+) create mode 100755 scripts/backup create mode 100755 scripts/restore create mode 100755 scripts/upgrade diff --git a/scripts/backup b/scripts/backup new file mode 100755 index 0000000..968ad50 --- /dev/null +++ b/scripts/backup @@ -0,0 +1,49 @@ +#!/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 /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 DATA DIR +#================================================= + +# Only relevant if there is a "data_dir" resource for this app +ynh_backup --src_path="$data_dir" --is_big + +#================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP LOGROTATE +#================================================= + +ynh_backup --src_path="/etc/logrotate.d/$app" + +#================================================= +# 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)." \ No newline at end of file diff --git a/scripts/restore b/scripts/restore new file mode 100755 index 0000000..be7373e --- /dev/null +++ b/scripts/restore @@ -0,0 +1,57 @@ +#!/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 /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" + +# $install_dir will automatically be initialized with some decent +# permissions by default ... however, you may need to recursively reapply +# ownership to all files such as after the ynh_setup_source step +chown -R $app:www-data "$install_dir" + +#================================================= +# RESTORE THE DATA DIRECTORY +#================================================= +ynh_script_progression --message="Restoring the data directory..." --weight=1 + +ynh_restore_file --origin_path="$data_dir" --not_mandatory + +# (Same as for install dir) +chown -R $app:www-data "$data_dir" + +#================================================= +# RESTORE SYSTEM CONFIGURATIONS +#================================================= + +ynh_restore_file --origin_path="/etc/systemd/system/$app.service" +systemctl enable $app.service --quiet + +yunohost service add $app --description="A short description of the app" --log="/var/log/$app/$app.log" + +ynh_restore_file --origin_path="/etc/logrotate.d/$app" + +#================================================= +# GENERIC FINALIZATION +#================================================= + +# Typically you only have either $app or php-fpm but not both at the same time... +ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Restoration completed for $app" --last \ No newline at end of file diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100755 index 0000000..3223b7c --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,110 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +# Settings are automatically loaded as bash variables +# in every app script context, therefore typically these will exist: +# - $domain +# - $path +# - $language +# - $install_dir +# - $port +# ... + +# In the context of upgrade, +# - resources are automatically provisioned / updated / deleted (depending on existing resources) +# - a safety backup is automatically created by the core and will be restored if the upgrade fails + +### This helper will compare the version of the currently installed app and the version of the upstream package. +### $upgrade_type can have 2 different values +### - UPGRADE_APP if the upstream app version has changed +### - UPGRADE_PACKAGE if only the YunoHost package has changed +### ynh_check_app_version_changed will stop the upgrade if the app is up to date. +### UPGRADE_APP should be used to upgrade the core app only if there's an upgrade to do. +upgrade_type=$(ynh_check_app_version_changed) + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +#ynh_script_progression --message="Ensuring downward compatibility..." --weight=1 + +# +# N.B. : the following setting migration snippets are provided as *EXAMPLES* +# of what you may want to do in some cases (e.g. a setting was not defined on +# some legacy installs and you therefore want to initiaze stuff during upgrade) +# + +# If db_name doesn't exist, create it +#if [ -z "$db_name" ]; then +# db_name=$(ynh_sanitize_dbid --db_name=$app) +# ynh_app_setting_set --app=$app --key=db_name --value=$db_name +#fi + +# If install_dir doesn't exist, create it +#if [ -z "$install_dir" ]; then +# install_dir=/var/www/$app +# ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir +#fi + +#================================================= +# 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="Checking for latest version..." --weight=1 + current_version=$($install_dir/cjdroute --version | grep "Cjdns version:" | cut -d'-' -f2,3) + latest_version=$(curl --silent "https://api.github.com/repos/dkoukoul/cjdns/releases/latest" | jq -r ".tag_name") + + # Extract release and subversion from current_version and latest_version + current_release=$(echo $current_version | cut -d'-' -f1 | tr -d 'v') + current_subversion=$(echo $current_version | cut -d'-' -f2) + latest_release=$(echo $latest_version | cut -d'-' -f1 | tr -d 'v') + latest_subversion=$(echo $latest_version | cut -d'-' -f2) + + # Compare release and subversion + if [ $latest_release -gt $current_release ] || [ $latest_release -eq $current_release ] && [ $latest_subversion -gt $current_subversion ] + then + ynh_script_progression --message="New version $latest_version available" --weight=1 + # Download, check integrity, uncompress and patch the source from manifest.toml + ynh_setup_source --dest_dir="$install_dir" + ynh_script_progression --message="Installing latest version..." --weight=1 + else + ynh_script_progression --message="You have the latest version: $latest_version" --weight=1 + fi +fi + +chmod -R o-rwx "$install_dir" +chown -R $app:www-data "$install_dir" +chmod +x "$install_dir/cjdroute" + + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Restarting cjdns service..." --weight=1 + +ynh_systemd_action --service_name=$app --action="restart" --log_path="systemd" +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Upgrade of $app completed" --last \ No newline at end of file From 005d50ab39f749fb30815ea6aa3c478271e25f79 Mon Sep 17 00:00:00 2001 From: dkoukoul Date: Thu, 21 Dec 2023 16:50:38 +0200 Subject: [PATCH 2/2] upgrade --- scripts/upgrade | 62 ++----------------------------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/scripts/upgrade b/scripts/upgrade index 3223b7c..d5ed1aa 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -9,51 +9,11 @@ source _common.sh source /usr/share/yunohost/helpers -# Settings are automatically loaded as bash variables -# in every app script context, therefore typically these will exist: -# - $domain -# - $path -# - $language -# - $install_dir -# - $port -# ... - -# In the context of upgrade, -# - resources are automatically provisioned / updated / deleted (depending on existing resources) -# - a safety backup is automatically created by the core and will be restored if the upgrade fails - -### This helper will compare the version of the currently installed app and the version of the upstream package. -### $upgrade_type can have 2 different values -### - UPGRADE_APP if the upstream app version has changed -### - UPGRADE_PACKAGE if only the YunoHost package has changed -### ynh_check_app_version_changed will stop the upgrade if the app is up to date. -### UPGRADE_APP should be used to upgrade the core app only if there's an upgrade to do. upgrade_type=$(ynh_check_app_version_changed) #================================================= # STANDARD UPGRADE STEPS #================================================= -# ENSURE DOWNWARD COMPATIBILITY -#================================================= -#ynh_script_progression --message="Ensuring downward compatibility..." --weight=1 - -# -# N.B. : the following setting migration snippets are provided as *EXAMPLES* -# of what you may want to do in some cases (e.g. a setting was not defined on -# some legacy installs and you therefore want to initiaze stuff during upgrade) -# - -# If db_name doesn't exist, create it -#if [ -z "$db_name" ]; then -# db_name=$(ynh_sanitize_dbid --db_name=$app) -# ynh_app_setting_set --app=$app --key=db_name --value=$db_name -#fi - -# If install_dir doesn't exist, create it -#if [ -z "$install_dir" ]; then -# install_dir=/var/www/$app -# ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir -#fi #================================================= # STOP SYSTEMD SERVICE @@ -70,26 +30,8 @@ ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app if [ "$upgrade_type" == "UPGRADE_APP" ] then - ynh_script_progression --message="Checking for latest version..." --weight=1 - current_version=$($install_dir/cjdroute --version | grep "Cjdns version:" | cut -d'-' -f2,3) - latest_version=$(curl --silent "https://api.github.com/repos/dkoukoul/cjdns/releases/latest" | jq -r ".tag_name") - - # Extract release and subversion from current_version and latest_version - current_release=$(echo $current_version | cut -d'-' -f1 | tr -d 'v') - current_subversion=$(echo $current_version | cut -d'-' -f2) - latest_release=$(echo $latest_version | cut -d'-' -f1 | tr -d 'v') - latest_subversion=$(echo $latest_version | cut -d'-' -f2) - - # Compare release and subversion - if [ $latest_release -gt $current_release ] || [ $latest_release -eq $current_release ] && [ $latest_subversion -gt $current_subversion ] - then - ynh_script_progression --message="New version $latest_version available" --weight=1 - # Download, check integrity, uncompress and patch the source from manifest.toml - ynh_setup_source --dest_dir="$install_dir" - ynh_script_progression --message="Installing latest version..." --weight=1 - else - ynh_script_progression --message="You have the latest version: $latest_version" --weight=1 - fi + ynh_script_progression --message="Installing latest version..." --weight=1 + ynh_setup_source --dest_dir="$install_dir" fi chmod -R o-rwx "$install_dir"