[feat] first working version

This commit is contained in:
alban 2022-06-04 10:44:45 +02:00
parent 0b26e87b3c
commit bb2addfee4
3 changed files with 152 additions and 76 deletions

4
Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM jrei/systemd-debian:11
COPY . /opt

View File

@ -1,3 +1,25 @@
# Home Assistant + Grafana
WIP
## Crash course
```
git clone <repo>
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

View File

@ -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 <<EOF > /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 <<EOF | sudo tee /etc/systemd/system/home-assistant@homeassistant.service
cat <<EOF | tee /etc/systemd/system/home-assistant@homeassistant.service
[Unit]
Description=Home Assistant
After=network-online.target
@ -39,8 +78,9 @@ RestartForceExitStatus=100
WantedBy=multi-user.target
EOF
# Homeassistant configuration
cat <<EOF | sudo -u homeassistant tee /home/homeassistant/.homeassistant/configuration.yaml
cat <<EOF | su homeassistant -c "tee /home/homeassistant/.homeassistant/configuration.yaml"
# File: /home/homeassistant/.homeassistant/configuration.yaml
# Loads default set of integrations. Do not remove.
default_config:
@ -48,7 +88,7 @@ default_config:
# Proxy Config
#homeassistant:
# external_url: "https://hassi.mpt.tmplab.org"
# external_url: "https://hass.example.com"
# http:
# use_x_forwarded_for: true
# trusted_proxies:
@ -73,73 +113,32 @@ sensor:
EOF
## GRAFANA INSTALL
systemctl daemon-reload
systemctl enable home-assistant@homeassistant.service
systemctl start home-assistant@homeassistant.service
sudo apt-get install -y apt-transport-https software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana
## USER ACTION
echo "
# Please create a user in home assistant
# 1. Then go to user profile in home assistant
# 2. Get long lived access token like eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJlM2ZkY2VmZjYyODQ0OWQ5OGY2OTRlNmU2YjUxYzUyYSIsImlhdCI6MTY1MTg0ODU0NiwiZXhwIjoxOTY3MjA4NTQ2fQ.R6_My5eBUMcgompY3L3SbTUCTEIffQaNMUHyUw8sro0
"
read -p "Please provide the user access token: " TOKEN
# This is probably useless: we don't need to start on a port lower than 3000
#cat <<EOF > /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 <<EOF > /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