Skip to content
758 linesCodeBlameRaw
1//! `g1t-runner` on your own machine: a self-hosted runner for g1t.
2//!
3//! ```text
4//! g1t-runner register --url https://g1t.sh --token g1trt_… [--name build-01]
5//! [--labels gpu,cuda] [--group Default] [--ephemeral] [--work-dir DIR]
6//! [--no-docker] [--image IMAGE] [--replace]
7//! [--memory 8g] [--cpus 4] [--pids-limit 4096] [--no-job-docker]
8//! g1t-runner run [--once]
9//! g1t-runner service install|start|stop|status|uninstall
10//! g1t-runner remove
11//! g1t-runner update
12//! g1t-runner version
13//! ```
14//!
15//! It only ever calls out, to `api.g1t.sh` over HTTPS: it polls for work
16//! (a poll waits up to 20 seconds for some, and is the runner's heartbeat),
17//! runs what it is given with the same harness g1t's sandboxes run (this
18//! program in another mode, see `exec`), and says how it ended. Nothing
19//! listens on the machine. Every command takes `--dir` (default
20//! `~/.g1t-runner`, or `G1T_RUNNER_DIR`), where its configuration is kept.
21//!
22//! How it keeps jobs from the machine is `sandbox`: in its image under
23//! Sysbox (`G1T_RUNNER_SANDBOX=nested`) it runs its own Docker Engine
24//! (`engine`) and refuses to start if it can reach the machine's.
25
26mod api;
27mod config;
28#[cfg(unix)]
29mod engine;
30mod exec;
31mod launcher;
32mod native;
33mod private;
34mod sandbox;
35mod service;
36mod update;
37
38use std::path::{Path, PathBuf};
39use std::sync::atomic::{AtomicBool, Ordering};
40use std::time::{Duration, Instant};
41
42use anyhow::{Context, Result, bail};
43use serde_json::json;
44
45use api::{Api, Failure};
46use config::Config;
47
48pub const VERSION: &str = env!("CARGO_PKG_VERSION");
49
50/// Set to stop the loop between polls: a Windows service's stop request.
51pub static STOP: AtomicBool = AtomicBool::new(false);
52
53/// How long a busy runner waits between heartbeats.
54const HEARTBEAT: Duration = Duration::from_secs(10);
55/// How long an idle poll waits for work.
56const IDLE_WAIT_MS: u64 = 20_000;
57/// How often an idle runner checks for a new version.
58const UPDATE_EVERY: Duration = Duration::from_secs(6 * 60 * 60);
59
60const COMMANDS: &[&str] = &["register", "register-and-run", "run", "service", "sandbox", "remove", "update", "version", "--version", "help", "--help", "-h"];
61
62/// Whether `args` (without the program) are a command of this one, rather
63/// than the harness's `MODE`.
64pub fn is_command(args: &[String]) -> bool {
65 args.first().is_some_and(|first| COMMANDS.contains(&first.as_str()))
66}
67
68pub fn log(message: &str) {
69 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0);
70 println!("{} {message}", g1t_now(now));
71}
72
73/// RFC 3339 for the log, without pulling in a date library.
74fn g1t_now(ms: u64) -> String {
75 let seconds = ms / 1000;
76 let (days, rest) = (seconds / 86_400, seconds % 86_400);
77 let z = days as i64 + 719_468;
78 let era = z.div_euclid(146_097);
79 let doe = z.rem_euclid(146_097);
80 let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
81 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
82 let mp = (5 * doy + 2) / 153;
83 let day = doy - (153 * mp + 2) / 5 + 1;
84 let month = if mp < 10 { mp + 3 } else { mp - 9 };
85 let year = yoe + era * 400 + i64::from(month <= 2);
86 format!("{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z", rest / 3600, rest % 3600 / 60, rest % 60)
87}
88
89/// `--name value` and `--flag` options.
90struct Options {
91 values: Vec<(String, String)>,
92 flags: Vec<String>,
93 rest: Vec<String>,
94}
95
96impl Options {
97 fn parse(args: &[String], takes_value: &[&str]) -> Result<Options> {
98 let mut options = Options { values: Vec::new(), flags: Vec::new(), rest: Vec::new() };
99 let mut i = 0;
100 while i < args.len() {
101 let arg = &args[i];
102 if let Some(name) = arg.strip_prefix("--") {
103 if let Some((name, value)) = name.split_once('=') {
104 options.values.push((name.to_owned(), value.to_owned()));
105 } else if takes_value.contains(&name) {
106 let Some(value) = args.get(i + 1) else { bail!("--{name} needs a value") };
107 options.values.push((name.to_owned(), value.clone()));
108 i += 1;
109 } else {
110 options.flags.push(name.to_owned());
111 }
112 } else {
113 options.rest.push(arg.clone());
114 }
115 i += 1;
116 }
117 Ok(options)
118 }
119
120 fn value(&self, name: &str) -> Option<&str> {
121 self.values.iter().rev().find(|(n, _)| n == name).map(|(_, v)| v.as_str())
122 }
123
124 fn flag(&self, name: &str) -> bool {
125 self.flags.iter().any(|f| f == name)
126 }
127
128 /// For `register-and-run` in a container: each option may come from
129 /// `G1T_RUNNER_<NAME>` instead (`G1T_RUNNER_TOKEN`, `G1T_RUNNER_LABELS`,
130 /// `G1T_RUNNER_EPHEMERAL=1`), so a Kubernetes secret can hold the token.
131 fn with_environment(mut self) -> Options {
132 let env_name = |name: &str| format!("G1T_RUNNER_{}", name.to_ascii_uppercase().replace('-', "_"));
133 for name in VALUE_OPTIONS {
134 if self.value(name).is_none()
135 && let Ok(value) = std::env::var(env_name(name))
136 && !value.is_empty()
137 {
138 self.values.push((name.to_owned(), value));
139 }
140 }
141 for name in ["ephemeral", "no-docker", "replace", "no-auto-update", "no-job-docker", "native"] {
142 if !self.flag(name) && std::env::var(env_name(name)).is_ok_and(|v| matches!(v.as_str(), "1" | "true" | "yes")) {
143 self.flags.push(name.to_owned());
144 }
145 }
146 self
147 }
148}
149
150/// The options that take a value.
151const VALUE_OPTIONS: [&str; 14] =
152 ["url", "api", "token", "name", "labels", "group", "work-dir", "image", "harness", "memory", "cpus", "pids-limit", "job-docker-image", "machine"];
153
154const HELP: &str = "g1t-runner: a self-hosted runner for g1t.
155
156 g1t-runner register --url https://g1t.sh --token <registration token>
157 [--name NAME] [--labels a,b] [--group GROUP] [--ephemeral]
158 [--work-dir DIR] [--no-docker] [--image IMAGE]
159 [--api URL] [--harness PATH] [--replace] [--no-auto-update]
160 [--memory 8g] [--cpus 4] [--pids-limit 4096] [--no-job-docker]
161 [--job-docker-image IMAGE] [--native]
162 g1t-runner run [--once] poll for work and run it
163 g1t-runner sandbox up|down|status|update [--json]
164 on your own computer: a Linux VM of the
165 runner's own for Linux jobs, and Windows
166 Sandbox for Windows jobs; see
167 `g1t-runner sandbox help`
168 g1t-runner register-and-run ... both, for containers; options may be
169 G1T_RUNNER_URL, _TOKEN, _LABELS, _EPHEMERAL…
170 g1t-runner service install|start|stop|status|uninstall
171 g1t-runner remove unregister this runner
172 g1t-runner update update to the newest release
173 g1t-runner version
174
175In a workflow step (the g1t/deploy and g1t/detect actions use these):
176 g1t-runner deploy build this checkout and put it on g1t.page
177 g1t-runner detect [--root-dir DIR] what that build would find
178
179Every command takes --dir DIR (default ~/.g1t-runner, or G1T_RUNNER_DIR).
180Run it as a container under Sysbox so neither it nor its jobs reach the
181machine: docker run --runtime=sysbox-runc … g1t.sh/flagon-io/g1t-runner,
182or on a desktop, g1t-runner sandbox up. --native runs jobs native to this
183OS, each in a throwaway VM (Windows Sandbox). --no-docker (host mode) is
184deprecated.
185Make a registration token under Settings, Runners on g1t.sh.
186Docs: https://docs.g1t.sh/guides/self-hosted-runners/";
187
188/// Runs a command. Returns the process's exit code.
189pub fn main(args: Vec<String>) -> i32 {
190 // Options of its own, and output the desktop app reads.
191 if args[0] == "sandbox" {
192 return launcher::main(&args[1..]);
193 }
194 let mut takes = VALUE_OPTIONS.to_vec();
195 takes.push("dir");
196 let options = match Options::parse(&args[1..], &takes) {
197 Ok(options) => options,
198 Err(error) => {
199 eprintln!("g1t-runner: {error:#}");
200 return 2;
201 }
202 };
203 let folder = config::folder(options.value("dir"));
204 let outcome = match args[0].as_str() {
205 "register" => register(&options, &folder),
206 // A container's one command: register unless it already is, then run.
207 "register-and-run" => {
208 let options = options.with_environment();
209 let ready = if config::load(&folder).is_ok() { Ok(()) } else { register(&options, &folder) };
210 ready.and_then(|()| run(&folder, options.flag("once")))
211 }
212 "run" => run(&folder, options.flag("once")),
213 "service" => match options.rest.first().map(String::as_str) {
214 Some(action) => config::load(&folder).and_then(|config| service::main(action, &config, &folder)),
215 None => Err(anyhow::anyhow!("service takes install, uninstall, start, stop or status")),
216 },
217 "remove" => remove(&folder),
218 "update" => config::load(&folder).and_then(|config| match update::update(&config)? {
219 Some(version) => {
220 log(&format!("Updated to {version}. Restart the runner (or its service) to use it."));
221 Ok(())
222 }
223 None => {
224 log(&format!("{VERSION} is the newest release."));
225 Ok(())
226 }
227 }),
228 "version" | "--version" => {
229 println!("g1t-runner {VERSION} ({})", update::platform());
230 Ok(())
231 }
232 _ => {
233 println!("{HELP}");
234 Ok(())
235 }
236 };
237 match outcome {
238 Ok(()) => 0,
239 Err(error) => {
240 eprintln!("g1t-runner: {error:#}");
241 1
242 }
243 }
244}
245
246fn hostname() -> String {
247 for name in ["COMPUTERNAME", "HOSTNAME"] {
248 if let Ok(value) = std::env::var(name).map(|v| v.trim().to_owned())
249 && !value.is_empty()
250 {
251 return value;
252 }
253 }
254 std::process::Command::new("hostname")
255 .output()
256 .ok()
257 .map(|out| String::from_utf8_lossy(&out.stdout).trim().to_owned())
258 .filter(|name| !name.is_empty())
259 .unwrap_or_else(|| "runner".to_owned())
260}
261
262/// Which computer this is, for g1t to know it when it registers again (its
263/// sandbox made again, its state lost) and let it take its runners' names
264/// back: a hash of the operating system's machine id, never the id itself.
265/// None in a container, whose machine id is its image's and so shared by
266/// every container made from it (the sandbox's launcher passes the
267/// computer's as `G1T_RUNNER_MACHINE`), and where the OS has none.
268pub fn machine_fingerprint() -> Option<String> {
269 use sha2::{Digest, Sha256};
270 let id = os_machine_id()?;
271 Some(hex::encode(Sha256::digest(format!("g1t-runner machine\n{id}"))))
272}
273
274fn os_machine_id() -> Option<String> {
275 let id = if cfg!(windows) {
276 // HKLM\SOFTWARE\Microsoft\Cryptography\MachineGuid, set when Windows is installed.
277 let out = hidden(std::process::Command::new("reg").args(["query", r"HKLM\SOFTWARE\Microsoft\Cryptography", "/v", "MachineGuid"])).output().ok()?;
278 String::from_utf8_lossy(&out.stdout).lines().find(|l| l.contains("MachineGuid"))?.split_whitespace().last()?.to_owned()
279 } else if cfg!(target_os = "macos") {
280 let out = std::process::Command::new("ioreg").args(["-rd1", "-c", "IOPlatformExpertDevice"]).output().ok()?;
281 let text = String::from_utf8_lossy(&out.stdout).into_owned();
282 text.lines().find(|l| l.contains("\"IOPlatformUUID\""))?.rsplit('"').nth(1)?.to_owned()
283 } else {
284 if Path::new("/.dockerenv").exists() || Path::new("/run/.containerenv").exists() {
285 return None;
286 }
287 ["/etc/machine-id", "/var/lib/dbus/machine-id"].iter().find_map(|path| std::fs::read_to_string(path).ok())?
288 };
289 let id = id.trim().to_ascii_lowercase();
290 (id.len() >= 8 && id.chars().any(|c| c != '0' && c != '-')).then_some(id)
291}
292
293/// No console window for a helper the runner runs on Windows.
294fn hidden(command: &mut std::process::Command) -> &mut std::process::Command {
295 #[cfg(windows)]
296 {
297 use std::os::windows::process::CommandExt;
298 const CREATE_NO_WINDOW: u32 = 0x0800_0000;
299 command.creation_flags(CREATE_NO_WINDOW);
300 }
301 command
302}
303
304/// A runner's name from a machine's: what g1t accepts.
305fn runner_name(given: &str) -> String {
306 let name: String = given
307 .chars()
308 .map(|c| if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') { c } else { '-' })
309 .take(64)
310 .collect();
311 if name.is_empty() { "runner".into() } else { name }
312}
313
314fn register(options: &Options, folder: &Path) -> Result<()> {
315 let Some(url) = options.value("url") else { bail!("--url is needed: the g1t you register with, such as https://g1t.sh") };
316 let Some(token) = options.value("token") else { bail!("--token is needed: make a registration token under Settings, Runners") };
317 let url = url.trim_end_matches('/').to_owned();
318 let api = options.value("api").map(|a| a.trim_end_matches('/').to_owned()).unwrap_or_else(|| config::api_for(&url));
319 let name = runner_name(options.value("name").map(str::to_owned).unwrap_or_else(hostname).as_str());
320 let labels: Vec<String> = options
321 .value("labels")
322 .unwrap_or_default()
323 .split(',')
324 .map(|l| l.trim().to_owned())
325 .filter(|l| !l.is_empty())
326 .collect();
327 let native = options.flag("native");
328 if native && options.flag("no-docker") {
329 bail!("--native runs each job in a VM of its own, and --no-docker runs it on this machine: pick one");
330 }
331 let docker = !options.flag("no-docker") && !native;
332 let isolation = isolation(docker, native)?;
333 if isolation == sandbox::Isolation::Host {
334 log(sandbox::HOST_MODE_AT_START);
335 } else if !native && !sandbox::nested_requested() && !exec::docker_ready() {
336 log("Docker does not answer here. Jobs will fail until it does.");
337 }
338 let pids_limit = match options.value("pids-limit") {
339 Some(text) => Some(text.parse::<u32>().ok().filter(|n| *n > 0).with_context(|| format!("--pids-limit takes a number of processes, not {text}"))?),
340 None => None,
341 };
342 let existing = config::load(folder).ok();
343 if existing.is_some() && !options.flag("replace") {
344 bail!("{} already holds a registered runner: run `g1t-runner remove` first, or register with --replace or another --dir", folder.display());
345 }
346 // The OS jobs run on: in Docker that is Linux, whatever the machine
347 // (Docker Desktop runs Linux containers on macOS and Windows). A
348 // runner says it runs jobs native to this OS only when it sandboxes
349 // them (--native), or when the person chose host mode (--no-docker).
350 let os = match std::env::consts::OS {
351 _ if docker => "linux",
352 "macos" => "macos",
353 "windows" => "windows",
354 _ => "linux",
355 };
356 let arch = if std::env::consts::ARCH == "aarch64" { "arm64" } else { "x64" };
357 let registered = Api::register(
358 &api,
359 &json!({
360 "token": token,
361 "name": name,
362 "labels": labels,
363 "os": os,
364 "arch": arch,
365 "version": VERSION,
366 "ephemeral": options.flag("ephemeral"),
367 "group": options.value("group"),
368 "replace": options.flag("replace"),
369 "isolation": isolation.as_str(),
370 "machine": options.value("machine").map(str::to_owned).or_else(machine_fingerprint),
371 }),
372 )?;
373 let work_dir = options.value("work-dir").map(PathBuf::from).unwrap_or_else(|| folder.join("work"));
374 let config = Config {
375 url,
376 api,
377 runner: registered.runner.id.clone(),
378 name: registered.runner.name.clone(),
379 credential: registered.credential,
380 workspace: registered.runner.workspace.clone(),
381 repo: registered.runner.repo.clone(),
382 group: registered.runner.group.clone(),
383 labels: registered.runner.labels.clone(),
384 ephemeral: options.flag("ephemeral"),
385 work_dir,
386 docker,
387 image: options.value("image").map(str::to_owned),
388 harness: options.value("harness").map(PathBuf::from),
389 auto_update: !options.flag("no-auto-update"),
390 memory: options.value("memory").map(str::to_owned),
391 cpus: options.value("cpus").map(str::to_owned),
392 pids_limit,
393 job_docker: !options.flag("no-job-docker"),
394 job_docker_image: options.value("job-docker-image").map(str::to_owned),
395 native,
396 };
397 config::save(folder, &config)?;
398 let place = match &config.repo {
399 Some(repo) => format!("for {repo}"),
400 None => format!("in {}{}", config.workspace, config.group.as_ref().map(|g| format!(", group {g}")).unwrap_or_default()),
401 };
402 log(&format!("Registered {} {place}, with labels {}.", config.name, config.labels.join(", ")));
403 log("Start it with `g1t-runner run`, or `g1t-runner service install` to keep it running.");
404 Ok(())
405}
406
407/// How this runner keeps work from its machine; refuses a nested runner
408/// that can reach the machine (sandbox.rs).
409fn isolation(docker: bool, native: bool) -> Result<sandbox::Isolation> {
410 use sandbox::{Isolation, SandboxDriver};
411 if native {
412 let Some(driver) = sandbox::native_driver() else {
413 bail!("--native: there is no sandbox for jobs native to {}; Linux jobs run in containers", std::env::consts::OS);
414 };
415 return driver.preflight().with_context(|| format!("--native needs {}", driver.name()));
416 }
417 if !docker {
418 // What would sandbox jobs native to this OS instead.
419 if let Some(driver) = sandbox::native_driver() {
420 match driver.preflight() {
421 Ok(_) => log(&format!("This machine can run {} jobs sandboxed, each in {}: register with --native instead of --no-docker.", std::env::consts::OS, driver.name())),
422 Err(why) => log(&format!("Sandboxed {} jobs, in {}: {why:#}", std::env::consts::OS, driver.name())),
423 }
424 }
425 return Ok(Isolation::Host);
426 }
427 if !sandbox::nested_requested() {
428 return sandbox::SharedDocker.preflight();
429 }
430 if !cfg!(target_os = "linux") {
431 bail!("{}=nested is for the runner's Linux image", sandbox::SANDBOX_ENV);
432 }
433 let isolation = sandbox::NestedDocker { allow_unsafe: sandbox::unsafe_allowed() }.preflight()?;
434 if isolation != Isolation::NestedContainer {
435 for problem in sandbox::Findings::read().problems() {
436 log(&format!("UNSAFE: {problem}"));
437 }
438 log(&format!("UNSAFE: {}=1 is set, so the runner starts anyway, and its jobs can reach this machine.", sandbox::UNSAFE_ENV));
439 }
440 Ok(isolation)
441}
442
443/// A nested runner's own Docker Engine, which it uses whatever else it
444/// can see (with the override, it starts despite that).
445fn prepare(docker: bool) -> Result<()> {
446 if !docker || !sandbox::nested_requested() {
447 return Ok(());
448 }
449 #[cfg(unix)]
450 {
451 let version = engine::start()?;
452 log(&format!("Started this runner's own Docker Engine {version}, inside its container."));
453 }
454 Ok(())
455}
456
457fn remove(folder: &Path) -> Result<()> {
458 let config = config::load(folder)?;
459 let api = Api { base: config.api.clone(), runner: config.runner.clone(), credential: config.credential.clone() };
460 match api.remove() {
461 Ok(()) | Err(Failure::Refused(401, _)) | Err(Failure::Refused(404, _)) => {}
462 Err(failure) => bail!("{failure}; the runner is still registered"),
463 }
464 config::forget(folder);
465 log(&format!("Removed {}.", config.name));
466 Ok(())
467}
468
469/// The loop, for a Windows service: its exit code.
470#[cfg_attr(not(windows), allow(dead_code))]
471pub fn serve_from_service() -> i32 {
472 let folder = config::folder(None);
473 let folder = std::env::args()
474 .collect::<Vec<_>>()
475 .windows(2)
476 .find(|pair| pair[0] == "--dir")
477 .map(|pair| PathBuf::from(&pair[1]))
478 .unwrap_or(folder);
479 match run(&folder, false) {
480 Ok(()) => 0,
481 Err(error) => {
482 eprintln!("g1t-runner: {error:#}");
483 1
484 }
485 }
486}
487
488/// Sleeps up to `duration`, waking early when `stop` says so.
489fn nap(duration: Duration, mut stop: impl FnMut() -> bool) {
490 let until = Instant::now() + duration;
491 while Instant::now() < until {
492 if STOP.load(Ordering::SeqCst) || stop() {
493 return;
494 }
495 std::thread::sleep(Duration::from_millis(250));
496 }
497}
498
499fn run(folder: &Path, once: bool) -> Result<()> {
500 // Its folder holds its credential and, while a Windows job runs, that
501 // job's script: a runner whose folder others can read does not start.
502 let mut config = config::load(folder)?;
503 private::folder(folder, "--dir")?;
504 let mut api = Api { base: config.api.clone(), runner: config.runner.clone(), credential: config.credential.clone() };
505 std::fs::create_dir_all(&config.work_dir).with_context(|| format!("could not make {}", config.work_dir.display()))?;
506 let isolation = isolation(config.docker, config.native)?;
507 prepare(config.docker)?;
508 match isolation {
509 sandbox::Isolation::Host => {}
510 sandbox::Isolation::Vm => native::sweep(folder),
511 _ => exec::sweep(&config),
512 }
513 // A drain or stop asked for before this start is done with.
514 let _ = std::fs::remove_file(drain_path(folder));
515 let _ = std::fs::remove_file(folder.join("stop"));
516 log(&format!(
517 "g1t-runner {VERSION}: {} ({}) is listening for work from {}. Isolation: {}.",
518 config.name,
519 config.labels.join(", "),
520 config.url,
521 isolation.describe()
522 ));
523 if isolation == sandbox::Isolation::Host {
524 log(sandbox::HOST_MODE_AT_START);
525 }
526 let mut current: Option<exec::Running> = None;
527 let mut ran_one = false;
528 let mut backoff = Duration::from_secs(2);
529 let mut last_update_check = Instant::now().checked_sub(UPDATE_EVERY).unwrap_or_else(Instant::now);
530 let mut said_no_key = false;
531 loop {
532 // Asked to stop now (`g1t-runner sandbox down`): as a service's stop.
533 if std::fs::remove_file(folder.join("stop")).is_ok() {
534 STOP.store(true, Ordering::SeqCst);
535 }
536 if STOP.load(Ordering::SeqCst) {
537 if let Some(mut running) = current.take() {
538 log(&format!("Stopping: {} is cut short.", running.name));
539 running.stop();
540 let _ = api.finished(&running.id, 1, Some("The self-hosted runner was stopped."));
541 running.clean();
542 }
543 say_status(folder, isolation, None, "stopped");
544 return Ok(());
545 }
546 // What was running has ended: say how.
547 if let Some(running) = current.as_mut()
548 && let Some(code) = running.ended()
549 {
550 let mut running = current.take().expect("checked above");
551 log(&format!("{} {} ended ({code}).", running.kind, running.name));
552 if let Err(failure) = api.finished(&running.id, code, None) {
553 log(&format!("Could not say how it ended: {failure}"));
554 }
555 running.clean();
556 ran_one = true;
557 if config.ephemeral || once {
558 if config.ephemeral {
559 let _ = api.remove();
560 config::forget(folder);
561 log("The ephemeral runner ran its job and removed itself.");
562 }
563 return Ok(());
564 }
565 }
566 // Asked to drain (`g1t-runner sandbox update`, before it swaps the
567 // sandbox's image): no new work, and once idle, stop.
568 let draining = drain_path(folder).exists();
569 if draining && current.is_none() {
570 let _ = std::fs::remove_file(drain_path(folder));
571 say_status(folder, isolation, None, "stopped");
572 log("Drained: no job is running, so the runner stops here.");
573 return Ok(());
574 }
575 let state = match (&current, draining) {
576 (Some(_), true) => "draining",
577 (Some(_), false) => "busy",
578 (None, _) => "idle",
579 };
580 say_status(folder, isolation, current.as_ref(), state);
581 // Idle: a newer release, now and then.
582 if current.is_none() && config.auto_update && last_update_check.elapsed() >= UPDATE_EVERY {
583 last_update_check = Instant::now();
584 if update::can_update() {
585 match update::update(&config) {
586 Ok(Some(version)) => {
587 log(&format!("Updated to {version}; starting it."));
588 return restart();
589 }
590 Ok(None) => {}
591 Err(error) => log(&format!("Could not check for a new version: {error:#}")),
592 }
593 } else if !said_no_key {
594 said_no_key = true;
595 log("This build cannot check releases, so it will not update itself.");
596 }
597 }
598 let running: Vec<String> = current.iter().map(|r| r.id.clone()).collect();
599 let wait = if current.is_some() { 0 } else { IDLE_WAIT_MS };
600 let poll = match api.poll(&running, wait, isolation.as_str()) {
601 Ok(poll) => {
602 backoff = Duration::from_secs(2);
603 poll
604 }
605 Err(Failure::Refused(401, message)) => {
606 if let Some(mut running) = current.take() {
607 running.stop();
608 running.clean();
609 }
610 bail!("g1t no longer knows this runner ({message}). Register it again.");
611 }
612 Err(failure) => {
613 log(&format!("{failure}; trying again in {}s", backoff.as_secs()));
614 nap(backoff, || false);
615 backoff = (backoff * 2).min(Duration::from_secs(60));
616 continue;
617 }
618 };
619 if let Some(fresh) = poll.credential {
620 config.credential = fresh.clone();
621 api.credential = fresh;
622 config::save(folder, &config)?;
623 }
624 if poll.removed {
625 config::forget(folder);
626 log("This runner was removed from g1t; stopping.");
627 return Ok(());
628 }
629 if let Some(running) = current.as_mut()
630 && poll.cancel.contains(&running.id)
631 {
632 log(&format!("{} was cancelled; stopping it.", running.name));
633 running.stop();
634 }
635 if let Some(running) = current.as_mut()
636 && running.overdue()
637 {
638 log(&format!("{} ran past its time limit; stopping it.", running.name));
639 running.stop();
640 }
641 if let Some(work) = poll.assignment {
642 log(&format!("Took {} {} ({}).", work.kind, work.name, work.repo));
643 match exec::start(&config, isolation, folder, &work) {
644 Ok(running) => current = Some(running),
645 Err(error) => {
646 log(&format!("Could not start it: {error:#}"));
647 let _ = api.finished(&work.id, 1, Some(&format!("The self-hosted runner {} could not start it: {error:#}", config.name)));
648 if config.ephemeral || once {
649 return Ok(());
650 }
651 }
652 }
653 continue;
654 }
655 if once && ran_one {
656 return Ok(());
657 }
658 // Busy: a heartbeat every few seconds until it ends.
659 if current.is_some() {
660 let mut check = || current.as_mut().is_some_and(|r| r.ended().is_some());
661 nap(HEARTBEAT, &mut check);
662 }
663 }
664}
665
666/// The file whose presence asks a runner to drain: take no new work, and
667/// stop once idle.
668pub fn drain_path(folder: &Path) -> PathBuf {
669 folder.join("drain")
670}
671
672/// The file a runner keeps saying what it is doing, for `g1t-runner
673/// sandbox status` (which reads it from inside the sandbox).
674pub fn status_path(folder: &Path) -> PathBuf {
675 folder.join("status.json")
676}
677
678/// What a runner says it is doing, in its status file.
679#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize, PartialEq)]
680pub struct RunnerStatus {
681 pub version: String,
682 pub isolation: String,
683 /// `idle`, `busy`, `draining` or `stopped`.
684 pub state: String,
685 pub job: Option<JobStatus>,
686 pub updated_at: String,
687}
688
689#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize, PartialEq)]
690pub struct JobStatus {
691 /// `workflow`, or `agent` for g1t's own work (a merge check or a
692 /// state of the merge queue).
693 pub kind: String,
694 pub id: String,
695 pub name: String,
696 pub repo: String,
697}
698
699fn say_status(folder: &Path, isolation: sandbox::Isolation, running: Option<&exec::Running>, state: &str) {
700 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0);
701 let status = RunnerStatus {
702 version: VERSION.to_owned(),
703 isolation: isolation.as_str().to_owned(),
704 state: state.to_owned(),
705 job: running.map(|r| JobStatus { kind: r.kind.clone(), id: r.id.clone(), name: r.name.clone(), repo: r.repo.clone() }),
706 updated_at: g1t_now(now),
707 };
708 let temp = folder.join("status.json.new");
709 if serde_json::to_vec_pretty(&status).ok().is_some_and(|bytes| std::fs::write(&temp, bytes).is_ok()) {
710 let _ = std::fs::rename(&temp, status_path(folder));
711 }
712}
713
714/// Runs the updated program in this one's place, with the same arguments.
715fn restart() -> Result<()> {
716 let me = std::env::current_exe()?;
717 let status = std::process::Command::new(me).args(std::env::args().skip(1)).status()?;
718 std::process::exit(status.code().unwrap_or(1));
719}
720
721#[cfg(test)]
722mod tests {
723 use super::*;
724
725 fn args(items: &[&str]) -> Vec<String> {
726 items.iter().map(|s| s.to_string()).collect()
727 }
728
729 #[test]
730 fn commands_are_told_apart_from_the_harness() {
731 assert!(is_command(&args(&["register", "--url", "x"])));
732 assert!(is_command(&args(&["run"])));
733 assert!(!is_command(&args(&[])));
734 assert!(!is_command(&args(&["something"])));
735 }
736
737 #[test]
738 fn options_read_both_ways() {
739 let options = Options::parse(&args(&["--url", "https://g1t.sh", "--labels=gpu,cuda", "--ephemeral", "install"]), &["url", "labels"]).unwrap();
740 assert_eq!(options.value("url"), Some("https://g1t.sh"));
741 assert_eq!(options.value("labels"), Some("gpu,cuda"));
742 assert!(options.flag("ephemeral"));
743 assert_eq!(options.rest, vec!["install".to_owned()]);
744 assert!(Options::parse(&args(&["--url"]), &["url"]).is_err());
745 }
746
747 #[test]
748 fn a_machines_name_becomes_a_runners() {
749 assert_eq!(runner_name("Build Box #1"), "Build-Box--1");
750 assert_eq!(runner_name(""), "runner");
751 assert_eq!(runner_name(&"x".repeat(80)).len(), 64);
752 }
753
754 #[test]
755 fn the_log_says_when() {
756 assert_eq!(g1t_now(1_790_918_179_123), "2026-10-02T05:16:19Z");
757 }
758}