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/_common.sh

180 lines
5.4 KiB
Bash
Raw Normal View History

2022-01-08 10:04:57 +01:00
#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
# Release to install
2023-11-04 11:24:37 +01:00
app_version=2023.11.0
2022-01-08 10:04:57 +01:00
2023-03-15 21:31:06 +01:00
# Requirements
2023-10-07 07:06:36 +02:00
py_required_version=3.11.6
2023-08-05 08:41:15 +02:00
pip_required="pip (>=21.3.1)"
2022-01-08 10:04:57 +01:00
2023-04-08 21:17:38 +02:00
# Fail2ban
failregex="^%(__prefix_line)s.*\[homeassistant.components.http.ban\] Login attempt or request with invalid authentication from.* \(<HOST>\).* Requested URL: ./auth/.*"
2022-01-08 10:04:57 +01:00
#=================================================
# PERSONAL HELPERS
#=================================================
# Check if directory/file already exists (path in argument)
myynh_check_path () {
[ -z "$1" ] && ynh_die "No argument supplied"
[ ! -e "$1" ] || ynh_die "$1 already exists"
}
# Create directory only if not already exists (path in argument)
myynh_create_dir () {
[ -z "$1" ] && ynh_die "No argument supplied"
[ -d "$1" ] || mkdir -p "$1"
}
# Install specific python version
# usage: myynh_install_python --python="3.8.6"
# | arg: -p, --python= - the python version to install
myynh_install_python () {
# 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 "$@"
# Check python version from APT
2022-01-08 16:17:29 +01:00
local py_apt_version=$(python3 --version | cut -d ' ' -f 2)
2022-01-08 10:04:57 +01:00
2022-12-16 14:44:08 +01:00
# Usefull variables
local python_major=${python%.*}
2022-01-08 10:04:57 +01:00
# Check existing built version of python in /usr/local/bin
2022-12-16 14:44:08 +01:00
if [ -e "/usr/local/bin/python$python_major" ]
2022-01-08 10:04:57 +01:00
then
2022-12-16 14:44:08 +01:00
local py_built_version=$(/usr/local/bin/python$python_major --version \
2022-01-08 10:04:57 +01:00
| cut -d ' ' -f 2)
else
2022-01-08 16:17:29 +01:00
local py_built_version=0
2022-01-08 10:04:57 +01:00
fi
# Compare version
2022-02-07 22:37:55 +01:00
if $(dpkg --compare-versions $py_apt_version ge $python)
2022-01-08 10:04:57 +01:00
then
# APT >= Required
ynh_print_info --message="Using provided python3..."
2022-01-08 16:17:29 +01:00
py_app_version="python3"
2022-01-08 10:04:57 +01:00
else
# Either python already built or to build
2022-02-07 22:37:55 +01:00
if $(dpkg --compare-versions $py_built_version ge $python)
2022-01-08 10:04:57 +01:00
then
# Built >= Required
ynh_print_info --message="Using already used python3 built version..."
2022-12-16 14:44:08 +01:00
py_app_version="/usr/local/bin/python${py_built_version%.*}"
2022-01-08 10:04:57 +01:00
else
# APT < Minimal & Actual < Minimal => Build & install Python into /usr/local/bin
ynh_print_info --message="Building python (may take a while)..."
2022-01-08 16:17:29 +01:00
2022-01-08 10:04:57 +01:00
# Store current direcotry
local MY_DIR=$(pwd)
2022-01-08 16:17:29 +01:00
# Create a temp direcotry
tmpdir="$(mktemp --directory)"
cd "$tmpdir"
2022-01-08 10:04:57 +01:00
# Download
2022-01-08 16:17:29 +01:00
wget --output-document="Python-$python.tar.xz" \
"https://www.python.org/ftp/python/$python/Python-$python.tar.xz" 2>&1
2022-01-08 10:04:57 +01:00
# Extract
tar xf "Python-$python.tar.xz"
2022-01-08 16:17:29 +01:00
2022-01-08 10:04:57 +01:00
# Install
cd "Python-$python"
./configure --enable-optimizations
ynh_exec_warn_less make -j4
ynh_exec_warn_less make altinstall
2022-01-08 16:17:29 +01:00
# Go back to working directory
cd "$MY_DIR"
2022-01-08 10:04:57 +01:00
# Clean
2022-01-08 16:17:29 +01:00
ynh_secure_remove "$tmpdir"
2022-01-08 10:04:57 +01:00
# Set version
2022-12-16 14:44:08 +01:00
py_app_version="/usr/local/bin/python$python_major"
2022-01-08 10:04:57 +01:00
fi
fi
# Save python version in settings
ynh_app_setting_set --app=$app --key=python --value="$python"
}
2022-02-21 21:33:31 +01:00
2022-01-08 10:04:57 +01:00
# Install/Upgrade Homeassistant in virtual environement
myynh_install_homeassistant () {
2022-12-16 14:44:08 +01:00
# Create the virtual environment
2023-02-12 22:01:31 +01:00
ynh_exec_as $app $py_app_version -m venv --without-pip "$install_dir"
2022-12-16 14:44:08 +01:00
# Run source in a 'sub shell'
(
# activate the virtual environment
set +o nounset
2023-02-12 22:01:31 +01:00
source "$install_dir/bin/activate"
2022-12-16 14:44:08 +01:00
set -o nounset
# add pip
2023-02-12 22:01:31 +01:00
ynh_exec_as $app "$install_dir/bin/python3" -m ensurepip
2022-12-16 14:44:08 +01:00
2023-02-15 12:31:34 +01:00
# install last version of pip
2023-07-21 07:14:34 +02:00
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$pip_required"
2023-02-15 12:31:34 +01:00
# install last version of wheel
2023-07-21 07:14:34 +02:00
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade wheel
2023-02-15 12:31:34 +01:00
# install last version of setuptools
2023-07-21 07:14:34 +02:00
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade setuptools
2023-02-15 12:31:34 +01:00
# install last version of mysqlclient
2023-07-21 07:14:34 +02:00
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade mysqlclient
2022-12-16 14:44:08 +01:00
# install Home Assistant
2023-07-21 07:14:34 +02:00
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$app==$app_version"
2022-12-16 14:44:08 +01:00
)
2022-01-08 10:04:57 +01:00
}
2022-01-13 21:34:59 +01:00
# Upgrade the virtual environment directory
myynh_upgrade_venv_directory () {
2023-06-23 14:36:19 +02:00
# Remove old python links before recreating them
2023-10-11 20:34:08 +02:00
find "$install_dir/bin/" -type l -name 'python*' \
2023-10-12 18:35:22 +02:00
-exec bash -c 'rm --force "$1"' _ {} \;
2023-06-23 14:36:19 +02:00
# Remove old python directories before recreating them
2023-10-11 20:34:08 +02:00
find "$install_dir/lib/" -mindepth 1 -maxdepth 1 -type d -name "python*" \
-not -path "*/python${py_required_version%.*}" \
2023-10-12 18:35:22 +02:00
-exec bash -c 'rm --force --recursive "$1"' _ {} \;
2023-10-11 20:34:08 +02:00
find "$install_dir/include/site/" -mindepth 1 -maxdepth 1 -type d -name "python*" \
-not -path "*/python${py_required_version%.*}" \
2023-10-12 18:35:22 +02:00
-exec bash -c 'rm --force --recursive "$1"' _ {} \;
2023-06-23 14:36:19 +02:00
# Upgrade the virtual environment directory
2023-02-12 22:01:31 +01:00
ynh_exec_as $app $py_app_version -m venv --upgrade "$install_dir"
2022-01-13 21:34:59 +01:00
}
2022-01-08 10:04:57 +01:00
# Set permissions
myynh_set_permissions () {
2023-02-12 22:01:31 +01:00
chown -R $app: "$install_dir"
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
2022-01-08 10:04:57 +01:00
2023-02-12 22:01:31 +01:00
chown -R $app: "$data_dir"
chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
2023-02-15 12:31:34 +01:00
[ ! -e "$data_dir/bin/" ] || chmod -R +x "$data_dir/bin/"
2022-01-08 10:04:57 +01:00
2023-02-15 12:31:34 +01:00
[ ! -e "$(dirname "$log_file")" ] || chown -R $app: "$(dirname "$log_file")"
2022-01-08 10:04:57 +01:00
2023-02-15 12:31:34 +01:00
[ ! -e "/etc/sudoers.d/$app" ] || chown -R root: "/etc/sudoers.d/$app"
2022-01-08 10:04:57 +01:00
}