diff --git a/README.md b/README.md index f7bc58e..c9f89d2 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ If you don't have YunoHost, please consult [the guide](https://yunohost.org/#/in Home automation platform -**Shipped version:** 2021.11.4~ynh1 +**Shipped version:** 2021.11.5~ynh1 **Demo:** https://demo.home-assistant.io diff --git a/README_fr.md b/README_fr.md index c2ab526..d7cadca 100644 --- a/README_fr.md +++ b/README_fr.md @@ -13,7 +13,7 @@ Si vous n'avez pas YunoHost, regardez [ici](https://yunohost.org/#/install) pour Plateforme domotique -**Version incluse :** 2021.11.4~ynh1 +**Version incluse :** 2021.11.5~ynh1 **Démo :** https://demo.home-assistant.io diff --git a/conf/homeassistant_conf_files/.storage/onboarding b/conf/homeassistant_conf_files/.storage/onboarding index b92aa42..9677c86 100644 --- a/conf/homeassistant_conf_files/.storage/onboarding +++ b/conf/homeassistant_conf_files/.storage/onboarding @@ -1,11 +1,12 @@ { + "version": 4, + "key": "onboarding", "data": { "done": [ "user", "core_config", + "analytics", "integration" ] - }, - "key": "onboarding", - "version": 3 + } } diff --git a/conf/homeassistant_conf_files/bin/upgrade_homeassistant.sh b/conf/homeassistant_conf_files/bin/upgrade_homeassistant.sh index 5bd35f1..36e08c5 100755 --- a/conf/homeassistant_conf_files/bin/upgrade_homeassistant.sh +++ b/conf/homeassistant_conf_files/bin/upgrade_homeassistant.sh @@ -8,7 +8,8 @@ DEBUG=0 # define usefull variables app="homeassistant" -final_path="/opt/yunohost/$app" +final_path="/var/www/$app" +data_path="/home/yunohost.app/$app" ########## END OF CONFIGURATION ########## @@ -49,16 +50,13 @@ $MY_PYTHON -m venv "$final_path" # activate the virtual environment source "$final_path/bin/activate" -# install last version of pip -pip install --upgrade pip - # install last version of wheel -pip install --upgrade wheel +pip --cache-dir "$data_path/.cache" install --upgrade wheel # upgrade homeassistant python package -pip install --upgrade $app +pip --cache-dir "$data_path/.cache" install --upgrade $app # restart homeassistant systemd service -sudo systemctl restart $app@$app.service +sudo systemctl restart $app.service exit 0 diff --git a/conf/homeassistant_conf_files/configuration.yaml b/conf/homeassistant_conf_files/configuration.yaml index 1815781..a500827 100644 --- a/conf/homeassistant_conf_files/configuration.yaml +++ b/conf/homeassistant_conf_files/configuration.yaml @@ -1,7 +1,7 @@ homeassistant: auth_providers: - type: command_line - command: __PATH__/bin/ynh_ldap-auth.sh + command: __DATA_PATH__/bin/ynh_ldap-auth.sh meta: true http: @@ -19,5 +19,5 @@ switch: - platform: command_line switches: upgrade_homeassistant: - command_on: "nohup bash -c __PATH__/bin/upgrade_homeassistant.sh $1 > /dev/null 2>&1 &" + command_on: "nohup bash -c __DATA_PATH__/bin/upgrade_homeassistant.sh $1 > /dev/null 2>&1 &" friendly_name: Upgrade Home Assistant diff --git a/conf/nginx.conf b/conf/nginx.conf index b61db04..4aae8e1 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,9 +1,6 @@ -location / { +# see https://github.com/home-assistant/addons/blob/master/nginx_proxy/data/nginx.conf - if ($scheme = http) { - rewrite ^ https://$server_name$request_uri? permanent; - } - +location / { proxy_pass http://localhost:__PORT__; proxy_set_header Host $host; proxy_redirect http:// https://; diff --git a/conf/sudoers b/conf/sudoers index 4f92b76..6c98bb8 100644 --- a/conf/sudoers +++ b/conf/sudoers @@ -1,5 +1,5 @@ # Grant sudo permissions to the user to manage his own systemd service -__NAME__ ALL=(ALL) NOPASSWD: /bin/systemctl stop __NAME__@__NAME__.service -__NAME__ ALL=(ALL) NOPASSWD: /bin/systemctl start __NAME__@__NAME__.service -__NAME__ ALL=(ALL) NOPASSWD: /bin/systemctl restart __NAME__@__NAME__.service -__NAME__ ALL=(ALL) NOPASSWD: /bin/systemctl status __NAME__@__NAME__.service +__APP__ ALL=(ALL) NOPASSWD: /bin/systemctl stop __APP__.service +__APP__ ALL=(ALL) NOPASSWD: /bin/systemctl start __APP__.service +__APP__ ALL=(ALL) NOPASSWD: /bin/systemctl restart __APP__.service +__APP__ ALL=(ALL) NOPASSWD: /bin/systemctl status __APP__.service diff --git a/conf/systemd.service b/conf/systemd.service index 10ab4d7..a42c0db 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -1,13 +1,13 @@ [Unit] Description=Home Assistant -After=network-online.target +After=network.target [Service] Type=simple User=__APP__ -WorkingDirectory=__PATH__ -ExecStart=/opt/yunohost/__APP__/bin/hass --config "__PATH__" --verbose -StandardOutput=append:/var/log/__APP__.log +WorkingDirectory=__DATA_PATH__ +ExecStart=__FINALPATH__/bin/hass --config "__DATA_PATH__" --log-file "__LOG_FILE__" --verbose +StandardOutput=append:__LOG_FILE__ StandardError=inherit # Sandboxing options to harden security @@ -24,7 +24,7 @@ DevicePolicy=closed ProtectSystem=full ProtectControlGroups=yes ProtectKernelModules=yes -ProtectKernelTunables=yes +# ProtectKernelTunables=yes LockPersonality=yes SystemCallFilter=~@clock @debug @module @mount @obsolete @reboot @setuid @swap diff --git a/manifest.json b/manifest.json index 46c8221..05b2755 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Home automation platform", "fr": "Plateforme domotique" }, - "version": "2021.11.4~ynh1", + "version": "2021.11.5~ynh1", "url": "https://github.com/home-assistant/home-assistant", "upstream": { "license": "Apache-2.0", @@ -20,7 +20,7 @@ "email": "ewilly@ewilly.fr" }, "requirements": { - "yunohost": ">= 4.1.0" + "yunohost": ">= 4.3.0" }, "multi_instance": false, "services": [ diff --git a/scripts/_common.sh b/scripts/_common.sh index 88e95c0..98af024 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -3,28 +3,16 @@ # # Release to install -VERSION=2021.11.4 +VERSION=2021.11.5 # 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_VERSION=$(curl -s "https://www.python.org/ftp/python/" | grep ">3.9" | tail -n1 | cut -d '/' -f 2 | cut -d '>' -f 2) +# Pyhton 3.9.2 will be shiped with bullseye PY_REQUIRED_VERSION=3.9.2 -# 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 -} - # Check if directory/file already exists (path in argument) myynh_check_path () { [ -z "$1" ] && ynh_die "No argument supplied" @@ -79,7 +67,6 @@ myynh_install_dependencies () { ynh_handle_getopts_args "$@" # Install main dependencies from apt - ynh_script_progression --message="Installing dependencies..." ynh_install_app_dependencies "${PKG_DEPENDENCIES}" # Check python version from APT @@ -96,40 +83,40 @@ myynh_install_dependencies () { # Compare version if [ $(myynh_version_compare $PY_APT_VERSION $python) -le 1 ]; then # APT >= Required - ynh_script_progression --message="Using provided python3..." + ynh_print_info --message="Using provided python3..." MY_PYTHON="python3" else # Either python already built or to build - ynh_script_progression --message="Installing additional dependencies..." - PKG_DEPENDENCIES="${PKG_DEPENDENCIES} tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libbz2-dev libexpat1-dev liblzma-dev wget tar" - ynh_install_app_dependencies "${PKG_DEPENDENCIES}" if [ $(myynh_version_compare $PY_BUILT_VERSION $python) -le 1 ]; then # Built >= Required - ynh_script_progression --message="Using already used python3 built version..." + ynh_print_info --message="Using already used python3 built version..." MY_PYTHON="/usr/local/bin/python${PY_BUILT_VERSION:0:3}" else - # APT < Minimal & Actual < Minimal => Build & install Python into /usr/local/bin - ynh_script_progression --message="Building python (may take a while)..." - # Store current direcotry - local MY_DIR=$(pwd) - # Download - wget -O "/tmp/Python-$python.tar.xz" "https://www.python.org/ftp/python/$python/Python-$python.tar.xz" 2>&1 - # Extract - cd /tmp - tar xf "Python-$python.tar.xz" - # Install - cd "Python-$python" - ./configure --enable-optimizations - ynh_exec_warn_less make -j4 - ynh_exec_warn_less make altinstall - # Clean - cd .. - ynh_secure_remove "Python-$python" - ynh_secure_remove "Python-$python.tar.xz" - # Set version - MY_PYTHON="/usr/local/bin/python${python:0:3}" - # Go back to working directory - cd $MY_DIR + ynh_print_info --message="Installing additional dependencies to build python..." + PKG_DEPENDENCIES="${PKG_DEPENDENCIES} tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libbz2-dev libexpat1-dev liblzma-dev wget tar" + ynh_install_app_dependencies "${PKG_DEPENDENCIES}" + # APT < Minimal & Actual < Minimal => Build & install Python into /usr/local/bin + ynh_print_info --message="Building python (may take a while)..." + # Store current direcotry + local MY_DIR=$(pwd) + # Download + wget -O "/tmp/Python-$python.tar.xz" "https://www.python.org/ftp/python/$python/Python-$python.tar.xz" 2>&1 + # Extract + cd /tmp + tar xf "Python-$python.tar.xz" + # Install + cd "Python-$python" + ./configure --enable-optimizations + ynh_exec_warn_less make -j4 + ynh_exec_warn_less make altinstall + # Clean + cd .. + ynh_secure_remove "Python-$python" + ynh_secure_remove "Python-$python.tar.xz" + # Set version + MY_PYTHON="/usr/local/bin/python${python:0:3}" + # Go back to working directory + cd $MY_DIR fi fi # Save python version in settings @@ -137,23 +124,31 @@ myynh_install_dependencies () { } # Install/Upgrade Homeassistant in virtual environement -# | arg: -p, --path= - the parent path of cache directory myynh_install_homeassistant () { - # Declare an array to define the options of this helper. - local legacy_args=u - local -A args_array=( [p]=path= ) - local path - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - exec_as $app -H -s /bin/bash -c " \ - echo 'create the virtual environment' \ - && $MY_PYTHON -m venv "$final_path" \ - && echo 'activate the virtual environment' \ - && source "$final_path/bin/activate" \ - && echo 'install last version of wheel' \ - && pip --cache-dir "$path/.cache" install --upgrade wheel \ - && echo 'install Home Assistant' \ - && pip --cache-dir "$path/.cache" install --upgrade $app==$VERSION \ - " + 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' \ + && source "$final_path/bin/activate" \ + && echo 'install last version of wheel' \ + && pip --cache-dir "$data_path/.cache" install --upgrade wheel \ + && echo 'install Home Assistant' \ + && pip --cache-dir "$data_path/.cache" install --upgrade $app==$VERSION \ + " +} + +# Set permissions +myynh_set_permissions () { + chown -R $app: "$final_path" + chmod 750 "$final_path" + chmod -R o-rwx "$final_path" + + chown -R $app: "$data_path" + chmod 750 "$data_path" + chmod -R o-rwx "$data_path" + chmod -R +x "$data_path/bin/" + + chown -R $app: "$(dirname "$log_file")" + + chown -R root: "/etc/sudoers.d/$app" } diff --git a/scripts/backup b/scripts/backup index 7e052b1..02d2ff8 100644 --- a/scripts/backup +++ b/scripts/backup @@ -11,15 +11,12 @@ ynh_abort_if_errors # retrieve arguments app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app="$app" --key=domain) - -# definie useful vars -final_path="/opt/yunohost/$app" -data_path="/home/yunohost.app/$app" - -ynh_print_info --message="Storing the python version in settings..." -# store python version -python=$(cat "$final_path/bin/python3" --version | cut -d ' ' -f 2) -ynh_app_setting_set --app="$app" --key=python --value="$python" +port=$(ynh_app_setting_get --app="$app" --key=port) +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) # backup source & conf files ynh_print_info --message="Declaring files to be backed up..." @@ -27,7 +24,8 @@ ynh_backup --src_path="$final_path" ynh_backup --src_path="$data_path" 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" +ynh_backup --src_path="/etc/systemd/system/$app.service" ynh_backup --src_path="/etc/logrotate.d/$app" +ynh_backup --src_path="$(dirname "$log_file")" ynh_print_info --message="Backup of $app completed" diff --git a/scripts/install b/scripts/install index 4c4283a..2321a9e 100644 --- a/scripts/install +++ b/scripts/install @@ -15,8 +15,9 @@ domain=$YNH_APP_ARG_DOMAIN is_public=$YNH_APP_ARG_IS_PUBLIC # definie useful vars -final_path="/opt/yunohost/$app" +final_path="/var/www/$app" data_path="/home/yunohost.app/$app" +log_file="/var/log/$app/$app.log" path_url="/" # check domain/path availability @@ -24,68 +25,75 @@ 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" +# save app settings +ynh_script_progression --message="Storing installation settings..." +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..." ynh_system_user_create --username="$app" -# 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" - # create a directory for the installation of Home Assistant myynh_create_dir "$final_path" -chown $app: "$final_path" +chown -R $app: "$final_path" + +# create a directory with its log file +myynh_create_dir "$(dirname "$log_file")" +touch "$log_file" # create a directory for the datas of Home Assistant -myynh_create_dir "$data_path/.$app" 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" # installation in a virtual environment ynh_script_progression --message="Installing Home Assistant in a virtual environment..." -ynh_exec_fully_quiet myynh_install_homeassistant --path="$data_path" +ynh_exec_fully_quiet myynh_install_homeassistant # set default configuration files and move all homeassistant_conf_files ynh_script_progression --message="Configuring the installation..." -ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="../conf/homeassistant_conf_files/configuration.yaml" -ynh_replace_string --match_string="__PATH__" --replace_string="$data_path/.$app" --target_file="../conf/homeassistant_conf_files/configuration.yaml" -cp -r "../conf/homeassistant_conf_files/." "$data_path/.$app/" -chown -R $app: "$data_path/.$app" -chmod -R +x "$data_path/.$app/bin/" +cp -r "../conf/homeassistant_conf_files/." "$data_path/" +ynh_add_config --template="../conf/homeassistant_conf_files/configuration.yaml" --destination="$data_path/configuration.yaml" + +# 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" # setup up systemd service ynh_script_progression --message="Adding the dedicated service..." -ynh_replace_string --match_string="__PATH__" --replace_string="$data_path/.$app" --target_file="../conf/systemd.service" -ynh_add_systemd_config --service="$app@$app" +ynh_add_systemd_config -## add service in admin panel -yunohost service add "$app@$app" --log "$data_path/.$app/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port +# add service in admin panel +yunohost service add "$app" --log "$log_file" --description "Home Assistant server" --needs_exposed_ports $port + +# set permissions +myynh_set_permissions # 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="/var/log/$app.log" --timeout=3600 +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@$app.service" -ynh_store_file_checksum --file="/etc/systemd/system/$app@$app.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@$app" --action=restart +ynh_systemd_action --service_name="$app" --action=restart # enable logrotate -ynh_use_logrotate --logfile="$data_path/.$app/home-assistant.log" +ynh_use_logrotate --logfile="$log_file" # create a dedicated nginx config ynh_script_progression --message="Configuring nginx web server..." diff --git a/scripts/remove b/scripts/remove index bbd487b..9fb0765 100644 --- a/scripts/remove +++ b/scripts/remove @@ -9,20 +9,21 @@ source /usr/share/yunohost/helpers 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 -final_path="/opt/yunohost/$app" -data_path="/home/yunohost.app/$app" +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) # Remove a service from the admin panel, added by `yunohost service add` -if ynh_exec_warn_less yunohost service status "$app@$app" >/dev/null ; then +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" + yunohost service remove "$app" fi # remove systemd service ynh_script_progression --message="Stopping and removing the systemd service..." -ynh_remove_systemd_config --service="$app@$app" +ynh_remove_systemd_config --service="$app" # remove the app-specific logrotate config ynh_script_progression --message="Removing logrotate configuration..." @@ -52,9 +53,11 @@ if yunohost firewall list | grep -q "\- $port$" ; then ynh_exec_warn_less yunohost firewall disallow TCP $port fi -# remove sudoers file ynh_script_progression --message="Removing various files..." +# 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..." diff --git a/scripts/restore b/scripts/restore index 9805564..b6dd55c 100644 --- a/scripts/restore +++ b/scripts/restore @@ -18,13 +18,12 @@ ynh_abort_if_errors 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) +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) -# definie useful vars -final_path="/opt/yunohost/$app" -data_path="/home/yunohost.app/$app" -path_url="/" - # check domain/path availability ynh_script_progression --message="Validating recovery parameters..." [ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder" @@ -41,13 +40,14 @@ ynh_restore_file --origin_path="/etc/sudoers.d/$app" # restore source ynh_script_progression --message="Restoring the app..." ynh_restore_file --origin_path="$final_path" -chown -R $app: "$final_path" # restore data ynh_script_progression --message="Restoring the data..." ynh_restore_file --origin_path="$data_path" -chown -R $app: "$data_path" -chmod -R +x "$data_path/.$app/bin" + +# restore log +ynh_script_progression --message="Restoring the app..." +ynh_restore_file --origin_path="$(dirname "$log_file")" # add required packages ynh_script_progression --message="Restoring the packages dependencies..." @@ -55,21 +55,24 @@ myynh_install_dependencies --python="$python" # restore the systemd service ynh_script_progression --message="Restoring the dedicated service..." -ynh_restore_file --origin_path="/etc/systemd/system/$app@$app.service" +ynh_restore_file --origin_path="/etc/systemd/system/$app.service" # add service in admin panel -yunohost service add "$app@$app" --log "$data_path/.$app/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port +yunohost service add "$app" --log "$log_file" --description "Home Assistant server" --needs_exposed_ports $port + +# set permissions +myynh_set_permissions # restart the app ynh_script_progression --message="Starting the Home Assistant server..." -sed --in-place "/ExecStart/s/$/ --verbose/" "/etc/systemd/system/$app@$app.service" -ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="/var/log/$app.log" --timeout=900 +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 # remove --verbose from service and restart -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" +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@$app" --action=restart +ynh_systemd_action --service_name="$app" --action=restart # restore logrotate ynh_script_progression --message="Restoring logrotate..." @@ -79,6 +82,5 @@ ynh_restore_file --origin_path="/etc/logrotate.d/$app" ynh_script_progression --message="Restoring nginx web server..." ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" ynh_systemd_action --service_name=nginx --action=reload -ynh_exec_fully_quiet sleep 60s #Only necessary for CI test otherwise curl failed to nginx page ynh_script_progression --message="Recovery of $app completed" --last diff --git a/scripts/upgrade b/scripts/upgrade index 2306394..d75fd2d 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -15,8 +15,22 @@ 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/yunohost.app/$app" +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" +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) +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..." @@ -28,56 +42,77 @@ if [ -f "/etc/yunohost/apps/$app/scripts/backup" ] ; then } fi -# move $data_path to new directory -ynh_script_progression --message="Moving existing datas to new location..." -if [ ! -d "$data_path" ] ; then +# 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..." +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" +fi +if [ ! -d "$final_path" ]; then + # 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" - ynh_replace_string --match_string="/home/homeassistant/.homeassistant" --replace_string="$data_path/.$app" --target_file="$data_path/.$app/configuration.yaml" + 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" +fi + +# installation in a virtual environment +ynh_script_progression --message="Installing Home Assistant in a virtual environment..." +ynh_exec_fully_quiet myynh_install_homeassistant + +# 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..." ynh_add_config --template="../conf/sudoers" --destination="/etc/sudoers.d/$app" -# 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 --line_match="Stopped Home Assistant" --log_path="/var/log/$app.log" --timeout=300 - -# installation in a virtual environment -ynh_script_progression --message="Installing Home Assistant in a virtual environment..." -ynh_exec_fully_quiet myynh_install_homeassistant --path="$data_path" - -# update script in bin -ynh_script_progression --message="Updating YunoHost script used by homeassitant..." -cp -r "../conf/homeassistant_conf_files/bin/." "$data_path/.$app/bin/" -chown -R $app: "$data_path/.$app/bin" -chmod -R +x "$data_path/.$app/bin/" - -# setup up systemd service -ynh_script_progression --message="Adding the dedicated service..." -ynh_replace_string --match_string="__PATH__" --replace_string="$data_path/.$app" --target_file="../conf/systemd.service" -ynh_add_systemd_config --service="$app@$app" - # add service in admin panel -yunohost service add "$app@$app" --log "$data_path/.$app/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port +yunohost service add "$app" --log "$log_file" --description "Home Assistant server" --needs_exposed_ports $port + +# 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@$app" --action=start --line_match="Home Assistant initialized" --log_path="/var/log/$app.log" --timeout=3600 +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@$app.service" -ynh_store_file_checksum --file="/etc/systemd/system/$app@$app.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@$app" --action=restart +ynh_systemd_action --service_name="$app" --action=restart # enable logrotate -ynh_use_logrotate --logfile="$data_path/.$app/home-assistant.log" --nonappend +ynh_use_logrotate --logfile="$log_file" --nonappend # create a dedicated nginx config ynh_script_progression --message="Configuring nginx web server..."