#!/usr/bin/env bash # install.sh — install & enable the semprini-maintainer systemd service. # # Run on the operator's workstation, in WSL (Ubuntu, systemd enabled), from # this checkout, as the user the service runs as (not root; sudo is used for # the systemd parts). Idempotent. # # It also takes over from the daemon on the core host: the two share one # Matrix bot, so they must never run at once. Just before the local service # starts, the host's service is stopped and disabled. The host keeps its # checkout and its git credentials: sync-to-host.sh's `--hold` and the host's # own pushes still use them. 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" grep -qi microsoft /proc/version || echo "WARN: this does not look like WSL; carrying on" [ "$(ps -p 1 -o comm=)" = systemd ] || die "systemd is not running: add [boot] systemd=true to /etc/wsl.conf, then wsl --shutdown" for bin in python3 git ssh curl; do command -v "$bin" >/dev/null || die "$bin not found" done case "$DIR" in /mnt/*) echo "WARN: $DIR is on the Windows filesystem; clone into ~ for speed and file modes";; esac if [ ! -f "$DIR/config.json" ]; then die "config.json missing. Copy config.example.json to config.json, bring the tokens over from the host's config.json (matrix, gitea), and adjust the paths." 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"]]["ssh"]) else: print(cfg[sys.argv[2]]) PY } CORE="$(cfg_get core-ssh)" echo "→ SSH to the core host ($CORE)" if ! ssh -o BatchMode=yes -o ConnectTimeout=15 "$CORE" true 2>/dev/null; then cat >&2 <> ~/.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 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: WSL is also the operator's shell. 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" 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" sudo systemctl restart "$UNIT" cat <