harf/install.sh

237 lines
6.3 KiB
Bash
Raw Permalink Normal View History

2022-05-06 16:40:59 +00:00
#! /bin/bash
2022-06-04 08:44:45 +00:00
# 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
2022-06-02 21:24:09 +00:00
# @todo check distrib / version
# @todo ask IP address
2022-06-04 08:44:45 +00:00
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[@]}
2022-06-02 21:24:09 +00:00
## HOMEASSISTANT INSTALL
2022-06-04 08:44:45 +00:00
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"
2022-05-06 16:40:59 +00:00
2022-06-04 08:44:45 +00:00
cat <<EOF > /home/homeassistant/install.sh
#!/bin/bash
set -x
2022-05-06 16:40:59 +00:00
cd /srv/homeassistant &&
python3 -m venv . &&
2022-06-04 08:44:45 +00:00
source /srv/homeassistant//bin/activate &&
2022-05-06 16:40:59 +00:00
python3 -m pip install wheel &&
2022-06-04 08:44:45 +00:00
pip3 install homeassistant
EOF
chmod +x /home/homeassistant/install.sh
su homeassistant -c /home/homeassistant/install.sh
2022-05-06 16:40:59 +00:00
2022-06-04 08:44:45 +00:00
cat <<EOF | tee /etc/systemd/system/home-assistant@homeassistant.service
2022-05-06 16:40:59 +00:00
[Unit]
Description=Home Assistant
After=network-online.target
[Service]
Type=simple
User=%i
WorkingDirectory=/home/%i/.homeassistant
ExecStart=/srv/homeassistant/bin/hass -c "/home/%i/.homeassistant"
RestartForceExitStatus=100
[Install]
WantedBy=multi-user.target
EOF
2022-06-04 08:44:45 +00:00
# Homeassistant configuration
2022-06-02 21:24:09 +00:00
2022-06-04 08:44:45 +00:00
cat <<EOF | su homeassistant -c "tee /home/homeassistant/.homeassistant/configuration.yaml"
2022-06-02 21:24:09 +00:00
# File: /home/homeassistant/.homeassistant/configuration.yaml
# Loads default set of integrations. Do not remove.
default_config:
# Proxy Config
#homeassistant:
2022-06-04 08:44:45 +00:00
# external_url: "https://hass.example.com"
2022-06-02 21:24:09 +00:00
# http:
# use_x_forwarded_for: true
# trusted_proxies:
# - 10.255.0.2
# login_attempts_threshold: 5
# Text to speech
tts:
- platform: google_translate
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
prometheus:
sensor:
- platform: command_line
name: CPU Temperature
command: "cat /sys/class/thermal/thermal_zone0/temp"
# If errors occur, make sure configuration file is encoded as UTF-8
unit_of_measurement: "°C"
value_template: "{{ value }}"
EOF
2022-06-04 08:44:45 +00:00
systemctl daemon-reload
systemctl enable home-assistant@homeassistant.service
systemctl start home-assistant@homeassistant.service
2022-06-02 21:24:09 +00:00
2022-06-04 08:44:45 +00:00
## 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
2022-06-02 21:24:09 +00:00
## PROMETHEUS INSTALL
2022-06-04 08:44:45 +00:00
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 -
2022-06-02 21:24:09 +00:00
tar xvf prometheus*.tar.gz
cd prometheus*/
2022-06-04 08:44:45 +00:00
mv prometheus promtool /usr/local/bin/
2022-06-02 21:24:09 +00:00
2022-06-04 08:44:45 +00:00
mkdir -p /var/lib/prometheus && chown prometheus:prometheus /var/lib/prometheus
mkdir -p /etc/prometheus
cat << EOF | tee /etc/prometheus/prometheus.yml
2022-06-02 21:24:09 +00:00
# File: /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'example'
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: "hass"
scrape_interval: 60s
metrics_path: /api/prometheus
authorization:
credentials: "$TOKEN"
scheme: http
static_configs:
- targets: ['localhost:8123']
EOF
2022-06-04 08:44:45 +00:00
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
2022-05-06 16:40:59 +00:00
metrics:
2022-06-02 21:24:09 +00:00
wal_directory: /tmp/wal
2022-05-06 16:40:59 +00:00
configs:
- name: integrations
scrape_configs:
2022-06-02 21:24:09 +00:00
- job_name: prometheus
metrics_path: /metrics
2022-05-06 16:40:59 +00:00
scheme: http
static_configs:
2022-06-02 21:24:09 +00:00
- targets: ['localhost:9090']
2022-05-06 16:40:59 +00:00
remote_write:
- url: http://cortex:9009/api/prom/push
EOF
2022-06-04 08:44:45 +00:00
systemctl enable grafana-server
systemctl start grafana-server
systemctl status home-assistant@homeassistant.service
systemctl status grafana-server
systemctl status prometheus