OXYGENHUB
# e0053e047704 oxygenflow / crates / flow-core / src / pipeline.rs
26.1 KiB · Rust · last changed Build image for Rust and Node jobs (images/rust: toolchain, rustfmt, clippy, Node 24, vendored crates, offline cargo); flow-init finds the program in the job's PATH (glibc's execvpe used PID 1's own); network "loopback" in M0 1 month ago source blame history raw
1
//! `.flow/pipeline.kdl`: the M0 subset of spec §3 (docs/m0.md §1), parsed
2
//! from KDL 2.0 and validated before anything runs. Every problem is
3
//! reported with its line, and all of them at once.
4
5
use serde::{Deserialize, Serialize};
6
use std::collections::{BTreeMap, BTreeSet};
7
use std::fmt;
8
9
/// Where a pipeline lives in the state being built.
10
pub const PIPELINE_PATH: &str = ".flow/pipeline.kdl";
11
12
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
13
pub struct Pipeline {
14
    pub schema_version: u32,
15
    /// By name.
16
    pub sandboxes: BTreeMap<String, Sandbox>,
17
    /// By name.
18
    pub jobs: BTreeMap<String, Job>,
19
}
20
21
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
22
#[serde(rename_all = "lowercase")]
23
pub enum Runtime {
24
    /// Tier 0 (spec §4.1); refused in M0.
25
    Wasm,
26
    /// Tier 1 (spec §4.2): namespaces, Landlock, seccomp, cgroups.
27
    Syscall,
28
    /// Tier 2 (spec §4.3), the default.
29
    Microvm,
30
}
31
32
impl Runtime {
33
    pub fn as_str(self) -> &'static str {
34
        match self {
35
            Runtime::Wasm => "wasm",
36
            Runtime::Syscall => "syscall",
37
            Runtime::Microvm => "microvm",
38
        }
39
    }
40
}
41
42
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
43
#[serde(rename_all = "lowercase")]
44
pub enum Network {
45
    Isolated,
46
    Loopback,
47
    Egress,
48
}
49
50
impl Network {
51
    pub fn as_str(self) -> &'static str {
52
        match self {
53
            Network::Isolated => "isolated",
54
            Network::Loopback => "loopback",
55
            Network::Egress => "egress",
56
        }
57
    }
58
}
59
60
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
61
pub struct Sandbox {
62
    pub name: String,
63
    pub runtime: Runtime,
64
    /// BLAKE3 of the rootfs image, hex.
65
    pub rootfs: String,
66
    pub memory_mib: u32,
67
    pub cpus: u32,
68
    pub network: Network,
69
}
70
71
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
72
pub struct Job {
73
    pub name: String,
74
    pub sandbox: String,
75
    /// Glob patterns over repository paths (spec §6.2).
76
    pub scope: Vec<String>,
77
    /// Sorted.
78
    pub depends_on: Vec<String>,
79
    pub env: BTreeMap<String, String>,
80
    /// Executed as is: no shell, no expansion (spec §2.2 I2).
81
    pub exec: Vec<String>,
82
    /// Ephemeral scratch mounts, sorted by target (spec §3.1 `mount`).
83
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
84
    pub mounts: Vec<Mount>,
85
}
86
87
/// An ephemeral scratch mount: empty at the start of every job and gone
88
/// after it, so it cannot carry anything between jobs (spec §2.2 I4).
89
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
90
pub struct Mount {
91
    pub source: MountSource,
92
    /// An absolute path under `/src` or `/tmp`.
93
    pub target: String,
94
    pub size_mib: u32,
95
}
96
97
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
98
#[serde(rename_all = "lowercase")]
99
pub enum MountSource {
100
    /// `ephemeral://tmpfs`: memory in the sandbox, counted against its memory-limit.
101
    Tmpfs,
102
    /// `ephemeral://disk`: a fresh sparse disk on the runner, for large build trees.
103
    Disk,
104
}
105
106
impl MountSource {
107
    pub fn as_str(self) -> &'static str {
108
        match self {
109
            MountSource::Tmpfs => "ephemeral://tmpfs",
110
            MountSource::Disk => "ephemeral://disk",
111
        }
112
    }
113
}
114
115
/// Mounts a job may declare.
116
pub const MAX_MOUNTS: usize = 4;
117
/// Sizes a mount may have: 16 MiB to 64 GiB.
118
pub const MIN_MOUNT_MIB: u64 = 16;
119
pub const MAX_MOUNT_MIB: u64 = 64 * 1024;
120
121
/// Whether `t` is a clean absolute path strictly under `/src` or `/tmp`.
122
fn mount_target_ok(t: &str) -> bool {
123
    let under = t.strip_prefix("/src/").or_else(|| t.strip_prefix("/tmp/"));
124
    under.is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != ".."))
125
}
126
127
/// One problem in a pipeline, at a 1-based line.
128
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
129
pub struct Problem {
130
    pub line: usize,
131
    pub message: String,
132
}
133
134
impl fmt::Display for Problem {
135
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136
        write!(f, "line {}: {}", self.line, self.message)
137
    }
138
}
139
140
/// Every problem found, in line order.
141
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
142
pub struct Problems(pub Vec<Problem>);
143
144
impl fmt::Display for Problems {
145
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146
        let lines: Vec<String> = self.0.iter().map(|p| p.to_string()).collect();
147
        f.write_str(&lines.join("\n"))
148
    }
149
}
150
151
const MIN_MEMORY_MIB: u64 = 64;
152
const MAX_MEMORY_MIB: u64 = 16 * 1024;
153
const MAX_CPUS: i128 = 16;
154
155
/// Nodes of the full spec that M0 recognises and refuses.
156
const LATER_TOP: &[&str] = &["publish", "secret"];
157
const LATER_JOB: &[&str] = &["output", "secret", "attest"];
158
159
struct Ctx<'a> {
160
    src: &'a str,
161
    problems: Vec<Problem>,
162
}
163
164
impl Ctx<'_> {
165
    fn line(&self, span: SourceSpan) -> usize {
166
        let at = span.offset().min(self.src.len());
167
        self.src[..at].bytes().filter(|b| *b == b'\n').count() + 1
168
    }
169
170
    fn err(&mut self, span: SourceSpan, message: impl Into<String>) {
171
        let line = self.line(span);
172
        self.problems.push(Problem { line, message: message.into() });
173
    }
174
175
    /// The node's arguments as strings; properties and other types are errors.
176
    fn strings(&mut self, node: &KdlNode) -> Vec<String> {
177
        let mut out = Vec::new();
178
        for e in node.entries() {
179
            if let Some(k) = e.name() {
180
                self.err(e.span(), format!("`{}` takes no property `{}`", node.name().value(), k.value()));
181
                continue;
182
            }
183
            match e.value() {
184
                KdlValue::String(s) => out.push(s.clone()),
185
                other => self.err(e.span(), format!("`{}` takes strings, not {}", node.name().value(), kind_of(other))),
186
            }
187
        }
188
        out
189
    }
190
191
    /// Exactly one string argument.
192
    fn one_string(&mut self, node: &KdlNode) -> Option<String> {
193
        let v = self.strings(node);
194
        if v.len() != 1 {
195
            self.err(node.span(), format!("`{}` takes one string", node.name().value()));
196
            return None;
197
        }
198
        v.into_iter().next()
199
    }
200
201
    fn one_integer(&mut self, node: &KdlNode) -> Option<i128> {
202
        match node.entries() {
203
            [e] if e.name().is_none() => match e.value() {
204
                KdlValue::Integer(n) => Some(*n),
205
                other => {
206
                    self.err(e.span(), format!("`{}` takes an integer, not {}", node.name().value(), kind_of(other)));
207
                    None
208
                }
209
            },
210
            _ => {
211
                self.err(node.span(), format!("`{}` takes one integer", node.name().value()));
212
                None
213
            }
214
        }
215
    }
216
217
    fn no_children(&mut self, node: &KdlNode) {
218
        if node.children().is_some() {
219
            self.err(node.span(), format!("`{}` takes no block", node.name().value()));
220
        }
221
    }
222
223
    fn name_ok(&mut self, span: SourceSpan, what: &str, name: &str) -> bool {
224
        let ok = !name.is_empty() && name.len() <= 64 && name.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b));
225
        if !ok {
226
            self.err(span, format!("{what} name {name:?} must be 1 to 64 of A-Z a-z 0-9 . _ -"));
227
        }
228
        ok
229
    }
230
}
231
232
use kdl::{KdlDocument, KdlNode, KdlValue};
233
use miette::SourceSpan;
234
235
fn kind_of(v: &KdlValue) -> &'static str {
236
    match v {
237
        KdlValue::String(_) => "a string",
238
        KdlValue::Integer(_) => "an integer",
239
        KdlValue::Float(_) => "a number",
240
        KdlValue::Bool(_) => "a boolean",
241
        KdlValue::Null => "null",
242
    }
243
}
244
245
/// `"512MiB"`, `"4GiB"`, `"65536KiB"` to MiB.
246
fn parse_memory(s: &str) -> Option<u64> {
247
    let (num, unit) = s.split_at(s.find(|c: char| !c.is_ascii_digit())?);
248
    let n: u64 = num.parse().ok()?;
249
    let mib = match unit {
250
        "KiB" => n.is_multiple_of(1024).then_some(n / 1024)?,
251
        "MiB" => n,
252
        "GiB" => n.checked_mul(1024)?,
253
        _ => return None,
254
    };
255
    Some(mib)
256
}
257
258
impl Pipeline {
259
    /// Parse and validate a pipeline document.
260
    pub fn parse(src: &str) -> Result<Pipeline, Problems> {
261
        let doc = match KdlDocument::parse_v2(src) {
262
            Ok(d) => d,
263
            Err(e) => {
264
                let mut ctx = Ctx { src, problems: Vec::new() };
265
                for d in &e.diagnostics {
266
                    let msg = d.message.clone().or_else(|| d.label.clone()).unwrap_or_else(|| "not valid KDL 2.0".into());
267
                    ctx.err(d.span, msg);
268
                }
269
                if ctx.problems.is_empty() {
270
                    ctx.problems.push(Problem { line: 1, message: "not valid KDL 2.0".into() });
271
                }
272
                return Err(Problems(ctx.problems));
273
            }
274
        };
275
        let mut ctx = Ctx { src, problems: Vec::new() };
276
        let mut schema = None;
277
        let mut sandboxes = BTreeMap::new();
278
        let mut jobs: BTreeMap<String, (Job, SourceSpan)> = BTreeMap::new();
279
        for node in doc.nodes() {
280
            match node.name().value() {
281
                "schema-version" => {
282
                    ctx.no_children(node);
283
                    match ctx.one_integer(node) {
284
                        Some(1) => schema = Some(1),
285
                        Some(n) => ctx.err(node.span(), format!("schema-version {n} is not supported; this runner reads 1")),
286
                        None => {}
287
                    }
288
                }
289
                "sandbox" => {
290
                    if let Some(sb) = parse_sandbox(&mut ctx, node) {
291
                        if sandboxes.contains_key(&sb.name) {
292
                            ctx.err(node.span(), format!("sandbox {:?} is declared twice", sb.name));
293
                        } else {
294
                            sandboxes.insert(sb.name.clone(), sb);
295
                        }
296
                    }
297
                }
298
                "job" => {
299
                    if let Some(job) = parse_job(&mut ctx, node) {
300
                        if jobs.contains_key(&job.name) {
301
                            ctx.err(node.span(), format!("job {:?} is declared twice", job.name));
302
                        } else {
303
                            jobs.insert(job.name.clone(), (job, node.span()));
304
                        }
305
                    }
306
                }
307
                n if LATER_TOP.contains(&n) => ctx.err(node.span(), format!("`{n}` is not in M0 yet")),
308
                n => ctx.err(node.span(), format!("unknown node `{n}`; expected schema-version, sandbox or job")),
309
            }
310
        }
311
        if schema.is_none() && !ctx.problems.iter().any(|p| p.message.contains("schema-version")) {
312
            ctx.problems.push(Problem { line: 1, message: "`schema-version 1` is required".into() });
313
        }
314
        if jobs.is_empty() && ctx.problems.is_empty() {
315
            ctx.problems.push(Problem { line: 1, message: "a pipeline needs at least one job".into() });
316
        }
317
        // References and cycles.
318
        for (job, span) in jobs.values() {
319
            if !job.sandbox.is_empty() && !sandboxes.contains_key(&job.sandbox) {
320
                ctx.err(*span, format!("job {:?} uses sandbox {:?}, which is not declared", job.name, job.sandbox));
321
            }
322
            for d in &job.depends_on {
323
                if !jobs.contains_key(d) {
324
                    ctx.err(*span, format!("job {:?} depends on {:?}, which is not declared", job.name, d));
325
                } else if d == &job.name {
326
                    ctx.err(*span, format!("job {:?} depends on itself", job.name));
327
                }
328
            }
329
        }
330
        let graph: BTreeMap<&str, Vec<&str>> =
331
            jobs.values().map(|(j, _)| (j.name.as_str(), j.depends_on.iter().map(String::as_str).collect())).collect();
332
        if let Some(cycle) = find_cycle(&graph) {
333
            let span = jobs[cycle[0]].1;
334
            ctx.err(span, format!("jobs depend on each other in a cycle: {}", cycle.join(" → ")));
335
        }
336
        if !ctx.problems.is_empty() {
337
            ctx.problems.sort_by_key(|p| p.line);
338
            return Err(Problems(ctx.problems));
339
        }
340
        Ok(Pipeline { schema_version: 1, sandboxes, jobs: jobs.into_iter().map(|(k, (j, _))| (k, j)).collect() })
341
    }
342
343
    /// Jobs in an order where each comes after what it depends on (ties by name).
344
    pub fn order(&self) -> Vec<&Job> {
345
        let mut done: BTreeSet<&str> = BTreeSet::new();
346
        let mut out = Vec::with_capacity(self.jobs.len());
347
        while out.len() < self.jobs.len() {
348
            let before = out.len();
349
            for j in self.jobs.values() {
350
                if !done.contains(j.name.as_str()) && j.depends_on.iter().all(|d| done.contains(d.as_str())) {
351
                    out.push(j);
352
                }
353
            }
354
            for j in &out[before..] {
355
                done.insert(j.name.as_str());
356
            }
357
            if out.len() == before {
358
                break; // validated pipelines have no cycles
359
            }
360
        }
361
        out
362
    }
363
364
    /// Each job's depth: 0 without dependencies, else one more than its deepest dependency.
365
    pub fn depths(&self) -> BTreeMap<&str, usize> {
366
        let mut depth: BTreeMap<&str, usize> = BTreeMap::new();
367
        for j in self.order() {
368
            let d = j.depends_on.iter().map(|x| depth.get(x.as_str()).copied().unwrap_or(0) + 1).max().unwrap_or(0);
369
            depth.insert(j.name.as_str(), d);
370
        }
371
        depth
372
    }
373
}
374
375
fn find_cycle<'a>(graph: &BTreeMap<&'a str, Vec<&'a str>>) -> Option<Vec<&'a str>> {
376
    #[derive(Clone, Copy, PartialEq)]
377
    enum Mark {
378
        Visiting,
379
        Done,
380
    }
381
    fn visit<'a>(
382
        n: &'a str,
383
        graph: &BTreeMap<&'a str, Vec<&'a str>>,
384
        marks: &mut BTreeMap<&'a str, Mark>,
385
        stack: &mut Vec<&'a str>,
386
    ) -> Option<Vec<&'a str>> {
387
        match marks.get(n) {
388
            Some(Mark::Done) => return None,
389
            Some(Mark::Visiting) => {
390
                let at = stack.iter().position(|s| *s == n).unwrap_or(0);
391
                let mut c = stack[at..].to_vec();
392
                c.push(n);
393
                return Some(c);
394
            }
395
            None => {}
396
        }
397
        marks.insert(n, Mark::Visiting);
398
        stack.push(n);
399
        for d in graph.get(n).into_iter().flatten() {
400
            if graph.contains_key(d) && *d != n {
401
                if let Some(c) = visit(d, graph, marks, stack) {
402
                    return Some(c);
403
                }
404
            }
405
        }
406
        stack.pop();
407
        marks.insert(n, Mark::Done);
408
        None
409
    }
410
    let mut marks = BTreeMap::new();
411
    for n in graph.keys() {
412
        let mut stack = Vec::new();
413
        if let Some(c) = visit(n, graph, &mut marks, &mut stack) {
414
            return Some(c);
415
        }
416
    }
417
    None
418
}
419
420
fn block<'a>(ctx: &mut Ctx, node: &'a KdlNode) -> &'a [KdlNode] {
421
    match node.children() {
422
        Some(d) => d.nodes(),
423
        None => {
424
            ctx.err(node.span(), format!("`{}` needs a block {{ … }}", node.name().value()));
425
            &[]
426
        }
427
    }
428
}
429
430
fn parse_sandbox(ctx: &mut Ctx, node: &KdlNode) -> Option<Sandbox> {
431
    let name = ctx.one_string(node)?;
432
    let named = ctx.name_ok(node.span(), "sandbox", &name);
433
    let (mut runtime, mut rootfs, mut memory, mut cpus, mut network) =
434
        (Some(Runtime::Microvm), None, Some(1024u32), Some(1u32), Some(Network::Isolated));
435
    let mut seen = BTreeSet::new();
436
    for c in block(ctx, node) {
437
        let key = c.name().value();
438
        if !seen.insert(key.to_string()) {
439
            ctx.err(c.span(), format!("`{key}` is given twice"));
440
        }
441
        ctx.no_children(c);
442
        match key {
443
            "runtime" => {
444
                runtime = match ctx.one_string(c).as_deref() {
445
                    Some("microvm") => Some(Runtime::Microvm),
446
                    Some("syscall") | Some("gvisor") => Some(Runtime::Syscall),
447
                    Some("wasm") => {
448
                        ctx.err(c.span(), "runtime \"wasm\" is not in M0 yet; use \"microvm\" or \"syscall\"");
449
                        None
450
                    }
451
                    Some(other) => {
452
                        ctx.err(c.span(), format!("runtime {other:?}: expected \"microvm\", \"syscall\" or \"wasm\""));
453
                        None
454
                    }
455
                    None => None,
456
                }
457
            }
458
            "rootfs" => {
459
                rootfs = match ctx.one_string(c) {
460
                    Some(r) => match r.strip_prefix("cas://blob/") {
461
                        Some(h) if h.len() == 64 && h.bytes().all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) => {
462
                            Some(h.to_string())
463
                        }
464
                        _ => {
465
                            ctx.err(c.span(), format!("rootfs {r:?}: expected \"cas://blob/\" and 64 lower-case hex digits"));
466
                            None
467
                        }
468
                    },
469
                    None => None,
470
                }
471
            }
472
            "memory-limit" => {
473
                memory = match ctx.one_string(c) {
474
                    Some(m) => match parse_memory(&m) {
475
                        Some(mib) if (MIN_MEMORY_MIB..=MAX_MEMORY_MIB).contains(&mib) => Some(mib as u32),
476
                        Some(_) => {
477
                            ctx.err(c.span(), format!("memory-limit {m:?} must be from 64MiB to 16GiB"));
478
                            None
479
                        }
480
                        None => {
481
                            ctx.err(c.span(), format!("memory-limit {m:?}: expected a whole number of KiB, MiB or GiB, as \"4GiB\""));
482
                            None
483
                        }
484
                    },
485
                    None => None,
486
                }
487
            }
488
            "cpu-limit" => {
489
                cpus = match ctx.one_integer(c) {
490
                    Some(n) if (1..=MAX_CPUS).contains(&n) => Some(n as u32),
491
                    Some(n) => {
492
                        ctx.err(c.span(), format!("cpu-limit {n} must be from 1 to {MAX_CPUS}"));
493
                        None
494
                    }
495
                    None => None,
496
                }
497
            }
498
            "network" => {
499
                network = match ctx.one_string(c).as_deref() {
500
                    Some("isolated") => Some(Network::Isolated),
501
                    // Loopback only: the guest's `lo` comes up, and it still has
502
                    // no device that reaches anything outside it.
503
                    Some("loopback") => Some(Network::Loopback),
504
                    Some("egress") => {
505
                        ctx.err(c.span(), "network \"egress\" is not in M0; M0 has \"isolated\" and \"loopback\"");
506
                        None
507
                    }
508
                    Some(other) => {
509
                        ctx.err(c.span(), format!("network {other:?}: expected \"isolated\", \"loopback\" or \"egress\""));
510
                        None
511
                    }
512
                    None => None,
513
                }
514
            }
515
            other => ctx.err(c.span(), format!("unknown sandbox setting `{other}`")),
516
        }
517
    }
518
    if rootfs.is_none() && !seen.contains("rootfs") {
519
        ctx.err(node.span(), format!("sandbox {name:?} needs a rootfs"));
520
    }
521
    if !named {
522
        return None;
523
    }
524
    Some(Sandbox { name, runtime: runtime?, rootfs: rootfs?, memory_mib: memory?, cpus: cpus?, network: network? })
525
}
526
527
/// `mount { source "ephemeral://disk"; target "/src/target"; size "8GiB" }`.
528
fn parse_mount(ctx: &mut Ctx, node: &KdlNode) -> Option<Mount> {
529
    let (mut source, mut target, mut size) = (None, None, None);
530
    let mut seen = BTreeSet::new();
531
    for c in block(ctx, node) {
532
        let key = c.name().value();
533
        if !seen.insert(key.to_string()) {
534
            ctx.err(c.span(), format!("`{key}` is given twice"));
535
            continue;
536
        }
537
        ctx.no_children(c);
538
        match key {
539
            "source" => {
540
                source = match ctx.one_string(c).as_deref() {
541
                    Some("ephemeral://tmpfs") => Some(MountSource::Tmpfs),
542
                    Some("ephemeral://disk") => Some(MountSource::Disk),
543
                    Some(other) => {
544
                        ctx.err(
545
                            c.span(),
546
                            format!("mount source {other:?} is not in M0 yet; M0 mounts \"ephemeral://tmpfs\" or \"ephemeral://disk\""),
547
                        );
548
                        None
549
                    }
550
                    None => None,
551
                }
552
            }
553
            "target" => {
554
                target = match ctx.one_string(c) {
555
                    Some(t) if mount_target_ok(&t) => Some(t),
556
                    Some(t) => {
557
                        ctx.err(
558
                            c.span(),
559
                            format!("mount target {t:?} must be an absolute path under /src or /tmp, without . or .. or a trailing /"),
560
                        );
561
                        None
562
                    }
563
                    None => None,
564
                }
565
            }
566
            "size" => {
567
                size = match ctx.one_string(c) {
568
                    Some(m) => match parse_memory(&m) {
569
                        Some(mib) if (MIN_MOUNT_MIB..=MAX_MOUNT_MIB).contains(&mib) => Some(mib as u32),
570
                        Some(_) => {
571
                            ctx.err(c.span(), format!("mount size {m:?} must be from 16MiB to 64GiB"));
572
                            None
573
                        }
574
                        None => {
575
                            ctx.err(c.span(), format!("mount size {m:?}: expected a whole number of KiB, MiB or GiB, as \"8GiB\""));
576
                            None
577
                        }
578
                    },
579
                    None => None,
580
                }
581
            }
582
            "readonly" | "include" => ctx.err(c.span(), format!("mount `{key}` is not in M0 yet")),
583
            other => ctx.err(c.span(), format!("unknown mount setting `{other}`")),
584
        }
585
    }
586
    for (name, missing) in [("source", source.is_none()), ("target", target.is_none()), ("size", size.is_none())] {
587
        if missing && !seen.contains(name) {
588
            ctx.err(node.span(), format!("a mount needs a {name}"));
589
        }
590
    }
591
    Some(Mount { source: source?, target: target?, size_mib: size? })
592
}
593
594
fn parse_job(ctx: &mut Ctx, node: &KdlNode) -> Option<Job> {
595
    let name = ctx.one_string(node)?;
596
    let named = ctx.name_ok(node.span(), "job", &name);
597
    let mut job = Job {
598
        name: name.clone(),
599
        sandbox: String::new(),
600
        scope: vec![],
601
        depends_on: vec![],
602
        env: BTreeMap::new(),
603
        exec: vec![],
604
        mounts: vec![],
605
    };
606
    let mut seen = BTreeSet::new();
607
    for c in block(ctx, node) {
608
        let key = c.name().value();
609
        if key != "scope" && key != "depends-on" && key != "mount" && !seen.insert(key.to_string()) {
610
            ctx.err(c.span(), format!("`{key}` is given twice"));
611
        }
612
        match key {
613
            "sandbox" => {
614
                ctx.no_children(c);
615
                job.sandbox = ctx.one_string(c).unwrap_or_default();
616
            }
617
            "scope" => {
618
                ctx.no_children(c);
619
                for g in ctx.strings(c) {
620
                    match globset::Glob::new(&g) {
621
                        Ok(_) => job.scope.push(g),
622
                        Err(e) => ctx.err(c.span(), format!("scope {g:?}: {e}")),
623
                    }
624
                }
625
            }
626
            "depends-on" => {
627
                ctx.no_children(c);
628
                job.depends_on.extend(ctx.strings(c));
629
            }
630
            "env" => {
631
                if !c.entries().is_empty() {
632
                    ctx.err(c.span(), "`env` takes a block of NAME \"value\" lines");
633
                }
634
                for e in block(ctx, c) {
635
                    let k = e.name().value().to_string();
636
                    let valid = !k.is_empty()
637
                        && !k.starts_with(|c: char| c.is_ascii_digit())
638
                        && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_');
639
                    if !valid {
640
                        ctx.err(e.span(), format!("environment name {k:?} must be letters, digits and _"));
641
                        continue;
642
                    }
643
                    ctx.no_children(e);
644
                    if let Some(v) = ctx.one_string(e) {
645
                        if job.env.insert(k.clone(), v).is_some() {
646
                            ctx.err(e.span(), format!("environment {k} is set twice"));
647
                        }
648
                    }
649
                }
650
            }
651
            "exec" => {
652
                ctx.no_children(c);
653
                job.exec = ctx.strings(c);
654
                if job.exec.is_empty() {
655
                    ctx.err(c.span(), "`exec` needs a program and its arguments");
656
                } else if job.exec.iter().any(|a| a.contains('\0')) {
657
                    ctx.err(c.span(), "`exec` arguments cannot contain NUL");
658
                }
659
            }
660
            "mount" => {
661
                if !c.entries().is_empty() {
662
                    ctx.err(c.span(), "`mount` takes a block of source, target and size");
663
                }
664
                if let Some(m) = parse_mount(ctx, c) {
665
                    if job.mounts.len() == MAX_MOUNTS {
666
                        ctx.err(c.span(), format!("a job has at most {MAX_MOUNTS} mounts"));
667
                    } else if let Some(other) = job.mounts.iter().find(|o| {
668
                        o.target == m.target
669
                            || m.target.starts_with(&format!("{}/", o.target))
670
                            || o.target.starts_with(&format!("{}/", m.target))
671
                    }) {
672
                        ctx.err(c.span(), format!("mount {:?} overlaps mount {:?}", m.target, other.target));
673
                    } else {
674
                        job.mounts.push(m);
675
                    }
676
                }
677
            }
678
            n if LATER_JOB.contains(&n) => ctx.err(c.span(), format!("`{n}` is not in M0 yet")),
679
            other => ctx.err(c.span(), format!("unknown job setting `{other}`")),
680
        }
681
    }
682
    job.mounts.sort_by(|a, b| a.target.cmp(&b.target));
683
    if job.sandbox.is_empty() && !seen.contains("sandbox") {
684
        ctx.err(node.span(), format!("job {name:?} needs a sandbox"));
685
    }
686
    if job.exec.is_empty() && !seen.contains("exec") {
687
        ctx.err(node.span(), format!("job {name:?} needs an exec"));
688
    }
689
    job.depends_on.sort();
690
    job.depends_on.dedup();
691
    // Kept even when incomplete, so its references are checked too; the
692
    // problems above already make the pipeline invalid.
693
    named.then_some(job)
694
}