1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/homeassistant_ynh.git synced 2024-09-03 19:26:16 +02:00
This commit is contained in:
ewilly 2022-01-07 09:52:55 +01:00
parent 06067f04c1
commit b8df76ed03
7 changed files with 646 additions and 251 deletions

View file

@ -14,12 +14,12 @@ PKG_DEPENDENCIES="python3 python3-dev python3-venv python3-pip libffi-dev libssl
PY_REQUIRED_VERSION=3.9.2 PY_REQUIRED_VERSION=3.9.2
# Create homeassistant user # Create homeassistant user
mynh_user_create () { mynh_system_user_create () {
USER_GROUPS="" USER_GROUPS=""
[ $(getent group dialout) ] && USER_GROUPS="${USER_GROUPS} dialout" [ $(getent group dialout) ] && USER_GROUPS="${USER_GROUPS} dialout"
[ $(getent group gpio) ] && USER_GROUPS="${USER_GROUPS} gpio" [ $(getent group gpio) ] && USER_GROUPS="${USER_GROUPS} gpio"
[ $(getent group i2c) ] && USER_GROUPS="${USER_GROUPS} i2c" [ $(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"
} }

View file

@ -3,13 +3,28 @@
# yunohost backup create -n "homeassistant-test" --apps homeassistant # yunohost backup create -n "homeassistant-test" --apps homeassistant
# yunohost backup delete homeassistant-test # yunohost backup delete homeassistant-test
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_abort_if_errors ynh_abort_if_errors
# retrieve arguments #=================================================
# LOAD SETTINGS
#=================================================
ynh_print_info --message="Loading installation settings..."
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app="$app" --key=domain) domain=$(ynh_app_setting_get --app="$app" --key=domain)
port=$(ynh_app_setting_get --app="$app" --key=port) port=$(ynh_app_setting_get --app="$app" --key=port)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path) 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) python=$(ynh_app_setting_get --app="$app" --key=python)
db_name=$(ynh_app_setting_get --app=$app --key=db_name) 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="$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/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" 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")" 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_print_info --message="Backing up the MySQL database..."
ynh_mysql_dump_db --database="$db_name" > db.sql 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)."

View file

@ -1,20 +1,35 @@
#!/bin/bash #!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# retrieve arguments #=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
old_domain=$YNH_APP_OLD_DOMAIN old_domain=$YNH_APP_OLD_DOMAIN
new_domain=$YNH_APP_NEW_DOMAIN new_domain=$YNH_APP_NEW_DOMAIN
# load settings #=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --time --weight=1 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 final_path=$(ynh_app_setting_get --app=$app --key=final_path)
ynh_script_progression --message="Backing up the app before changing its url (may take a while)..." --time --weight=1
#=================================================
# 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_backup_before_upgrade
ynh_clean_setup () { ynh_clean_setup () {
ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf" ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf"
@ -22,24 +37,60 @@ ynh_clean_setup () {
} }
ynh_abort_if_errors ynh_abort_if_errors
# check which parts should be changed #=================================================
# CHECK WHICH PARTS SHOULD BE CHANGED
#=================================================
change_domain=0 change_domain=0
if [ "$old_domain" != "$new_domain" ]; then if [ "$old_domain" != "$new_domain" ]
then
change_domain=1 change_domain=1
fi fi
# Change the domain for nginx #=================================================
if [ $change_domain -eq 1 ]; then # STANDARD MODIFICATIONS
ynh_script_progression --message="Updating nginx web server configuration..." --time --weight=1 #=================================================
# 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 # Delete file checksum for the old conf file location
ynh_delete_file_checksum --file="$old_nginx_conf" ynh_delete_file_checksum --file="$nginx_conf_path"
mv "$old_nginx_conf" "$new_nginx_conf" mv $nginx_conf_path /etc/nginx/conf.d/$new_domain.d/$app.conf
# Store file checksum for the new config file location # 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 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 ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Change of URL completed for $app" --time --last ynh_script_progression --message="Change of URL completed for $app" --time --last

View file

@ -1,116 +1,195 @@
#!/bin/bash #!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () { ynh_clean_setup () {
ynh_clean_check_starting true
} }
ynh_abort_if_errors ynh_abort_if_errors
# retrieve arguments #=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$YNH_APP_ARG_DOMAIN domain=$YNH_APP_ARG_DOMAIN
is_public=$YNH_APP_ARG_IS_PUBLIC is_public=$YNH_APP_ARG_IS_PUBLIC
# definie useful vars #=================================================
# DEFINE USEFULL VARS
#=================================================
final_path="/var/www/$app" final_path="/var/www/$app"
data_path="/home/yunohost.app/$app" data_path="/home/yunohost.app/$app"
log_file="/var/log/$app/$app.log" log_file="/var/log/$app/$app.log"
path_url="/" path_url="/"
# check domain/path availability #=================================================
ynh_script_progression --message="Validating installation parameters..." # CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder" #=================================================
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" 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..." # STORE SETTINGS FROM MANIFEST
port=$(ynh_find_port 8123) #=================================================
ynh_exec_fully_quiet yunohost firewall allow TCP "$port" ynh_script_progression --message="Storing installation settings..." --time --weight=1
# 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=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=final_path --value="$final_path"
ynh_app_setting_set --app="$app" --key=data_path --value="$data_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=log_file --value="$log_file"
ynh_app_setting_set --app="$app" --key=path_url --value="$path_url" 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..." # STANDARD MODIFICATIONS
mynh_user_create #=================================================
# 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 port=$(ynh_find_port 8123)
myynh_create_dir "$final_path" ynh_app_setting_set --app="$app" --key=port --value="$port"
chown -R $app: "$final_path" ynh_exec_warn_less yunohost firewall allow --no-upnp TCP $port
# create a directory with its log file #=================================================
myynh_create_dir "$(dirname "$log_file")" # INSTALL DEPENDENCIES
touch "$log_file" #=================================================
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" 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_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$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 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 # installation in a virtual environment
ynh_script_progression --message="Installing Home Assistant in a virtual environment..." ynh_script_progression --message="Installing Home Assistant in a virtual environment..."
ynh_exec_fully_quiet myynh_install_homeassistant ynh_exec_fully_quiet myynh_install_homeassistant
# set default configuration files and move all homeassistant_conf_files #=================================================
ynh_script_progression --message="Configuring the installation..." # NGINX CONFIGURATION
cp -r "../conf/homeassistant_conf_files/." "$data_path/" #=================================================
ynh_add_config --template="../conf/homeassistant_conf_files/configuration.yaml" --destination="$data_path/configuration.yaml" 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 # grant sudo permissions to the user to manage his own systemd service
myynh_create_dir "/etc/sudoers.d" myynh_create_dir "/etc/sudoers.d"
ynh_add_config --template="../conf/sudoers" --destination="/etc/sudoers.d/$app" 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 # setup up systemd service
ynh_script_progression --message="Adding the dedicated service..." ynh_script_progression --message="Adding the dedicated service..."
ynh_add_systemd_config 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 # 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 # start systemd service with --verbose
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
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_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app.service"
ynh_store_file_checksum --file="/etc/systemd/system/$app.service" ynh_store_file_checksum --file="/etc/systemd/system/$app.service"
systemctl daemon-reload 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" [ $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

View file

@ -2,11 +2,22 @@
# to test the functionnality : # to test the functionnality :
# yunohost app remove homeassistant --purge # yunohost app remove homeassistant --purge
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# retrieve arguments #=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain) domain=$(ynh_app_setting_get --app=$app --key=domain)
port=$(ynh_app_setting_get --app=$app --key=port) port=$(ynh_app_setting_get --app=$app --key=port)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path) 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_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$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 if ynh_exec_warn_less yunohost service status "$app" >/dev/null ; then
ynh_script_progression --message="Removing $app service integration..." ynh_script_progression --message="Removing $app service integration..."
yunohost service remove "$app" yunohost service remove "$app"
fi 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" 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 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 ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name
# remove metapackage and its dependencies #=================================================
ynh_script_progression --message="Removing dependencies..." # REMOVE APP MAIN DIR
ynh_remove_app_dependencies #=================================================
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" ynh_secure_remove --file="$final_path"
# remove a directory securely if --purge option is used #=================================================
if [ "${YNH_APP_PURGE:-0}" -eq 1 ] ; then # REMOVE DATA DIR
ynh_script_progression --message="Removing app data directory..." #=================================================
ynh_secure_remove --file="$data_path"
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 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 ynh_remove_nginx_config
# close port #=================================================
if yunohost firewall list | grep -q "\- $port$" ; then # REMOVE DEPENDENCIES
ynh_script_progression --message="Closing port $port..." #=================================================
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 ynh_exec_warn_less yunohost firewall disallow TCP $port
fi 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 # remove sudoers file
ynh_secure_remove --file="/etc/sudoers.d/$app" ynh_secure_remove --file="/etc/sudoers.d/$app"
# Remove the log files # Remove the log files
ynh_secure_remove --file="$(dirname "$log_file")" ynh_secure_remove --file="$(dirname "$log_file")"
# delete a system user #=================================================
ynh_script_progression --message="Removing the dedicated system user..." # GENERIC FINALIZATION
ynh_system_user_delete --username="$app" #=================================================
# 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

View file

@ -5,17 +5,31 @@
# yunohost backup restore "homeassistant-test" # yunohost backup restore "homeassistant-test"
# yunohost backup delete "homeassistant-test" # yunohost backup delete "homeassistant-test"
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source ../settings/scripts/_common.sh source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () { ynh_clean_setup () {
ynh_clean_check_starting true
} }
ynh_abort_if_errors ynh_abort_if_errors
# retrieve arguments #=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --time --weight=1
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app="$app" --key=domain) domain=$(ynh_app_setting_get --app="$app" --key=domain)
port=$(ynh_app_setting_get --app="$app" --key=port) port=$(ynh_app_setting_get --app="$app" --key=port)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path) 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_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name db_user=$db_name
# check domain/path availability #=================================================
ynh_script_progression --message="Validating recovery parameters..." # CHECK IF THE APP CAN BE RESTORED
[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder" #=================================================
ynh_script_progression --message="Validating restoration parameters..." --time --weight=1
# restore port # check app main dir availability
ynh_script_progression --message="Restoring the port and opening it..." [ ! -d "$final_path" ] || ynh_die --message="There is already a directory: $final_path "
ynh_exec_warn_less yunohost firewall allow TCP $port
# restore dedicated system user # check port availability
ynh_script_progression --message="Restoring dedicated user and rights folders..." ynh_port_available --port=$port || ynh_die --message="Port $port is needs to be available for this app"
mynh_user_create
ynh_restore_file --origin_path="/etc/sudoers.d/$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" 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" ynh_restore_file --origin_path="$final_path"
# restore data #=================================================
ynh_script_progression --message="Restoring the data..." # RESTORE THE DATA DIRECTORY
ynh_restore_file --origin_path="$data_path" #=================================================
ynh_script_progression --message="Restoring the data directory..." --time --weight=1
# restore log ynh_restore_file --origin_path="$datadir" --not_mandatory
ynh_script_progression --message="Restoring the log file..."
ynh_restore_file --origin_path="$(dirname "$log_file")" 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" 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) 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_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 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" ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
systemctl enable $app.service --quiet 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 ynh_restore_file --origin_path="/etc/logrotate.d/$app"
myynh_set_permissions
# 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" 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 # remove --verbose from service and restart
ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app.service" 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 systemctl daemon-reload
ynh_systemd_action --service_name="$app" --action=restart ynh_systemd_action --service_name="$app" --action=restart
# restore logrotate #=================================================
ynh_script_progression --message="Restoring logrotate..." # GENERIC FINALIZATION
ynh_restore_file --origin_path="/etc/logrotate.d/$app" #=================================================
# RELOAD NGINX AND PHP-FPM
#=================================================
ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..." --time --weight=1
# reload nginx ynh_systemd_action --service_name=php$phpversion-fpm --action=reload
ynh_script_progression --message="Reloading NGINX web server..."
ynh_systemd_action --service_name=nginx --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 debug purpose
for ((i = 1 ; i <= 30 ; i++)); do for ((i = 1 ; i <= 10 ; i++)); do
ynh_print_warn --message= "\r$i) $(date)" 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" echo -e -n "\r"
sleep 10 sleep 10
done done

View file

@ -1,139 +1,211 @@
#!/bin/bash #!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh source _common.sh
source /usr/share/yunohost/helpers source /usr/share/yunohost/helpers
# manage script failure #=================================================
ynh_clean_setup () { # LOAD SETTINGS
ynh_clean_check_starting #=================================================
} ynh_script_progression --message="Loading installation settings..." --time --weight=1
ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app="$app" --key=domain) domain=$(ynh_app_setting_get --app="$app" --key=domain)
port=$(ynh_app_setting_get --app="$app" --key=port) 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 if [ -z $(ynh_app_setting_get --app="$app" --key=final_path) ]; then
final_path="/var/www/$app" final_path="/var/www/$app"
data_path="/home/yunohost.app/$app" data_path="/home/yunohost.app/$app"
log_file="/var/log/$app/$app.log" log_file="/var/log/$app/$app.log"
path_url="/" path_url="/"
ynh_app_setting_set --app="$app" --key=final_path --value="$final_path" 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=data_path --value="$data_path"
ynh_app_setting_set --app="$app" --key=log_file --value="$log_file" ynh_app_setting_set --app="$app" --key=log_file --value="$log_file"
ynh_app_setting_set --app="$app" --key=path_url --value="$path_url" ynh_app_setting_set --app="$app" --key=path_url --value="$path_url"
else else
final_path=$(ynh_app_setting_get --app="$app" --key=final_path) final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
data_path=$(ynh_app_setting_get --app="$app" --key=data_path) data_path=$(ynh_app_setting_get --app="$app" --key=data_path)
log_file=$(ynh_app_setting_get --app="$app" --key=log_file) log_file=$(ynh_app_setting_get --app="$app" --key=log_file)
path_url=$(ynh_app_setting_get --app="$app" --key=path_url) path_url=$(ynh_app_setting_get --app="$app" --key=path_url)
python=$(ynh_app_setting_get --app="$app" --key=python) python=$(ynh_app_setting_get --app="$app" --key=python)
fi fi
# use prior backup and restore on error only if backup feature exists on installed instance # changes introduced in 2021.11.5~ynh1
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..."
if [ -f "/etc/systemd/system/$app@$app.service" ]; then if [ -f "/etc/systemd/system/$app@$app.service" ]; then
# remove old systemd service # remove old systemd service
if ynh_exec_warn_less yunohost service status "$app@$app" >/dev/null ; then if ynh_exec_warn_less yunohost service status "$app@$app" >/dev/null ; then
yunohost service remove "$app@$app" yunohost service remove "$app@$app"
fi fi
ynh_remove_systemd_config --service="$app@$app" ynh_remove_systemd_config --service="$app@$app"
fi fi
if [ ! -d "$final_path" ]; then if [ ! -d "$final_path" ]; then
# move $final_path to new directory # move $final_path to new directory
mv "/opt/yunohost/$app" "$final_path" mv "/opt/yunohost/$app" "$final_path"
chown -R $app: "$final_path" chown -R $app: "$final_path"
fi fi
if [ ! -d "$data_path" ]; then if [ ! -d "$data_path" ]; then
# move $data_path to new directory # move $data_path to new directory
mv "/""home""/$app" "$data_path" mv "/""home""/$app" "$data_path"
find "$data_path/.$app" -maxdepth 1 -mindepth 1 -exec mv {} "$data_path" \; find "$data_path/.$app" -maxdepth 1 -mindepth 1 -exec mv {} "$data_path" \;
rmdir "$data_path/.$app" rmdir "$data_path/.$app"
ynh_replace_string --match_string="/home/homeassistant/.homeassistant" --replace_string="$data_path" --target_file="$data_path/configuration.yaml" ynh_replace_string --match_string="/home/homeassistant/.homeassistant" --replace_string="$data_path" --target_file="$data_path/configuration.yaml"
chown -R $app: "$data_path" chown -R $app: "$data_path"
fi fi
if [ ! -f "$log_file" ]; then if [ ! -f "$log_file" ]; then
# create a directory with its log file # create a directory with its log file
myynh_create_dir "$(dirname "$log_file")" myynh_create_dir "$(dirname "$log_file")"
touch "$log_file" touch "$log_file"
fi fi
# changes introduced in 2021.12.8~ynh1
if [ -z $(ynh_app_setting_get --app="$app" --key=db_name) ]; then if [ -z $(ynh_app_setting_get --app="$app" --key=db_name) ]; then
# create a MySQL database # create a MySQL database
db_name=$(ynh_sanitize_dbid --db_name=$app) db_name=$(ynh_sanitize_dbid --db_name=$app)
db_user=$db_name db_user=$db_name
ynh_app_setting_set --app=$app --key=db_name --value=$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 ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name
db_pwd=$(ynh_app_setting_get --app="$app" --key=mysqlpwd) db_pwd=$(ynh_app_setting_get --app="$app" --key=mysqlpwd)
if [ -z $(sed -n "/recorder:/=" configuration.yaml) ]; then if [ -z $(sed -n "/recorder:/=" configuration.yaml) ]; then
sed -i "$ a recorder:" "$data_path/configuration.yaml" 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" 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 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" 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
fi fi
# installation in a virtual environment #=================================================
ynh_script_progression --message="Installing Home Assistant in a virtual environment..." # CREATE DEDICATED USER
ynh_exec_fully_quiet myynh_install_homeassistant #=================================================
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/" 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" 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 myynh_set_permissions
# start systemd service #=================================================
ynh_script_progression --message="Starting the Home Assistant server..." # SETUP SYSTEMD
systemctl daemon-reload #=================================================
ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600 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 # remove --verbose from service
ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$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" ynh_store_file_checksum --file="/etc/systemd/system/$app.service"
systemctl daemon-reload 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_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