1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/homeassistant_ynh.git synced 2024-09-03 19:26:16 +02:00

Refractor and build pyhton if needed

This commit is contained in:
ewilly 2020-11-14 14:10:51 +01:00
parent b07405d0f8
commit 16b0732b8c
7 changed files with 144 additions and 69 deletions

View file

@ -4,20 +4,22 @@
app="homeassistant" app="homeassistant"
final_path="/opt/yunohost/$app" final_path="/opt/yunohost/$app"
# stop homeassistant systemd service local MY_PYTHON=$(readlink -e "$final_path/bin/python")
#sudo systemctl stop $app@$app.service
# create the virtual environment # create the virtual environment
python3 -m venv "$final_path" $MY_PYTHON -m venv "$final_path"
# activate the virtual environment # activate the virtual environment
. "$final_path/bin/activate" source "$final_path/bin/activate"
# upgrade required python package # install last version of pip
python3 -m pip install --upgrade wheel pip install --upgrade pip
# install last version of wheel
pip install --upgrade wheel
# upgrade homeassistant python package # upgrade homeassistant python package
pip3 install --upgrade $app pip install --upgrade $app
# restart homeassistant systemd service # restart homeassistant systemd service
sudo systemctl restart $app@$app.service sudo systemctl restart $app@$app.service

View file

@ -1,13 +0,0 @@
title: Home
views:
- path: default_view
title: Home
badges:
- entity: sun.sun
cards:
- type: entities
entities:
- sensor.current_version
- sensor.latest_available_version
- switch.upgrade_homeassistant
title: Home Assistant

View file

@ -6,7 +6,7 @@
"en": "Home automation platform", "en": "Home automation platform",
"fr": "Plateforme domotique" "fr": "Plateforme domotique"
}, },
"version": "1.1.0", "version": "0.117.5~ynh1",
"url": "https://github.com/home-assistant/home-assistant", "url": "https://github.com/home-assistant/home-assistant",
"license": "Apache-2.0", "license": "Apache-2.0",
"maintainer": { "maintainer": {

View file

@ -3,22 +3,14 @@
# #
# Release to install # Release to install
VERSION=0.114.4 VERSION=0.117.6
# Package dependencies # Package dependencies
PKG_DEPENDENCIES="python3 python3-venv python3-pip build-essential libssl-dev libffi-dev python3-dev" PKG_DEPENDENCIES="python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5"
# Check if directory/file already exists (path in argument) # Requirements (Major.Minor.Patch)
myynh_check_path () { # PY_VERSION=$(curl -s "https://www.python.org/ftp/python/" | grep ">3.8" | tail -n1 | cut -d '/' -f 2 | cut -d '>' -f 2)
[ -z "$1" ] && ynh_die "No argument supplied" PY_VERSION=3.8.6
[ ! -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"
}
# Execute a command as another user # Execute a command as another user
# usage: exec_as USER COMMAND [ARG ...] # usage: exec_as USER COMMAND [ARG ...]
@ -32,3 +24,115 @@ exec_as() {
sudo -u "$USER" "$@" sudo -u "$USER" "$@"
fi fi
} }
# 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
}
# Package dependencies
myynh_install_dependencies () {
# Install main dependencies from apt
ynh_script_progression --message="Installing dependencies..."
ynh_install_app_dependencies "${PKG_DEPENDENCIES}"
# 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${PY_VERSION:0:3}" ]; then
local PY_BUILT_VERSION=$(/usr/local/bin/python${PY_VERSION:0:3} --version \
| cut -d ' ' -f 2)
else
local PY_BUILT_VERSION=0
fi
# Compare version
if [ $(myynh_version_compare $PY_APT_VERSION $PY_VERSION) -le 1 ]; then
# APT >= Required
ynh_script_progression --message="Using provided python3..."
MY_PYTHON="python3"
else
# Either python already built or to build
ynh_script_progression --message="Installing additional dependencies..."
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}"
if [ $(myynh_version_compare $PY_BUILT_VERSION $PY_VERSION) -le 1 ]; then
# Built >= Required
ynh_script_progression --message="Using already used python3 built version..."
MY_PYTHON="/usr/local/bin/python${PY_BUILT_VERSION:0:3}"
else
# APT < Minimal & Actual < Minimal => Build & install Python into /usr/local/bin
ynh_script_progression --message="Building python (may take a while)..."
# Download
wget -O "/tmp/Python-$PY_VERSION.tar.xz" "https://www.python.org/ftp/python/$PY_VERSION/Python-$PY_VERSION.tar.xz"
# Extract
cd /tmp
tar xf "Python-$PY_VERSION.tar.xz"
# Install
cd "Python-$PY_VERSION"
./configure --enable-optimizations
make -j4
make altinstall
# Clean
cd ..
ynh_secure_remove "Python-$PY_VERSION"
ynh_secure_remove "Python-$PY_VERSION.tar.xz"
# Set version
MY_PYTHON="/usr/local/bin/python${PY_VERSION:0:3}"
fi
fi
}
# Install/Upgrade Homeassistant in virtual environement
myynh_install_homeassistant () {
exec_as $app -H -s /bin/bash -c " \
echo 'create the virtual environment' \
&& $MY_PYTHON -m venv "$final_path" \
&& echo 'activate the virtual environment' \
&& source "$final_path/bin/activate" \
&& echo 'install last version of pip' \
&& pip install --upgrade pip \
&& echo 'install last version of wheel' \
&& pip install --upgrade wheel \
&& echo 'install Home Assistant' \
&& pip install --upgrade $app==$VERSION \
"
}
# 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"
}

View file

@ -25,10 +25,6 @@ path_url=$(ynh_normalize_url_path "/")
ynh_webpath_available "$domain" "$path_url" || ynh_die "$domain/$path_url is not available, please use an other domain." ynh_webpath_available "$domain" "$path_url" || ynh_die "$domain/$path_url is not available, please use an other domain."
ynh_webpath_register $app "$domain" "$path_url" ynh_webpath_register $app "$domain" "$path_url"
# add required packages
ynh_script_progression --message="Installing dependencies..."
ynh_install_app_dependencies "$PKG_DEPENDENCIES"
# save app settings # save app settings
ynh_script_progression --message="Storing installation settings..." ynh_script_progression --message="Storing installation settings..."
ynh_app_setting_set $app domain "$domain" ynh_app_setting_set $app domain "$domain"
@ -52,36 +48,29 @@ chown $app: "$final_path"
myynh_create_dir "$data_path" myynh_create_dir "$data_path"
chown -R $app: "$home_path" chown -R $app: "$home_path"
# build (if needed) & install Pyhton
myynh_install_dependencies
# installation in a virtual environment # installation in a virtual environment
ynh_script_progression --message="Installing Home Assistant in a virtual environment..." ynh_script_progression --message="Installing Home Assistant in a virtual environment..."
exec_as $app -H -s /bin/bash -c " \ myynh_install_homeassistant
echo 'create the virtual environment' \
&& python3 -m venv "$final_path" \
&& echo 'activate the virtual environment' \
&& source "$final_path/bin/activate" \
&& echo 'install a required python package' \
&& python3 -m pip install --upgrade wheel \
&& echo 'install Home Assistant' \
&& pip3 install --upgrade $app==$VERSION \
"
# set default configuration files # set default configuration files and move all homeassistant_conf_files
ynh_script_progression --message="Configuring the installation..." ynh_script_progression --message="Configuring the installation..."
ynh_replace_string "__PORT__" "$port" "../conf/homeassistant_conf_files/configuration.yaml" ynh_replace_string "__PORT__" "$port" "../conf/homeassistant_conf_files/configuration.yaml"
chmod -R +x "../conf/homeassistant_conf_files/bin/"
## move all homeassistant_conf_files
cp -r "../conf/homeassistant_conf_files/." "$data_path/" cp -r "../conf/homeassistant_conf_files/." "$data_path/"
chown -R $app: "$data_path" chown -R $app: "$data_path"
chmod -R +x "$data_path/bin/"
# setup up systemd service # setup up systemd service
ynh_script_progression --message="Adding the dedicated service..." ynh_script_progression --message="Adding the dedicated service..."
ynh_add_systemd_config --service="$app@$app" ynh_add_systemd_config --service="$app@$app"
## add service in admin panel ## add service in admin panel
yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port
# start systemd service # start systemd service
ynh_script_progression --message="Starting the Home Assistant server..." ynh_script_progression --message="Starting the Home Assistant server..."
ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=1000 ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600
# remove --verbose from service # remove --verbose from service
ynh_replace_string " --verbose" "" "/etc/systemd/system/$app@$app.service" ynh_replace_string " --verbose" "" "/etc/systemd/system/$app@$app.service"
systemctl daemon-reload systemctl daemon-reload

View file

@ -28,7 +28,7 @@ path_url=$(ynh_normalize_url_path "/")
ynh_webpath_available $domain $path_url || ynh_die "$domain/$path_url is not available, please use an other domain." ynh_webpath_available $domain $path_url || ynh_die "$domain/$path_url is not available, please use an other domain."
# add required packages # add required packages
ynh_install_app_dependencies "$PKG_DEPENDENCIES" myynh_install_dependencies
# restore dedicated system user # restore dedicated system user
ynh_system_user_create --username="$app" ynh_system_user_create --username="$app"
@ -51,7 +51,7 @@ chmod -R +x "$home_path/.homeassistant/bin"
ynh_exec_fully_quiet yunohost firewall allow TCP $port ynh_exec_fully_quiet yunohost firewall allow TCP $port
# add service in admin panel # add service in admin panel
yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port
# start systemd service # start systemd service
ynh_systemd_action --service_name="$app@$app" --action=start ynh_systemd_action --service_name="$app@$app" --action=start

View file

@ -41,9 +41,8 @@ ynh_script_progression --message="Creating dedicated user, rights and folders...
myynh_create_dir "/etc/sudoers.d" myynh_create_dir "/etc/sudoers.d"
cp "../conf/sudoers" "/etc/sudoers.d/$app" cp "../conf/sudoers" "/etc/sudoers.d/$app"
# add required packages # build (if needed) & install Pyhton
ynh_script_progression --message="Installing dependencies..." myynh_install_dependencies
ynh_install_app_dependencies "$PKG_DEPENDENCIES"
# stop systemd service # stop systemd service
ynh_script_progression --message="Stoping service..." ynh_script_progression --message="Stoping service..."
@ -51,16 +50,8 @@ ynh_systemd_action --service_name="$app@$app" --action=stop
# installation in a virtual environment # installation in a virtual environment
ynh_script_progression --message="Installing Home Assistant in a virtual environment..." ynh_script_progression --message="Installing Home Assistant in a virtual environment..."
exec_as $app -H -s /bin/bash -c " \ myynh_install_homeassistant
echo 'create the virtual environment' \
&& python3 -m venv "$final_path" \
&& echo 'activate the virtual environment' \
&& source "$final_path/bin/activate" \
&& echo 'install a required python package' \
&& python3 -m pip install --upgrade wheel \
&& echo 'install Home Assistant' \
&& pip3 install --upgrade $app==$VERSION \
"
# update script in bin # update script in bin
ynh_script_progression --message="Updating yunohost script used by homeassitant..." ynh_script_progression --message="Updating yunohost script used by homeassitant..."
cp -r "../conf/homeassistant_conf_files/bin/." "$data_path/bin/" cp -r "../conf/homeassistant_conf_files/bin/." "$data_path/bin/"
@ -70,10 +61,12 @@ chmod -R +x "$data_path/bin/"
# setup up systemd service # setup up systemd service
ynh_script_progression --message="Adding the dedicated service..." ynh_script_progression --message="Adding the dedicated service..."
ynh_add_systemd_config --service="$app@$app" ynh_add_systemd_config --service="$app@$app"
## add service in admin panel
yunohost service add "$app@$app" --log "$data_path/home-assistant.log" --description "Home Assistant server" --needs_exposed_ports $port
# start systemd service # start systemd service
ynh_script_progression --message="Starting the Home Assistant server..." ynh_script_progression --message="Starting the Home Assistant server..."
ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=1000 ynh_systemd_action --service_name="$app@$app" --action=start --line_match="Home Assistant initialized" --log_path="systemd" --timeout=3600
# remove --verbose from service # remove --verbose from service
ynh_replace_string " --verbose" "" "/etc/systemd/system/$app@$app.service" ynh_replace_string " --verbose" "" "/etc/systemd/system/$app@$app.service"
systemctl daemon-reload systemctl daemon-reload