One-line runner install: /api/v1/ci/runners/install.sh sets up packages, a ci user with subids and linger, downloads the forge binary with the registration token (sha256 checked), runs check, registers and starts the user service
Browse files at this commit8298537b3448d5aa1298096891d65c16de20fa34Parent: 3cdd794Checks pending
Show checks
ci/buildQueuedDetails
- feat
- one-line runner install: /api/v1/ci/runners/install.sh sets up packages, a ci user with subids and linger, downloads the forge binary with the registration token (sha256 checked), runs check, registers and starts the user service
- --uninstall removes it
- the token page shows the command
6 changed files +241 -9
- crates/forge-web-app/src/ci/api.rs +40 -0
- crates/forge-web-app/src/ci/api_tests.rs +37 -0
- crates/forge-web-app/src/ci/install.sh +141 -0
- crates/forge-web-pages/src/pages/admin_runners.rs +5 -9
- crates/forge-web/src/routes.snap +2 -0
- docs/ci.md +16 -0
crates/forge-web-app/src/ci/api.rsmodified+40 -0
| @@ -476,6 +476,46 @@ async fn runner_binary(cx: &Cx) -> topcoat::Result<Response> { | |||
| 476 | } | 476 | } |
| 477 | } | 477 | } |
| 478 | 478 | ||
| 479 | +const INSTALL_SCRIPT: &str = include_str!("install.sh"); | ||
| 480 | + | ||
| 481 | +#[route(GET "/api/v1/ci/runners/install.sh")] | ||
| 482 | +async fn runner_install_script(cx: &Cx) -> topcoat::Result<Response> { | ||
| 483 | + let state = context::<AppState>(cx); | ||
| 484 | + let script = INSTALL_SCRIPT | ||
| 485 | + .replace("@URL@", state.config.base_url.trim_end_matches('/')) | ||
| 486 | + .replace("@TARGET@", &self_update::target()); | ||
| 487 | + let mut res = Response::new(Body::from(script)); | ||
| 488 | + res.headers_mut().insert( | ||
| 489 | + http::header::CONTENT_TYPE, | ||
| 490 | + HeaderValue::from_static("text/x-shellscript; charset=utf-8"), | ||
| 491 | + ); | ||
| 492 | + Ok(res) | ||
| 493 | +} | ||
| 494 | + | ||
| 495 | +#[route(GET "/api/v1/ci/runners/install/forge")] | ||
| 496 | +async fn runner_install_binary(cx: &Cx) -> topcoat::Result<Response> { | ||
| 497 | + let state = context::<AppState>(cx); | ||
| 498 | + let Some(token) = bearer_token(headers(cx)) else { | ||
| 499 | + return Ok(*unauthorized()); | ||
| 500 | + }; | ||
| 501 | + let hash = forge_core::token::hash_token(token); | ||
| 502 | + match state.store.registration_token_used(&hash).await { | ||
| 503 | + Ok(false) => {} | ||
| 504 | + Ok(true) => return Ok(*unauthorized()), | ||
| 505 | + Err(e) => return Ok(*api::internal(e)), | ||
| 506 | + } | ||
| 507 | + match self_update::binary_response().await { | ||
| 508 | + Ok(res) => Ok(res), | ||
| 509 | + Err(e) => { | ||
| 510 | + tracing::error!(error = %e, "ci: serving the install binary failed"); | ||
| 511 | + Ok(api::error( | ||
| 512 | + StatusCode::INTERNAL_SERVER_ERROR, | ||
| 513 | + "the binary is unavailable", | ||
| 514 | + )) | ||
| 515 | + } | ||
| 516 | + } | ||
| 517 | +} | ||
| 518 | + | ||
| 479 | fn empty(status: StatusCode) -> Response { | 519 | fn empty(status: StatusCode) -> Response { |
| 480 | let mut res = Response::new(topcoat::router::Body::empty()); | 520 | let mut res = Response::new(topcoat::router::Body::empty()); |
| 481 | *res.status_mut() = status; | 521 | *res.status_mut() = status; |
crates/forge-web-app/src/ci/api_tests.rsmodified+37 -0
| @@ -1381,3 +1381,40 @@ async fn claim_and_heartbeat_store_the_runner_build() { | |||
| 1381 | Some("0.1.0 (4d21658, 2026-10-19)") | 1381 | Some("0.1.0 (4d21658, 2026-10-19)") |
| 1382 | ); | 1382 | ); |
| 1383 | } | 1383 | } |
| 1384 | + | ||
| 1385 | +#[tokio::test] | ||
| 1386 | +async fn install_script_and_binary_follow_the_registration_token() { | ||
| 1387 | + let env = bare(|_| {}).await; | ||
| 1388 | + let script = send( | ||
| 1389 | + &env.router, | ||
| 1390 | + "GET", | ||
| 1391 | + "/api/v1/ci/runners/install.sh", | ||
| 1392 | + &[], | ||
| 1393 | + Vec::new(), | ||
| 1394 | + ) | ||
| 1395 | + .await; | ||
| 1396 | + assert_eq!(script.status, 200); | ||
| 1397 | + let text = String::from_utf8(script.body).unwrap(); | ||
| 1398 | + assert!(!text.contains("@URL@") && !text.contains("@TARGET@")); | ||
| 1399 | + | ||
| 1400 | + let uri = "/api/v1/ci/runners/install/forge"; | ||
| 1401 | + let token = env | ||
| 1402 | + .store | ||
| 1403 | + .create_registration_token(&env.owner_id, 24) | ||
| 1404 | + .await | ||
| 1405 | + .unwrap(); | ||
| 1406 | + assert_eq!( | ||
| 1407 | + send(&env.router, "GET", uri, &[], Vec::new()).await.status, | ||
| 1408 | + 401 | ||
| 1409 | + ); | ||
| 1410 | + let sent = send(&env.router, "GET", uri, &[bearer(&token)], Vec::new()).await; | ||
| 1411 | + assert_eq!(sent.status, 200); | ||
| 1412 | + assert!(sent.headers.contains_key("forge-binary-sha256")); | ||
| 1413 | + env.store.register_runner(&token, "r", &[]).await.unwrap(); | ||
| 1414 | + assert_eq!( | ||
| 1415 | + send(&env.router, "GET", uri, &[bearer(&token)], Vec::new()) | ||
| 1416 | + .await | ||
| 1417 | + .status, | ||
| 1418 | + 401 | ||
| 1419 | + ); | ||
| 1420 | +} | ||
crates/forge-web-app/src/ci/install.shadded+141 -0
| @@ -0,0 +1,141 @@ | |||
| 1 | +#!/bin/sh | ||
| 2 | +set -eu | ||
| 3 | + | ||
| 4 | +URL='@URL@' | ||
| 5 | +TARGET='@TARGET@' | ||
| 6 | + | ||
| 7 | +fail() { | ||
| 8 | + echo "error: $*" >&2 | ||
| 9 | + exit 1 | ||
| 10 | +} | ||
| 11 | + | ||
| 12 | +as_user() { | ||
| 13 | + systemd-run --machine="$RUNNER_USER@.host" --user --wait --pipe --quiet "$@" </dev/null | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +user_ctl() { | ||
| 17 | + systemctl --machine="$RUNNER_USER@.host" --user "$@" | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +add_range() { | ||
| 21 | + file=$1 | ||
| 22 | + flag=$2 | ||
| 23 | + grep -q "^$RUNNER_USER:" "$file" 2>/dev/null && return 0 | ||
| 24 | + start=$(cat /etc/subuid /etc/subgid 2>/dev/null | awk -F: 'BEGIN { m = 100000 } { e = $2 + $3; if (e > m) m = e } END { print m }') | ||
| 25 | + usermod "$flag" "$start-$((start + 65535))" "$RUNNER_USER" | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +install_packages() { | ||
| 29 | + if command -v dnf >/dev/null; then | ||
| 30 | + dnf install -y git passt shadow-utils systemd-container curl </dev/null | ||
| 31 | + elif command -v apt-get >/dev/null; then | ||
| 32 | + apt-get update </dev/null | ||
| 33 | + DEBIAN_FRONTEND=noninteractive apt-get install -y git passt uidmap systemd-container curl </dev/null | ||
| 34 | + else | ||
| 35 | + fail "no dnf or apt-get: install git, passt, newuidmap and systemd-container, then run this again" | ||
| 36 | + fi | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +uninstall() { | ||
| 40 | + id "$RUNNER_USER" >/dev/null 2>&1 || fail "there is no user $RUNNER_USER" | ||
| 41 | + home=$(getent passwd "$RUNNER_USER" | cut -d: -f6) | ||
| 42 | + [ -f "$home/.config/forge-runner/runner.toml" ] || fail "$RUNNER_USER has no forge runner, so it is not removed" | ||
| 43 | + loginctl disable-linger "$RUNNER_USER" || true | ||
| 44 | + loginctl terminate-user "$RUNNER_USER" 2>/dev/null || true | ||
| 45 | + pkill -KILL -u "$RUNNER_USER" || true | ||
| 46 | + sleep 1 | ||
| 47 | + userdel -r "$RUNNER_USER" 2>/dev/null || true | ||
| 48 | + case $home in | ||
| 49 | + /home/?*) [ -d "$home" ] && rm -rf --one-file-system -- "$home" ;; | ||
| 50 | + esac | ||
| 51 | + sed -i "/^$RUNNER_USER:/d" /etc/subuid /etc/subgid | ||
| 52 | + echo "removed $RUNNER_USER. Delete the runner at $URL/admin/runners too." | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +install_runner() { | ||
| 56 | + [ -n "$TOKEN" ] || fail "--token is required" | ||
| 57 | + [ "$(uname -m)-linux" = "$TARGET" ] || fail "this forge serves a $TARGET binary, this host is $(uname -m)-linux" | ||
| 58 | + command -v systemctl >/dev/null || fail "systemd is required" | ||
| 59 | + | ||
| 60 | + install_packages | ||
| 61 | + | ||
| 62 | + id "$RUNNER_USER" >/dev/null 2>&1 || useradd -m "$RUNNER_USER" | ||
| 63 | + add_range /etc/subuid --add-subuids | ||
| 64 | + add_range /etc/subgid --add-subgids | ||
| 65 | + loginctl enable-linger "$RUNNER_USER" | ||
| 66 | + systemctl start "user@$(id -u "$RUNNER_USER").service" | ||
| 67 | + | ||
| 68 | + home=$(getent passwd "$RUNNER_USER" | cut -d: -f6) | ||
| 69 | + group=$(id -gn "$RUNNER_USER") | ||
| 70 | + bin="$home/.local/bin/forge" | ||
| 71 | + unit_dir="$home/.config/systemd/user" | ||
| 72 | + runuser -u "$RUNNER_USER" -- mkdir -p "$home/.local/bin" "$unit_dir" | ||
| 73 | + | ||
| 74 | + tmp=$(mktemp) | ||
| 75 | + trap 'rm -f "$tmp" "$tmp.h"' EXIT | ||
| 76 | + curl -fsS -H "Authorization: Bearer $TOKEN" -D "$tmp.h" -o "$tmp" "$URL/api/v1/ci/runners/install/forge" \ | ||
| 77 | + || fail "download failed: the token may be wrong, used or expired" | ||
| 78 | + want=$(grep -i '^forge-binary-sha256:' "$tmp.h" | cut -d: -f2 | tr -d ' \r') | ||
| 79 | + got=$(sha256sum "$tmp" | cut -d' ' -f1) | ||
| 80 | + [ -n "$want" ] && [ "$want" = "$got" ] || fail "the downloaded binary does not match its checksum" | ||
| 81 | + install -o "$RUNNER_USER" -g "$group" -m 755 "$tmp" "$bin" | ||
| 82 | + | ||
| 83 | + as_user "$bin" runner check || fail "runner check failed: fix the FAIL lines above and run this again" | ||
| 84 | + | ||
| 85 | + if [ -f "$home/.config/forge-runner/runner.toml" ]; then | ||
| 86 | + echo "already registered, keeping $home/.config/forge-runner/runner.toml" | ||
| 87 | + elif [ -n "$LABELS" ]; then | ||
| 88 | + as_user "$bin" runner register --url "$URL" --token "$TOKEN" --name "$NAME" --labels "$LABELS" | ||
| 89 | + else | ||
| 90 | + as_user "$bin" runner register --url "$URL" --token "$TOKEN" --name "$NAME" | ||
| 91 | + fi | ||
| 92 | + | ||
| 93 | + cat >"$unit_dir/forge-runner.service" <<'EOF' | ||
| 94 | +[Unit] | ||
| 95 | +Description=forge CI runner | ||
| 96 | +After=network-online.target | ||
| 97 | +Wants=network-online.target | ||
| 98 | + | ||
| 99 | +[Service] | ||
| 100 | +ExecStart=%h/.local/bin/forge runner run --config %h/.config/forge-runner/runner.toml | ||
| 101 | +Restart=on-failure | ||
| 102 | +RestartSec=10 | ||
| 103 | +TimeoutStopSec=120 | ||
| 104 | + | ||
| 105 | +[Install] | ||
| 106 | +WantedBy=default.target | ||
| 107 | +EOF | ||
| 108 | + chown "$RUNNER_USER:$group" "$unit_dir/forge-runner.service" | ||
| 109 | + user_ctl daemon-reload | ||
| 110 | + user_ctl enable --now forge-runner.service | ||
| 111 | + echo "runner $NAME is running as $RUNNER_USER. Logs: journalctl _SYSTEMD_USER_UNIT=forge-runner.service -f" | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +main() { | ||
| 115 | + RUNNER_USER=ci | ||
| 116 | + TOKEN= | ||
| 117 | + NAME=$(hostname) | ||
| 118 | + LABELS= | ||
| 119 | + MODE=install | ||
| 120 | + while [ $# -gt 0 ]; do | ||
| 121 | + case $1 in | ||
| 122 | + --token) TOKEN=$2; shift 2 ;; | ||
| 123 | + --name) NAME=$2; shift 2 ;; | ||
| 124 | + --labels) LABELS=$2; shift 2 ;; | ||
| 125 | + --user) RUNNER_USER=$2; shift 2 ;; | ||
| 126 | + --uninstall) MODE=uninstall; shift ;; | ||
| 127 | + *) fail "unknown option: $1" ;; | ||
| 128 | + esac | ||
| 129 | + done | ||
| 130 | + [ "$(id -u)" = 0 ] || fail "run this as root" | ||
| 131 | + case $RUNNER_USER in | ||
| 132 | + root | "" | *[!a-z0-9_-]*) fail "--user must be a plain non-root user name" ;; | ||
| 133 | + esac | ||
| 134 | + if [ "$MODE" = uninstall ]; then | ||
| 135 | + uninstall | ||
| 136 | + else | ||
| 137 | + install_runner | ||
| 138 | + fi | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +main "$@" | ||
crates/forge-web-pages/src/pages/admin_runners.rsmodified+5 -9
| @@ -253,7 +253,7 @@ fn back_to_list(notice: &str) -> SeeOther { | |||
| 253 | 253 | ||
| 254 | fn register_command(base_url: &str, token: &str) -> String { | 254 | fn register_command(base_url: &str, token: &str) -> String { |
| 255 | format!( | 255 | format!( |
| 256 | - "forge runner register \\\n --url {} \\\n --token {token} \\\n --name <name> \\\n --labels linux,x86_64 \\\n --config ~/.config/forge-runner/runner.toml", | 256 | + "curl -fsSL {}/api/v1/ci/runners/install.sh | sudo sh -s -- --token {token}", |
| 257 | base_url.trim_end_matches('/') | 257 | base_url.trim_end_matches('/') |
| 258 | ) | 258 | ) |
| 259 | } | 259 | } |
| @@ -302,7 +302,7 @@ async fn runners_view( | |||
| 302 | if rows.is_empty() { | 302 | if rows.is_empty() { |
| 303 | <section class="sec"> | 303 | <section class="sec"> |
| 304 | <h2>(g::t(lang, K::AddARunner))</h2> | 304 | <h2>(g::t(lang, K::AddARunner))</h2> |
| 305 | - <pre><code>"forge runner register --url <forge-url> --token <token> --name <name> --labels linux,x86_64"</code></pre> | 305 | + <pre><code>"curl -fsSL <forge-url>/api/v1/ci/runners/install.sh | sudo sh -s -- --token <token>"</code></pre> |
| 306 | </section> | 306 | </section> |
| 307 | } | 307 | } |
| 308 | </div> | 308 | </div> |
| @@ -1888,14 +1888,14 @@ mod tests { | |||
| 1888 | let cmd = register_command("https://forge.example/", "tok123"); | 1888 | let cmd = register_command("https://forge.example/", "tok123"); |
| 1889 | assert_eq!( | 1889 | assert_eq!( |
| 1890 | cmd, | 1890 | cmd, |
| 1891 | - "forge runner register \\\n --url https://forge.example \\\n --token tok123 \\\n --name <name> \\\n --labels linux,x86_64 \\\n --config ~/.config/forge-runner/runner.toml" | 1891 | + "curl -fsSL https://forge.example/api/v1/ci/runners/install.sh | sudo sh -s -- --token tok123" |
| 1892 | ); | 1892 | ); |
| 1893 | } | 1893 | } |
| 1894 | 1894 | ||
| 1895 | fn token_from(page: &str) -> String { | 1895 | fn token_from(page: &str) -> String { |
| 1896 | page.split("--token ") | 1896 | page.split("--token ") |
| 1897 | .nth(1) | 1897 | .nth(1) |
| 1898 | - .and_then(|rest| rest.split(' ').next()) | 1898 | + .and_then(|rest| rest.split([' ', '<']).next()) |
| 1899 | .expect("token") | 1899 | .expect("token") |
| 1900 | .to_owned() | 1900 | .to_owned() |
| 1901 | } | 1901 | } |
| @@ -2011,11 +2011,7 @@ mod tests { | |||
| 2011 | .await; | 2011 | .await; |
| 2012 | assert_eq!(status, 200); | 2012 | assert_eq!(status, 200); |
| 2013 | let token = token_from(&created); | 2013 | let token = token_from(&created); |
| 2014 | - assert!(created.contains("forge runner register \\\n --url")); | 2014 | + assert!(created.contains("/api/v1/ci/runners/install.sh | sudo sh -s -- --token ")); |
| 2015 | - assert!( | ||
| 2016 | - created.contains("--name <name> \\\n --labels linux,x86_64 \\\n --config ~/.config/forge-runner/runner.toml"), | ||
| 2017 | - "placeholder is escaped" | ||
| 2018 | - ); | ||
| 2019 | assert_eq!( | 2015 | assert_eq!( |
| 2020 | created.matches(&token).count(), | 2016 | created.matches(&token).count(), |
| 2021 | 2, | 2017 | 2, |
crates/forge-web/src/routes.snapmodified+2 -0
| @@ -240,6 +240,8 @@ route /api/v1/ci/runners/binary Only([GET]) | |||
| 240 | route /api/v1/ci/runners/claim Only([POST]) | 240 | route /api/v1/ci/runners/claim Only([POST]) |
| 241 | route /api/v1/ci/runners/config Only([GET]) | 241 | route /api/v1/ci/runners/config Only([GET]) |
| 242 | route /api/v1/ci/runners/config Only([PUT]) | 242 | route /api/v1/ci/runners/config Only([PUT]) |
| 243 | +route /api/v1/ci/runners/install.sh Only([GET]) | ||
| 244 | +route /api/v1/ci/runners/install/forge Only([GET]) | ||
| 243 | route /api/v1/ci/runners/log Only([POST]) | 245 | route /api/v1/ci/runners/log Only([POST]) |
| 244 | route /api/v1/ci/runners/metrics Only([POST]) | 246 | route /api/v1/ci/runners/metrics Only([POST]) |
| 245 | route /api/v1/ci/runners/register Only([POST]) | 247 | route /api/v1/ci/runners/register Only([POST]) |
docs/ci.mdmodified+16 -0
| @@ -483,6 +483,22 @@ binary, so an operator deploys one file. The default `builtin` executor needs no | |||
| 483 | The `forge-runner` binary is a development and test convenience only. It has the same subcommands, | 483 | The `forge-runner` binary is a development and test convenience only. It has the same subcommands, |
| 484 | but operators should use `forge runner`, which the examples below show. | 484 | but operators should use `forge runner`, which the examples below show. |
| 485 | 485 | ||
| 486 | +### Quick install | ||
| 487 | + | ||
| 488 | +On a fresh Linux host with systemd and `dnf` or `apt-get`, create a registration token at | ||
| 489 | +`/admin/runners` and run the command the page shows, as root: | ||
| 490 | + | ||
| 491 | +```sh | ||
| 492 | +curl -fsSL https://forge.example/api/v1/ci/runners/install.sh | sudo sh -s -- --token <token> | ||
| 493 | +``` | ||
| 494 | + | ||
| 495 | +It installs `git`, `passt`, `newuidmap` and `systemd-container`, creates the user `ci` with a | ||
| 496 | +subordinate id range and lingering, downloads the forge's own binary (the token authorizes the | ||
| 497 | +download, and the script checks its sha256), runs `forge runner check`, registers, and starts the | ||
| 498 | +systemd user service. Options: `--name` (default: the hostname), `--labels`, `--user` (default | ||
| 499 | +`ci`). `--uninstall` removes the runner user, its files and its id ranges; it refuses a user that | ||
| 500 | +has no `runner.toml`. The steps below do the same by hand. | ||
| 501 | + | ||
| 486 | ### 1. Check the host | 502 | ### 1. Check the host |
| 487 | 503 | ||
| 488 | The builtin executor needs: | 504 | The builtin executor needs: |