From b8df76ed03a5cc9ff69456f6a4d6aeb4a9c62252 Mon Sep 17 00:00:00 2001 From: ewilly Date: Fri, 7 Jan 2022 09:52:55 +0100 Subject: [PATCH] Fix --- scripts/_common.sh | 4 +- scripts/backup | 74 +++++++++++-- scripts/change_url | 83 +++++++++++--- scripts/install | 191 ++++++++++++++++++++++---------- scripts/remove | 112 ++++++++++++++----- scripts/restore | 167 ++++++++++++++++++++-------- scripts/upgrade | 266 ++++++++++++++++++++++++++++----------------- 7 files changed, 646 insertions(+), 251 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 8f76b58..82b1c02 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -14,12 +14,12 @@ PKG_DEPENDENCIES="python3 python3-dev python3-venv python3-pip libffi-dev libssl PY_REQUIRED_VERSION=3.9.2 # Create homeassistant user -mynh_user_create () { +mynh_system_user_create () { USER_GROUPS="" [ $(getent group dialout) ] && USER_GROUPS="${USER_GROUPS} dialout" [ $(getent group gpio) ] && USER_GROUPS="${USER_GROUPS} gpio" [ $(getent group i2c) ] && USER_GROUPS="${USER_GROUPS} i2c" - ynh_system_user_create --username="$app" --groups="$USER_GROUPS" + ynh_system_user_create --username="$app" --groups="$USER_GROUPS" --home_dir="$data_path" } diff --git a/scripts/backup b/scripts/backup index 8333b29..b16ecdd 100644 --- a/scripts/backup +++ b/scripts/backup @@ -3,13 +3,28 @@ # yunohost backup create -n "homeassistant-test" --apps homeassistant # yunohost backup delete homeassistant-test +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + ynh_abort_if_errors -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_print_info --message="Loading installation settings..." + app=$YNH_APP_INSTANCE_NAME + domain=$(ynh_app_setting_get --app="$app" --key=domain) port=$(ynh_app_setting_get --app="$app" --key=port) final_path=$(ynh_app_setting_get --app="$app" --key=final_path) @@ -19,18 +34,59 @@ path_url=$(ynh_app_setting_get --app="$app" --key=path_url) python=$(ynh_app_setting_get --app="$app" --key=python) db_name=$(ynh_app_setting_get --app=$app --key=db_name) -# backup source & conf files -ynh_print_info --message="Declaring files to be backed up..." +#================================================= +# 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="$final_path" -ynh_backup --src_path="$data_path" + +#================================================= +# BACKUP THE DATA DIR +#================================================= + +ynh_backup --src_path="$data_path" --is_big + +#================================================= +# BACKUP THE NGINX CONFIGURATION +#================================================= + ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" -ynh_backup --src_path="/etc/sudoers.d/$app" -ynh_backup --src_path="/etc/systemd/system/$app.service" + +#================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP LOGROTATE +#================================================= + ynh_backup --src_path="/etc/logrotate.d/$app" + +#================================================= +# BACKUP SYSTEMD +#================================================= + +ynh_backup --src_path="/etc/systemd/system/$app.service" + +#================================================= +# BACKUP VARIOUS FILES +#================================================= + +ynh_backup --src_path="/etc/sudoers.d/$app" ynh_backup --src_path="$(dirname "$log_file")" -# backup the MySQL database +#================================================= +# BACKUP THE MYSQL DATABASE +#================================================= ynh_print_info --message="Backing up the MySQL database..." ynh_mysql_dump_db --database="$db_name" > db.sql -ynh_print_info --message="Backup of $app completed" +#================================================= +# 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 index ed0fa06..0d4b22f 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -1,20 +1,35 @@ #!/bin/bash +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# retrieve arguments +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + app=$YNH_APP_INSTANCE_NAME + old_domain=$YNH_APP_OLD_DOMAIN new_domain=$YNH_APP_NEW_DOMAIN -# load settings +#================================================= +# LOAD SETTINGS +#================================================= ynh_script_progression --message="Loading installation settings..." --time --weight=1 -old_nginx_conf="/etc/nginx/conf.d/$old_domain.d/$app.conf" -new_nginx_conf="/etc/nginx/conf.d/$new_domain.d/$app.conf" -# manage script failure -ynh_script_progression --message="Backing up the app before changing its url (may take a while)..." --time --weight=1 +final_path=$(ynh_app_setting_get --app=$app --key=final_path) + +#================================================= +# BACKUP BEFORE CHANGE URL THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before changing its URL (may take a while)..." --time --weight=1 + ynh_backup_before_upgrade ynh_clean_setup () { ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf" @@ -22,24 +37,60 @@ ynh_clean_setup () { } ynh_abort_if_errors -# check which parts should be changed +#================================================= +# CHECK WHICH PARTS SHOULD BE CHANGED +#================================================= + change_domain=0 -if [ "$old_domain" != "$new_domain" ]; then +if [ "$old_domain" != "$new_domain" ] +then change_domain=1 fi -# Change the domain for nginx -if [ $change_domain -eq 1 ]; then - ynh_script_progression --message="Updating nginx web server configuration..." --time --weight=1 +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." --time --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..." --time --weight=1 + +nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf + +# Change the domain for NGINX +if [ $change_domain -eq 1 ] +then # Delete file checksum for the old conf file location - ynh_delete_file_checksum --file="$old_nginx_conf" - mv "$old_nginx_conf" "$new_nginx_conf" + ynh_delete_file_checksum --file="$nginx_conf_path" + mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf # Store file checksum for the new config file location - ynh_store_file_checksum --file="$new_nginx_conf" + ynh_store_file_checksum --file="/etc/nginx/conf.d/$new_domain.d/$app.conf" fi -# reload nginx -ynh_script_progression --message="Reloading nginx web server..." --time --weight=1 +#================================================= +# GENERIC FINALISATION +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --time --weight=1 + +ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --time --weight=1 + ynh_systemd_action --service_name=nginx --action=reload +#================================================= +# END OF SCRIPT +#================================================= + ynh_script_progression --message="Change of URL completed for $app" --time --last diff --git a/scripts/install b/scripts/install index bebd1d1..4d5e17c 100644 --- a/scripts/install +++ b/scripts/install @@ -1,116 +1,195 @@ #!/bin/bash +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + ynh_clean_setup () { - ynh_clean_check_starting + true } ynh_abort_if_errors -# retrieve arguments +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= + app=$YNH_APP_INSTANCE_NAME + domain=$YNH_APP_ARG_DOMAIN is_public=$YNH_APP_ARG_IS_PUBLIC -# definie useful vars +#================================================= +# DEFINE USEFULL VARS +#================================================= + final_path="/var/www/$app" data_path="/home/yunohost.app/$app" log_file="/var/log/$app/$app.log" path_url="/" -# check domain/path availability -ynh_script_progression --message="Validating installation parameters..." -[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder" +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= +ynh_script_progression --message="Validating installation parameters..." --time --weight=1 + +[ ! -d "$final_path" ] || ynh_die --message="There is already a directory: $final_path " ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url" -# find a free port & open it -ynh_script_progression --message="Looking for a free port and opening it..." -port=$(ynh_find_port 8123) -ynh_exec_fully_quiet yunohost firewall allow TCP "$port" - -# save app settings -ynh_script_progression --message="Storing installation settings..." +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_script_progression --message="Storing installation settings..." --time --weight=1 ynh_app_setting_set --app="$app" --key=domain --value="$domain" -ynh_app_setting_set --app="$app" --key=port --value="$port" ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" ynh_app_setting_set --app="$app" --key=data_path --value="$data_path" ynh_app_setting_set --app="$app" --key=log_file --value="$log_file" ynh_app_setting_set --app="$app" --key=path_url --value="$path_url" -# create a dedicated system user -ynh_script_progression --message="Creating dedicated user, rights and folders..." -mynh_user_create +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# FIND AND OPEN A PORT +#================================================= +ynh_script_progression --message="Finding an available port..." --time --weight=1 -# create a directory for the installation of Home Assistant -myynh_create_dir "$final_path" -chown -R $app: "$final_path" +port=$(ynh_find_port 8123) +ynh_app_setting_set --app="$app" --key=port --value="$port" +ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $port -# create a directory with its log file -myynh_create_dir "$(dirname "$log_file")" -touch "$log_file" +#================================================= +# INSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Installing dependencies..." --time --weight=1 -# create a directory for the datas of Home Assistant -myynh_create_dir "$data_path/.cache" -chown -R $app: "$data_path" - -# build (if needed) & install Pyhton -ynh_script_progression --message="Installing dependencies..." myynh_install_dependencies --python="$PY_REQUIRED_VERSION" -# create a MySQL database -ynh_script_progression --message="Creating a MySQL database..." +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Configuring system user..." --time --weight=1 + +mynh_system_user_create + +#================================================= +# CREATE A MYSQL DATABASE +#================================================= +ynh_script_progression --message="Creating a MySQL database..." --time --weight=1 + db_name=$(ynh_sanitize_dbid --db_name=$app) db_user=$db_name ynh_app_setting_set --app=$app --key=db_name --value=$db_name ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= +ynh_script_progression --message="Setting up source files..." --time --weight=1 + +# create a directory for the installation of Home Assistant +myynh_create_dir "$final_path" +chown -R $app: "$final_path" + +# create a directory for the datas of Home Assistant +myynh_create_dir "$data_path/.cache" +chown -R $app: "$data_path" + # installation in a virtual environment ynh_script_progression --message="Installing Home Assistant in a virtual environment..." ynh_exec_fully_quiet myynh_install_homeassistant -# set default configuration files and move all homeassistant_conf_files -ynh_script_progression --message="Configuring the installation..." -cp -r "../conf/homeassistant_conf_files/." "$data_path/" -ynh_add_config --template="../conf/homeassistant_conf_files/configuration.yaml" --destination="$data_path/configuration.yaml" +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Configuring NGINX web server..." --time --weight=1 + +ynh_add_nginx_config + +#================================================= +# SPECIFIC SETUP +#================================================= # grant sudo permissions to the user to manage his own systemd service myynh_create_dir "/etc/sudoers.d" ynh_add_config --template="../conf/sudoers" --destination="/etc/sudoers.d/$app" +#================================================= +# ADD A CONFIGURATION +#================================================= + +cp -r "../conf/homeassistant_conf_files/." "$data_path/" +ynh_add_config --template="../conf/homeassistant_conf_files/configuration.yaml" --destination="$data_path/configuration.yaml" + +#================================================= +# SET FILE OWNERSHIP / PERMISSIONS +#================================================= + +myynh_set_permissions + +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Configuring a systemd service..." --time --weight=1 + # setup up systemd service ynh_script_progression --message="Adding the dedicated service..." ynh_add_systemd_config +#================================================= +# GENERIC FINALIZATION +#================================================= +# SETUP LOGROTATE +#================================================= +ynh_script_progression --message="Configuring log rotation..." --time --weight=1 + +ynh_use_logrotate --logfile="$log_file" + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1 + # add service in admin panel -yunohost service add "$app" --log="$log_file" --description="Home Assistant server" --needs_exposed_ports=$port +yunohost service add "$app" --description="Home Assistant server" --log="$log_file" --needs_exposed_ports=$port -# set permissions -myynh_set_permissions +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --time --weight=1 -# start systemd service -ynh_script_progression --message="Starting the Home Assistant server..." -ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600 +# start systemd service with --verbose +ynh_systemd_action --service_name="$app" --action="start" --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600 -# remove --verbose from service +# remove --verbose from systemd service ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app.service" ynh_store_file_checksum --file="/etc/systemd/system/$app.service" systemctl daemon-reload -ynh_systemd_action --service_name="$app" --action=restart +ynh_systemd_action --service_name="$app" --action="restart" -# enable logrotate -ynh_use_logrotate --logfile="$log_file" +#================================================= +# SETUP SSOWAT +#================================================= +ynh_script_progression --message="Configuring permissions..." --time --weight=1 -# create a dedicated nginx config -ynh_script_progression --message="Configuring nginx web server..." -ynh_add_nginx_config - -# reload nginx -ynh_systemd_action --service_name=nginx --action=reload - -# unprotect app access if public (needed for Android app to work) -ynh_script_progression --message="Configuring permissions..." [ $is_public -eq 1 ] && ynh_permission_update --permission="main" --add="visitors" -ynh_script_progression --message="Installation of $app completed" --last +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --time --weight=1 + +ynh_systemd_action --service_name=nginx --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Installation of $app completed" --time --last diff --git a/scripts/remove b/scripts/remove index 0d240cc..c064505 100644 --- a/scripts/remove +++ b/scripts/remove @@ -2,11 +2,22 @@ # to test the functionnality : # yunohost app remove homeassistant --purge +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 + app=$YNH_APP_INSTANCE_NAME + domain=$(ynh_app_setting_get --app=$app --key=domain) port=$(ynh_app_setting_get --app=$app --key=port) final_path=$(ynh_app_setting_get --app="$app" --key=final_path) @@ -17,56 +28,103 @@ python=$(ynh_app_setting_get --app="$app" --key=python) db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_user=$db_name -# Remove a service from the admin panel, added by `yunohost service add` +#================================================= +# STANDARD REMOVE +#================================================= +# REMOVE SERVICE INTEGRATION IN YUNOHOST +#================================================= + if ynh_exec_warn_less yunohost service status "$app" >/dev/null ; then ynh_script_progression --message="Removing $app service integration..." yunohost service remove "$app" fi -# remove systemd service -ynh_script_progression --message="Stopping and removing the systemd service..." +#================================================= +# STOP AND REMOVE SERVICE +#================================================= +ynh_script_progression --message="Stopping and removing the systemd service..." --time --weight=1 + ynh_remove_systemd_config --service="$app" -# remove the app-specific logrotate config -ynh_script_progression --message="Removing logrotate configuration..." +#================================================= +# REMOVE LOGROTATE CONFIGURATION +#================================================= +ynh_script_progression --message="Removing logrotate configuration..." --time --weight=1 + ynh_remove_logrotate -# remove the MySQL database -ynh_script_progression --message="Removing the MySQL database..." +#================================================= +# REMOVE THE MYSQL DATABASE +#================================================= +ynh_script_progression --message="Removing the MySQL database..." --time --weight=1 + ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name -# remove metapackage and its dependencies -ynh_script_progression --message="Removing dependencies..." -ynh_remove_app_dependencies +#================================================= +# REMOVE APP MAIN DIR +#================================================= +ynh_script_progression --message="Removing app main directory..." --time --weight=1 -# remove the app directory securely -ynh_script_progression --message="Removing app main directory..." ynh_secure_remove --file="$final_path" -# remove a directory securely if --purge option is used -if [ "${YNH_APP_PURGE:-0}" -eq 1 ] ; then - ynh_script_progression --message="Removing app data directory..." - ynh_secure_remove --file="$data_path" +#================================================= +# REMOVE DATA DIR +#================================================= + +if [ "${YNH_APP_PURGE:-0}" -eq 1 ] +then + ynh_script_progression --message="Removing app data directory..." --time --weight=1 + ynh_secure_remove --file="$datadir" fi -# remove the dedicated nginx config -ynh_script_progression --message="Removing NGINX web server configuration..." +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Removing NGINX web server configuration..." --time --weight=1 + ynh_remove_nginx_config -# close port -if yunohost firewall list | grep -q "\- $port$" ; then - ynh_script_progression --message="Closing port $port..." +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." --time --weight=1 + +ynh_remove_app_dependencies + +#================================================= +# CLOSE A PORT +#================================================= + +if yunohost firewall list | grep -q "\- $port$" +then + ynh_script_progression --message="Closing port $port..." --time --weight=1 ynh_exec_warn_less yunohost firewall disallow TCP $port fi -ynh_script_progression --message="Removing various files..." +#================================================= +# SPECIFIC REMOVE +#================================================= +# REMOVE VARIOUS FILES +#================================================= +ynh_script_progression --message="Removing various files..." --time --weight=1 + # remove sudoers file ynh_secure_remove --file="/etc/sudoers.d/$app" + # Remove the log files ynh_secure_remove --file="$(dirname "$log_file")" -# delete a system user -ynh_script_progression --message="Removing the dedicated system user..." -ynh_system_user_delete --username="$app" +#================================================= +# GENERIC FINALIZATION +#================================================= +# REMOVE DEDICATED USER +#================================================= +ynh_script_progression --message="Removing the dedicated system user..." --time --weight=1 -ynh_script_progression --message="Removal of $app completed" --last +ynh_system_user_delete --username=$app + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Removal of $app completed" --time --last diff --git a/scripts/restore b/scripts/restore index 8f0f3aa..d68cf93 100644 --- a/scripts/restore +++ b/scripts/restore @@ -5,17 +5,31 @@ # yunohost backup restore "homeassistant-test" # yunohost backup delete "homeassistant-test" +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source ../settings/scripts/_common.sh source /usr/share/yunohost/helpers -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + ynh_clean_setup () { - ynh_clean_check_starting + true } ynh_abort_if_errors -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 + app=$YNH_APP_INSTANCE_NAME + domain=$(ynh_app_setting_get --app="$app" --key=domain) port=$(ynh_app_setting_get --app="$app" --key=port) final_path=$(ynh_app_setting_get --app="$app" --key=final_path) @@ -26,60 +40,118 @@ python=$(ynh_app_setting_get --app="$app" --key=python) db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_user=$db_name -# check domain/path availability -ynh_script_progression --message="Validating recovery parameters..." -[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder" +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= +ynh_script_progression --message="Validating restoration parameters..." --time --weight=1 -# restore port -ynh_script_progression --message="Restoring the port and opening it..." -ynh_exec_warn_less yunohost firewall allow TCP $port +# check app main dir availability +[ ! -d "$final_path" ] || ynh_die --message="There is already a directory: $final_path " -# restore dedicated system user -ynh_script_progression --message="Restoring dedicated user and rights folders..." -mynh_user_create -ynh_restore_file --origin_path="/etc/sudoers.d/$app" +# check port availability +ynh_port_available --port=$port || ynh_die --message="Port $port is needs to be available for this app" + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Restoring the NGINX configuration..." --time --weight=1 -# restore nginx -ynh_script_progression --message="Restoring nginx web server..." ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" -# restore source -ynh_script_progression --message="Restoring the app..." +#================================================= +# RECREATE THE DEDICATED USER +#================================================= +ynh_script_progression --message="Recreating the dedicated system user..." --time --weight=1 + +mynh_user_create + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= +ynh_script_progression --message="Restoring the app main directory..." --time --weight=1 + ynh_restore_file --origin_path="$final_path" -# restore data -ynh_script_progression --message="Restoring the data..." -ynh_restore_file --origin_path="$data_path" +#================================================= +# RESTORE THE DATA DIRECTORY +#================================================= +ynh_script_progression --message="Restoring the data directory..." --time --weight=1 -# restore log -ynh_script_progression --message="Restoring the log file..." -ynh_restore_file --origin_path="$(dirname "$log_file")" +ynh_restore_file --origin_path="$datadir" --not_mandatory + +mkdir -p $datadir + +#================================================= +# SPECIFIC RESTORATION +#================================================= +# REINSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Reinstalling dependencies..." --time --weight=1 -# add required packages -ynh_script_progression --message="Restoring the packages dependencies..." myynh_install_dependencies --python="$python" -# restore the MySQL database -ynh_script_progression --message="Restoring the MySQL database..." +#================================================= +# RESTORE THE MYSQL DATABASE +#================================================= +ynh_script_progression --message="Restoring the MySQL database..." --time --weight=1 + 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 -# restore the systemd service -ynh_script_progression --message="Restoring the dedicated service..." +#================================================= +# RESTORE VARIOUS FILES +#================================================= +ynh_script_progression --message="Restoring various files..." --time --weight=1 + +ynh_restore_file --origin_path="/etc/sudoers.d/$app" + +ynh_restore_file --origin_path="$(dirname "$log_file")" + +#================================================= +# SET FILE OWNERSHIP / PERMISSIONS +#================================================= + +myynh_set_permissions + +#================================================= +# RESTORE SYSTEMD +#================================================= +ynh_script_progression --message="Restoring the systemd configuration..." --time --weight=1 + ynh_restore_file --origin_path="/etc/systemd/system/$app.service" systemctl enable $app.service --quiet -# add service in admin panel -yunohost service add "$app" --log="$log_file" --description="Home Assistant server" --needs_exposed_ports=$port +#================================================= +# RESTORE THE LOGROTATE CONFIGURATION +#================================================= +ynh_script_progression --message="Restoring the logrotate configuration..." --time --weight=1 -# set permissions -myynh_set_permissions +ynh_restore_file --origin_path="/etc/logrotate.d/$app" -# restart the app -ynh_script_progression --message="Starting the Home Assistant server..." +#================================================= +# RESTORE THE PORT +#================================================= +ynh_script_progression --message="Restoring the port and opening it..." +ynh_exec_warn_less yunohost firewall allow TCP $port + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1 + +yunohost service add "$app" --description="Home Assistant server" --log="$log_file" --needs_exposed_ports=$port + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --time --weight=1 + +# start systemd service with --verbose sed --in-place "/ExecStart/s/$/ --verbose/" "/etc/systemd/system/$app.service" -ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=900 +ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600 # remove --verbose from service and restart ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app.service" @@ -87,20 +159,27 @@ ynh_store_file_checksum --file="/etc/systemd/system/$app.service" systemctl daemon-reload ynh_systemd_action --service_name="$app" --action=restart -# restore logrotate -ynh_script_progression --message="Restoring logrotate..." -ynh_restore_file --origin_path="/etc/logrotate.d/$app" +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= +ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..." --time --weight=1 -# reload nginx -ynh_script_progression --message="Reloading NGINX web server..." +ynh_systemd_action --service_name=php$phpversion-fpm --action=reload ynh_systemd_action --service_name=nginx --action=reload -ynh_script_progression --message="Restoration completed for $app" --last +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Restoration completed for $app" --time --last #for debug purpose -for ((i = 1 ; i <= 30 ; i++)); do +for ((i = 1 ; i <= 10 ; i++)); do ynh_print_warn --message= "\r$i) $(date)" - curl --silent --show-error --insecure --location --header "Host: $domain" --resolve "$domain":443:127.0.0.1 "https://localhost/" + ynh_local_curl + #curl --silent --show-error --insecure --location --header "Host: $domain" --resolve "$domain":443:127.0.0.1 "https://localhost/" echo -e -n "\r" sleep 10 done diff --git a/scripts/upgrade b/scripts/upgrade index 8334e44..5e76db2 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,139 +1,211 @@ #!/bin/bash +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# manage script failure -ynh_clean_setup () { - ynh_clean_check_starting -} -ynh_abort_if_errors +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." --time --weight=1 -# retrieve arguments app=$YNH_APP_INSTANCE_NAME + domain=$(ynh_app_setting_get --app="$app" --key=domain) port=$(ynh_app_setting_get --app="$app" --key=port) -# definie useful vars +#================================================= +# CHECK VERSION +#================================================= + +upgrade_type=$(ynh_check_app_version_changed) + +#================================================= +# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --time --weight=1 + +ynh_backup_before_upgrade +ynh_clean_setup () { + ynh_restore_upgradebackup +} +ynh_abort_if_errors + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." --time --weight=1 + +ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" + +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +ynh_script_progression --message="Ensuring downward compatibility..." --time --weight=1 + +# changes introduce in if [ -z $(ynh_app_setting_get --app="$app" --key=final_path) ]; then - final_path="/var/www/$app" - data_path="/home/yunohost.app/$app" - log_file="/var/log/$app/$app.log" - path_url="/" - ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" - ynh_app_setting_set --app="$app" --key=data_path --value="$data_path" - ynh_app_setting_set --app="$app" --key=log_file --value="$log_file" - ynh_app_setting_set --app="$app" --key=path_url --value="$path_url" + final_path="/var/www/$app" + data_path="/home/yunohost.app/$app" + log_file="/var/log/$app/$app.log" + path_url="/" + ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" + ynh_app_setting_set --app="$app" --key=data_path --value="$data_path" + ynh_app_setting_set --app="$app" --key=log_file --value="$log_file" + ynh_app_setting_set --app="$app" --key=path_url --value="$path_url" else - final_path=$(ynh_app_setting_get --app="$app" --key=final_path) - data_path=$(ynh_app_setting_get --app="$app" --key=data_path) - log_file=$(ynh_app_setting_get --app="$app" --key=log_file) - path_url=$(ynh_app_setting_get --app="$app" --key=path_url) - python=$(ynh_app_setting_get --app="$app" --key=python) + final_path=$(ynh_app_setting_get --app="$app" --key=final_path) + data_path=$(ynh_app_setting_get --app="$app" --key=data_path) + log_file=$(ynh_app_setting_get --app="$app" --key=log_file) + path_url=$(ynh_app_setting_get --app="$app" --key=path_url) + python=$(ynh_app_setting_get --app="$app" --key=python) fi -# use prior backup and restore on error only if backup feature exists on installed instance -ynh_script_progression --message="Creating backup in case of failure..." -if [ -f "/etc/yunohost/apps/$app/scripts/backup" ] ; then - ynh_backup_before_upgrade # Backup the current version of the app - ynh_clean_setup () { - ynh_restore_upgradebackup - ynh_clean_check_starting - } -fi - -# build (if needed) & install Pyhton -ynh_script_progression --message="Installing dependencies..." -myynh_install_dependencies --python="$PY_REQUIRED_VERSION" - -# stop systemd service -ynh_script_progression --message="Stoping service..." -ynh_systemd_action --service_name="$app" --action=stop --line_match="Stopped Home Assistant" --log_path="$log_file" --timeout=300 - -# migrate to new app architecture -ynh_script_progression --message="If needed, migrating to new app architecture..." +# changes introduced in 2021.11.5~ynh1 if [ -f "/etc/systemd/system/$app@$app.service" ]; then - # remove old systemd service - if ynh_exec_warn_less yunohost service status "$app@$app" >/dev/null ; then - yunohost service remove "$app@$app" - fi - ynh_remove_systemd_config --service="$app@$app" + # remove old systemd service + if ynh_exec_warn_less yunohost service status "$app@$app" >/dev/null ; then + yunohost service remove "$app@$app" + fi + ynh_remove_systemd_config --service="$app@$app" fi if [ ! -d "$final_path" ]; then - # move $final_path to new directory - mv "/opt/yunohost/$app" "$final_path" - chown -R $app: "$final_path" + # move $final_path to new directory + mv "/opt/yunohost/$app" "$final_path" + chown -R $app: "$final_path" fi if [ ! -d "$data_path" ]; then - # move $data_path to new directory - mv "/""home""/$app" "$data_path" - find "$data_path/.$app" -maxdepth 1 -mindepth 1 -exec mv {} "$data_path" \; - rmdir "$data_path/.$app" - ynh_replace_string --match_string="/home/homeassistant/.homeassistant" --replace_string="$data_path" --target_file="$data_path/configuration.yaml" - chown -R $app: "$data_path" + # move $data_path to new directory + mv "/""home""/$app" "$data_path" + find "$data_path/.$app" -maxdepth 1 -mindepth 1 -exec mv {} "$data_path" \; + rmdir "$data_path/.$app" + ynh_replace_string --match_string="/home/homeassistant/.homeassistant" --replace_string="$data_path" --target_file="$data_path/configuration.yaml" + chown -R $app: "$data_path" fi if [ ! -f "$log_file" ]; then - # create a directory with its log file - myynh_create_dir "$(dirname "$log_file")" - touch "$log_file" + # create a directory with its log file + myynh_create_dir "$(dirname "$log_file")" + touch "$log_file" fi + +# changes introduced in 2021.12.8~ynh1 if [ -z $(ynh_app_setting_get --app="$app" --key=db_name) ]; then - # create a MySQL database - db_name=$(ynh_sanitize_dbid --db_name=$app) - db_user=$db_name - ynh_app_setting_set --app=$app --key=db_name --value=$db_name - ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name - db_pwd=$(ynh_app_setting_get --app="$app" --key=mysqlpwd) - if [ -z $(sed -n "/recorder:/=" configuration.yaml) ]; then - sed -i "$ a recorder:" "$data_path/configuration.yaml" - sed -i "$ a \ db_url: mysql://$db_user:$db_pwd@127.0.0.1/$db_name?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4" "$data_path/configuration.yaml" - else - sed -i "/recorder:/a \ db_url: mysql://$db_user:$db_pwd@127.0.0.1/$db_name?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4" "$data_path/configuration.yaml" - fi + # create a MySQL database + db_name=$(ynh_sanitize_dbid --db_name=$app) + db_user=$db_name + ynh_app_setting_set --app=$app --key=db_name --value=$db_name + ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name + db_pwd=$(ynh_app_setting_get --app="$app" --key=mysqlpwd) + if [ -z $(sed -n "/recorder:/=" configuration.yaml) ]; then + sed -i "$ a recorder:" "$data_path/configuration.yaml" + sed -i "$ a \ db_url: mysql://$db_user:$db_pwd@127.0.0.1/$db_name?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4" "$data_path/configuration.yaml" + else + sed -i "/recorder:/a \ db_url: mysql://$db_user:$db_pwd@127.0.0.1/$db_name?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4" "$data_path/configuration.yaml" + fi fi -# installation in a virtual environment -ynh_script_progression --message="Installing Home Assistant in a virtual environment..." -ynh_exec_fully_quiet myynh_install_homeassistant +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1 + +mynh_system_user_create + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= + +if [ "$upgrade_type" == "UPGRADE_APP" ] +then + ynh_script_progression --message="Installing Home Assistant in a virtual environment..." --time --weight=1 + + ynh_exec_fully_quiet myynh_install_homeassistant +fi + +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading NGINX web server configuration..." --time --weight=1 + +ynh_add_nginx_config + +#================================================= +# UPGRADE DEPENDENCIES +#================================================= +ynh_script_progression --message="Upgrading dependencies..." --time --weight=1 + +myynh_install_dependencies --python="$PY_REQUIRED_VERSION" + +#================================================= +# UPDATE A CONFIG FILE +#================================================= +ynh_script_progression --message="Updating a configuration file..." --time --weight=1 -# update script in bin -ynh_script_progression --message="Updating YunoHost script used by homeassitant..." cp -r "../conf/homeassistant_conf_files/bin/." "$data_path/bin/" -# setup up systemd service -ynh_script_progression --message="Adding the dedicated service..." -ynh_add_systemd_config - -# grant sudo permissions to the user to manage his own systemd service -ynh_script_progression --message="Creating dedicated user, rights and folders..." -mynh_user_create ynh_add_config --template="../conf/sudoers" --destination="/etc/sudoers.d/$app" -# add service in admin panel -yunohost service add "$app" --log="$log_file" --description="Home Assistant server" --needs_exposed_ports=$port +#================================================= +# SET FILE OWNERSHIP / PERMISSIONS +#================================================= -# set permissions myynh_set_permissions -# start systemd service -ynh_script_progression --message="Starting the Home Assistant server..." -systemctl daemon-reload -ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600 +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Upgrading systemd configuration..." --time --weight=1 + +# Create a dedicated systemd config +ynh_add_systemd_config + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SETUP LOGROTATE +#================================================= +ynh_script_progression --message="Upgrading logrotate configuration..." --time --weight=1 + +# Use logrotate to manage app-specific logfile(s) +ynh_use_logrotate --non-append + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1 + +yunohost service add "$app" --log="$log_file" --description="Home Assistant server" --needs_exposed_ports=$port + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." --time --weight=1 + +# start systemd service with --verbose +ynh_systemd_action --service_name="$app" --action="start" --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600 # remove --verbose from service ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app.service" ynh_store_file_checksum --file="/etc/systemd/system/$app.service" systemctl daemon-reload -ynh_systemd_action --service_name="$app" --action=restart +ynh_systemd_action --service_name="$app" --action="restart" -# enable logrotate -ynh_use_logrotate --logfile="$log_file" --nonappend +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." --time --weight=1 -# create a dedicated nginx config -ynh_script_progression --message="Configuring nginx web server..." -ynh_add_nginx_config - -# reload nginx ynh_systemd_action --service_name=nginx --action=reload -ynh_script_progression --message="Installation of $app completed" --last +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Upgrade of $app completed" --time --last