Files
semprini-maintainer/install.sh
T
paulandClaude Opus 5 556ffb6561 Run on the stack host, react to Matrix events, keep the repo current
The stack moved to AWS Lightsail, so the maintainer now runs on that host
as ubuntu and is triggered by events rather than polling:

- A long-poll /sync delivers operator messages and #stack-alerts within
  about a second. Follow-ups resume the Claude session, so the Stack
  Maintenance room works like a chat. status, cancel, new, upgrade and
  help are answered even while a run is in progress.
- FIRING alerts from the Alertmanager bridge are batched and triaged in a
  thread, with a per-fingerprint cooldown.
- Agent runs go one at a time from a persisted priority queue, so an
  unusable agent defers work instead of losing it.
- Health checks add host resources (disk, memory, OOM kills, restart
  loops, stale Kuma) and reboot detection; one cycle's findings share a run.
- The daily report and the weekly upgrade run at fixed UTC times; the
  upgrade covers every image on the host.
- Applied changes are committed to semprini-core main and pushed. The
  daemon fast-forwards before a run and pushes leftovers after it.
- The Claude credential comes from `claude setup-token` via
  --set-claude-token. Self-update runs hourly, gated by --check.
- install.sh renders the unit for the current user and configures the git
  credential helper.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 23:22:34 +12:00

74 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# install.sh — install & enable the semprini-maintainer systemd service.
#
# Run on the stack host, from this checkout, as the user the service runs as
# (not root; sudo is used for the systemd parts). Idempotent.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UNIT="semprini-maintainer.service"
DEST="/etc/systemd/system/$UNIT"
USER_NAME="$(id -un)"
[ "$USER_NAME" != root ] || { echo "ERROR: run as the service user, not root"; exit 1; }
echo "→ Pre-flight checks"
for bin in docker python3 git curl; do
command -v "$bin" >/dev/null || { echo "ERROR: $bin not found"; exit 1; }
done
id -nG | grep -qw docker || echo "WARN: $USER_NAME is not in the docker group"
if [ ! -f "$DIR/config.json" ]; then
echo "ERROR: config.json missing. Copy config.example.json to config.json and"
echo " adjust the paths, then run register-matrix-bot.sh and register-gitea-bot.sh."
exit 1
fi
chmod 600 "$DIR/config.json"
cfg_get() {
python3 - "$DIR" "$1" <<'PY'
import sys
sys.path.insert(0, sys.argv[1])
import maintainer
cfg = maintainer.load_config(sys.argv[1] + "/config.json")
if sys.argv[2] == "git-credential":
env = maintainer.agent_git_env(cfg) # also writes the credential store
if env:
print(cfg["gitea"]["web_base"].rstrip("/"), env["GIT_CONFIG_VALUE_2"])
else:
print(cfg[sys.argv[2]])
PY
}
CLAUDE_BIN="$(cfg_get claude_bin)"
if [ ! -x "$CLAUDE_BIN" ]; then
echo "→ Installing Claude Code"
curl -fsSL https://claude.ai/install.sh | bash
fi
echo "→ Checking maintainer.py and config"
python3 -m py_compile "$DIR/maintainer.py"
python3 "$DIR/maintainer.py" --config "$DIR/config.json" --check
# Git on this host authenticates to Gitea as claude-code — for the agent, for
# the daemon's own pushes, and for sync-to-host.sh's fast-forward alike.
read -r WEB HELPER <<<"$(cfg_get git-credential)" || true
if [ -n "${WEB:-}" ]; then
echo "→ Git credential helper for $WEB"
git config --global "credential.$WEB.helper" "$HELPER"
else
echo "WARN: no Gitea token — the agent cannot push. Run register-gitea-bot.sh."
fi
echo "→ Installing $UNIT"
sed -e "s|@USER@|$USER_NAME|g" -e "s|@HOME@|$HOME|g" -e "s|@DIR@|$DIR|g" \
"$DIR/$UNIT" | sudo tee "$DEST" >/dev/null
sudo systemctl daemon-reload
sudo systemctl enable "$UNIT"
sudo systemctl restart "$UNIT"
echo
echo "✓ Installed and started."
echo " Logs: journalctl -u $UNIT -f"
echo " Status: systemctl status $UNIT (or send 'status' in Matrix)"
echo " Stop: sudo systemctl stop $UNIT"