76 lines
3.4 KiB
Bash
Executable File
76 lines
3.4 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")"
|
|
NO_START=0
|
|
API_ENABLED=""
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--enable-api) API_ENABLED=true ;;
|
|
--disable-api) API_ENABLED=false ;;
|
|
--no-start) NO_START=1 ;;
|
|
-h|--help) echo 'Usage: sudo ./install.sh [--no-start] [--enable-api|--disable-api]'; exit 0 ;;
|
|
*) echo "ERROR: unknown option: $arg (hardware is installed separately)" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
for required in app/pigway_pi_control.py app/plugin_api.py app/pigway_pi_control_api.py config/pigway-pi-control.conf systemd/pigway-pi-control.service systemd/pigway-pi-control-api.service; do
|
|
[ -f "$required" ] || { echo "ERROR: required file missing: $required" >&2; exit 1; }
|
|
done
|
|
[ "$(id -u)" -eq 0 ] || { echo 'ERROR: run installer as root' >&2; exit 1; }
|
|
# The monitor has no I2C, GPIO, Pillow, smbus2 or cooling-board requirement.
|
|
command -v python3 >/dev/null || { apt-get update; apt-get install -y python3; }
|
|
mkdir -p /usr/local/share/pigway-pi-control
|
|
install -m 0644 app/plugin_api.py /usr/local/share/pigway-pi-control/plugin_api.py
|
|
install -m 0755 app/pigway_pi_control.py /usr/local/sbin/pigway-pi-control
|
|
install -m 0755 app/pigway_pi_control_api.py /usr/local/sbin/pigway-pi-control-api
|
|
if [ -f /etc/pigway-pi-control.conf ]; then
|
|
cp /etc/pigway-pi-control.conf "/etc/pigway-pi-control.conf.bak.$(date +%Y%m%d-%H%M%S)"
|
|
else
|
|
install -m 0644 config/pigway-pi-control.conf /etc/pigway-pi-control.conf
|
|
fi
|
|
# Legacy hardware sections are deliberately preserved for explicit migration,
|
|
# but ignored by the monitor and never exposed as monitor configuration.
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
import configparser
|
|
p=Path('/etc/pigway-pi-control.conf');c=configparser.ConfigParser();c.read(p)
|
|
with p.open('a') as f:
|
|
for section,values in {'device':{'identifier':'auto','name':'auto'},'plugins':{},'api':{'bind':'0.0.0.0','port':'6001','log_limit':'200'}}.items():
|
|
if not c.has_section(section):
|
|
f.write('\n['+section+']\n'+''.join(k+' = '+v+'\n' for k,v in values.items()))
|
|
PY
|
|
if [ ! -s /etc/pigway-pi-control-api.token ]; then
|
|
(umask 077; python3 -c 'import secrets; print(secrets.token_urlsafe(32))' > /etc/pigway-pi-control-api.token)
|
|
fi
|
|
chmod 0600 /etc/pigway-pi-control-api.token
|
|
install -m 0644 systemd/pigway-pi-control.service /etc/systemd/system/
|
|
install -m 0644 systemd/pigway-pi-control-api.service /etc/systemd/system/
|
|
if [ -z "$API_ENABLED" ]; then
|
|
API_ENABLED=false
|
|
if [ -t 0 ]; then
|
|
read -r -p 'Enable remote management API? [y/N] ' answer
|
|
case "$answer" in y|Y|yes|YES) API_ENABLED=true ;; esac
|
|
fi
|
|
fi
|
|
API_ENABLED="$API_ENABLED" python3 - <<'PYAPI'
|
|
import os,sys
|
|
sys.path.insert(0,'app')
|
|
import pigway_pi_control_api as api
|
|
api.save_ini_text(api.replace_ini_value('api','enabled',os.environ['API_ENABLED']),'install')
|
|
PYAPI
|
|
systemctl daemon-reload
|
|
if [ "$NO_START" -eq 0 ]; then
|
|
systemctl enable pigway-pi-control.service >/dev/null
|
|
systemctl restart pigway-pi-control.service
|
|
systemctl is-active --quiet pigway-pi-control.service
|
|
if [ "$API_ENABLED" = true ]; then
|
|
systemctl enable pigway-pi-control-api.service >/dev/null
|
|
systemctl restart pigway-pi-control-api.service
|
|
systemctl is-active --quiet pigway-pi-control-api.service
|
|
else
|
|
systemctl disable --now pigway-pi-control-api.service
|
|
fi
|
|
fi
|
|
echo 'Monitor installed. Hardware plugins are installed and connected separately.'
|
|
echo 'Config: /etc/pigway-pi-control.conf'
|
|
echo 'API token: sudo cat /etc/pigway-pi-control-api.token'
|