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

185 lines
5.4 KiB
Bash
Raw Normal View History

2022-01-08 10:04:57 +01:00
#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
# Release to install
2022-03-03 07:24:00 +01:00
app_version=2022.3.0
2022-01-08 10:04:57 +01:00
# Package dependencies
2022-01-11 17:29:55 +01: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"
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
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"
}
2022-02-21 21:33:31 +01:00
myynh_compile_libffi () {
ynh_print_info --message="Building libffi..."
# Download
2022-02-22 08:07:45 +01:00
wget "https://github.com/libffi/libffi/releases/download/v3.3/libffi-3.3.tar.gz" 2>&1
2022-02-21 21:33:31 +01:00
# Extract
tar zxf libffi-3.3.tar.gz
# Install
cd libffi-3.3
2022-02-23 07:01:34 +01:00
ynh_exec_warn_less ./configure
2022-02-22 20:45:29 +01:00
ynh_exec_warn_less make install
2022-02-21 21:33:31 +01:00
ldconfig
2022-02-22 20:45:29 +01:00
#Exit
cd ..
2022-02-21 21:33:31 +01:00
}
2022-01-08 10:04:57 +01:00
# 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
# Check existing built version of python in /usr/local/bin
if [ -e "/usr/local/bin/python${python:0:3}" ]
then
2022-01-08 16:17:29 +01:00
local py_built_version=$(/usr/local/bin/python${python:0:3} --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-01-08 16:17:29 +01:00
py_app_version="/usr/local/bin/python${py_built_version:0:3}"
2022-01-08 10:04:57 +01:00
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}"
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-01-08 16:17:29 +01:00
py_app_version="/usr/local/bin/python${python:0:3}"
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 () {
ynh_exec_as $app -H -s /bin/bash -c " \
echo 'create the virtual environment' \
2022-01-13 21:34:59 +01:00
&& $py_app_version -m venv "$final_path" \
2022-01-08 10:04:57 +01:00
&& 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 \
"
}
2022-01-13 21:34:59 +01:00
# Upgrade the virtual environment directory
myynh_upgrade_venv_directory () {
ynh_exec_as $app -H -s /bin/bash -c " \
echo 'Upgrade the virtual environment directory' \
&& $py_app_version -m venv --upgrade "$final_path" \
"
}
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"
}