diff --git a/conf/homeassistant_conf_files/configuration.yaml b/conf/homeassistant_conf_files/configuration.yaml index a500827..d00ad1e 100644 --- a/conf/homeassistant_conf_files/configuration.yaml +++ b/conf/homeassistant_conf_files/configuration.yaml @@ -11,6 +11,9 @@ http: - 127.0.0.1 - ::1 +recorder: + db_url: mysql+pymysql://__DB_USER__:__DB_PWD__@localhost:3306/__DB_NAME__?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4 + # Apply default set of integrations default_config: diff --git a/conf/systemd.service b/conf/systemd.service index a42c0db..2e35f8e 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -1,6 +1,6 @@ [Unit] Description=Home Assistant -After=network.target +After=network.target mysql.service [Service] Type=simple diff --git a/scripts/_common.sh b/scripts/_common.sh index 0bfac6b..298ee78 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -6,7 +6,7 @@ VERSION=2021.12.7 # 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" +PKG_DEPENDENCIES="python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5 libmariadbclient-dev libmariadb-dev-compat" # Requirements (Major.Minor.Patch) # PY_VERSION=$(curl -s "https://www.python.org/ftp/python/" | grep ">3.9" | tail -n1 | cut -d '/' -f 2 | cut -d '>' -f 2) @@ -135,6 +135,8 @@ myynh_install_homeassistant () { && source "$final_path/bin/activate" \ && echo 'install last version of wheel' \ && pip --cache-dir "$data_path/.cache" install --upgrade wheel \ + && echo 'install last version of mysqlclient' \ + && pip --cache-dir "$data_path/.cache" install --upgrade mysqlclient \ && echo 'install Home Assistant' \ && pip --cache-dir "$data_path/.cache" install --upgrade $app==$VERSION \ " diff --git a/scripts/backup b/scripts/backup index 02d2ff8..8333b29 100644 --- a/scripts/backup +++ b/scripts/backup @@ -17,6 +17,7 @@ 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) +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..." @@ -28,4 +29,8 @@ ynh_backup --src_path="/etc/systemd/system/$app.service" ynh_backup --src_path="/etc/logrotate.d/$app" ynh_backup --src_path="$(dirname "$log_file")" +# backup the MySQL database +ynh_print_info --message="Backing up the MySQL database..." +ynh_mysql_dump_db --database="$db_name" > db.sql + ynh_print_info --message="Backup of $app completed" diff --git a/scripts/install b/scripts/install index 6778034..57e09ac 100644 --- a/scripts/install +++ b/scripts/install @@ -59,6 +59,13 @@ chown -R $app: "$data_path" ynh_script_progression --message="Installing dependencies..." myynh_install_dependencies --python="$PY_REQUIRED_VERSION" +# create a MySQL database +ynh_script_progression --message="Creating a MySQL database..." +db_name=$(ynh_sanitize_dbid --db_name=$app) +db_user=$db_name +ynh_app_setting_set --app=$app --key=db_name --value=$db_name +ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name + # installation in a virtual environment ynh_script_progression --message="Installing Home Assistant in a virtual environment..." ynh_exec_fully_quiet myynh_install_homeassistant diff --git a/scripts/remove b/scripts/remove index 9fb0765..0d240cc 100644 --- a/scripts/remove +++ b/scripts/remove @@ -14,6 +14,8 @@ 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) +db_name=$(ynh_app_setting_get --app=$app --key=db_name) +db_user=$db_name # Remove a service from the admin panel, added by `yunohost service add` if ynh_exec_warn_less yunohost service status "$app" >/dev/null ; then @@ -29,6 +31,10 @@ ynh_remove_systemd_config --service="$app" ynh_script_progression --message="Removing logrotate configuration..." ynh_remove_logrotate +# remove the MySQL database +ynh_script_progression --message="Removing the MySQL database..." +ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name + # remove metapackage and its dependencies ynh_script_progression --message="Removing dependencies..." ynh_remove_app_dependencies diff --git a/scripts/restore b/scripts/restore index 1a4a152..2ae8093 100644 --- a/scripts/restore +++ b/scripts/restore @@ -23,6 +23,8 @@ 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) +db_name=$(ynh_app_setting_get --app=$app --key=db_name) +db_user=$db_name # check domain/path availability ynh_script_progression --message="Validating recovery parameters..." @@ -53,6 +55,12 @@ ynh_restore_file --origin_path="$(dirname "$log_file")" ynh_script_progression --message="Restoring the packages dependencies..." myynh_install_dependencies --python="$python" +# restore the MySQL database +ynh_script_progression --message="Restoring the MySQL database..." +db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) +ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd +ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql + # restore the systemd service ynh_script_progression --message="Restoring the dedicated service..." ynh_restore_file --origin_path="/etc/systemd/system/$app.service" diff --git a/scripts/upgrade b/scripts/upgrade index d75fd2d..0e8f29a 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -77,6 +77,20 @@ if [ ! -f "$log_file" ]; then myynh_create_dir "$(dirname "$log_file")" touch "$log_file" fi +if [ -z $(ynh_app_setting_get --app="$app" --key=db_name) ]; then + # create a MySQL database + db_name=$(ynh_sanitize_dbid --db_name=$app) + db_user=$db_name + ynh_app_setting_set --app=$app --key=db_name --value=$db_name + ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name + db_pwd=$(ynh_app_setting_get --app="$app" --key=mysqlpwd) + if [ -z $(sed -n "/recorder:/=" configuration.yaml) ]; then + sed -i "$ a recorder:" "$data_path/configuration.yaml" + sed -i "$ a \ db_url: mysql+pymysql://$db_user:$db_pwd@localhost:3306/$db_name?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4" "$data_path/configuration.yaml" + else + sed -i "/recorder:/a \ db_url: mysql+pymysql://$db_user:$db_pwd@localhost:3306/$db_name?unix_socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4" "$data_path/configuration.yaml" + fi +fi # installation in a virtual environment ynh_script_progression --message="Installing Home Assistant in a virtual environment..."