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 2020-11-16 15:10:12 +01:00
parent 6c4ffa1e83
commit d3dfc8b7d1
9 changed files with 123 additions and 59 deletions

View file

@ -34,8 +34,8 @@ How to configure this app: From an admin panel
#### Multi-user support
* Are LDAP and HTTP auth supported?
* Can the app be used by multiple users?
* Are LDAP and HTTP auth supported? LDAP=Yes | HTTP auth=No
* Can the app be used by multiple users? Yes
#### Supported architectures

View file

@ -2,30 +2,24 @@
; Manifest
domain="domain.tld" (DOMAIN)
is_public=1 (PUBLIC|public=1|private=0)
; Actions
is_public=1|0
; Checks
pkg_linter=1
setup_sub_dir=0
setup_root=1
setup_nourl=0
setup_private=0
setup_public=0
setup_private=1
setup_public=1
upgrade=1
upgrade=1 from_commit=b07405d0f885f28764031dd80d27af84a90007b1
backup_restore=1
multi_instance=0
incorrect_path=0
port_already_use=1
change_url=0
change_url=1
actions=1
config_panel=0
;;; Levels
Level 1=auto
Level 2=auto
Level 3=auto
Level 4=na
Level 5=1
Level 6=auto
Level 7=auto
Level 8=0
Level 9=0
Level 10=0
Level 5=auto
;;; Options
Email=
Notification=none
Notification=all

View file

@ -10,7 +10,7 @@ PKG_DEPENDENCIES="python3 python3-dev python3-venv python3-pip libffi-dev libssl
# 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=3.8.6
PY_REQUIRED_VERSION=3.8.6
# Execute a command as another user
# usage: exec_as USER COMMAND [ARG ...]
@ -56,7 +56,16 @@ myynh_version_compare () {
}
# Package dependencies
# usage: myynh_install_dependencies --python="3.8.6"
# | arg: -p, --python= - the python version to install
myynh_install_dependencies () {
# Declare an array to define the options of this helper.
local legacy_args=u
local -A args_array=( [p]=python= )
local python
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
# Install main dependencies from apt
ynh_script_progression --message="Installing dependencies..."
ynh_install_app_dependencies "${PKG_DEPENDENCIES}"
@ -65,15 +74,15 @@ myynh_install_dependencies () {
local PY_APT_VERSION=$(python3 --version | cut -d ' ' -f 2)
# Check existing built version of python in /usr/local/bin
if [ -e "/usr/local/bin/python${PY_VERSION:0:3}" ]; then
local PY_BUILT_VERSION=$(/usr/local/bin/python${PY_VERSION:0:3} --version \
if [ -e "/usr/local/bin/python${python:0:3}" ]; then
local PY_BUILT_VERSION=$(/usr/local/bin/python${python:0:3} --version \
| cut -d ' ' -f 2)
else
local PY_BUILT_VERSION=0
fi
# Compare version
if [ $(myynh_version_compare $PY_APT_VERSION $PY_VERSION) -le 1 ]; then
if [ $(myynh_version_compare $PY_APT_VERSION $python) -le 1 ]; then
# APT >= Required
ynh_script_progression --message="Using provided python3..."
MY_PYTHON="python3"
@ -82,31 +91,37 @@ myynh_install_dependencies () {
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 $PY_VERSION) -le 1 ]; then
if [ $(myynh_version_compare $PY_BUILT_VERSION $python) -le 1 ]; then
# Built >= Required
ynh_script_progression --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-$PY_VERSION.tar.xz" "https://www.python.org/ftp/python/$PY_VERSION/Python-$PY_VERSION.tar.xz"
wget -O "/tmp/Python-$python.tar.xz" "https://www.python.org/ftp/python/$python/Python-$python.tar.xz"
# Extract
cd /tmp
tar xf "Python-$PY_VERSION.tar.xz"
tar xf "Python-$python.tar.xz"
# Install
cd "Python-$PY_VERSION"
cd "Python-$python"
./configure --enable-optimizations
make -j4
make altinstall
# Clean
cd ..
ynh_secure_remove "Python-$PY_VERSION"
ynh_secure_remove "Python-$PY_VERSION.tar.xz"
ynh_secure_remove "Python-$python"
ynh_secure_remove "Python-$python.tar.xz"
# Set version
MY_PYTHON="/usr/local/bin/python${PY_VERSION:0:3}"
MY_PYTHON="/usr/local/bin/python${python:0:3}"
# Go back to working directory
cd $MY_DIR
fi
fi
# Save python version in settings
ynh_app_setting_set $app python "$python"
}
# Install/Upgrade Homeassistant in virtual environement

View file

@ -10,7 +10,7 @@ ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
domain=$(ynh_app_setting_get --app="$app" --key=domain)
# definie useful vars
final_path="/opt/yunohost/$app"

45
scripts/change_url Normal file
View file

@ -0,0 +1,45 @@
#!/bin/bash
source _common.sh
source /usr/share/yunohost/helpers
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
old_domain=$YNH_APP_OLD_DOMAIN
new_domain=$YNH_APP_NEW_DOMAIN
# load settings
ynh_script_progression --message="Loading installation settings..." --time --weight=1
old_nginx_conf="/etc/nginx/conf.d/$old_domain.d/$app.conf"
new_nginx_conf="/etc/nginx/conf.d/$new_domain.d/$app.conf"
# manage script failure
ynh_script_progression --message="Backing up the app before changing its url (may take a while)..." --time --weight=1
ynh_backup_before_upgrade
ynh_clean_setup () {
ynh_secure_remove --file="/etc/nginx/conf.d/$new_domain.d/$app.conf"
ynh_restore_upgradebackup
}
ynh_abort_if_errors
# check which parts should be changed
change_domain=0
if [ "$old_domain" != "$new_domain" ]; then
change_domain=1
fi
# Change the domain for nginx
if [ $change_domain -eq 1 ]; then
ynh_script_progression --message="Updating nginx web server configuration..." --time --weight=1
# Delete file checksum for the old conf file location
ynh_delete_file_checksum --file="$old_nginx_conf"
mv "$old_nginx_conf" "$new_nginx_conf"
# Store file checksum for the new config file location
ynh_store_file_checksum --file="$new_nginx_conf"
fi
# reload nginx
ynh_script_progression --message="Reloading nginx web server..." --time --weight=1
ynh_systemd_action --service_name=nginx --action=reload
ynh_script_progression --message="Change of URL completed for $app" --time --last

View file

@ -18,26 +18,26 @@ is_public=$YNH_APP_ARG_IS_PUBLIC
final_path="/opt/yunohost/$app"
home_path="/home/$app"
data_path="/home/$app/.$app"
path_url=$(ynh_normalize_url_path --path_url="/")
# check domain/path availability
ynh_script_progression --message="Validating installation parameters..."
path_url=$(ynh_normalize_url_path "/")
ynh_webpath_available "$domain" "$path_url" || ynh_die "$domain/$path_url is not available, please use an other domain."
ynh_webpath_register $app "$domain" "$path_url"
[ ! -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 domain "$domain"
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 port $port
ynh_exec_fully_quiet yunohost firewall allow TCP $port
ynh_app_setting_set --app="$app" --key=port --value="$port"
ynh_exec_fully_quiet yunohost firewall allow TCP "$port"
# create a dedicated system user
ynh_script_progression --message="Creating dedicated user, rights and folders..."
ynh_system_user_create $app
ynh_system_user_create --username="$app"
## grant sudo permissions to the user to manage his own systemd service
myynh_create_dir "/etc/sudoers.d"
cp "../conf/sudoers" "/etc/sudoers.d/$app"
@ -49,7 +49,7 @@ myynh_create_dir "$data_path"
chown -R $app: "$home_path"
# build (if needed) & install Pyhton
myynh_install_dependencies
myynh_install_dependencies --python="$PY_REQUIRED_VERSION"
# installation in a virtual environment
ynh_script_progression --message="Installing Home Assistant in a virtual environment..."
@ -57,7 +57,7 @@ myynh_install_homeassistant
# set default configuration files and move all homeassistant_conf_files
ynh_script_progression --message="Configuring the installation..."
ynh_replace_string "__PORT__" "$port" "../conf/homeassistant_conf_files/configuration.yaml"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="../conf/homeassistant_conf_files/configuration.yaml"
cp -r "../conf/homeassistant_conf_files/." "$data_path/"
chown -R $app: "$data_path"
chmod -R +x "$data_path/bin/"
@ -72,7 +72,7 @@ yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --descrip
ynh_script_progression --message="Starting the Home Assistant server..."
ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600
# remove --verbose from service
ynh_replace_string " --verbose" "" "/etc/systemd/system/$app@$app.service"
ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app@$app.service"
systemctl daemon-reload
ynh_systemd_action --service_name="$app@$app" --action=restart
@ -84,6 +84,6 @@ 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

View file

@ -40,7 +40,7 @@ ynh_secure_remove --file="/etc/systemd/system/$app@$app.service"
ynh_exec_fully_quiet yunohost firewall disallow TCP $port
# delete a system user
ynh_system_user_delete --username=$app
ynh_system_user_delete --username="$app"
# remove sudoers file
ynh_secure_remove --file="/etc/sudoers.d/$app"

View file

@ -15,20 +15,18 @@ ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
port=$(ynh_app_setting_get --app=$app --key=port)
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"
home_path="/home/$app"
data_path="/home/$app/.$app"
path_url=$(ynh_normalize_url_path --path_url="/")
# check domain/path availability
path_url=$(ynh_normalize_url_path "/")
ynh_webpath_available $domain $path_url || ynh_die "$domain/$path_url is not available, please use an other domain."
# add required packages
myynh_install_dependencies
[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder"
ynh_webpath_available --domain="$domain" --path_url="$path_url" || ynh_die "$domain/$path_url is not available, please use an other domain."
# restore dedicated system user
ynh_system_user_create --username="$app"
@ -41,6 +39,9 @@ ynh_restore_file --origin_path="/etc/systemd/system/$app@$app.service"
# restore source
ynh_restore_file --origin_path="$final_path"
# add required packages
myynh_install_dependencies --python=$("$final_path/bin/python" --version | cut -d ' ' -f 2)
# restore data
ynh_restore_file --origin_path="$home_path"
chown -R $app: "$home_path"
@ -53,8 +54,17 @@ ynh_exec_fully_quiet yunohost firewall allow TCP $port
# add service in admin panel
yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port
# start systemd service
ynh_systemd_action --service_name="$app@$app" --action=start
# add --verbose to service
sed --in-place "/ExecStart/s/$/ --verbose/" "/etc/systemd/system/$app@$app.service"
# start
ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600
# remove --verbose from service and restart
ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app@$app.service"
systemctl daemon-reload
ynh_systemd_action --service_name="$app@$app" --action=restart
# reload nginx
ynh_systemd_action --service_name=nginx --action=reload

View file

@ -11,15 +11,15 @@ ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
port=$(ynh_app_setting_get --app=$app --key=port)
domain=$(ynh_app_setting_get --app="$app" --key=domain)
port=$(ynh_app_setting_get --app="$app" --key=port)
# Cleaning legacy permissions
ynh_script_progression --message="Cleaning legacy permissions..."
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
is_public=$(ynh_app_setting_get --app="$app" --key=is_public)
if [ -n "$is_public" ]; then
ynh_app_setting_delete --app=$app --key=is_public
ynh_app_setting_delete --app=$app --key=unprotected_uris
ynh_app_setting_delete --app="$app" --key=is_public
ynh_app_setting_delete --app="$app" --key=unprotected_uris
fi
# definie useful vars
@ -42,7 +42,7 @@ myynh_create_dir "/etc/sudoers.d"
cp "../conf/sudoers" "/etc/sudoers.d/$app"
# build (if needed) & install Pyhton
myynh_install_dependencies
myynh_install_dependencies --python="$PY_REQUIRED_VERSION"
# stop systemd service
ynh_script_progression --message="Stoping service..."
@ -68,7 +68,7 @@ yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --descrip
ynh_script_progression --message="Starting the Home Assistant server..."
ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600
# remove --verbose from service
ynh_replace_string " --verbose" "" "/etc/systemd/system/$app@$app.service"
ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app@$app.service"
systemctl daemon-reload
ynh_systemd_action --service_name="$app@$app" --action=restart