Files
semprini-maintainer/install.sh
T
paulandCopilot 12e6fef51c Run on the core host, on Claude Sonnet 5.5 via the Anthropic API; no Ollama
The daemon moves from WSL on the workstation to the core host, where it runs
commands directly (core is the only host). One model profile, claude, with a
200k context and the existing cost caps. install.sh runs on the core host.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-26 16:59:28 +13:00

117 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# install.sh — install & enable the semprini-maintainer systemd service.
#
# Run on the core host (ubuntu@3.107.254.151), from this checkout, as the
# user the service runs as (ubuntu, not root; sudo is used for the systemd
# parts). Idempotent.
#
# One Matrix bot means one daemon. If config.json's repo host is reached over
# SSH (the daemon runs elsewhere), the service on that host is stopped and
# disabled before this one starts. A daemon on any other machine (the
# workstation's WSL, until 2026-11-26) must be stopped by hand first.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UNIT="semprini-maintainer.service"
DEST="/etc/systemd/system/$UNIT"
USER_NAME="$(id -un)"
die() { echo "ERROR: $*" >&2; exit 1; }
[ "$USER_NAME" != root ] || die "run as the service user, not root"
echo "→ Pre-flight checks"
[ "$(ps -p 1 -o comm=)" = systemd ] || die "systemd is not running"
for bin in python3 git ssh curl; do
command -v "$bin" >/dev/null || die "$bin not found"
done
if [ ! -f "$DIR/config.json" ]; then
die "config.json missing. Copy config.example.json to config.json, bring the tokens
over from the previous daemon's config.json (matrix, gitea), then set the
model's key: .venv/bin/python maintainer.py --set-api-key claude"
fi
chmod 600 "$DIR/config.json"
UV="$HOME/.local/bin/uv"
if [ ! -x "$UV" ]; then
echo "→ Installing uv"
curl -LsSf https://astral.sh/uv/install.sh | sh >/dev/null
fi
echo "→ Installing dependencies into .venv"
( cd "$DIR" && "$UV" sync --frozen --quiet )
PY="$DIR/.venv/bin/python"
cfg_get() {
"$PY" - "$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"])
elif sys.argv[2] == "core-ssh":
print(cfg["hosts"][cfg["repo_host"]].get("ssh", ""))
else:
print(cfg[sys.argv[2]])
PY
}
CORE="$(cfg_get core-ssh)"
if [ -n "$CORE" ]; then
echo "→ SSH to the core host ($CORE)"
if ! ssh -o BatchMode=yes -o ConnectTimeout=15 "$CORE" true 2>/dev/null; then
cat >&2 <<EOF
ERROR: cannot reach $CORE over SSH without a password. Once:
ssh-keygen -t ed25519 -N "" -C "semprini-maintainer@\$(hostname)" -f ~/.ssh/id_ed25519
ssh-keyscan -H ${CORE#*@} >> ~/.ssh/known_hosts
# from a machine that can already log in (Windows PowerShell, say):
# type \\\\wsl\$\\Ubuntu\\home\\$USER_NAME\\.ssh\\id_ed25519.pub | ssh $CORE "cat >> ~/.ssh/authorized_keys"
Then run install.sh again.
EOF
exit 1
fi
fi
echo "→ Checking maintainer.py, the config, the hosts and the models"
"$PY" "$DIR/maintainer.py" --config "$DIR/config.json" --check
# The daemon's own checkout fetches from Gitea as claude-code (self-update).
# Scoped to this repository.
read -r WEB HELPER <<<"$(cfg_get git-credential)" || true
if [ -n "${WEB:-}" ]; then
echo "→ Git credential helper for $WEB, in this checkout only"
git -C "$DIR" config "credential.$WEB.helper" "$HELPER"
else
echo "WARN: no Gitea token — the agent cannot open pull requests. Copy it from the host's config.json."
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"
if [ -n "$CORE" ]; then
echo "→ Retiring the daemon on $CORE (one Matrix bot, one daemon)"
ssh -o BatchMode=yes "$CORE" '
if systemctl is-enabled --quiet semprini-maintainer 2>/dev/null ||
systemctl is-active --quiet semprini-maintainer 2>/dev/null; then
sudo systemctl disable --now semprini-maintainer && echo " stopped and disabled"
else
echo " not running there"
fi' || die "could not stop the host's daemon; stop it by hand before starting this one:
ssh $CORE sudo systemctl disable --now semprini-maintainer"
fi
sudo systemctl restart "$UNIT"
cat <<EOF
✓ Installed and started.
Logs: journalctl -u $UNIT -f
Status: systemctl status $UNIT (or send 'status' in Matrix)
Stop: sudo systemctl stop $UNIT
Model: $PY $DIR/maintainer.py --try-model claude
EOF