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
2022-01-08 16:17:29 +01:00

193 lines
5.4 KiB
Bash

#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
# Release to install
app_version=2021.12.8
# 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 libturbojpeg0 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)
# Pyhton 3.9.2 will be shiped with bullseye
py_required_version=3.9.2
#=================================================
# 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"
}
# Compare version in arguments
myynh_version_compare () {
# myynh_version_compare A B
# 0 -> A = B
# 1 -> A > B
# 2 -> A < B
if [[ $1 == $2 ]]
then
echo 0; return
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
echo 1; return
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
echo 2; return
fi
done
echo 1; return
}
# 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
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${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 $python) -le 1 ]
then
# APT >= Required
ynh_print_info --message="Using provided python3..."
py_app_version="python3"
else
# Either python already built or to build
if [ $(myynh_version_compare $py_built_version $python) -le 1 ]
then
# Built >= Required
ynh_print_info --message="Using already used python3 built version..."
py_app_version="/usr/local/bin/python${py_built_version:0:3}"
else
ynh_print_info --message="Installing additional dependencies to build python..."
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}"
# APT < Minimal & Actual < Minimal => Build & install Python into /usr/local/bin
ynh_print_info --message="Building python (may take a while)..."
# Store current direcotry
local MY_DIR=$(pwd)
# Create a temp direcotry
tmpdir="$(mktemp --directory)"
cd "$tmpdir"
# Download
wget --output-document="Python-$python.tar.xz" \
"https://www.python.org/ftp/python/$python/Python-$python.tar.xz" 2>&1
# Extract
tar xf "Python-$python.tar.xz"
# Install
cd "Python-$python"
./configure --enable-optimizations
ynh_exec_warn_less make -j4
ynh_exec_warn_less make altinstall
# Go back to working directory
cd "$MY_DIR"
# Clean
ynh_secure_remove "$tmpdir"
# Set version
py_app_version="/usr/local/bin/python${python:0:3}"
fi
fi
# Save python version in settings
ynh_app_setting_set --app=$app --key=python --value="$python"
}
# Install/Upgrade Homeassistant in virtual environement
myynh_install_homeassistant () {
ynh_exec_as $app -H -s /bin/bash -c " \
echo 'create the virtual environment' \
&& $py_app_version -m venv "$final_path" \
&& echo 'activate the virtual environment' \
&& 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==$app_version \
"
}
# 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"
}