From f5413da537772d8adee88a28cd21a80a54fa18a8 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Sun, 15 Jan 2023 17:50:05 +0100 Subject: [PATCH] homeassistant (core) - first version to upgrade/change venv, pip and homeassistant itself --- seqs/homeassistant.cfg.example | 6 ++ seqs/homeassistant.sh | 137 +++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 seqs/homeassistant.cfg.example create mode 100755 seqs/homeassistant.sh diff --git a/seqs/homeassistant.cfg.example b/seqs/homeassistant.cfg.example new file mode 100644 index 0000000..1d1babe --- /dev/null +++ b/seqs/homeassistant.cfg.example @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +readonly sc_haUser="homeassistant" +readonly sc_haVenv="/srv/homeassistant" +# Additional pip packages (homeassistant already included) +readonly sc_haDeps=() diff --git a/seqs/homeassistant.sh b/seqs/homeassistant.sh new file mode 100755 index 0000000..2099d0c --- /dev/null +++ b/seqs/homeassistant.sh @@ -0,0 +1,137 @@ +#!/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 +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 [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 ''; } +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 [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 <