Skip to content

Commit

Permalink
Move to helpers 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain committed Sep 16, 2024
1 parent 2061938 commit 358878e
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 257 deletions.
3 changes: 2 additions & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ code = "https://github.com/home-assistant/core"
cpe = "cpe:2.3:a:home-assistant:home-assistant"

[integration]
yunohost = ">= 11.2"
yunohost = ">= 11.2.18"
helpers_version = "2.1"
architectures = ["amd64", "arm64"]
multi_instance = false
ldap = true
Expand Down
99 changes: 49 additions & 50 deletions scripts/_common.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash

#=================================================
# COMMON VARIABLES
# COMMON VARIABLES AND CUSTOM HELPERS
#=================================================

# App version
## yq is not a dependencie of yunohost package so tomlq command is not available
## (see https://github.com/YunoHost/yunohost/blob/dev/debian/control)
app_version=$(cat ../manifest.toml 2>/dev/null \
| grep 'version = ' | cut -d '=' -f 2 \
| grep '^version = ' | cut -d '=' -f 2 \
| cut -d '~' -f 1 | tr -d ' "') #2024.2.5

# Python required version
Expand All @@ -24,10 +24,6 @@ py_required_version=$(curl -Ls https://www.python.org/ftp/python/ \
# Fail2ban
failregex="^%(__prefix_line)s.*\[homeassistant.components.http.ban\] Login attempt or request with invalid authentication from.* \(<HOST>\).* Requested URL: ./auth/.*"

#=================================================
# PERSONAL HELPERS
#=================================================

# Check if directory/file already exists (path in argument)
myynh_check_path () {
[ -z "$1" ] && ynh_die "No argument supplied"
Expand All @@ -50,13 +46,13 @@ myynh_install_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
Expand All @@ -65,107 +61,110 @@ myynh_install_python () {
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..."
ynh_print_info "Using provided python3..."

py_app_version="python3"

else
# Either python already built or to build
# 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..."
ynh_print_info "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
ynh_print_info "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
ynh_hide_warnings make -j4
ynh_hide_warnings make altinstall

# Go back to working directory
cd "$MY_DIR"

# Clean
ynh_secure_remove "$tmpdir"
ynh_safe_rm "$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"
# Save python version in settings

ynh_app_setting_set --key=python --value="$python"
}

# Install/Upgrade Homeassistant in virtual environement
myynh_install_homeassistant () {
# Requirements
pip_required=$(curl -Ls https://pypi.org/pypi/$app/$app_version/json | jq -r '.info.requires_dist[]' | grep 'pip') #pip (<23.1,>=21.0)

# Create the virtual environment
ynh_exec_as $app $py_app_version -m venv --without-pip "$install_dir"
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
ynh_exec_as_app "$install_dir/bin/python3" -m ensurepip

# install last version of pip
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$pip_required"
ynh_hide_warnings ynh_exec_as_app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$pip_required"

# install last version of wheel
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade wheel
ynh_hide_warnings ynh_exec_as_app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade wheel

# install last version of setuptools
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade setuptools
ynh_hide_warnings ynh_exec_as_app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade setuptools

# install last version of mysqlclient
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade mysqlclient
ynh_hide_warnings ynh_exec_as_app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade mysqlclient

# install Home Assistant
ynh_exec_warn_less ynh_exec_as $app "$install_dir/bin/pip3" --cache-dir "$data_dir/.cache" install --upgrade "$app==$app_version"
ynh_hide_warnings 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 () {

# Remove old python links before recreating them
if [ -e "$install_dir/bin/" ]
then
find "$install_dir/bin/" -type l -name 'python*' \
-exec bash -c 'rm --force "$1"' _ {} \;
fi

# Remove old python directories before recreating them
if [ -e "$install_dir/lib/" ]
then
Expand All @@ -179,19 +178,19 @@ myynh_upgrade_venv_directory () {
-not -path "*/python${py_required_version%.*}" \
-exec bash -c 'rm --force --recursive "$1"' _ {} \;
fi

# Upgrade the virtual environment directory
ynh_exec_as $app $py_app_version -m venv --upgrade "$install_dir"
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 u=rwX,g=rX,o= "$install_dir"
chmod -R o-rwx "$install_dir"

chown -R $app: "$data_dir"
chmod 750 "$data_dir"
chmod u=rwX,g=rX,o= "$data_dir"
chmod -R o-rwx "$data_dir"
[ -e "$data_dir/bin/" ] && chmod -R +x "$data_dir/bin/"

Expand Down
36 changes: 13 additions & 23 deletions scripts/backup
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,65 @@
# yunohost backup create -n "homeassistant-test" --apps homeassistant
# yunohost backup delete homeassistant-test

#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================

source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers

#=================================================
# DECLARE DATA AND CONF FILES TO BACKUP
#=================================================

ynh_print_info --message="Declaring files to be backed up..."
ynh_print_info "Declaring files to be backed up..."

#=================================================
# BACKUP THE APP MAIN DIR
#=================================================

ynh_backup --src_path="$install_dir"
ynh_backup "$install_dir"

#=================================================
# BACKUP THE DATA DIR
#=================================================

ynh_backup --src_path="$data_dir" --is_big
ynh_backup "$data_dir"

#=================================================
# BACKUP THE NGINX CONFIGURATION
#=================================================

ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf"

#=================================================
# BACKUP FAIL2BAN CONFIGURATION
#=================================================

ynh_backup --src_path="/etc/fail2ban/jail.d/$app.conf"
ynh_backup --src_path="/etc/fail2ban/filter.d/$app.conf"
ynh_backup "/etc/fail2ban/jail.d/$app.conf"
ynh_backup "/etc/fail2ban/filter.d/$app.conf"

#=================================================
# SPECIFIC BACKUP
#=================================================
# BACKUP LOGROTATE
#=================================================

ynh_backup --src_path="/etc/logrotate.d/$app"
ynh_backup "/etc/logrotate.d/$app"

#=================================================
# BACKUP SYSTEMD
#=================================================

ynh_backup --src_path="/etc/systemd/system/$app.service"
ynh_backup "/etc/systemd/system/$app.service"

#=================================================
# BACKUP VARIOUS FILES
#=================================================

ynh_backup --src_path="/etc/sudoers.d/$app"
ynh_backup --src_path="$(dirname "$log_file")"
ynh_backup "/etc/sudoers.d/$app"
ynh_backup "$(dirname "$log_file")"

#=================================================
# BACKUP THE MYSQL DATABASE
#=================================================
ynh_print_info --message="Backing up the MySQL database..."
ynh_mysql_dump_db --database=$db_name > db.sql
ynh_print_info "Backing up the MySQL database..."
ynh_mysql_dump_db > db.sql

#=================================================
# END OF SCRIPT
#=================================================

ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)."
ynh_print_info "Backup script completed for $app. (YunoHost will then actually copy those files to the archive)."
Loading

0 comments on commit 358878e

Please sign in to comment.