From a7b14c33bda49b44997b3b57fb04e2cfa2c2aed1 Mon Sep 17 00:00:00 2001 From: Alexander Tratsevskiy Date: Sat, 29 Apr 2023 11:27:55 +0300 Subject: [PATCH] =?UTF-8?q?homeassistant:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homeassistant/init.homeassistant | 2 +- scripts/homeassistant/step/1-install.sh | 68 ++++----- scripts/homeassistant/step/3-zigbee2mqtt.sh | 132 +++++++++--------- .../homeassistant/step/4-hass-configurator.sh | 101 +++++++------- scripts/homeassistant/step/5-config.sh | 2 +- 5 files changed, 156 insertions(+), 149 deletions(-) diff --git a/profiles/templates/homeassistant/homeassistant/init.homeassistant b/profiles/templates/homeassistant/homeassistant/init.homeassistant index 34de5cf..a3b8dde 100644 --- a/profiles/templates/homeassistant/homeassistant/init.homeassistant +++ b/profiles/templates/homeassistant/homeassistant/init.homeassistant @@ -12,7 +12,7 @@ serverlog="/var/log/homeassistant/server.log" warningslog="/var/log/homeassistant/warnings.log" retry="20" -command=/var/calculate/www/${RC_SVCNAME}/${RC_SVCNAME}-live/.venv/bin/hass +command=/var/calculate/www/${RC_SVCNAME}/${RC_SVCNAME}-live/bin/hass directory=/var/calculate/www/${RC_SVCNAME}/${RC_SVCNAME}-live start_stop_daemon_args="--user $user --stdout $stdoutlog --stderr $serverlog --wait 10" diff --git a/scripts/homeassistant/step/1-install.sh b/scripts/homeassistant/step/1-install.sh index 71adc2d..3482279 100644 --- a/scripts/homeassistant/step/1-install.sh +++ b/scripts/homeassistant/step/1-install.sh @@ -15,59 +15,61 @@ configure() { local live_dir="$home_dir/homeassistant-live" local live_ver="$(get_live_ver $live_dir)" + # отобразим наличие обновления и выйдем if [[ $action == 'check' ]]; then if [[ $live_ver != $last_ver ]]; then einfo "homeassistant: $last_ver update available, $live_ver installed" - eval $__result=1 # наличие обновления + eval $__result=1 fi return 0 fi + # выйдем если нет обновления + [[ $live_ver == $last_ver ]] && return + + # подготовим пути if [[ ! -e $home_dir ]]; then - mkdir -p $home_dir/versions + mkdir -p ${home_dir}/versions chmod 700 $home_dir chown -R homeassistant: $home_dir fi - touch ${log_dir}/homeassistant.log chown homeassistant: ${log_dir}/homeassistant.log - if [[ $live_ver != $last_ver ]]; then - if [[ $live_ver == '' ]]; then - echo Install Home Assistant - else - echo Update Home Assistant - fi + if [[ $live_ver == '' ]]; then + echo Install Home Assistant + else + echo Update Home Assistant + fi - su - homeassistant -s /bin/bash -c "$(cat <<- EOF - set -ueo pipefail - export PATH="/lib/rc/bin:$PATH" + su - homeassistant -s /bin/bash -c "$(cat <<- EOF + set -ueo pipefail + export PATH="/lib/rc/bin:$PATH" - ebegin 'Create a virtualenv' - test -e ${work_dir} && rm -rf ${work_dir} - python -m venv ${work_dir}/.venv - source ${work_dir}/.venv/bin/activate - eend + ebegin 'Create a virtualenv' + test -e ${work_dir} && rm -rf ${work_dir} + python -m venv ${work_dir} + source ${work_dir}/bin/activate + eend - ebegin 'Install all Python dependencies' - python -m pip install wheel &>>${log_dir}/homeassistant.log - eend + ebegin 'Install all Python dependencies' + python -m pip install wheel &>>${log_dir}/homeassistant.log + eend - ebegin "Install Home Assistant ${last_ver}" - pip install homeassistant==${last_ver} &>>${log_dir}/homeassistant.log - eend + ebegin "Install Home Assistant ${last_ver}" + pip install homeassistant==${last_ver} &>>${log_dir}/homeassistant.log + eend - ebegin 'Install PostgreSQL dependencies' - pip install psycopg2 &>>${log_dir}/homeassistant.log - eend + ebegin 'Install PostgreSQL dependencies' + pip install psycopg2 &>>${log_dir}/homeassistant.log + eend - ln -snf versions/homeassistant-$last_ver $live_dir - EOF - )" + ln -snf versions/homeassistant-$last_ver $live_dir + EOF + )" - if [[ $live_ver != '' ]]; then - rc-service -s homeassistant restart - echo - fi + if [[ $live_ver != '' ]]; then + rc-service -s homeassistant restart + echo fi } diff --git a/scripts/homeassistant/step/3-zigbee2mqtt.sh b/scripts/homeassistant/step/3-zigbee2mqtt.sh index cc66701..d1c10e7 100644 --- a/scripts/homeassistant/step/3-zigbee2mqtt.sh +++ b/scripts/homeassistant/step/3-zigbee2mqtt.sh @@ -17,10 +17,11 @@ configure() { local live_ver="$(get_live_ver $live_dir)" local conf_dir="/var/calculate/zigbee2mqtt" + # отобразим наличие обновления и выйдем if [[ $action == 'check' ]]; then if [[ $live_ver != $last_ver ]]; then einfo "zigbee2mqtt: $last_ver update available, $live_ver installed" - eval $__result=1 # наличие обновления + eval $__result=1 fi return 0 fi @@ -36,8 +37,12 @@ configure() { fi fi + # выйдем если нет обновления + [[ $live_ver == $last_ver ]] && return + + # подготовим пути if [[ ! -e $home_dir ]]; then - mkdir -p $home_dir/versions + mkdir -p ${home_dir}/versions chmod 700 $home_dir chown -R zigbee2mqtt: $home_dir fi @@ -46,75 +51,73 @@ configure() { chmod 700 $conf_dir chown -R zigbee2mqtt: $conf_dir fi - touch ${log_dir}/zigbee2mqtt.log chown zigbee2mqtt: ${log_dir}/zigbee2mqtt.log - if [[ $live_ver != $last_ver ]]; then - if [[ $live_ver == '' ]]; then - echo Install Zigbee2MQTT - else - echo Update Zigbee2MQTT - fi + if [[ $live_ver == '' ]]; then + echo Install Zigbee2MQTT + else + echo Update Zigbee2MQTT + fi - su - zigbee2mqtt -s /bin/bash -c "$(cat <<- EOF - set -ueo pipefail - export PATH="/lib/rc/bin:$PATH" + su - zigbee2mqtt -s /bin/bash -c "$(cat <<- EOF + set -ueo pipefail + export PATH="/lib/rc/bin:$PATH" - ebegin Download zigbee2mqtt ${last_ver} - test -e ${work_dir} && rm -rf ${work_dir} # удалим если было прервано - wget -q https://github.com/Koenkk/zigbee2mqtt/archive/refs/tags/${last_ver}.zip \ - -O zigbee2mqtt-${last_ver}.zip - eend + ebegin Download zigbee2mqtt ${last_ver} + test -e ${work_dir} && rm -rf ${work_dir} # удалим если было прервано + wget -q https://github.com/Koenkk/zigbee2mqtt/archive/refs/tags/${last_ver}.zip \ + -O zigbee2mqtt-${last_ver}.zip + eend - ebegin 'Extract the archive' - unzip -q -d versions zigbee2mqtt-${last_ver}.zip - rm zigbee2mqtt-${last_ver}.zip - eend + ebegin 'Extract the archive' + unzip -q -d versions zigbee2mqtt-${last_ver}.zip + rm zigbee2mqtt-${last_ver}.zip + eend - # вынесем настройки - if [[ -z "$(ls -A $conf_dir)" ]]; then - mv versions/zigbee2mqtt-${last_ver}/data/* ${conf_dir} - fi - rm -rf versions/zigbee2mqtt-${last_ver}/data - ln -s ${conf_dir} versions/zigbee2mqtt-${last_ver}/data - - ebegin 'Create a virtualenv' - python -m venv ${work_dir}/.venv - eend - - ebegin 'Activate environment' - source ${work_dir}/.venv/bin/activate - eend - - ebegin 'Upgrade pip, wheel and setuptools' - pip install --upgrade pip wheel setuptools &>>${log_dir}/zigbee2mqtt.log - eend - - ebegin 'Install Node environment' - pip install nodeenv &>>${log_dir}/zigbee2mqtt.log - eend - - ebegin 'Init Node environment ${ini[zigbee2mqtt.nodeenv]}' - nodeenv -p -n ${ini[zigbee2mqtt.nodeenv]} &>>${log_dir}/zigbee2mqtt.log - eend - - einfo 'Install dependencies' - cd ${work_dir} - npm ci &>>${log_dir}/zigbee2mqtt.log - cd + # вынесем настройки + if [[ -z "$(ls -A $conf_dir)" ]]; then + mv versions/zigbee2mqtt-${last_ver}/data/* ${conf_dir} + fi + rm -rf versions/zigbee2mqtt-${last_ver}/data + ln -s ${conf_dir} versions/zigbee2mqtt-${last_ver}/data + + ebegin 'Create a virtualenv' + python -m venv ${work_dir}/.venv + eend + + ebegin 'Activate environment' + source ${work_dir}/.venv/bin/activate + eend + + ebegin 'Upgrade pip, wheel and setuptools' + pip install --upgrade pip wheel setuptools &>>${log_dir}/zigbee2mqtt.log + eend + + ebegin 'Install Node environment' + pip install nodeenv &>>${log_dir}/zigbee2mqtt.log + eend + + ebegin 'Init Node environment ${ini[zigbee2mqtt.nodeenv]}' + nodeenv -p -n ${ini[zigbee2mqtt.nodeenv]} &>>${log_dir}/zigbee2mqtt.log + eend + + einfo 'Install dependencies' + cd ${work_dir} + npm ci &>>${log_dir}/zigbee2mqtt.log + cd - ln -snf versions/zigbee2mqtt-${last_ver} $live_dir - EOF - )" + ln -snf versions/zigbee2mqtt-${last_ver} $live_dir + EOF + )" - if [[ $live_ver != '' ]]; then - rc-service -s zigbee2mqtt restart - echo - else - ebegin 'Setup zigbee2mqtt' - mv ${conf_dir}/configuration.yaml ${conf_dir}/configuration.yaml.old - cat > ${conf_dir}/configuration.yaml << EOF + if [[ $live_ver != '' ]]; then + rc-service -s zigbee2mqtt restart + echo + else + ebegin 'Setup zigbee2mqtt' + mv ${conf_dir}/configuration.yaml ${conf_dir}/configuration.yaml.old + cat > ${conf_dir}/configuration.yaml << EOF # Home Assistant integration (MQTT discovery) homeassistant: true @@ -139,8 +142,7 @@ frontend: port: 8080 host: 127.0.0.1 EOF - chown zigbee2mqtt: ${conf_dir}/configuration.yaml - eend - fi + chown zigbee2mqtt: ${conf_dir}/configuration.yaml + eend fi } diff --git a/scripts/homeassistant/step/4-hass-configurator.sh b/scripts/homeassistant/step/4-hass-configurator.sh index 7a7c32f..4fd4adf 100644 --- a/scripts/homeassistant/step/4-hass-configurator.sh +++ b/scripts/homeassistant/step/4-hass-configurator.sh @@ -16,16 +16,21 @@ configure() { local live_ver="$(get_live_ver $live_dir)" local conf_dir="/var/calculate/hass-configurator" + # отобразим наличие обновления и выйдем if [[ $action == 'check' ]]; then if [[ $live_ver != $last_ver ]]; then einfo "hass-configurator: $last_ver update available, $live_ver installed" - eval $__result=1 # наличие обновления + eval $__result=1 fi return 0 fi + # выйдем если нет обновления + [[ $live_ver == $last_ver ]] && return + + # подготовим пути if [[ ! -e $home_dir ]]; then - mkdir -p $home_dir/versions + mkdir -p ${home_dir}/versions chmod 700 $home_dir chown -R hass-configurator: $home_dir fi @@ -34,58 +39,57 @@ configure() { chmod 700 $conf_dir chown -R hass-configurator: $conf_dir fi - touch ${log_dir}/hass-configurator.log chown hass-configurator: ${log_dir}/hass-configurator.log - if [[ $live_ver != $last_ver ]]; then - if [[ $live_ver == '' ]]; then - echo Install HASS Configurator - else - echo Update Configurator - fi + if [[ $live_ver == '' ]]; then + echo Install HASS Configurator + else + echo Update Configurator + fi - su - hass-configurator -s /bin/bash -c "$(cat <<- EOF - set -ueo pipefail - export PATH="/lib/rc/bin:$PATH" + # выполним настройки от пользователя hass-configurator + su - hass-configurator -s /bin/bash -c "$(cat <<- EOF + set -ueo pipefail + export PATH="/lib/rc/bin:$PATH" - ebegin Download hass-configurator ${last_ver} - test -e ${work_dir} && rm -rf ${work_dir} # удалим если было прервано - wget https://github.com/danielperna84/hass-configurator/archive/refs/tags/${last_ver}.zip \ - -O hass-configurator-${last_ver}.zip &>>${log_dir}/hass-configurator.log - eend + ebegin Download hass-configurator ${last_ver} + test -e ${work_dir} && rm -rf ${work_dir} # удалим если было прервано + wget https://github.com/danielperna84/hass-configurator/archive/refs/tags/${last_ver}.zip \ + -O hass-configurator-${last_ver}.zip &>>${log_dir}/hass-configurator.log + eend - ebegin 'Extract the archive' - unzip -q -d versions hass-configurator-${last_ver}.zip - rm hass-configurator-${last_ver}.zip - eend - - ebegin 'Create a virtualenv' - python -m venv ${work_dir}/.venv - eend - - ebegin 'Activate environment' - source ${work_dir}/.venv/bin/activate - eend - - ebegin 'Upgrade pip and wheel' - pip install --upgrade pip wheel &>>${log_dir}/hass-configurator.log - eend - - ebegin 'Install HASS Configurator' - pip install hass-configurator &>>${log_dir}/hass-configurator.log - eend + ebegin 'Extract the archive' + unzip -q -d versions hass-configurator-${last_ver}.zip + rm hass-configurator-${last_ver}.zip + eend + + ebegin 'Create a virtualenv' + python -m venv ${work_dir}/.venv + eend + + ebegin 'Activate environment' + source ${work_dir}/.venv/bin/activate + eend + + ebegin 'Upgrade pip and wheel' + pip install --upgrade pip wheel &>>${log_dir}/hass-configurator.log + eend + + ebegin 'Install HASS Configurator' + pip install hass-configurator &>>${log_dir}/hass-configurator.log + eend - ln -snf versions/hass-configurator-${last_ver} $live_dir - EOF - )" + ln -snf versions/hass-configurator-${last_ver} $live_dir + EOF + )" - if [[ $live_ver != '' ]]; then - rc-service -s hass-configurator restart - echo - else - ebegin 'Setup HASS Configurator' - cat > $conf_dir/settings.conf << EOF + if [[ $live_ver != '' ]]; then + rc-service -s hass-configurator restart + echo + else + ebegin 'Setup HASS Configurator' + cat > $conf_dir/settings.conf << EOF { "LISTENIP": "127.0.0.1", "PORT": 3218, @@ -113,9 +117,8 @@ configure() { "NOTIFY_SERVICE": "persistent_notification.create" } EOF - chown hass-configurator: $conf_dir/settings.conf - eend - fi + chown hass-configurator: $conf_dir/settings.conf + eend fi } diff --git a/scripts/homeassistant/step/5-config.sh b/scripts/homeassistant/step/5-config.sh index d05c7df..ab20352 100644 --- a/scripts/homeassistant/step/5-config.sh +++ b/scripts/homeassistant/step/5-config.sh @@ -23,7 +23,7 @@ configure() { set -ueo pipefail export PATH="/lib/rc/bin:$PATH" - source ${work_dir}/.venv/bin/activate + source ${work_dir}/bin/activate hass --config ${conf_dir} &>>${log_dir}/config.log & id_hass=\$!