Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
715 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] [--agent-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", "agent-image", "harness", "memory", "cpus", "pids-limit", "job-docker-image"];
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] [--agent-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/// A runner's name from a machine's: what g1t accepts.
263fn runner_name(given: &str) -> String {
264 let name: String = given
265 .chars()
266 .map(|c| if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') { c } else { '-' })
267 .take(64)
268 .collect();
269 if name.is_empty() { "runner".into() } else { name }
270}
271
272fn register(options: &Options, folder: &Path) -> Result<()> {
273 let Some(url) = options.value("url") else { bail!("--url is needed: the g1t you register with, such as https://g1t.sh") };
274 let Some(token) = options.value("token") else { bail!("--token is needed: make a registration token under Settings, Runners") };
275 let url = url.trim_end_matches('/').to_owned();
276 let api = options.value("api").map(|a| a.trim_end_matches('/').to_owned()).unwrap_or_else(|| config::api_for(&url));
277 let name = runner_name(options.value("name").map(str::to_owned).unwrap_or_else(hostname).as_str());
278 let labels: Vec<String> = options
279 .value("labels")
280 .unwrap_or_default()
281 .split(',')
282 .map(|l| l.trim().to_owned())
283 .filter(|l| !l.is_empty())
284 .collect();
285 let native = options.flag("native");
286 if native && options.flag("no-docker") {
287 bail!("--native runs each job in a VM of its own, and --no-docker runs it on this machine: pick one");
288 }
289 let docker = !options.flag("no-docker") && !native;
290 let isolation = isolation(docker, native)?;
291 if isolation == sandbox::Isolation::Host {
292 log(sandbox::HOST_MODE_AT_START);
293 } else if !native && !sandbox::nested_requested() && !exec::docker_ready() {
294 log("Docker does not answer here. Jobs will fail until it does.");
295 }
296 let pids_limit = match options.value("pids-limit") {
297 Some(text) => Some(text.parse::<u32>().ok().filter(|n| *n > 0).with_context(|| format!("--pids-limit takes a number of processes, not {text}"))?),
298 None => None,
299 };
300 let existing = config::load(folder).ok();
301 if existing.is_some() && !options.flag("replace") {
302 bail!("{} already holds a registered runner: run `g1t-runner remove` first, or register with --replace or another --dir", folder.display());
303 }
304 // The OS jobs run on: in Docker that is Linux, whatever the machine
305 // (Docker Desktop runs Linux containers on macOS and Windows). A
306 // runner says it runs jobs native to this OS only when it sandboxes
307 // them (--native), or when the person chose host mode (--no-docker).
308 let os = match std::env::consts::OS {
309 _ if docker => "linux",
310 "macos" => "macos",
311 "windows" => "windows",
312 _ => "linux",
313 };
314 let arch = if std::env::consts::ARCH == "aarch64" { "arm64" } else { "x64" };
315 let registered = Api::register(
316 &api,
317 &json!({
318 "token": token,
319 "name": name,
320 "labels": labels,
321 "os": os,
322 "arch": arch,
323 "version": VERSION,
324 "ephemeral": options.flag("ephemeral"),
325 "group": options.value("group"),
326 "replace": options.flag("replace"),
327 "isolation": isolation.as_str(),
328 }),
329 )?;
330 let work_dir = options.value("work-dir").map(PathBuf::from).unwrap_or_else(|| folder.join("work"));
331 let config = Config {
332 url,
333 api,
334 runner: registered.runner.id.clone(),
335 name: registered.runner.name.clone(),
336 credential: registered.credential,
337 workspace: registered.runner.workspace.clone(),
338 repo: registered.runner.repo.clone(),
339 group: registered.runner.group.clone(),
340 labels: registered.runner.labels.clone(),
341 ephemeral: options.flag("ephemeral"),
342 work_dir,
343 docker,
344 image: options.value("image").map(str::to_owned),
345 agent_image: options.value("agent-image").map(str::to_owned),
346 harness: options.value("harness").map(PathBuf::from),
347 auto_update: !options.flag("no-auto-update"),
348 memory: options.value("memory").map(str::to_owned),
349 cpus: options.value("cpus").map(str::to_owned),
350 pids_limit,
351 job_docker: !options.flag("no-job-docker"),
352 job_docker_image: options.value("job-docker-image").map(str::to_owned),
353 native,
354 };
355 config::save(folder, &config)?;
356 let place = match &config.repo {
357 Some(repo) => format!("for {repo}"),
358 None => format!("in {}{}", config.workspace, config.group.as_ref().map(|g| format!(", group {g}")).unwrap_or_default()),
359 };
360 log(&format!("Registered {} {place}, with labels {}.", config.name, config.labels.join(", ")));
361 log("Start it with `g1t-runner run`, or `g1t-runner service install` to keep it running.");
362 Ok(())
363}
364
365/// How this runner keeps work from its machine; refuses a nested runner
366/// that can reach the machine (sandbox.rs).
367fn isolation(docker: bool, native: bool) -> Result<sandbox::Isolation> {
368 use sandbox::{Isolation, SandboxDriver};
369 if native {
370 let Some(driver) = sandbox::native_driver() else {
371 bail!("--native: there is no sandbox for jobs native to {}; Linux jobs run in containers", std::env::consts::OS);
372 };
373 return driver.preflight().with_context(|| format!("--native needs {}", driver.name()));
374 }
375 if !docker {
376 // What would sandbox jobs native to this OS instead.
377 if let Some(driver) = sandbox::native_driver() {
378 match driver.preflight() {
379 Ok(_) => log(&format!("This machine can run {} jobs sandboxed, each in {}: register with --native instead of --no-docker.", std::env::consts::OS, driver.name())),
380 Err(why) => log(&format!("Sandboxed {} jobs, in {}: {why:#}", std::env::consts::OS, driver.name())),
381 }
382 }
383 return Ok(Isolation::Host);
384 }
385 if !sandbox::nested_requested() {
386 return sandbox::SharedDocker.preflight();
387 }
388 if !cfg!(target_os = "linux") {
389 bail!("{}=nested is for the runner's Linux image", sandbox::SANDBOX_ENV);
390 }
391 let isolation = sandbox::NestedDocker { allow_unsafe: sandbox::unsafe_allowed() }.preflight()?;
392 if isolation != Isolation::NestedContainer {
393 for problem in sandbox::Findings::read().problems() {
394 log(&format!("UNSAFE: {problem}"));
395 }
396 log(&format!("UNSAFE: {}=1 is set, so the runner starts anyway, and its jobs can reach this machine.", sandbox::UNSAFE_ENV));
397 }
398 Ok(isolation)
399}
400
401/// A nested runner's own Docker Engine, which it uses whatever else it
402/// can see (with the override, it starts despite that).
403fn prepare(docker: bool) -> Result<()> {
404 if !docker || !sandbox::nested_requested() {
405 return Ok(());
406 }
407 #[cfg(unix)]
408 {
409 let version = engine::start()?;
410 log(&format!("Started this runner's own Docker Engine {version}, inside its container."));
411 }
412 Ok(())
413}
414
415fn remove(folder: &Path) -> Result<()> {
416 let config = config::load(folder)?;
417 let api = Api { base: config.api.clone(), runner: config.runner.clone(), credential: config.credential.clone() };
418 match api.remove() {
419 Ok(()) | Err(Failure::Refused(401, _)) | Err(Failure::Refused(404, _)) => {}
420 Err(failure) => bail!("{failure}; the runner is still registered"),
421 }
422 config::forget(folder);
423 log(&format!("Removed {}.", config.name));
424 Ok(())
425}
426
427/// The loop, for a Windows service: its exit code.
428#[cfg_attr(not(windows), allow(dead_code))]
429pub fn serve_from_service() -> i32 {
430 let folder = config::folder(None);
431 let folder = std::env::args()
432 .collect::<Vec<_>>()
433 .windows(2)
434 .find(|pair| pair[0] == "--dir")
435 .map(|pair| PathBuf::from(&pair[1]))
436 .unwrap_or(folder);
437 match run(&folder, false) {
438 Ok(()) => 0,
439 Err(error) => {
440 eprintln!("g1t-runner: {error:#}");
441 1
442 }
443 }
444}
445
446/// Sleeps up to `duration`, waking early when `stop` says so.
447fn nap(duration: Duration, mut stop: impl FnMut() -> bool) {
448 let until = Instant::now() + duration;
449 while Instant::now() < until {
450 if STOP.load(Ordering::SeqCst) || stop() {
451 return;
452 }
453 std::thread::sleep(Duration::from_millis(250));
454 }
455}
456
457fn run(folder: &Path, once: bool) -> Result<()> {
458 // Its folder holds its credential and, while a Windows job runs, that
459 // job's script: a runner whose folder others can read does not start.
460 let mut config = config::load(folder)?;
461 private::folder(folder, "--dir")?;
462 let mut api = Api { base: config.api.clone(), runner: config.runner.clone(), credential: config.credential.clone() };
463 std::fs::create_dir_all(&config.work_dir).with_context(|| format!("could not make {}", config.work_dir.display()))?;
464 let isolation = isolation(config.docker, config.native)?;
465 prepare(config.docker)?;
466 match isolation {
467 sandbox::Isolation::Host => {}
468 sandbox::Isolation::Vm => native::sweep(folder),
469 _ => exec::sweep(&config),
470 }
471 // A drain or stop asked for before this start is done with.
472 let _ = std::fs::remove_file(drain_path(folder));
473 let _ = std::fs::remove_file(folder.join("stop"));
474 log(&format!(
475 "g1t-runner {VERSION}: {} ({}) is listening for work from {}. Isolation: {}.",
476 config.name,
477 config.labels.join(", "),
478 config.url,
479 isolation.describe()
480 ));
481 if isolation == sandbox::Isolation::Host {
482 log(sandbox::HOST_MODE_AT_START);
483 }
484 let mut current: Option<exec::Running> = None;
485 let mut ran_one = false;
486 let mut backoff = Duration::from_secs(2);
487 let mut last_update_check = Instant::now().checked_sub(UPDATE_EVERY).unwrap_or_else(Instant::now);
488 let mut said_no_key = false;
489 loop {
490 // Asked to stop now (`g1t-runner sandbox down`): as a service's stop.
491 if std::fs::remove_file(folder.join("stop")).is_ok() {
492 STOP.store(true, Ordering::SeqCst);
493 }
494 if STOP.load(Ordering::SeqCst) {
495 if let Some(mut running) = current.take() {
496 log(&format!("Stopping: {} is cut short.", running.name));
497 running.stop();
498 let _ = api.finished(&running.id, 1, Some("The self-hosted runner was stopped."));
499 running.clean();
500 }
501 say_status(folder, isolation, None, "stopped");
502 return Ok(());
503 }
504 // What was running has ended: say how.
505 if let Some(running) = current.as_mut()
506 && let Some(code) = running.ended()
507 {
508 let mut running = current.take().expect("checked above");
509 log(&format!("{} {} ended ({code}).", running.kind, running.name));
510 if let Err(failure) = api.finished(&running.id, code, None) {
511 log(&format!("Could not say how it ended: {failure}"));
512 }
513 running.clean();
514 ran_one = true;
515 if config.ephemeral || once {
516 if config.ephemeral {
517 let _ = api.remove();
518 config::forget(folder);
519 log("The ephemeral runner ran its job and removed itself.");
520 }
521 return Ok(());
522 }
523 }
524 // Asked to drain (`g1t-runner sandbox update`, before it swaps the
525 // sandbox's image): no new work, and once idle, stop.
526 let draining = drain_path(folder).exists();
527 if draining && current.is_none() {
528 let _ = std::fs::remove_file(drain_path(folder));
529 say_status(folder, isolation, None, "stopped");
530 log("Drained: no job is running, so the runner stops here.");
531 return Ok(());
532 }
533 let state = match (&current, draining) {
534 (Some(_), true) => "draining",
535 (Some(_), false) => "busy",
536 (None, _) => "idle",
537 };
538 say_status(folder, isolation, current.as_ref(), state);
539 // Idle: a newer release, now and then.
540 if current.is_none() && config.auto_update && last_update_check.elapsed() >= UPDATE_EVERY {
541 last_update_check = Instant::now();
542 if update::can_update() {
543 match update::update(&config) {
544 Ok(Some(version)) => {
545 log(&format!("Updated to {version}; starting it."));
546 return restart();
547 }
548 Ok(None) => {}
549 Err(error) => log(&format!("Could not check for a new version: {error:#}")),
550 }
551 } else if !said_no_key {
552 said_no_key = true;
553 log("This build cannot check releases, so it will not update itself.");
554 }
555 }
556 let running: Vec<String> = current.iter().map(|r| r.id.clone()).collect();
557 let wait = if current.is_some() { 0 } else { IDLE_WAIT_MS };
558 let poll = match api.poll(&running, wait, isolation.as_str()) {
559 Ok(poll) => {
560 backoff = Duration::from_secs(2);
561 poll
562 }
563 Err(Failure::Refused(401, message)) => {
564 if let Some(mut running) = current.take() {
565 running.stop();
566 running.clean();
567 }
568 bail!("g1t no longer knows this runner ({message}). Register it again.");
569 }
570 Err(failure) => {
571 log(&format!("{failure}; trying again in {}s", backoff.as_secs()));
572 nap(backoff, || false);
573 backoff = (backoff * 2).min(Duration::from_secs(60));
574 continue;
575 }
576 };
577 if let Some(fresh) = poll.credential {
578 config.credential = fresh.clone();
579 api.credential = fresh;
580 config::save(folder, &config)?;
581 }
582 if poll.removed {
583 config::forget(folder);
584 log("This runner was removed from g1t; stopping.");
585 return Ok(());
586 }
587 if let Some(running) = current.as_mut()
588 && poll.cancel.contains(&running.id)
589 {
590 log(&format!("{} was cancelled; stopping it.", running.name));
591 running.stop();
592 }
593 if let Some(running) = current.as_mut()
594 && running.overdue()
595 {
596 log(&format!("{} ran past its time limit; stopping it.", running.name));
597 running.stop();
598 }
599 if let Some(work) = poll.assignment {
600 log(&format!("Took {} {} ({}).", work.kind, work.name, work.repo));
601 match exec::start(&config, isolation, folder, &work) {
602 Ok(running) => current = Some(running),
603 Err(error) => {
604 log(&format!("Could not start it: {error:#}"));
605 let _ = api.finished(&work.id, 1, Some(&format!("The self-hosted runner {} could not start it: {error:#}", config.name)));
606 if config.ephemeral || once {
607 return Ok(());
608 }
609 }
610 }
611 continue;
612 }
613 if once && ran_one {
614 return Ok(());
615 }
616 // Busy: a heartbeat every few seconds until it ends.
617 if current.is_some() {
618 let mut check = || current.as_mut().is_some_and(|r| r.ended().is_some());
619 nap(HEARTBEAT, &mut check);
620 }
621 }
622}
623
624/// The file whose presence asks a runner to drain: take no new work, and
625/// stop once idle.
626pub fn drain_path(folder: &Path) -> PathBuf {
627 folder.join("drain")
628}
629
630/// The file a runner keeps saying what it is doing, for `g1t-runner
631/// sandbox status` (which reads it from inside the sandbox).
632pub fn status_path(folder: &Path) -> PathBuf {
633 folder.join("status.json")
634}
635
636/// What a runner says it is doing, in its status file.
637#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize, PartialEq)]
638pub struct RunnerStatus {
639 pub version: String,
640 pub isolation: String,
641 /// `idle`, `busy`, `draining` or `stopped`.
642 pub state: String,
643 pub job: Option<JobStatus>,
644 pub updated_at: String,
645}
646
647#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize, PartialEq)]
648pub struct JobStatus {
649 /// `workflow` or `agent`.
650 pub kind: String,
651 pub id: String,
652 pub name: String,
653 pub repo: String,
654}
655
656fn say_status(folder: &Path, isolation: sandbox::Isolation, running: Option<&exec::Running>, state: &str) {
657 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0);
658 let status = RunnerStatus {
659 version: VERSION.to_owned(),
660 isolation: isolation.as_str().to_owned(),
661 state: state.to_owned(),
662 job: running.map(|r| JobStatus { kind: r.kind.clone(), id: r.id.clone(), name: r.name.clone(), repo: r.repo.clone() }),
663 updated_at: g1t_now(now),
664 };
665 let temp = folder.join("status.json.new");
666 if serde_json::to_vec_pretty(&status).ok().is_some_and(|bytes| std::fs::write(&temp, bytes).is_ok()) {
667 let _ = std::fs::rename(&temp, status_path(folder));
668 }
669}
670
671/// Runs the updated program in this one's place, with the same arguments.
672fn restart() -> Result<()> {
673 let me = std::env::current_exe()?;
674 let status = std::process::Command::new(me).args(std::env::args().skip(1)).status()?;
675 std::process::exit(status.code().unwrap_or(1));
676}
677
678#[cfg(test)]
679mod tests {
680 use super::*;
681
682 fn args(items: &[&str]) -> Vec<String> {
683 items.iter().map(|s| s.to_string()).collect()
684 }
685
686 #[test]
687 fn commands_are_told_apart_from_the_harness() {
688 assert!(is_command(&args(&["register", "--url", "x"])));
689 assert!(is_command(&args(&["run"])));
690 assert!(!is_command(&args(&[])));
691 assert!(!is_command(&args(&["something"])));
692 }
693
694 #[test]
695 fn options_read_both_ways() {
696 let options = Options::parse(&args(&["--url", "https://g1t.sh", "--labels=gpu,cuda", "--ephemeral", "install"]), &["url", "labels"]).unwrap();
697 assert_eq!(options.value("url"), Some("https://g1t.sh"));
698 assert_eq!(options.value("labels"), Some("gpu,cuda"));
699 assert!(options.flag("ephemeral"));
700 assert_eq!(options.rest, vec!["install".to_owned()]);
701 assert!(Options::parse(&args(&["--url"]), &["url"]).is_err());
702 }
703
704 #[test]
705 fn a_machines_name_becomes_a_runners() {
706 assert_eq!(runner_name("Build Box #1"), "Build-Box--1");
707 assert_eq!(runner_name(""), "runner");
708 assert_eq!(runner_name(&"x".repeat(80)).len(), 64);
709 }
710
711 #[test]
712 fn the_log_says_when() {
713 assert_eq!(g1t_now(1_790_918_179_123), "2026-10-02T05:16:19Z");
714 }
715}