1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/homeassistant_ynh.git synced 2024-09-03 19:26:16 +02:00
homeassistant_ynh/scripts/install

115 lines
4.6 KiB
Text
Raw Normal View History

2018-09-25 19:03:30 +02:00
#!/bin/bash
source _common.sh
source /usr/share/yunohost/helpers
# manage script failure
2020-08-13 20:50:44 +02:00
ynh_clean_setup () {
ynh_clean_check_starting
}
2018-09-25 19:03:30 +02:00
ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain=$YNH_APP_ARG_DOMAIN
is_public=$YNH_APP_ARG_IS_PUBLIC
2018-10-18 05:16:40 +02:00
2018-09-25 19:03:30 +02:00
# definie useful vars
2018-10-18 05:16:40 +02:00
final_path="/opt/yunohost/$app"
2021-11-20 19:05:14 +01:00
data_path="/home/yunohost.app/$app"
2021-11-24 06:37:01 +01:00
log_file="/var/log/$app/$app.log"
2021-01-05 15:18:56 +01:00
path_url="/"
2018-09-25 19:03:30 +02:00
# check domain/path availability
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Validating installation parameters..."
2020-11-16 15:10:12 +01:00
[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder"
ynh_webpath_register --app="$app" --domain="$domain" --path_url="$path_url"
2018-09-25 19:03:30 +02:00
# find a free port & open it
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Looking for a free port and opening it..."
2018-09-25 19:03:30 +02:00
port=$(ynh_find_port 8123)
2020-11-16 15:10:12 +01:00
ynh_exec_fully_quiet yunohost firewall allow TCP "$port"
2018-09-25 19:03:30 +02:00
2021-11-27 21:27:01 +01:00
# 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"
2021-11-20 19:05:14 +01:00
# grant sudo permissions to the user to manage his own systemd service
myynh_create_dir "/etc/sudoers.d"
2021-11-20 19:05:14 +01:00
ynh_add_config --template="../conf/sudoers" --destination="/etc/sudoers.d/$app"
2021-11-27 21:27:01 +01:00
# create a dedicated system user
ynh_script_progression --message="Creating dedicated user, rights and folders..."
ynh_system_user_create --username="$app"
2021-11-20 19:05:14 +01:00
# create a directory for the installation of Home Assistant
2018-10-18 05:16:40 +02:00
myynh_create_dir "$final_path"
chown $app: "$final_path"
2021-11-20 19:05:14 +01:00
2021-11-23 22:27:54 +01:00
# create a directory with its log file
2021-11-24 22:23:50 +01:00
myynh_create_dir "$(dirname "$log_file")"
touch "$log_file"
chown $app: "$log_file"
2021-11-23 22:27:54 +01:00
2021-11-20 19:05:14 +01:00
# 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"
2018-09-25 19:03:30 +02:00
2020-11-14 14:10:51 +01:00
# build (if needed) & install Pyhton
2021-11-24 22:23:50 +01:00
ynh_script_progression --message="Installing dependencies..."
2021-01-18 21:12:47 +01:00
myynh_install_dependencies --python="$PY_REQUIRED_VERSION"
2020-11-14 14:10:51 +01:00
2018-09-25 19:03:30 +02:00
# installation in a virtual environment
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Installing Home Assistant in a virtual environment..."
2021-11-20 19:05:14 +01:00
ynh_exec_fully_quiet myynh_install_homeassistant --path="$data_path"
2020-11-14 14:10:51 +01:00
# set default configuration files and move all homeassistant_conf_files
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Configuring the installation..."
2020-11-16 15:10:12 +01:00
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="../conf/homeassistant_conf_files/configuration.yaml"
2021-11-24 22:49:56 +01:00
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="../conf/homeassistant_conf_files/configuration.yaml"
2021-11-27 21:51:34 +01:00
ynh_replace_string --match_string="__DATAPATH__" --replace_string="$data_path" --target_file="../conf/homeassistant_conf_files/configuration.yaml"
2021-11-20 19:05:14 +01:00
cp -r "../conf/homeassistant_conf_files/." "$data_path/.$app/"
chown -R $app: "$data_path/.$app"
chmod -R +x "$data_path/.$app/bin/"
2018-09-25 19:03:30 +02:00
2020-08-13 20:50:44 +02:00
# setup up systemd service
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Adding the dedicated service..."
2021-11-27 21:51:34 +01:00
ynh_replace_string --match_string="__DATAPATH__" --replace_string="$data_path" --target_file="../conf/systemd.service"
ynh_replace_string --match_string="__LOGFILE__" --replace_string="$log_file" --target_file="../conf/systemd.service"
2021-11-21 21:25:39 +01:00
ynh_add_systemd_config --service="$app"
2021-11-20 19:05:14 +01:00
2019-06-10 12:07:28 +02:00
## add service in admin panel
2021-11-23 22:27:54 +01:00
yunohost service add "$app" --log "$log_file" --description "Home Assistant server" --needs_exposed_ports $port
2018-09-25 19:03:30 +02:00
2020-08-13 20:50:44 +02:00
# start systemd service
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Starting the Home Assistant server..."
2021-11-24 06:37:01 +01:00
ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600
2021-11-20 19:05:14 +01:00
2020-08-14 14:26:27 +02:00
# remove --verbose from service
2021-11-21 21:25:39 +01:00
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"
2020-08-14 14:26:27 +02:00
systemctl daemon-reload
2021-11-21 21:25:39 +01:00
ynh_systemd_action --service_name="$app" --action=restart
2018-09-25 19:03:30 +02:00
2021-02-20 09:53:53 +01:00
# enable logrotate
2021-11-23 22:27:54 +01:00
ynh_use_logrotate --logfile="$log_file"
2021-02-20 09:53:53 +01:00
2018-09-25 19:03:30 +02:00
# create a dedicated nginx config
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Configuring nginx web server..."
2018-09-25 19:03:30 +02:00
ynh_add_nginx_config
2021-11-20 19:05:14 +01:00
# reload nginx
2020-08-13 20:50:44 +02:00
ynh_systemd_action --service_name=nginx --action=reload
2018-09-25 19:03:30 +02:00
# unprotect app access if public (needed for Android app to work)
2020-08-12 22:22:59 +02:00
ynh_script_progression --message="Configuring permissions..."
2020-11-16 15:10:12 +01:00
[ $is_public -eq 1 ] && ynh_permission_update --permission="main" --add="visitors"
2019-06-10 12:07:28 +02:00
ynh_script_progression --message="Installation of $app completed" --last