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
2023-03-15 20:42:26 +00:00

162 lines
4.5 KiB
Bash

#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
# Release to install
app_version=2023.3.4
# Requirements
py_required_version=3.10.10
pip_required="pip (<23.1,>=21.0)"
#=================================================
# 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
local py_apt_version=$(python3 --version | cut -d ' ' -f 2)
# Usefull variables
local python_major=${python%.*}
# Check existing built version of python in /usr/local/bin
if [ -e "/usr/local/bin/python$python_major" ]
then
local py_built_version=$(/usr/local/bin/python$python_major --version \
| cut -d ' ' -f 2)
else
local py_built_version=0
fi
# Compare version
if $(dpkg --compare-versions $py_apt_version ge $python)
then
# APT >= Required
ynh_print_info --message="Using provided python3..."
py_app_version="python3"
else
# Either python already built or to build
if $(dpkg --compare-versions $py_built_version ge $python)
then
# Built >= Required
ynh_print_info --message="Using already used python3 built version..."
py_app_version="/usr/local/bin/python${py_built_version%.*}"
else
# 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_major"
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 () {
# Create the virtual environment
ynh_exec_as $app $py_app_version -m venv --without-pip "$install_dir"
# Run source in a 'sub shell'
(
# activate the virtual environment
set +o nounset
source "$install_dir/bin/activate"
set -o nounset
# add pip
ynh_exec_as $app "$install_dir/bin/python3" -m ensurepip
# install last version of pip
ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$pip_required"
# install last version of wheel
ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade wheel
# install last version of setuptools
ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade setuptools
# install last version of mysqlclient
ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade mysqlclient
# install Home Assistant
ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$app==$app_version"
)
}
# Upgrade the virtual environment directory
myynh_upgrade_venv_directory () {
ynh_exec_as $app $py_app_version -m venv --upgrade "$install_dir"
}
# Set permissions
myynh_set_permissions () {
chown -R $app: "$install_dir"
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app: "$data_dir"
chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
[ ! -e "$data_dir/bin/" ] || chmod -R +x "$data_dir/bin/"
[ ! -e "$(dirname "$log_file")" ] || chown -R $app: "$(dirname "$log_file")"
[ ! -e "/etc/sudoers.d/$app" ] || chown -R root: "/etc/sudoers.d/$app"
}