49 lines
2.6 KiB
Bash
Executable File
49 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Native host runner, with pinned user-space tools and no extra CI images.
|
|
set -euo pipefail
|
|
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
[ "$(id -u)" -eq 0 ] || { echo 'Run with sudo on the runner host' >&2; exit 1; }
|
|
for tool in docker curl python3 git tar xz flock runuser systemctl; do
|
|
command -v "$tool" >/dev/null || { echo "Install missing prerequisite: $tool" >&2; exit 1; }
|
|
done
|
|
docker info >/dev/null
|
|
docker compose version >/dev/null
|
|
docker buildx version >/dev/null
|
|
id gitea-runner >/dev/null 2>&1 || useradd --system --create-home --home-dir /var/lib/gitea-runner --shell /usr/bin/bash gitea-runner
|
|
# Reuse the established service account and runner registration.
|
|
runner_home="$(getent passwd gitea-runner | cut -d: -f6)"
|
|
[ "$runner_home" = /var/lib/gitea-runner ] || { echo 'Unexpected runner home; inspect the existing service first' >&2; exit 1; }
|
|
runuser -u gitea-runner -- docker info >/dev/null || { echo "The runner user needs access to Docker before setup" >&2; exit 1; }
|
|
command -v gitea-runner >/dev/null || { echo 'Install gitea-runner 3.0.2 at /usr/local/bin/gitea-runner first' >&2; exit 1; }
|
|
mkdir -p /etc/gitea-runner
|
|
stamp="$(date -u +%Y%m%dT%H%M%SZ)"
|
|
for existing in /etc/gitea-runner/config.yaml /etc/systemd/system/gitea-runner.service; do
|
|
[ ! -f "$existing" ] || cp -p "$existing" "$existing.before-$stamp"
|
|
done
|
|
scratch="$(mktemp -d)"
|
|
trap 'rm -rf "$scratch"' EXIT
|
|
chmod 755 "$scratch"
|
|
install -m 0644 "$here/../workflows/install-ci-tools.sh" "$here/../workflows/tool-versions.env" "$scratch/"
|
|
runuser -u gitea-runner -- bash "$scratch/install-ci-tools.sh"
|
|
install -m 0644 "$here/config.yaml" /etc/gitea-runner/config.yaml
|
|
python3 - <<'PYLABELS'
|
|
import json
|
|
from pathlib import Path
|
|
registration = Path('/var/lib/gitea-runner/.runner')
|
|
if registration.exists():
|
|
labels = json.loads(registration.read_text()).get('labels', [])
|
|
labels = [label for label in labels if isinstance(label, str) and label.split(':')[0] != 'homelab']
|
|
labels.append('homelab:host')
|
|
config = Path('/etc/gitea-runner/config.yaml')
|
|
config.write_text(config.read_text().replace(' - homelab:host', '\n'.join(' - ' + json.dumps(label) for label in labels)))
|
|
PYLABELS
|
|
install -m 0644 "$here/gitea-runner.service" /etc/systemd/system/gitea-runner.service
|
|
if [ ! -f /var/lib/gitea-runner/.runner ]; then
|
|
echo 'Register once as gitea-runner with homelab:host before starting the service.'
|
|
exit 0
|
|
fi
|
|
systemctl daemon-reload
|
|
systemctl enable --now gitea-runner.service
|
|
systemctl restart gitea-runner.service
|
|
echo "Runner ready. Configuration backups: *.before-$stamp"
|