|
| 1 | +#!/usr/bin/env bash |
| 2 | +# hapi-runner-watchdog.sh |
| 3 | +# |
| 4 | +# Belt-and-braces watchdog: if THIS host's machine entry is missing from the |
| 5 | +# hub's /api/machines list OR its runner.status is not "running", restart the |
| 6 | +# hapi-runner.service systemd unit. |
| 7 | +# |
| 8 | +# Run by hapi-runner-watchdog.service (oneshot) on the schedule defined by |
| 9 | +# hapi-runner-watchdog.timer. Logs to journald. |
| 10 | +# |
| 11 | +# Configuration via env vars (optionally loaded from |
| 12 | +# /home/heavygee/.hapi/runner-watchdog.env by the service unit): |
| 13 | +# |
| 14 | +# HAPI_API_URL (default: http://127.0.0.1:3006) |
| 15 | +# CLI_API_TOKEN (required - Bearer token for /api/machines) |
| 16 | +# If missing, falls back to settings.json's cliApiToken. |
| 17 | +# HAPI_HOME (default: ~/.hapi) - location of settings.json + runner.state.json |
| 18 | +# HAPI_WATCHDOG_DRY_RUN (default: empty) - if "1", log decision but don't actually restart |
| 19 | +# HAPI_WATCHDOG_GRACE_SEC (default: 30) - how recently the runner must have |
| 20 | +# heartbeated to be considered alive even if absent |
| 21 | +# from the hub list (handles brief reconnect windows). |
| 22 | +# |
| 23 | +# Exit codes: |
| 24 | +# 0 - runner healthy OR successfully restarted |
| 25 | +# 1 - probe failed AND restart attempt also failed |
| 26 | +# 2 - misconfigured (missing token, etc.) - StartLimitPreventExitStatus |
| 27 | +# in the unit catches this so we don't restart-loop. |
| 28 | + |
| 29 | +set -euo pipefail |
| 30 | + |
| 31 | +log() { |
| 32 | + printf '%s [watchdog] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" |
| 33 | +} |
| 34 | + |
| 35 | +HAPI_API_URL="${HAPI_API_URL:-http://127.0.0.1:3006}" |
| 36 | +HAPI_HOME="${HAPI_HOME:-$HOME/.hapi}" |
| 37 | +GRACE_SEC="${HAPI_WATCHDOG_GRACE_SEC:-30}" |
| 38 | +SETTINGS_FILE="$HAPI_HOME/settings.json" |
| 39 | +STATE_FILE="$HAPI_HOME/runner.state.json" |
| 40 | + |
| 41 | +if [[ ! -f "$SETTINGS_FILE" ]]; then |
| 42 | + log "settings.json missing at $SETTINGS_FILE; nothing to do" |
| 43 | + exit 0 |
| 44 | +fi |
| 45 | + |
| 46 | +# Token resolution: env wins, then settings.json. |
| 47 | +TOKEN="${CLI_API_TOKEN:-}" |
| 48 | +if [[ -z "$TOKEN" ]] && command -v jq >/dev/null; then |
| 49 | + TOKEN="$(jq -r '.cliApiToken // empty' "$SETTINGS_FILE" 2>/dev/null || true)" |
| 50 | +fi |
| 51 | +if [[ -z "$TOKEN" ]]; then |
| 52 | + log "ERROR: no CLI_API_TOKEN in env or settings.json (cliApiToken)" |
| 53 | + exit 2 |
| 54 | +fi |
| 55 | + |
| 56 | +MACHINE_ID="" |
| 57 | +if command -v jq >/dev/null; then |
| 58 | + MACHINE_ID="$(jq -r '.machineId // empty' "$SETTINGS_FILE" 2>/dev/null || true)" |
| 59 | +fi |
| 60 | +if [[ -z "$MACHINE_ID" ]]; then |
| 61 | + log "ERROR: no machineId in settings.json" |
| 62 | + exit 2 |
| 63 | +fi |
| 64 | + |
| 65 | +# Probe the hub. Use --fail so HTTP errors come back as non-zero. |
| 66 | +TMP_RESP="$(mktemp)" |
| 67 | +trap 'rm -f "$TMP_RESP"' EXIT |
| 68 | + |
| 69 | +HTTP_CODE="$(curl -sS \ |
| 70 | + -o "$TMP_RESP" \ |
| 71 | + -w '%{http_code}' \ |
| 72 | + -H "Authorization: Bearer $TOKEN" \ |
| 73 | + --max-time 5 \ |
| 74 | + "$HAPI_API_URL/cli/machines/" 2>/dev/null || echo 'curl_fail')" |
| 75 | + |
| 76 | +if [[ "$HTTP_CODE" == "curl_fail" || "$HTTP_CODE" != "200" ]]; then |
| 77 | + log "hub probe failed (HTTP $HTTP_CODE at $HAPI_API_URL/cli/machines/); not restarting on a failed probe" |
| 78 | + exit 0 |
| 79 | +fi |
| 80 | + |
| 81 | +# Look up THIS machine in the response. The hub returns |
| 82 | +# { "machines": [ { "id": "...", "runner": { "status": "running", ... }, ... } ] } |
| 83 | +# but the shape has shifted historically; accept either { "machines": [] } or top-level array. |
| 84 | +HEALTHY="false" |
| 85 | +if command -v jq >/dev/null; then |
| 86 | + HEALTHY="$(jq --arg mid "$MACHINE_ID" ' |
| 87 | + (if type == "array" then . else (.machines // []) end) |
| 88 | + | map(select(.id == $mid)) |
| 89 | + | if length == 0 then false |
| 90 | + else (.[0].runner.status // .[0].runnerState.status // "unknown") == "running" |
| 91 | + end |
| 92 | + | tostring |
| 93 | + ' "$TMP_RESP" 2>/dev/null || echo 'parse_fail')" |
| 94 | +fi |
| 95 | + |
| 96 | +if [[ "$HEALTHY" == "true" ]]; then |
| 97 | + log "machine $MACHINE_ID present + runner running on $HAPI_API_URL; no action" |
| 98 | + exit 0 |
| 99 | +fi |
| 100 | + |
| 101 | +# Before restarting, give the runner a grace window in case it's mid-reconnect. |
| 102 | +if [[ -f "$STATE_FILE" ]] && command -v jq >/dev/null; then |
| 103 | + LAST_HEARTBEAT="$(jq -r '.lastHeartbeat // empty' "$STATE_FILE" 2>/dev/null || true)" |
| 104 | + if [[ -n "$LAST_HEARTBEAT" ]]; then |
| 105 | + # lastHeartbeat is JS toLocaleString format, not ISO; can't trivially parse |
| 106 | + # to epoch in pure bash. Use file mtime as a rough proxy. |
| 107 | + LAST_MTIME="$(stat -c '%Y' "$STATE_FILE" 2>/dev/null || echo 0)" |
| 108 | + NOW="$(date +%s)" |
| 109 | + AGE=$((NOW - LAST_MTIME)) |
| 110 | + if (( AGE < GRACE_SEC )); then |
| 111 | + log "machine $MACHINE_ID not in hub list but runner heartbeat ${AGE}s old (< grace ${GRACE_SEC}s); skipping restart" |
| 112 | + exit 0 |
| 113 | + fi |
| 114 | + log "machine $MACHINE_ID absent or runner not running; heartbeat ${AGE}s stale" |
| 115 | + fi |
| 116 | +fi |
| 117 | + |
| 118 | +if [[ "${HAPI_WATCHDOG_DRY_RUN:-}" == "1" ]]; then |
| 119 | + log "DRY_RUN: would `sudo systemctl restart hapi-runner.service`" |
| 120 | + exit 0 |
| 121 | +fi |
| 122 | + |
| 123 | +log "restarting hapi-runner.service via systemctl" |
| 124 | +if systemctl restart hapi-runner.service 2>/dev/null; then |
| 125 | + log "restart OK" |
| 126 | + exit 0 |
| 127 | +fi |
| 128 | + |
| 129 | +# Fallback: try sudo (the unit user is heavygee but may have NOPASSWD for this one) |
| 130 | +if sudo -n systemctl restart hapi-runner.service 2>/dev/null; then |
| 131 | + log "restart OK (via sudo)" |
| 132 | + exit 0 |
| 133 | +fi |
| 134 | + |
| 135 | +log "ERROR: could not restart hapi-runner.service (permission?)" |
| 136 | +exit 1 |
0 commit comments