237 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			237 lines
		
	
	
		
			6.3 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#! /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 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
 | 
						|
 | 
						|
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"
 | 
						|
 | 
						|
cat <<EOF > /home/homeassistant/install.sh
 | 
						|
#!/bin/bash
 | 
						|
set -x
 | 
						|
cd /srv/homeassistant &&
 | 
						|
python3 -m venv . &&
 | 
						|
source /srv/homeassistant//bin/activate &&
 | 
						|
python3 -m pip install wheel &&
 | 
						|
pip3 install homeassistant
 | 
						|
EOF
 | 
						|
chmod +x /home/homeassistant/install.sh
 | 
						|
su homeassistant -c /home/homeassistant/install.sh
 | 
						|
 | 
						|
 | 
						|
 | 
						|
cat <<EOF | tee /etc/systemd/system/home-assistant@homeassistant.service
 | 
						|
[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
 | 
						|
 | 
						|
# Homeassistant configuration
 | 
						|
 | 
						|
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:
 | 
						|
 | 
						|
 | 
						|
# Proxy Config
 | 
						|
#homeassistant:
 | 
						|
#  external_url: "https://hass.example.com"
 | 
						|
# 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
 | 
						|
 | 
						|
 | 
						|
systemctl daemon-reload
 | 
						|
systemctl enable home-assistant@homeassistant.service
 | 
						|
systemctl start home-assistant@homeassistant.service
 | 
						|
 | 
						|
 | 
						|
## 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
 | 
						|
 | 
						|
 | 
						|
## PROMETHEUS INSTALL
 | 
						|
 | 
						|
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*/
 | 
						|
mv prometheus promtool /usr/local/bin/
 | 
						|
 | 
						|
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.
 | 
						|
  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
 | 
						|
 | 
						|
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:
 | 
						|
    - name: integrations
 | 
						|
      scrape_configs:
 | 
						|
        - job_name: prometheus 
 | 
						|
          metrics_path: /metrics
 | 
						|
          scheme: http
 | 
						|
          static_configs:
 | 
						|
            - targets: ['localhost:9090']
 | 
						|
      remote_write:
 | 
						|
        - url: http://cortex:9009/api/prom/push
 | 
						|
EOF
 | 
						|
 | 
						|
systemctl enable grafana-server
 | 
						|
systemctl start grafana-server
 | 
						|
systemctl status home-assistant@homeassistant.service
 | 
						|
systemctl status grafana-server
 | 
						|
systemctl status prometheus
 |