#!/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"