From 1bb12283bed53e54f4e0f08e3f0e0adde9a0b391 Mon Sep 17 00:00:00 2001 From: Alexander Tratsevskiy Date: Thu, 30 Mar 2023 23:32:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D1=8B?= =?UTF-8?q?=20homeassistant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homeassistant/0-etc.homeassistant.shadow | 2 +- .../homeassistant/homeassistant/ini.env | 2 +- .../homeassistant/init.homeassistant | 7 +-- .../homeassistant/log/.calculate_directory | 2 +- .../homeassistant/runlevel.homeassistant | 2 +- .../www/homeassistant/.calculate_directory | 2 +- .../homeassistant/homeassistant/x-ini.env | 4 +- .../homeassistant/nginx/homeassistant.conf | 53 +++---------------- .../homeassistant/nginx/link.homeassistant | 1 - .../nginx/ssl/.calculate_directory | 1 - .../postgresql/.calculate_directory | 1 - .../homeassistant/postgresql/0-get_ver | 5 -- .../postgresql/database/.calculate_directory | 1 - .../postgresql/etc/.calculate_directory | 1 - .../homeassistant/postgresql/etc/pg_hba.conf | 10 ---- .../postgresql/etc/pg_ident.conf | 1 - .../postgresql/etc/postgresql.conf | 22 -------- .../homeassistant/postgresql/ini.env | 4 -- .../homeassistant/postgresql/link.database | 1 - .../templates/homeassistant/postgresql/pgpass | 3 -- profiles/templates/homeassistant/world | 5 -- scripts/homeassistant/setup.sh | 4 -- scripts/homeassistant/step/1-install.sh | 8 +-- .../step/{3-config.sh => 2-config.sh} | 20 +++++-- scripts/homeassistant/step/2-postgresql.sh | 33 ------------ 25 files changed, 34 insertions(+), 161 deletions(-) delete mode 100644 profiles/templates/homeassistant/nginx/link.homeassistant delete mode 100644 profiles/templates/homeassistant/nginx/ssl/.calculate_directory delete mode 100644 profiles/templates/homeassistant/postgresql/.calculate_directory delete mode 100644 profiles/templates/homeassistant/postgresql/0-get_ver delete mode 100644 profiles/templates/homeassistant/postgresql/database/.calculate_directory delete mode 100644 profiles/templates/homeassistant/postgresql/etc/.calculate_directory delete mode 100644 profiles/templates/homeassistant/postgresql/etc/pg_hba.conf delete mode 100644 profiles/templates/homeassistant/postgresql/etc/pg_ident.conf delete mode 100644 profiles/templates/homeassistant/postgresql/etc/postgresql.conf delete mode 100644 profiles/templates/homeassistant/postgresql/ini.env delete mode 100644 profiles/templates/homeassistant/postgresql/link.database delete mode 100644 profiles/templates/homeassistant/postgresql/pgpass rename scripts/homeassistant/step/{3-config.sh => 2-config.sh} (53%) delete mode 100755 scripts/homeassistant/step/2-postgresql.sh diff --git a/profiles/templates/homeassistant/homeassistant/0-etc.homeassistant.shadow b/profiles/templates/homeassistant/homeassistant/0-etc.homeassistant.shadow index aa7a872..963c738 100644 --- a/profiles/templates/homeassistant/homeassistant/0-etc.homeassistant.shadow +++ b/profiles/templates/homeassistant/homeassistant/0-etc.homeassistant.shadow @@ -1,2 +1,2 @@ # Calculate append=after path=/etc name=#-cut(2,.)-# grep(/etc/#-cut(2,.)-#,#-cut(1,.)-#)== protected -homeassistant:!:19443:::::: +homeassistant:!:19446:::::: diff --git a/profiles/templates/homeassistant/homeassistant/ini.env b/profiles/templates/homeassistant/homeassistant/ini.env index 29ca4ce..db90a6c 100644 --- a/profiles/templates/homeassistant/homeassistant/ini.env +++ b/profiles/templates/homeassistant/homeassistant/ini.env @@ -1,4 +1,4 @@ -# Calculate format=samba path=/var/calculate chmod=600 chown=homeassistant:homeassistant comment=# grep(/var/calculate/ini.env,homeassistant)== protected +# Calculate format=samba path=/var/calculate chmod=600 chown=homeassistant:homeassistant grep(/var/calculate/ini.env,homeassistant_sites_domain)== protected [homeassistant] protocol = http domain = home.example.org diff --git a/profiles/templates/homeassistant/homeassistant/init.homeassistant b/profiles/templates/homeassistant/homeassistant/init.homeassistant index aa9c7fd..788aceb 100644 --- a/profiles/templates/homeassistant/homeassistant/init.homeassistant +++ b/profiles/templates/homeassistant/homeassistant/init.homeassistant @@ -1,5 +1,7 @@ -# Calculate path=/etc/init.d name=#-cut(1,.)-# chmod=755 comment=# exists(/var/calculate/www/homeassistant/.venv)!= +# Calculate path=/etc/init.d name=#-cut(1,.)-# chmod=755 comment=# exists(/var/calculate/www/homeassistant/.homeassistant)!= #!/sbin/openrc-run +# Copyright 2022 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 description="Open-source home automation platform" @@ -25,8 +27,7 @@ command_background=yes pidfile=/run/homeassistant.pid depend() { - need net postgresql -# need net nginx postgresql + need net nginx } start_pre() { diff --git a/profiles/templates/homeassistant/homeassistant/log/.calculate_directory b/profiles/templates/homeassistant/homeassistant/log/.calculate_directory index c4ea7e3..cbca030 100644 --- a/profiles/templates/homeassistant/homeassistant/log/.calculate_directory +++ b/profiles/templates/homeassistant/homeassistant/log/.calculate_directory @@ -1 +1 @@ -# Calculate path=/var/log name=homeassistant chown=homeassistant:homeassistant +# Calculate path=/var/log name=homeassistant chmod=750 chown=homeassistant:homeassistant diff --git a/profiles/templates/homeassistant/homeassistant/runlevel.homeassistant b/profiles/templates/homeassistant/homeassistant/runlevel.homeassistant index fafbec5..d31c707 100644 --- a/profiles/templates/homeassistant/homeassistant/runlevel.homeassistant +++ b/profiles/templates/homeassistant/homeassistant/runlevel.homeassistant @@ -1 +1 @@ -# Calculate path=/etc/runlevels/default name=#-cut(1,.)-# link=/etc/init.d/#-cut(1,.)-# protected symbolic exists(/var/calculate/www/homeassistant/.venv)!= +# Calculate path=/etc/runlevels/default name=#-cut(1,.)-# link=/etc/init.d/#-cut(1,.)-# protected symbolic exists(/var/calculate/www/homeassistant/.homeassistant)!= diff --git a/profiles/templates/homeassistant/homeassistant/www/homeassistant/.calculate_directory b/profiles/templates/homeassistant/homeassistant/www/homeassistant/.calculate_directory index a6ec39b..cbe41bd 100644 --- a/profiles/templates/homeassistant/homeassistant/www/homeassistant/.calculate_directory +++ b/profiles/templates/homeassistant/homeassistant/www/homeassistant/.calculate_directory @@ -1 +1 @@ -# Calculate chmod=750 chown=homeassistant:nginx +# Calculate chmod=750 chown=homeassistant:homeassistant diff --git a/profiles/templates/homeassistant/homeassistant/x-ini.env b/profiles/templates/homeassistant/homeassistant/x-ini.env index d916800..8824140 100644 --- a/profiles/templates/homeassistant/homeassistant/x-ini.env +++ b/profiles/templates/homeassistant/homeassistant/x-ini.env @@ -1,4 +1,4 @@ -# Calculate run=/bin/bash grep(/var/calculate/ini.env,home\.example\.org)!= +# Calculate run=/bin/bash grep(/var/calculate/ini.env,home\.example.org)!= # Подставим IP eth0, пока у шаблонов нет необходимой функции interface=$(ip route | grep -m 1 'default via' | awk '{print $5}') @@ -7,4 +7,4 @@ then interface=eth0 fi IP=$(ifconfig $interface 2>/dev/null | grep 'inet ' | awk '{print $2}') -sed -i -E "s/^(domain =).*/\1 $IP/g" /var/calculate/ini.env +sed -i -E "s/(domain =).*/\1 $IP/g" /var/calculate/ini.env diff --git a/profiles/templates/homeassistant/nginx/homeassistant.conf b/profiles/templates/homeassistant/nginx/homeassistant.conf index 99e30d5..08d24f2 100644 --- a/profiles/templates/homeassistant/nginx/homeassistant.conf +++ b/profiles/templates/homeassistant/nginx/homeassistant.conf @@ -1,50 +1,9 @@ # Calculate path=/etc/nginx/sites-enabled exists(/etc/nginx/sites-enabled/#-cut()-#)== comment=# -map $http_upgrade $connection_upgrade { - default upgrade; - '' close; -} - -server { - # These shouldn't need to be changed - listen 80 default_server; - #return 301 https://$host$request_uri; -} - server { - # Update this line to be your domain - #server_name home.example.org; - - access_log /var/log/nginx/homeassistant.access_log main; - error_log /var/log/nginx/homeassistant.error_log info; - - # Ensure these lines point to your SSL certificate and key - ssl_certificate /etc/nginx/ssl/homeassistant/fullchain.pem; - ssl_certificate_key /etc/nginx/ssl/homeassistant/privkey.pem; - - # Ensure this line points to your dhparams file -# ssl_dhparam /etc/nginx/ssl/dhparams.pem; - - - # These shouldn't need to be changed -# listen [::]:443 ssl default_server ipv6only=off; # if your nginx version is >= 1.9.5 you can also add the "http2" flag here - listen 443 ssl default_server; # if your nginx version is >= 1.9.5 you can also add the "http2" flag here - add_header Strict-Transport-Security "max-age=31536000; includeSubdomains"; - - ssl_protocols TLSv1.2; - ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4"; - ssl_prefer_server_ciphers on; - ssl_session_cache shared:SSL:10m; - - proxy_buffering off; - - location / { - proxy_pass http://127.0.0.1:8123; - proxy_set_header Host localhost; - #proxy_set_header Host $host; - proxy_redirect http:// https://; - proxy_http_version 1.1; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade; - } + listen 80 default_server; + location / { + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header Host $http_host; + proxy_pass "http://127.0.0.1:8123"; + } } diff --git a/profiles/templates/homeassistant/nginx/link.homeassistant b/profiles/templates/homeassistant/nginx/link.homeassistant deleted file mode 100644 index fbc4222..0000000 --- a/profiles/templates/homeassistant/nginx/link.homeassistant +++ /dev/null @@ -1 +0,0 @@ -# Calculate path=/etc/nginx/ssl name=#-cut(1,.)-# link=/var/calculate/ssl symbolic protected diff --git a/profiles/templates/homeassistant/nginx/ssl/.calculate_directory b/profiles/templates/homeassistant/nginx/ssl/.calculate_directory deleted file mode 100644 index 814ccaf..0000000 --- a/profiles/templates/homeassistant/nginx/ssl/.calculate_directory +++ /dev/null @@ -1 +0,0 @@ -# Calculate path=/var/calculate diff --git a/profiles/templates/homeassistant/postgresql/.calculate_directory b/profiles/templates/homeassistant/postgresql/.calculate_directory deleted file mode 100644 index 0e3fbac..0000000 --- a/profiles/templates/homeassistant/postgresql/.calculate_directory +++ /dev/null @@ -1 +0,0 @@ -# Calculate env=install ac_install_merge==on mergepkg(dev-db/postgresql)!= append=skip diff --git a/profiles/templates/homeassistant/postgresql/0-get_ver b/profiles/templates/homeassistant/postgresql/0-get_ver deleted file mode 100644 index 520431b..0000000 --- a/profiles/templates/homeassistant/postgresql/0-get_ver +++ /dev/null @@ -1,5 +0,0 @@ -# Calculate run=/bin/bash - -ver=$(ls -d /var/db/pkg/dev-db/postgresql-*) -ver=${ver#*postgresql-} -echo ${ver%%.*} > /tmp/pgsqlver diff --git a/profiles/templates/homeassistant/postgresql/database/.calculate_directory b/profiles/templates/homeassistant/postgresql/database/.calculate_directory deleted file mode 100644 index 16336cf..0000000 --- a/profiles/templates/homeassistant/postgresql/database/.calculate_directory +++ /dev/null @@ -1 +0,0 @@ -# Calculate path=/var/calculate name=postgresql chmod=700 chown=postgres:postgres diff --git a/profiles/templates/homeassistant/postgresql/etc/.calculate_directory b/profiles/templates/homeassistant/postgresql/etc/.calculate_directory deleted file mode 100644 index f733fc4..0000000 --- a/profiles/templates/homeassistant/postgresql/etc/.calculate_directory +++ /dev/null @@ -1 +0,0 @@ -# Calculate path=/etc name=postgresql-#-load(char,/tmp/pgsqlver)-# diff --git a/profiles/templates/homeassistant/postgresql/etc/pg_hba.conf b/profiles/templates/homeassistant/postgresql/etc/pg_hba.conf deleted file mode 100644 index ab6594f..0000000 --- a/profiles/templates/homeassistant/postgresql/etc/pg_hba.conf +++ /dev/null @@ -1,10 +0,0 @@ -# Calculate chmod=600 comment=# chown=postgres:postgres - -# TYPE DATABASE USER ADDRESS METHOD - -# "local" is for Unix domain socket connections only -local all all password -# IPv4 local connections: -host all all 127.0.0.1/32 password -# IPv6 local connections: -host all all ::1/128 password diff --git a/profiles/templates/homeassistant/postgresql/etc/pg_ident.conf b/profiles/templates/homeassistant/postgresql/etc/pg_ident.conf deleted file mode 100644 index 4559188..0000000 --- a/profiles/templates/homeassistant/postgresql/etc/pg_ident.conf +++ /dev/null @@ -1 +0,0 @@ -# Calculate chmod=600 comment=# chown=postgres:postgres diff --git a/profiles/templates/homeassistant/postgresql/etc/postgresql.conf b/profiles/templates/homeassistant/postgresql/etc/postgresql.conf deleted file mode 100644 index ed064ba..0000000 --- a/profiles/templates/homeassistant/postgresql/etc/postgresql.conf +++ /dev/null @@ -1,22 +0,0 @@ -# Calculate chmod=600 comment=# chown=postgres:postgres - -max_connections = 100 # (change requires restart) -shared_buffers = 128MB # min 128kB -dynamic_shared_memory_type = posix # the default is the first option -max_wal_size = 1GB -min_wal_size = 80MB -log_timezone = 'W-SU' -datestyle = 'iso, dmy' -timezone = 'W-SU' -lc_messages = '#-os_install_locale_lang-#.UTF-8' -lc_monetary = '#-os_install_locale_lang-#.UTF-8' -lc_numeric = 'C' -lc_time = '#-os_install_locale_lang-#.UTF-8' -#?os_locale_language==ru# -default_text_search_config = 'pg_catalog.russian' -#!os_locale_language# -default_text_search_config = 'pg_catalog.english' -#os_locale_language# -plperl.on_init = 'use utf8; use re; package utf8; require "utf8_heavy.pl";' - -listen_addresses = '127.0.0.1' diff --git a/profiles/templates/homeassistant/postgresql/ini.env b/profiles/templates/homeassistant/postgresql/ini.env deleted file mode 100644 index fb91a53..0000000 --- a/profiles/templates/homeassistant/postgresql/ini.env +++ /dev/null @@ -1,4 +0,0 @@ -# Calculate format=samba path=/var/calculate grep(/var/calculate/ini.env,postgresql)== comment=# protected -[postgresql] -postgres_password = #-rnd(pas,12)-# -homeassistant_password = #-rnd(pas,12)-# diff --git a/profiles/templates/homeassistant/postgresql/link.database b/profiles/templates/homeassistant/postgresql/link.database deleted file mode 100644 index 6c5199d..0000000 --- a/profiles/templates/homeassistant/postgresql/link.database +++ /dev/null @@ -1 +0,0 @@ -# Calculate path=/var/lib name=postgresql link=/var/calculate/postgresql symbolic protected diff --git a/profiles/templates/homeassistant/postgresql/pgpass b/profiles/templates/homeassistant/postgresql/pgpass deleted file mode 100644 index fe2c9d8..0000000 --- a/profiles/templates/homeassistant/postgresql/pgpass +++ /dev/null @@ -1,3 +0,0 @@ -# Calculate path=/root name=.pgpass chmod=600 comment=# protected -localhost:*:*:postgres:#-ini(postgresql.postgres_password)-# -localhost:*:*:homeassistant:#-ini(postgresql.homeassistant_password)-# diff --git a/profiles/templates/homeassistant/world b/profiles/templates/homeassistant/world index 9e9f7ed..1f4b9cf 100644 --- a/profiles/templates/homeassistant/world +++ b/profiles/templates/homeassistant/world @@ -1,7 +1,2 @@ # Calculate env=update ac_update_sync==on path=/var/lib/portage cl_update_world==merge||cl_update_world==rebuild format=world -app-admin/syslog-ng -app-text/tree -dev-db/postgresql -#media-video/ffmpeg -sys-process/cronie www-servers/nginx diff --git a/scripts/homeassistant/setup.sh b/scripts/homeassistant/setup.sh index 2c4c1f8..6607085 100755 --- a/scripts/homeassistant/setup.sh +++ b/scripts/homeassistant/setup.sh @@ -1,5 +1,4 @@ #!/bin/bash -# Install Home Assistant set -ueo pipefail export PATH="/lib/rc/bin:$PATH" @@ -7,9 +6,6 @@ scriptpath=$(dirname $(readlink -f $0)) . /var/db/repos/calculate/scripts/ini.sh -echo "Home Assistant setup" -chown homeassistant: /var/calculate/ini.env - for script in $scriptpath/step/*.sh; do "$script" done diff --git a/scripts/homeassistant/step/1-install.sh b/scripts/homeassistant/step/1-install.sh index b270b1f..39b44fb 100755 --- a/scripts/homeassistant/step/1-install.sh +++ b/scripts/homeassistant/step/1-install.sh @@ -13,16 +13,12 @@ if [[ ! -e ~/.venv ]]; then einfo 'Create a virtualenv' cd ~ python -m venv .venv + echo 'source ~/.venv/bin/activate' >> ~/.bashrc source .venv/bin/activate - echo - einfo 'Install Python dependencies' + einfo 'Install all Python dependencies' python -m pip install wheel - echo einfo 'Install Home Assistant' pip install homeassistant - - eend fi - diff --git a/scripts/homeassistant/step/3-config.sh b/scripts/homeassistant/step/2-config.sh similarity index 53% rename from scripts/homeassistant/step/3-config.sh rename to scripts/homeassistant/step/2-config.sh index cb44b40..9ee9ab9 100755 --- a/scripts/homeassistant/step/3-config.sh +++ b/scripts/homeassistant/step/2-config.sh @@ -5,17 +5,27 @@ export PATH="/lib/rc/bin:$PATH" SCRIPT=$(readlink -f $0) [[ $UID == 0 ]] && exec su - homeassistant -c "$SCRIPT" +source .venv/bin/activate . /var/db/repos/container/scripts/functions.sh . /var/db/repos/calculate/scripts/ini.sh -cd ~ -timeout 5 /var/calculate/www/homeassistant/.venv/bin/hass || true +hass >/dev/null & +id_hass=$! + +echo; einfon "Check for the first start Home Assistant " +while ! curl http://127.0.0.1:8123 2>/dev/null; do + echo -n . + sleep 1 +done +kill $id_hass + +cat >> ~/.homeassistant/configuration.yaml <> .homeassistant/configuration.yaml << EOF http: use_x_forwarded_for: true trusted_proxies: 127.0.0.1 EOF -fi + +echo +eend diff --git a/scripts/homeassistant/step/2-postgresql.sh b/scripts/homeassistant/step/2-postgresql.sh deleted file mode 100755 index 43f1610..0000000 --- a/scripts/homeassistant/step/2-postgresql.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -set -ueo pipefail - -[[ -n "$(ls -A /var/lib/postgresql)" ]] && exit - -export PATH="/lib/rc/bin:$PATH" - -. /var/db/repos/container/scripts/functions.sh -. /var/db/repos/calculate/scripts/ini.sh - -pgver() { - local ver=$(ls -d /var/db/pkg/dev-db/postgresql-*) - ver=${ver#*postgresql-} - if [[ ${1:-} == 'slot' ]]; then - echo ${ver%%.*} - else - echo ${ver%%-*} - fi -} - -emerge --config =dev-db/postgresql-$(pgver) - -/etc/init.d/postgresql-$(pgver slot) start - -psql -U postgres -c "ALTER USER postgres WITH PASSWORD '${ini[postgresql.postgres_password]}'" -psql -U postgres -c "CREATE ROLE ${ini[postgresql.homeassistant_user]} WITH login" -psql -U postgres -c "CREATE DATABASE homeassistant OWNER homeassistant" -psql -U postgres -c "ALTER USER ${ini[postgresql.homeassistant_user]} WITH PASSWORD '${ini[postgresql.homeassistant_password]}'" - -cl-core-setup -n postgresql -f - -/etc/init.d/postgresql-$(pgver slot) restart