Files
shell_sequencer/seqs/homeassistant.sh

138 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Homeassistant core
readonly toolName="homeassistant"
readonly toolService="home-assistant"
# Already defined by sequencer.sh, but may be overwritten
#readonly seq_configName="${sq_scriptName:?}.cfg"
#readonly seq_configTemplate="${seq_origin:?}/${sq_configName:?}.example"
sq_aptOpt=
sq_config=0
seq_config() {
## or to use sequencer api with global config file:
if ! initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then
# End if no configuration file exists
dry || return 1
fi
## Apt cmdline option to suppress user interaction
interactive || sq_aptOpt="-y"
## Return of non zero value will abort the sequence
return 0
}
step_4_info() { echo 'Create home-assistant venv'; }
step_4_alias() { echo 'venv'; }
step_4_options() { echo '[PYTHON BINARY]'; }
step_4() {
shift
local pyBin="python3"
[ -d "${sc_haVenv}" ] && die "${toolName} venv already exists"
[ -f "${1:-}" ] && pyBin="${1}"
exe mkdir -p "${sc_haVenv:?}"
exe chown "${sc_haUser}:" "${sc_haVenv}"
runAsHa createVenv "${pyBin}" "${sc_haVenv}"
}
# createVenv <PYTHON BINARY> <VENV LOCATION>
createVenv() {
cd "${2:?}" && "${1:?}" -m venv .
}
step_20_info() { echo "Upgrade pip"; }
step_20_alias() { echo 'upgradepip'; }
step_20() {
runAsHa updatePy "${sc_haVenv}" pip
endReturn "Could not upgrade pip"
}
step_21_info() { echo "Upgrade $toolName core installation"; }
step_21_alias() { echo "upgrade"; }
step_21() {
exe service "${toolService}" stop
endReturn "Could not stop ${toolName}"
runAsHa updatePy "${sc_haVenv}" homeassistant "${sc_haDeps[@]}"
endReturn "Could not upgrade ${toolName}"
exe service "${toolService}" start
}
# updateHa <HA VENV ROOT> [OPTIONAL PIP PACKAGE ...]
updatePy() {
source "${1:?}/bin/activate"
shift
pip3 install --upgrade "$@"
}
step_30_info() { echo 'Upgrade venv to new python version'; }
step_30_options() { echo '<PYTHON VERSION>'; }
step_30_alias() { echo 'upgradevenv'; }
step_30() {
shift
local pyVer="${1:-}"
local buDate="$(date +%Y%m%d-%H%M%S)"
endIfEmpty pyVer "No python version given"
exe service "${toolService}" stop
endReturn "Could not stop ${toolName}"
exe mv "${sc_haVenv}" "${sc_haVenv}_bu_${buDate}" 2>/dev/null || true
local pyBin="$(command -v "python${pyVer}")"
step venv "${pyBin}"
}
step_31_info() { echo "Install ${toolName} in venv"; }
step_31() {
runAsHa installHa "${sc_haVenv}" "${sc_haDeps[@]}"
}
installHa() {
cd "${1:?}" && source "bin/activate" || return 1
shift
python3 -m pip install wheel
pip3 install homeassistant "$@"
}
step_32_info() { echo "Start $toolName in new venv"; }
step_32() {
exe service "${toolService}" start
}
# runAsHa <FUNCTION> [ARGS...]
# Run given function as home-assistant user inside the virtual environment
runAsHa() {
local func="${1:?}"
shift
exe sudo -Hu "${sc_haUser}" bash -c "$(declare -f "${func}"); ${func} "'"$@"' "" "$@"
}
step_50_alias() { echo 'notes'; }
step_50() {
color green
cat <<NOTES_END
# Systemd service example
[Unit]
Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=homeassistant
WorkingDirectory=/home/homeassistant/.homeassistant
ExecStart=${sc_haVenv:-"/srv/homeassistant"}/bin/hass -c "/home/homeassistant/.homeassistant"
RestartForceExitStatus=100
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
NOTES_END
}
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh