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