From bb2addfee4f3b303ff76c8111b7ed6a2dfba36da Mon Sep 17 00:00:00 2001 From: alban Date: Sat, 4 Jun 2022 10:44:45 +0200 Subject: [PATCH] [feat] first working version --- Dockerfile | 4 ++ README.md | 24 ++++++- install.sh | 200 +++++++++++++++++++++++++++++++++-------------------- 3 files changed, 152 insertions(+), 76 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e4eedfd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM jrei/systemd-debian:11 + +COPY . /opt + diff --git a/README.md b/README.md index 3ebe8f8..3fef0f6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ # Home Assistant + Grafana -WIP +## Crash course + +``` +git clone +cd harf +bash install.sh +``` +## Testing with docker + +``` +docker build -t harf:latest . +docker run --rm -d --name harf --tmpfs /tmp --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro harf +docker exec -it harf bash /opt/install.sh +docker stop harf +``` + +# Why + +We needed an install script to configure as services + +- Home Assistant +- Prometheus +- Grafana diff --git a/install.sh b/install.sh index ce97a67..b03886c 100755 --- a/install.sh +++ b/install.sh @@ -1,29 +1,68 @@ #! /bin/bash +# To debug, uncomment +# set -x + +# To stop on any error, uncomment +set -e + +# check for root +if [ "$EUID" -ne 0 ] + then echo "Please run as root" + exit +fi -# @todo check for root # @todo check distrib / version # @todo ask IP address +apt update + +declare -a PACKAGES +PACKAGES+=(apt-transport-https) +PACKAGES+=(autoconf) +PACKAGES+=(build-essential) +#PACKAGES+=(chromium-browser) +PACKAGES+=(git) +PACKAGES+=(libffi-dev) +PACKAGES+=(libjpeg-dev) +PACKAGES+=(libopenjp2-7) +PACKAGES+=(libssl-dev) +PACKAGES+=(libtiff5) +PACKAGES+=(libturbojpeg0-dev) +PACKAGES+=(python3) +PACKAGES+=(python3-dev) +PACKAGES+=(python3-pip) +PACKAGES+=(python3-venv) +PACKAGES+=(software-properties-common) +PACKAGES+=(tzdata) +PACKAGES+=(vim) +PACKAGES+=(wget) +PACKAGES+=(zlib1g-dev) + +apt-get install -y ${PACKAGES[@]} ## HOMEASSISTANT INSTALL -sudo apt update -sudo apt install git vim python3 python3-pip chromium-browser -sudo apt-get install -y python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libopenjp2-7 libtiff5 libturbojpeg0-dev tzdata -sudo useradd -rm homeassistant -sudo mkdir /srv/homeassistant -sudo chown homeassistant:homeassistant /srv/homeassistant +mkdir -p /srv/homeassistant +getent passwd homeassistant || useradd -rm homeassistant -s /bin/bash +chown homeassistant:homeassistant /srv/homeassistant +su homeassistant -c "mkdir -p /home/homeassistant/.homeassistant" -sudo su homeassistant -c " +cat < /home/homeassistant/install.sh +#!/bin/bash +set -x cd /srv/homeassistant && python3 -m venv . && -source bin/activate && +source /srv/homeassistant//bin/activate && python3 -m pip install wheel && -pip3 install homeassistant -" +pip3 install homeassistant +EOF +chmod +x /home/homeassistant/install.sh +su homeassistant -c /home/homeassistant/install.sh -cat < /etc/systemd/system/grafana-server.service.d/override.conf -#[Service] -## Give the CAP_NET_BIND_SERVICE capability -#CapabilityBoundingSet=CAP_NET_BIND_SERVICE -#AmbientCapabilities=CAP_NET_BIND_SERVICE -# -## A private user cannot have process capabilities on the host's user -## namespace and thus CAP_NET_BIND_SERVICE has no effect. -#PrivateUsers=false -#EOF ## PROMETHEUS INSTALL -curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi - +getent passwd prometheus || useradd -rm prometheus +wget -q -O - https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi - tar xvf prometheus*.tar.gz cd prometheus*/ -sudo mv prometheus promtool /usr/local/bin/ +mv prometheus promtool /usr/local/bin/ -cat << EOF | sudo tee /etc/systemd/system/prometheus.service -[Unit] -Description=Prometheus -Documentation=https://prometheus.io/docs/introduction/overview/ -Wants=network-online.target -After=network-online.target - -[Service] -Type=simple -User=prometheus -Group=prometheus -ExecReload=/bin/kill -HUP $MAINPID -ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries --web.listen-address=0.0.0.0:9090 --web.external-url= - -SyslogIdentifier=prometheus -Restart=always - -[Install] -WantedBy=multi-user.target -EOF - -sudo systemctl daemon-reload -sudo systemctl enable home-assistant@homeassistant.service -sudo systemctl start home-assistant@homeassistant.service -sudo systemctl status home-assistant@homeassistant.service -sudo systemctl enable prometheus -sudo systemctl start prometheus -sudo systemctl status prometheus -sudo systemctl enable grafana-server -sudo systemctl start grafana-server -sudo systemctl status grafana-server - - -echo " -# create user in home ass -# go to user profile in home ass -# get long lived access token like eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJlM2ZkY2VmZjYyODQ0OWQ5OGY2OTRlNmU2YjUxYzUyYSIsImlhdCI6MTY1MTg0ODU0NiwiZXhwIjoxOTY3MjA4NTQ2fQ.R6_My5eBUMcgompY3L3SbTUCTEIffQaNMUHyUw8sro0 -" -cat << EOF | sudo tee /etc/prometheus/prometheus.yml +mkdir -p /var/lib/prometheus && chown prometheus:prometheus /var/lib/prometheus +mkdir -p /etc/prometheus +cat << EOF | tee /etc/prometheus/prometheus.yml # File: /etc/prometheus/prometheus.yml global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. @@ -167,8 +166,55 @@ scrape_configs: - targets: ['localhost:8123'] EOF -read -p "Please provide your access token: " TOKEN -cat << EOF | sudo tee /etc/grafana/grafana.ini +cat << EOF | tee /etc/systemd/system/prometheus.service +[Unit] +Description=Prometheus +Documentation=https://prometheus.io/docs/introduction/overview/ +Wants=network-online.target +After=network-online.target + +[Service] +Type=simple +User=prometheus +Group=prometheus +ExecReload=/bin/kill -HUP $MAINPID +ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries --web.listen-address=0.0.0.0:9090 --web.external-url= + +SyslogIdentifier=prometheus +Restart=always + +[Install] +WantedBy=multi-user.target +EOF + + + +systemctl daemon-reload +systemctl enable prometheus +systemctl start prometheus + + +## GRAFANA INSTALL + + +wget -q -O - https://packages.grafana.com/gpg.key | apt-key add - +echo "deb https://packages.grafana.com/oss/deb stable main" | tee -a /etc/apt/sources.list.d/grafana.list +apt-get update +apt-get install grafana + +# This is probably useless: we don't need to start on a port lower than 3000 +#cat < /etc/systemd/system/grafana-server.service.d/override.conf +#[Service] +## Give the CAP_NET_BIND_SERVICE capability +#CapabilityBoundingSet=CAP_NET_BIND_SERVICE +#AmbientCapabilities=CAP_NET_BIND_SERVICE +# +## A private user cannot have process capabilities on the host's user +## namespace and thus CAP_NET_BIND_SERVICE has no effect. +#PrivateUsers=false +#EOF + +cat << EOF | tee /etc/grafana/grafana.ini metrics: wal_directory: /tmp/wal configs: @@ -183,4 +229,8 @@ metrics: - url: http://cortex:9009/api/prom/push EOF -sudo systemctl reload grafana-server +systemctl enable grafana-server +systemctl start grafana-server +systemctl status home-assistant@homeassistant.service +systemctl status grafana-server +systemctl status prometheus