From a0dbf31b34c30859dafe623474283ec1ffb35065 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 15 May 2021 20:26:30 +0200 Subject: [PATCH] apply example_ynh --- check_process | 4 +- manifest.json | 92 ++++++++++------------ scripts/_common.sh | 43 +++-------- scripts/backup | 76 ++++++++++++++---- scripts/change_url | 113 ++++++++++++++++++++++----- scripts/install | 189 ++++++++++++++++++++++++++++++++++----------- scripts/remove | 119 ++++++++++++++++++++++------ scripts/restore | 157 +++++++++++++++++++++++++++---------- scripts/upgrade | 155 +++++++++++++++++++++++++++---------- 9 files changed, 679 insertions(+), 269 deletions(-) diff --git a/check_process b/check_process index 5e5eb9c..4a6f000 100644 --- a/check_process +++ b/check_process @@ -1,7 +1,7 @@ ;; Test complet ; Manifest - domain="domain.tld" (DOMAIN) - is_public=1 (PUBLIC|public=1|private=0) + domain="domain.tld" + is_public=1 ; Actions is_public=1|0 ; Checks diff --git a/manifest.json b/manifest.json index 569baad..c311356 100644 --- a/manifest.json +++ b/manifest.json @@ -1,53 +1,45 @@ { - "name": "Home Assistant", - "id": "homeassistant", - "packaging_format": 1, - "description": { - "en": "Home automation platform", - "fr": "Plateforme domotique" - }, - "version": "2021.4.6~ynh1", - "url": "https://github.com/home-assistant/home-assistant", - "license": "Apache-2.0", - "maintainer": { - "name": "ewilly", - "email": "ewilly@ewilly.fr" - }, - "requirements": { + "name": "Home Assistant", + "id": "homeassistant", + "packaging_format": 1, + "description": { + "en": "Home automation platform", + "fr": "Plateforme domotique" + }, + "version": "2021.4.6~ynh1", + "url": "https://github.com/home-assistant/home-assistant", + "license": "Apache-2.0", + "maintainer": { + "name": "ewilly", + "email": "ewilly@ewilly.fr" + }, + "requirements": { "yunohost": ">= 4.0.0" - }, - "multi_instance": false, - "services": [ - "nginx" - ], - "arguments": { - "install": [ - { - "name": "domain", - "type": "domain", - "ask": { - "en": "Choose a domain for Home Assistant", - "fr": "Choisissez un domaine pour Home Assistant" - }, - "help": { - "en": "Installation in a path is not possible", - "fr": "L'installation sous un chemin de domaine n'est pas possible" - }, - "example": "domain.org or homeassistant.domain.org" - }, - { - "name": "is_public", - "type": "boolean", - "ask": { - "en": "Should this application be public?", - "fr": "Est-ce que cette application doit ĂȘtre visible publiquement ?" - }, - "help": { - "en": "If not public, Smartphone app will not work", - "fr": "Dans le cas contraire, l'application sur Smartphone ne fonctionnera pas" - }, - "default": true - } - ] - } + }, + "multi_instance": false, + "services": [ + "nginx" + ], + "arguments": { + "install": [ + { + "name": "domain", + "type": "domain", + "help": { + "en": "Installation in a path is not possible", + "fr": "L'installation sous un chemin de domaine n'est pas possible" + }, + "example": "domain.org or homeassistant.domain.org" + }, + { + "name": "is_public", + "type": "boolean", + "help": { + "en": "If not public, Smartphone app will not work", + "fr": "Dans le cas contraire, l'application sur Smartphone ne fonctionnera pas" + }, + "default": true + } + ] + } } diff --git a/scripts/_common.sh b/scripts/_common.sh index 1b13c58..4312d61 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,29 +1,22 @@ -# -# Common variables & functions -# +#!/bin/bash + +#================================================= +# COMMON VARIABLES +#================================================= + +# Package dependencies +pkg_dependencies="python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5" # Release to install VERSION=2021.4.6 -# Package dependencies -PKG_DEPENDENCIES="python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5" - # Requirements (Major.Minor.Patch) # PY_VERSION=$(curl -s "https://www.python.org/ftp/python/" | grep ">3.8" | tail -n1 | cut -d '/' -f 2 | cut -d '>' -f 2) PY_REQUIRED_VERSION=3.8.7 -# Execute a command as another user -# usage: exec_as USER COMMAND [ARG ...] -exec_as() { - local USER=$1 - shift 1 - - if [[ $USER = $(whoami) ]]; then - eval "$@" - else - sudo -u "$USER" "$@" - fi -} +#================================================= +# PERSONAL HELPERS +#================================================= # Compare version in arguments myynh_version_compare () { @@ -126,7 +119,7 @@ myynh_install_dependencies () { # Install/Upgrade Homeassistant in virtual environement myynh_install_homeassistant () { - exec_as $app -H -s /bin/bash -c " \ + ynh_exec_as $app -H -s /bin/bash -c " \ echo 'create the virtual environment' \ && $MY_PYTHON -m venv "$final_path" \ && echo 'activate the virtual environment' \ @@ -139,15 +132,3 @@ myynh_install_homeassistant () { && pip install --upgrade $app==$VERSION \ " } - -# Check if directory/file already exists (path in argument) -myynh_check_path () { - [ -z "$1" ] && ynh_die "No argument supplied" - [ ! -e "$1" ] || ynh_die "$1 already exists" -} - -# Create directory only if not already exists (path in argument) -myynh_create_dir () { - [ -z "$1" ] && ynh_die "No argument supplied" - [ -d "$1" ] || mkdir -p "$1" -} diff --git a/scripts/backup b/scripts/backup index b5a73b4..8177db1 100644 --- a/scripts/backup +++ b/scripts/backup @@ -1,19 +1,35 @@ #!/bin/bash -# to test the functionnality : -# yunohost backup create -n "homeassistant-test" --apps homeassistant -# yunohost backup delete homeassistant-test +#================================================= +# 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 -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +ynh_clean_setup () { + true +} +# Exit if an error occurs during the execution of the script ynh_abort_if_errors -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_print_info --message="Loading installation settings..." + app=$YNH_APP_INSTANCE_NAME + +final_path=$(ynh_app_setting_get --app=$app --key=final_path) domain=$(ynh_app_setting_get --app="$app" --key=domain) -# definie useful vars -final_path="/opt/yunohost/$app" home_path="/home/$app" ynh_print_info --message="Storing the python version in settings..." @@ -21,13 +37,47 @@ ynh_print_info --message="Storing the python version in settings..." python=$(cat "$final_path/bin/python3" --version | cut -d ' ' -f 2) ynh_app_setting_set --app="$app" --key=python --value="$python" -# 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="$home_path" + +#================================================= +# 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@$app.service" + +#================================================= +# SPECIFIC BACKUP +#================================================= +# BACKUP LOGROTATE +#================================================= + ynh_backup --src_path="/etc/logrotate.d/$app" -ynh_print_info --message="Backup of $app completed" +#================================================= +# BACKUP SYSTEMD +#================================================= + +ynh_backup --src_path="/etc/systemd/system/$app@$app.service" + +#================================================= +# BACKUP VARIOUS FILES +#================================================= + +ynh_backup --src_path="$home_path" --is_big + +ynh_backup --src_path="/etc/sudoers.d/$app" + +#================================================= +# 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..ecb87ee 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -1,45 +1,116 @@ #!/bin/bash +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + source _common.sh source /usr/share/yunohost/helpers -# retrieve arguments -app=$YNH_APP_INSTANCE_NAME +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + old_domain=$YNH_APP_OLD_DOMAIN new_domain=$YNH_APP_NEW_DOMAIN -# 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" +app=$YNH_APP_INSTANCE_NAME -# manage script failure -ynh_script_progression --message="Backing up the app before changing its url (may take a while)..." --time --weight=1 +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --message="Loading installation settings..." + +# Needed for helper "ynh_add_nginx_config" +final_path=$(ynh_app_setting_get --app=$app --key=final_path) + +# Add settings here as needed by your application +#db_name=$(ynh_app_setting_get --app=$app --key=db_name) +#db_user=$db_name +#db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) + +#================================================= +# BACKUP BEFORE CHANGE URL THEN ACTIVE TRAP +#================================================= +ynh_script_progression --message="Backing up the app before changing its URL (may take a while)..." + +# Backup the current version of the app ynh_backup_before_upgrade ynh_clean_setup () { + # Remove the new domain config file, the remove script won't do it as it doesn't know yet its location. ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf" + + # Restore it if the upgrade fails ynh_restore_upgradebackup } +# Exit if an error occurs during the execution of the script 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 - # Delete file checksum for the old conf file location - ynh_delete_file_checksum --file="$old_nginx_conf" - mv "$old_nginx_conf" "$new_nginx_conf" - # Store file checksum for the new config file location - ynh_store_file_checksum --file="$new_nginx_conf" +change_path=0 +if [ "$old_path" != "$new_path" ] +then + change_path=1 fi -# reload nginx -ynh_script_progression --message="Reloading nginx web server..." --time --weight=1 +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." + +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..." + +nginx_conf_path=/etc/nginx/conf.d/$old_domain.d/$app.conf + +# Change the path in the NGINX config file +if [ $change_path -eq 1 ] +then + # Make a backup of the original NGINX config file if modified + ynh_backup_if_checksum_is_different --file="$nginx_conf_path" + # Set global variables for NGINX helper + domain="$old_domain" + path_url="$new_path" + # Create a dedicated NGINX config + ynh_add_nginx_config +fi + +# 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="$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="/etc/nginx/conf.d/$new_domain.d/$app.conf" +fi + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." + ynh_systemd_action --service_name=nginx --action=reload -ynh_script_progression --message="Change of URL completed for $app" --time --last +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Change of URL completed for $app" --last diff --git a/scripts/install b/scripts/install index a369a79..4b6d41f 100644 --- a/scripts/install +++ b/scripts/install @@ -1,51 +1,110 @@ #!/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 } +# Exit if an error occurs during the execution of the script ynh_abort_if_errors -# retrieve arguments -app=$YNH_APP_INSTANCE_NAME +#================================================= +# RETRIEVE ARGUMENTS FROM THE MANIFEST +#================================================= + domain=$YNH_APP_ARG_DOMAIN +path_url="/" is_public=$YNH_APP_ARG_IS_PUBLIC -# definie useful vars -final_path="/opt/yunohost/$app" +app=$YNH_APP_INSTANCE_NAME + +#================================================= +# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS +#================================================= +ynh_script_progression --message="Validating installation parameters..." + +final_path=/opt/yunohost/$app +test ! -e "$final_path" || ynh_die --message="This path already contains a folder" + +# Register (book) web path +ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url + +#================================================= +# STORE SETTINGS FROM MANIFEST +#================================================= +ynh_script_progression --message="Storing installation settings..." + +ynh_app_setting_set --app=$app --key=domain --value=$domain + +#================================================= +# STANDARD MODIFICATIONS +#================================================= +# FIND AND OPEN A PORT +#================================================= +ynh_script_progression --message="Finding an available port..." + +# Find an available port +port=$(ynh_find_port --port=8123) +ynh_app_setting_set --app=$app --key=port --value=$port + +# Open the port +# ynh_script_progression --message="Configuring firewall..." +ynh_exec_warn_less yunohost firewall allow TCP $port + +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Configuring system user..." + +# Create a system user +ynh_system_user_create --username=$app --home_dir=$final_path + +## grant sudo permissions to the user to manage his own systemd service +mkdir -p "/etc/sudoers.d" +cp "../conf/sudoers" "/etc/sudoers.d/$app" + +#================================================= +# DOWNLOAD, CHECK AND UNPACK SOURCE +#================================================= +ynh_script_progression --message="Setting up source files..." + +ynh_app_setting_set --app=$app --key=final_path --value=$final_path +mkdir -p "$final_path" + +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R $app:www-data "$final_path" + +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Configuring NGINX web server..." + +# Create a dedicated NGINX config +ynh_add_nginx_config + +#================================================= +# SPECIFIC SETUP +#================================================= +# ... +#================================================= + +## create a directory for the datas of Home Assistant home_path="/home/$app" data_path="/home/$app/.$app" -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" -ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url" - -# save app settings -ynh_script_progression --message="Storing installation settings..." -ynh_app_setting_set --app="$app" --key=domain --value="$domain" - -# find a free port & open it -ynh_script_progression --message="Looking for a free port and opening it..." -port=$(ynh_find_port 8123) -ynh_app_setting_set --app="$app" --key=port --value="$port" -ynh_exec_fully_quiet yunohost firewall allow TCP "$port" - -# create a dedicated system user -ynh_script_progression --message="Creating dedicated user, rights and folders..." -ynh_system_user_create --username="$app" -## grant sudo permissions to the user to manage his own systemd service -myynh_create_dir "/etc/sudoers.d" -cp "../conf/sudoers" "/etc/sudoers.d/$app" -## create a directory for the installation of Home Assistant -myynh_create_dir "$final_path" -chown $app: "$final_path" -## create a directory for the datas of Home Assistant -myynh_create_dir "$data_path" +mkdir -p "$data_path" chown -R $app: "$home_path" # build (if needed) & install Pyhton @@ -62,32 +121,68 @@ cp -r "../conf/homeassistant_conf_files/." "$data_path/" chown -R $app: "$data_path" chmod -R +x "$data_path/bin/" -# setup up systemd service -ynh_script_progression --message="Adding the dedicated service..." +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Configuring a systemd service..." + +# Create a dedicated systemd config ynh_add_systemd_config --service="$app@$app" -## add service in admin panel + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SETUP LOGROTATE +#================================================= +ynh_script_progression --message="Configuring log rotation..." + +# Use logrotate to manage application logfile(s) +ynh_use_logrotate --logfile="$data_path/home-assistant.log" + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port -# start systemd service -ynh_script_progression --message="Starting the Home Assistant server..." -ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600 +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." + +# Start a systemd service +ynh_systemd_action --service_name="$app@$app" --action="start" --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600 + # remove --verbose from service ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app@$app.service" ynh_store_file_checksum --file="/etc/systemd/system/$app@$app.service" systemctl daemon-reload ynh_systemd_action --service_name="$app@$app" --action=restart -# enable logrotate -ynh_use_logrotate --logfile="$data_path/home-assistant.log" -# create a dedicated nginx config -ynh_script_progression --message="Configuring nginx web server..." -ynh_add_nginx_config -## reload nginx +#================================================= +# SETUP SSOWAT +#================================================= +ynh_script_progression --message="Configuring permissions..." + +# Make app public if necessary +if [ $is_public -eq 1 ] +then + # Everyone can access the app. + # The "main" permission is automatically created before the install script. + ynh_permission_update --permission="main" --add="visitors" +fi + +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." + 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" +#================================================= +# END OF SCRIPT +#================================================= ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove index 817e6b4..66d68a2 100644 --- a/scripts/remove +++ b/scripts/remove @@ -1,49 +1,120 @@ #!/bin/bash -# to test the functionnality : -# yunohost app remove homeassistant + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= source _common.sh source /usr/share/yunohost/helpers -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --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) # definie useful vars -final_path="/opt/yunohost/$app" home_path="/home/$app" -# remove metapackage and its dependencies -ynh_remove_app_dependencies +#================================================= +# STANDARD REMOVE +#================================================= +# REMOVE SERVICE INTEGRATION IN YUNOHOST +#================================================= -# remove the app directory securely -ynh_secure_remove --file="$final_path" - -# remove the dedicated nginx config -ynh_remove_nginx_config - -# remove a directory securely -ynh_secure_remove --file="$home_path" - -# Remove a service from the admin panel, added by `yunohost service add` -if yunohost service status "$app@$app" >/dev/null 2>&1 ; then - yunohost service remove "$app@$app" +# Remove the service from the list of services known by YunoHost (added from `yunohost service add`) +if ynh_exec_warn_less yunohost service status $app >/dev/null +then + ynh_script_progression --message="Removing $app service integration..." + yunohost service remove "$app@$app" fi -# remove systemd service +#================================================= +# STOP AND REMOVE SERVICE +#================================================= +ynh_script_progression --message="Stopping and removing the systemd service..." + +# Remove the dedicated systemd config ynh_systemd_action --service_name="$app@$app" --action=stop ynh_systemd_action --service_name="$app@$app" --action=disable ynh_secure_remove --file="/etc/systemd/system/$app@$app.service" -# remove logrotate config +#================================================= +# REMOVE DEPENDENCIES +#================================================= +ynh_script_progression --message="Removing dependencies..." + +# Remove metapackage and its dependencies +ynh_remove_app_dependencies + +#================================================= +# REMOVE APP MAIN DIR +#================================================= +ynh_script_progression --message="Removing app main directory..." + +# Remove the app directory securely +ynh_secure_remove --file="$final_path" + +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Removing NGINX web server configuration..." + +# Remove the dedicated NGINX config +ynh_remove_nginx_config + +#================================================= +# REMOVE LOGROTATE CONFIGURATION +#================================================= +ynh_script_progression --message="Removing logrotate configuration..." + +# Remove the app-specific logrotate config ynh_remove_logrotate -# close port -ynh_exec_fully_quiet yunohost firewall disallow TCP $port +#================================================= +# CLOSE A PORT +#================================================= -# delete a system user -ynh_system_user_delete --username="$app" +if yunohost firewall list | grep -q "\- $port$" +then + ynh_script_progression --message="Closing port $port..." + ynh_exec_warn_less yunohost firewall disallow TCP $port +fi + +#================================================= +# SPECIFIC REMOVE +#================================================= +# REMOVE VARIOUS FILES +#================================================= +ynh_script_progression --message="Removing various files..." + +# Remove a directory securely +ynh_secure_remove --file="$home_path" + + + +#================================================= +# GENERIC FINALIZATION +#================================================= +# REMOVE DEDICATED USER +#================================================= +ynh_script_progression --message="Removing the dedicated system user..." + +# Delete a system user +ynh_system_user_delete --username=$app # remove sudoers file ynh_secure_remove --file="/etc/sudoers.d/$app" + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Removal of $app completed" diff --git a/scripts/restore b/scripts/restore index cb7a2ea..b735501 100644 --- a/scripts/restore +++ b/scripts/restore @@ -1,23 +1,35 @@ #!/bin/bash -# to test the functionnality : -# yunohost backup create -n "homeassistant-test" --apps homeassistant -# yunohost app remove homeassistant -# yunohost backup restore "homeassistant-test" +#================================================= +# 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 -# manage script failure +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + ynh_clean_setup () { ynh_clean_check_starting } +# Exit if an error occurs during the execution of the script ynh_abort_if_errors -# retrieve arguments +#================================================= +# LOAD SETTINGS +#================================================= +ynh_script_progression --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) -python=$(ynh_app_setting_get --app="$app" --key=python) + +domain=$(ynh_app_setting_get --app=$app --key=domain) +port=$(ynh_app_setting_get --app=$app --key=port) +python=$(ynh_app_setting_get --app=$app --key=python) # definie useful vars final_path="/opt/yunohost/$app" @@ -25,55 +37,122 @@ home_path="/home/$app" data_path="/home/$app/.$app" path_url="/" -ynh_script_progression --message="Validating recovery parameters..." -# check domain/path availability -[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder" -ynh_webpath_available --domain="$domain" --path_url="$path_url" || ynh_die "$domain/$path_url is not available, please use an other domain." +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= +ynh_script_progression --message="Validating restoration parameters..." + +ynh_webpath_available --domain=$domain --path_url=$path_url \ + || ynh_die --message="Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die --message="There is already a directory: $final_path " + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Restoring the NGINX configuration..." + +ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# RECREATE THE DEDICATED USER +#================================================= +ynh_script_progression --message="Recreating the dedicated system user..." + +# Create the dedicated user (if not existing) +ynh_system_user_create --username=$app --home_dir=$final_path + +ynh_restore_file --origin_path="/etc/sudoers.d/$app" + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= +ynh_script_progression --message="Restoring the app main directory..." + +ynh_restore_file --origin_path="$final_path" + +chmod 750 "$final_path" +chmod -R o-rwx "$final_path" +chown -R $app:www-data "$final_path" + +#================================================= +# SPECIFIC RESTORATION +#================================================= +# REINSTALL DEPENDENCIES +#================================================= +ynh_script_progression --message="Reinstalling dependencies..." + +# Define and install dependencies +# add required packages +myynh_install_dependencies --python="$python" + +#================================================= +# RESTORE VARIOUS FILES +#================================================= +ynh_script_progression --message="Restoring the data..." + +# restore data +ynh_restore_file --origin_path="$home_path" +chown -R $app: "$home_path" +chmod -R +x "$home_path/.homeassistant/bin" ynh_script_progression --message="Restoring the port and opening it..." # restore port [ $port -eq $(ynh_find_port $port) ] || ynh_die "$port is not available, please use an other port" ynh_exec_fully_quiet yunohost firewall allow TCP $port -ynh_script_progression --message="Restoring dedicated user, rights and folders..." -# restore dedicated system user -ynh_system_user_create --username="$app" -ynh_restore_file --origin_path="/etc/sudoers.d/$app" -# restore source -ynh_restore_file --origin_path="$final_path" -chown -R $app: "$final_path" +#================================================= +# RESTORE SYSTEMD +#================================================= +ynh_script_progression --message="Restoring the systemd configuration..." -ynh_script_progression --message="Restoring the data..." -# restore data -ynh_restore_file --origin_path="$home_path" -chown -R $app: "$home_path" -chmod -R +x "$home_path/.homeassistant/bin" - -ynh_script_progression --message="Restoring the packages dependencies..." -# add required packages -myynh_install_dependencies --python="$python" - -ynh_script_progression --message="Restoring the dedicated service..." ynh_restore_file --origin_path="/etc/systemd/system/$app@$app.service" -# add service in admin panel -yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port ynh_script_progression --message="Starting the Home Assistant server..." # add --verbose to service sed --in-place "/ExecStart/s/$/ --verbose/" "/etc/systemd/system/$app@$app.service" + # start ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600 + # remove --verbose from service and restart ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app@$app.service" systemctl daemon-reload + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + +yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port + +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." + ynh_systemd_action --service_name="$app@$app" --action=restart --line_match="Started Home Assistant" --log_path="systemd" --timeout=3600 -ynh_script_progression --message="Restoring nginx web server..." -ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" -# restore logrotate +#================================================= +# RESTORE THE LOGROTATE CONFIGURATION +#================================================= +ynh_script_progression --message="Restoring the logrotate configuration..." + ynh_restore_file --origin_path="/etc/logrotate.d/$app" -# reload nginx -ynh_systemd_action --service_name=nginx --action=reload +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND PHP-FPM +#================================================= +ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..." -ynh_script_progression --message="Recovery of $app completed" --last +ynh_systemd_action --service_name=php$phpversion-fpm --action=reload + +#================================================= +# END OF SCRIPT +#================================================= + +ynh_script_progression --message="Restoration completed for $app" --last diff --git a/scripts/upgrade b/scripts/upgrade index 97eee49..f4ed918 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -1,53 +1,100 @@ #!/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..." -# 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) -# Cleaning legacy permissions -ynh_script_progression --message="Cleaning legacy permissions..." -is_public=$(ynh_app_setting_get --app="$app" --key=is_public) -if [ -n "$is_public" ]; then - ynh_app_setting_delete --app="$app" --key=is_public - ynh_app_setting_delete --app="$app" --key=unprotected_uris -fi +domain=$(ynh_app_setting_get --app=$app --key=domain) +port=$(ynh_app_setting_get --app=$app --key=port) # definie useful vars final_path="/opt/yunohost/$app" data_path="/home/$app/.$app" -# 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 - } +#================================================= +# CHECK VERSION +#================================================= +ynh_script_progression --message="Checking 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)..." + +# Backup the current version of the app +ynh_backup_before_upgrade +ynh_clean_setup () { + # Restore it if the upgrade fails + ynh_restore_upgradebackup + ynh_clean_check_starting +} +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# STANDARD UPGRADE STEPS +#================================================= +# STOP SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Stopping a systemd service..." + +ynh_systemd_action --service_name="$app@$app" --action=stop + +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +ynh_script_progression --message="Ensuring downward compatibility..." + +# Cleaning legacy permissions +ynh_script_progression --message="Cleaning legacy permissions..." +is_public=$(ynh_app_setting_get --app="$app" --key=is_public) +if [ -n "$is_public" ]; then + ynh_app_setting_delete --app=$app --key=is_public + ynh_app_setting_delete --app=$app --key=unprotected_uris fi -# grant sudo permissions to the user to manage his own systemd service -ynh_script_progression --message="Creating dedicated user, rights and folders..." -myynh_create_dir "/etc/sudoers.d" +#================================================= +# CREATE DEDICATED USER +#================================================= +ynh_script_progression --message="Making sure dedicated system user exists..." + +# Create a dedicated user (if not existing) +ynh_system_user_create --username=$app --home_dir=$final_path + +mkdir -p "/etc/sudoers.d" cp "../conf/sudoers" "/etc/sudoers.d/$app" +#================================================= +# NGINX CONFIGURATION +#================================================= +ynh_script_progression --message="Upgrading NGINX web server configuration..." + +# Create a dedicated NGINX config +ynh_add_nginx_config + +#================================================= +# SPECIFIC UPGRADE +#================================================= +# ... +#================================================= + # build (if needed) & install Pyhton myynh_install_dependencies --python="$PY_REQUIRED_VERSION" -# stop systemd service -ynh_script_progression --message="Stoping service..." -ynh_systemd_action --service_name="$app@$app" --action=stop - # installation in a virtual environment ynh_script_progression --message="Installing Home Assistant in a virtual environment..." myynh_install_homeassistant @@ -58,14 +105,36 @@ cp -r "../conf/homeassistant_conf_files/bin/." "$data_path/bin/" chown -R $app: "$data_path/bin" chmod -R +x "$data_path/bin/" -# setup up systemd service -ynh_script_progression --message="Adding the dedicated service..." +#================================================= +# SETUP SYSTEMD +#================================================= +ynh_script_progression --message="Upgrading systemd configuration..." + +# Create a dedicated systemd config ynh_add_systemd_config --service="$app@$app" -## add service in admin panel + +#================================================= +# GENERIC FINALIZATION +#================================================= +# SETUP LOGROTATE +#================================================= +ynh_script_progression --message="Upgrading logrotate configuration..." + +# Use logrotate to manage app-specific logfile(s) +ynh_use_logrotate --logfile="$data_path/home-assistant.log" --nonappend + +#================================================= +# INTEGRATE SERVICE IN YUNOHOST +#================================================= +ynh_script_progression --message="Integrating service in YunoHost..." + yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port -# start systemd service -ynh_script_progression --message="Starting the Home Assistant server..." +#================================================= +# START SYSTEMD SERVICE +#================================================= +ynh_script_progression --message="Starting a systemd service..." + ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600 # remove --verbose from service ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app@$app.service" @@ -73,13 +142,15 @@ ynh_store_file_checksum --file="/etc/systemd/system/$app@$app.service" systemctl daemon-reload ynh_systemd_action --service_name="$app@$app" --action=restart -# enable logrotate -ynh_use_logrotate --logfile="$data_path/home-assistant.log" --nonappend +#================================================= +# RELOAD NGINX +#================================================= +ynh_script_progression --message="Reloading NGINX web server..." -# 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" --last