Files
harbour-aira/bridge/install.sh
T
Carlo Baratto 02d467f46c bridge: install.sh, un comando per installare tutto
Clona il repository, crea l'ambiente, chiede le credenziali (password senza eco,
mai nella cronologia), verifica di riuscire a leggere l'impianto, installa e
avvia il servizio, e stampa indirizzo e token da mettere nell'app.

Non serve root e non tocca niente fuori dalla home. I percorsi sono variabili
(AIRA_INSTALL_DIR, AIRA_CONFIG_DIR, AIRA_PORT) con valori predefiniti sensati,
cosi' lo script funziona per chiunque: i valori predefiniti dei programmi esistenti
non sono stati cambiati, per non toccare installazioni in corso.

Provato per davvero: clonazione, ambiente Python con pyairahome/grpc/cryptography,
lettura del token, e il file del servizio passato a systemd-analyze verify. Il ramo
che usa systemctl --user non e' verificabile da qui (nessun bus utente): da provare
su una macchina con sessione systemd.

README: installazione in un comando, con la procedura manuale come ripiego.
setup_creds.py: messaggi in inglese, per il pubblico del forum.
2026-10-12 15:04:48 +02:00

182 lines
7.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# One-command install of the Aira bridge for the harbour-aira Sailfish app.
#
# What it does, in order:
# 1. clones (or updates) this repository into ~/harbour-aira
# 2. creates a private Python environment and installs the bridge
# 3. asks for your Aira email and password, typed without echo, and saves
# them in a file only you can read (never in the shell history, never in
# the logs, never on the phone)
# 4. checks that the bridge can log in and read your plant
# 5. installs and starts it as a systemd user service
# 6. prints the address and the token to type into the app
#
# Run it with: bash install.sh
#
# It changes nothing outside your home directory, and it never needs root.
set -euo pipefail
REPO_URL="${AIRA_REPO_URL:-https://git.hackatoniclife.com/kaneda/harbour-aira.git}"
INSTALL_DIR="${AIRA_INSTALL_DIR:-$HOME/harbour-aira}"
CONFIG_DIR="${AIRA_CONFIG_DIR:-$HOME/.config/aira}"
PORT="${AIRA_PORT:-8790}"
SKIP_SERVICE="${AIRA_SKIP_SERVICE:-0}"
SKIP_CREDS="${AIRA_SKIP_CREDS:-0}"
say() { printf '\n\033[1m%s\033[0m\n' "$*"; }
ok() { printf ' ok %s\n' "$*"; }
warn() { printf ' note %s\n' "$*"; }
die() { printf '\n error: %s\n\n' "$*" >&2; exit 1; }
# ---------------------------------------------------------------- prerequisites
say "Checking what this machine has"
command -v git >/dev/null 2>&1 || die "git is not installed. On Debian/Raspberry Pi OS: apt install git"
command -v python3 >/dev/null 2>&1 || die "python3 is not installed. On Debian/Raspberry Pi OS: apt install python3 python3-venv"
PY_OK=$(python3 -c 'import sys; print(1 if sys.version_info >= (3, 9) else 0)')
[ "$PY_OK" = "1" ] || die "python3 is $(python3 -V 2>&1 | cut -d' ' -f2), but the bridge needs 3.9 or newer."
python3 -c 'import venv' 2>/dev/null || die "the venv module is missing. On Debian/Raspberry Pi OS: apt install python3-venv"
ok "git $(git --version | cut -d' ' -f3), $(python3 -V 2>&1)"
# ------------------------------------------------------------------- the bridge
say "Fetching the bridge into $INSTALL_DIR"
if [ -d "$INSTALL_DIR/.git" ]; then
git -C "$INSTALL_DIR" pull --ff-only >/dev/null || warn "could not update the existing copy; using it as it is"
ok "already there, brought up to date"
else
mkdir -p "$(dirname "$INSTALL_DIR")"
git clone --quiet "$REPO_URL" "$INSTALL_DIR"
ok "cloned"
fi
BRIDGE="$INSTALL_DIR/bridge"
[ -f "$BRIDGE/aira_bridge.py" ] || die "the bridge is not where it should be ($BRIDGE)."
say "Installing the bridge and its libraries"
if [ ! -x "$BRIDGE/.venv/bin/python" ]; then
python3 -m venv "$BRIDGE/.venv"
fi
"$BRIDGE/.venv/bin/python" -m pip install --quiet --upgrade pip
# pyairahome pulls in grpcio, protobuf, pycognito, cryptography: on a Raspberry
# Pi this step is the slow one, a few minutes. Progress is hidden on purpose,
# the alternative is a page of compiler output that means nothing to a user.
"$BRIDGE/.venv/bin/python" -m pip install --quiet -r "$BRIDGE/requirements.txt"
ok "libraries in place"
# ------------------------------------------------------------- your credentials
mkdir -p "$CONFIG_DIR"
chmod 700 "$CONFIG_DIR"
if [ "$SKIP_CREDS" = "1" ]; then
warn "skipping the credentials step (AIRA_SKIP_CREDS=1)"
elif [ -f "$CONFIG_DIR/aira.json" ]; then
say "Aira credentials already present"
ok "$CONFIG_DIR/aira.json, left untouched"
else
say "Your Aira account"
printf ' Use the same email and password as the Aira app.\n'
printf ' The password is typed without echo: it is not shown, not saved in\n'
printf ' the shell history, and it stays on this machine.\n'
AIRA_CONFIG_DIR="$CONFIG_DIR" "$BRIDGE/.venv/bin/python" "$BRIDGE/setup_creds.py" \
|| die "credentials not saved."
fi
# ------------------------------------------------------------------ sanity check
if [ "$SKIP_CREDS" != "1" ]; then
say "Talking to your heat pump (read-only)"
printf ' This logs in to Aira and reads the current state. Nothing is changed.\n\n'
if AIRA_CONFIG_DIR="$CONFIG_DIR" "$BRIDGE/.venv/bin/python" "$BRIDGE/aira_probe.py"; then
ok "the bridge can reach your plant"
else
die "the bridge could not read your plant. Check the email and password, and that this machine can reach the internet."
fi
fi
# ------------------------------------------------------------------ the service
started=0
if [ "$SKIP_SERVICE" = "1" ]; then
warn "skipping the service step (AIRA_SKIP_SERVICE=1)"
elif command -v systemctl >/dev/null 2>&1 && systemctl --user show-environment >/dev/null 2>&1; then
say "Installing the service, so it starts by itself"
UNIT_DIR="$HOME/.config/systemd/user"
mkdir -p "$UNIT_DIR"
cat > "$UNIT_DIR/harbour-aira-bridge.service" <<UNIT
# Local HTTP bridge for the harbour-aira Sailfish app (generated by install.sh).
[Unit]
Description=Aira bridge for the harbour-aira app (local heat pump HTTP API)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
WorkingDirectory=$BRIDGE
Environment=AIRA_CONFIG_DIR=$CONFIG_DIR
ExecStart=$BRIDGE/.venv/bin/python $BRIDGE/aira_bridge.py --host 0.0.0.0 --port $PORT
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target
UNIT
# Without lingering the service stops as soon as you close your session,
# which looks exactly like a broken bridge from the phone.
loginctl enable-linger "$USER" >/dev/null 2>&1 || warn "could not enable lingering: the service may stop when you log out"
systemctl --user daemon-reload
systemctl --user enable --now harbour-aira-bridge.service >/dev/null
started=1
ok "service installed and started"
else
warn "no systemd user session here: start the bridge by hand when you need it"
printf ' AIRA_CONFIG_DIR=%s %s/.venv/bin/python %s/aira_bridge.py --port %s\n' \
"$CONFIG_DIR" "$BRIDGE" "$BRIDGE" "$PORT"
fi
# ---------------------------------------------------------- what the app needs
if [ "$started" = "1" ]; then
printf ' waiting for the bridge to answer'
for _ in $(seq 1 30); do
if curl -fsS "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then break; fi
printf '.'; sleep 1
done
printf '\n'
if curl -fsS "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then
ok "the bridge is answering"
else
warn "the bridge is not answering yet; check: systemctl --user status harbour-aira-bridge"
fi
fi
TOKEN=""
if [ -f "$CONFIG_DIR/aira_bridge.json" ]; then
TOKEN=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("token",""))' "$CONFIG_DIR/aira_bridge.json" 2>/dev/null || true)
fi
# The address the phone should use: this machine's address on the LAN.
ADDR=$(hostname -I 2>/dev/null | awk '{print $1}')
[ -n "$ADDR" ] || ADDR="<address-of-this-machine>"
cat <<LAST
------------------------------------------------------------------
In the app, open Settings and enter:
address http://$ADDR:$PORT
token ${TOKEN:-<see $CONFIG_DIR/aira_bridge.json>}
Both are private: the token is the key to your plant on your own
network. Do not share it, and do not forward this port from the
internet. Nothing here is exposed outside your LAN.
Useful commands:
systemctl --user status harbour-aira-bridge
systemctl --user restart harbour-aira-bridge
journalctl --user -u harbour-aira-bridge -f
To update later, run this script again: it pulls the new version.
------------------------------------------------------------------
LAST