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

176 lines
5.2 KiB
Bash
Raw Normal View History

2022-01-08 10:04:57 +01:00
#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
# Release to install
2023-02-08 07:07:36 +01:00
app_version=2023.2.3
2022-01-08 10:04:57 +01:00
# Package dependencies
2022-04-15 21:17:45 +02:00
pkg_dependencies="python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5 libturbojpeg0 libmariadb-dev libmariadb-dev-compat rustc"
2022-01-08 10:04:57 +01:00
# 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)
# Pyhton 3.9.2 will be shiped with bullseye
2022-12-16 14:44:08 +01:00
py_required_version=3.10.9
2022-01-08 10:04:57 +01:00
#=================================================
# PERSONAL HELPERS
#=================================================
# Create homeassistant user
mynh_system_user_create () {
user_groups=""
[ $(getent group dialout) ] && user_groups="${user_groups} dialout"
[ $(getent group gpio) ] && user_groups="${user_groups} gpio"
[ $(getent group i2c) ] && user_groups="${user_groups} i2c"
ynh_system_user_create --username=$app --groups="$user_groups" --home_dir="$data_path"
}
# 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
ynh_print_info --message="Installing additional dependencies to build python..."
2022-12-16 14:44:08 +01:00
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 libnss3-dev libreadline-dev"
2022-01-08 10:04:57 +01:00
ynh_install_app_dependencies "${pkg_dependencies}"
2022-01-08 16:17:29 +01:00
2022-01-08 10:04:57 +01:00
# 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
ynh_exec_as $app $py_app_version -m venv --without-pip "$final_path"
# Run source in a 'sub shell'
(
# activate the virtual environment
set +o nounset
source "$final_path/bin/activate"
set -o nounset
# add pip
ynh_exec_as $app "$final_path/bin/python3" -m ensurepip
# install last version of wheel
ynh_exec_as $app "$final_path/bin/pip3" --cache-dir "$data_path/.cache" install --upgrade wheel
# install last version of mysqlclient
ynh_exec_as $app "$final_path/bin/pip3" --cache-dir "$data_path/.cache" install --upgrade mysqlclient
# install Home Assistant
ynh_exec_as $app "$final_path/bin/pip3" --cache-dir "$data_path/.cache" install --upgrade $app==$app_version
)
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 () {
2022-12-16 14:44:08 +01:00
ynh_exec_as $app $py_app_version -m venv --upgrade "$final_path"
2022-01-13 21:34:59 +01:00
}
2022-01-08 10:04:57 +01:00
# Set permissions
myynh_set_permissions () {
chown -R $app: "$final_path"
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app: "$data_path"
chmod 750 "$data_path"
chmod -R o-rwx "$data_path"
chmod -R +x "$data_path/bin/"
chown -R $app: "$(dirname "$log_file")"
chown -R root: "/etc/sudoers.d/$app"
}