forked from heavy-duty/rig
116 lines
4.7 KiB
Bash
116 lines
4.7 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# rig forgejo-runner remove — take the service down and wipe this box's
|
||
|
|
# registration. Convergent: a box with nothing installed exits 0.
|
||
|
|
#
|
||
|
|
# There is no --local flag here, and its absence is the design. The GitHub
|
||
|
|
# sibling offers --local as an ESCAPE HATCH from a real deregistration
|
||
|
|
# handshake (config.sh remove --token, against an endpoint that mints removal
|
||
|
|
# tokens). Forgejo has no such handshake and no such endpoint: local is the
|
||
|
|
# only thing removal can ever be. Shipping the flag would advertise a
|
||
|
|
# server-side alternative that does not exist, and an operator would spend the
|
||
|
|
# afternoon hunting for the token that turns it off.
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
HERE="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
|
||
|
|
# shellcheck source=SCRIPTDIR/lib/forgejo-runner-config.sh
|
||
|
|
. "$HERE/lib/forgejo-runner-config.sh"
|
||
|
|
|
||
|
|
log() { printf 'rig-forgejo-runner: %s\n' "$*"; }
|
||
|
|
warn() { printf 'rig-forgejo-runner: WARNING: %s\n' "$*" >&2; }
|
||
|
|
die() { printf 'rig-forgejo-runner: ERROR: %s\n' "$1" >&2; exit "${2:-1}"; }
|
||
|
|
|
||
|
|
usage() {
|
||
|
|
cat <<'EOF'
|
||
|
|
usage: rig forgejo-runner remove [--user <name>]
|
||
|
|
|
||
|
|
--user <name> unprivileged service user (default: the tenant user `ci`
|
||
|
|
when it exists, else forgejo-runner)
|
||
|
|
|
||
|
|
Stops and disables the systemd service, then wipes this box's registration.
|
||
|
|
The binary and the user stay put, so a later `rig forgejo-runner install`
|
||
|
|
re-registers without downloading anything.
|
||
|
|
|
||
|
|
Forgejo has no runner deregistration endpoint, so this is always local-only:
|
||
|
|
the box is cleaned, and the runner stays listed as offline in the instance
|
||
|
|
until you delete it under Actions > Runners. No token is needed or asked for.
|
||
|
|
|
||
|
|
Convergent: safe to re-run; a box with no runner installed exits 0.
|
||
|
|
EOF
|
||
|
|
}
|
||
|
|
|
||
|
|
# --- args (validated before the root check, so errors are testable) ---------
|
||
|
|
RUNNER_USER=""
|
||
|
|
while [ $# -gt 0 ]; do
|
||
|
|
case "$1" in
|
||
|
|
--user)
|
||
|
|
[ $# -ge 2 ] || die "--user needs a value" 2
|
||
|
|
RUNNER_USER="$2"; shift 2 ;;
|
||
|
|
--local)
|
||
|
|
# Named rather than "unknown flag": it is the GitHub sibling's spelling,
|
||
|
|
# and the answer is that removal here is ALWAYS what --local means.
|
||
|
|
die "--local is not a flag here: Forgejo has no deregistration endpoint, so 'rig forgejo-runner remove' is always local-only. Re-run it without the flag, then delete the offline runner in the instance's Actions > Runners." 2 ;;
|
||
|
|
-h|--help) usage; exit 0 ;;
|
||
|
|
*) die "unknown flag: $1" 2 ;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
|
||
|
|
if [ -z "$RUNNER_USER" ]; then
|
||
|
|
if id -u ci >/dev/null 2>&1; then RUNNER_USER="ci"; else RUNNER_USER="forgejo-runner"; fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
# --- validation ------------------------------------------------------------
|
||
|
|
[ "$RUNNER_USER" != "root" ] || die "runner user must not be root" 2
|
||
|
|
|
||
|
|
# --- guards ----------------------------------------------------------------
|
||
|
|
[ "$(id -u)" -eq 0 ] || die "must run as root"
|
||
|
|
|
||
|
|
UNIT=/etc/systemd/system/forgejo-runner.service
|
||
|
|
|
||
|
|
# --- nothing to remove? -----------------------------------------------------
|
||
|
|
if ! id -u "$RUNNER_USER" >/dev/null 2>&1; then
|
||
|
|
log "no ${RUNNER_USER} user on this box; nothing to remove"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
USER_HOME="$(getent passwd "$RUNNER_USER" | cut -d: -f6)"
|
||
|
|
RUNNER_DIR="$USER_HOME/forgejo-runner"
|
||
|
|
if [ ! -e "$RUNNER_DIR/.runner" ] && [ ! -e "$UNIT" ]; then
|
||
|
|
log "no runner registered in ${RUNNER_DIR}; nothing to remove"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
INSTANCE="$(forgejo_runner_instance "$RUNNER_DIR")"
|
||
|
|
RUNNER_NAME="$(forgejo_runner_name "$RUNNER_DIR")"
|
||
|
|
|
||
|
|
# --- service ---------------------------------------------------------------
|
||
|
|
# First, in both paths: stopping after the registration is wiped would strand a
|
||
|
|
# running daemon polling with credentials that no longer exist on disk.
|
||
|
|
if [ -e "$UNIT" ]; then
|
||
|
|
log "stopping and disabling forgejo-runner.service"
|
||
|
|
systemctl stop forgejo-runner >/dev/null 2>&1 || true
|
||
|
|
systemctl disable forgejo-runner >/dev/null 2>&1 || true
|
||
|
|
rm -f "$UNIT"
|
||
|
|
systemctl daemon-reload
|
||
|
|
else
|
||
|
|
log "no service installed; skipping"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# --- registration -----------------------------------------------------------
|
||
|
|
if [ -e "$RUNNER_DIR/.runner" ]; then
|
||
|
|
rm -f "$RUNNER_DIR/.runner"
|
||
|
|
log "wiped the local registration"
|
||
|
|
fi
|
||
|
|
rm -f "$RUNNER_DIR/.rig-labels"
|
||
|
|
|
||
|
|
# END WITH THE ABSENCE ASSERT: "removed" is a claim, and claims get verified
|
||
|
|
# (the `rig uninstall` precedent).
|
||
|
|
leftover=""
|
||
|
|
[ -e "$RUNNER_DIR/.runner" ] && leftover="$leftover $RUNNER_DIR/.runner"
|
||
|
|
[ -e "$UNIT" ] && leftover="$leftover $UNIT"
|
||
|
|
if [ -n "$leftover" ]; then
|
||
|
|
printf 'rig-forgejo-runner: remove INCOMPLETE — still present:%s\n' "$leftover" >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
log "runner removed; the binary stays for a future rig forgejo-runner install"
|
||
|
|
warn "the runner${RUNNER_NAME:+ ${RUNNER_NAME}} is still listed as offline in ${INSTANCE:-the instance} — delete it under Actions > Runners. Forgejo has no deregistration endpoint, so rig cannot do this for you."
|