summaryrefslogtreecommitdiffstats
path: root/src/bin/sandbox-agent.rs
blob: 1c2a8d80773555d7b627fb9109ec859d8a323b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! The ephemeral sandbox agent — see `aur_build::sandbox::agent`.
//!
//! This is the piece that runs *inside* the disposable sandbox (a Kata
//! container, a throwaway VM, a test's temp process). It dials the hub,
//! serves exactly one lease, and dies with it.

use std::time::Duration;

fn main() {
    let mut hub = None;
    let mut token = None;
    let mut profile = Some("extra-x86_64".to_string());
    let mut ttl = 90u64;
    let args: Vec<String> = std::env::args().skip(1).collect();
    let mut i = 0;
    while i < args.len() {
        let take = |i: &mut usize| {
            *i += 1;
            args.get(*i).cloned().ok_or_else(|| format!("{} needs a value", args[*i - 1]))
        };
        let r = match args[i].as_str() {
            "--hub" => take(&mut i).map(|v| hub = Some(v)),
            "--token" => take(&mut i).map(|v| token = Some(v)),
            "--profile" => take(&mut i).map(|v| profile = Some(v)),
            "--ttl" => take(&mut i).and_then(|v| v.parse::<u64>().map(|t| ttl = t).map_err(|_| "bad ttl".to_string())),
            other => Err(format!("unknown argument {other}")),
        };
        if let Err(e) = r {
            eprintln!("sandbox-agent: {e}");
            eprintln!("usage: sandbox-agent --hub HOST:PORT --token T [--profile P] [--ttl SECS]");
            std::process::exit(2);
        }
        i += 1;
    }
    let (hub, token) = match (hub, token) {
        (Some(h), Some(t)) => (h, t),
        _ => {
            eprintln!("usage: sandbox-agent --hub HOST:PORT --token T [--profile P] [--ttl SECS]");
            std::process::exit(2);
        }
    };
    let opts = aur_build::sandbox::agent::AgentOptions {
        hub,
        token,
        profile: profile.unwrap(),
        ttl: Duration::from_secs(ttl),
    };
    if let Err(e) = aur_build::sandbox::agent::serve(&opts) {
        eprintln!("sandbox-agent: {e}");
        std::process::exit(1);
    }
}