| e0918b88 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use serde::{Deserialize, Serialize}; |
| 6 |
use std::collections::{BTreeMap, BTreeSet}; |
| 7 |
use std::fmt; |
| 8 |
|
| 9 |
|
| 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 |
|
| 16 |
pub sandboxes: BTreeMap<String, Sandbox>, |
| 17 |
|
| 18 |
pub jobs: BTreeMap<String, Job>, |
| bf7bb1b8 Andy McCall ↶ |
19 |
|
| 20 |
|
| 21 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 22 |
pub on: Option<On>, |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 27 |
pub struct On { |
| 28 |
|
| 29 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 30 |
pub push: Option<Vec<String>>, |
| 31 |
|
| 32 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 33 |
pub tag: Option<Vec<String>>, |
| 34 |
|
| 35 |
#[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 36 |
pub manual: bool, |
| 37 |
|
| 38 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 39 |
pub schedule: Vec<String>, |
| e0918b88 Andy McCall |
40 |
} |
| 41 |
|
| bf7bb1b8 Andy McCall ↶ |
42 |
|
| 43 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 44 |
pub enum Event<'a> { |
| 45 |
|
| 46 |
Push(&'a str), |
| 47 |
|
| 48 |
Tag(&'a str), |
| 49 |
|
| 50 |
Manual, |
| 51 |
|
| 52 |
Schedule, |
| 53 |
} |
| 54 |
|
| 55 |
impl On { |
| 56 |
|
| 57 |
pub fn default_on() -> On { |
| 58 |
On { push: Some(vec![]), tag: None, manual: true, schedule: vec![] } |
| 59 |
} |
| 60 |
|
| 61 |
pub fn matches(&self, event: Event<'_>) -> bool { |
| 62 |
let any = |globs: &Option<Vec<String>>, name: &str| match globs { |
| 63 |
None => false, |
| 64 |
Some(g) if g.is_empty() => true, |
| 65 |
Some(g) => g.iter().any(|p| globset::Glob::new(p).is_ok_and(|p| p.compile_matcher().is_match(name))), |
| 66 |
}; |
| 67 |
match event { |
| 68 |
Event::Push(b) => any(&self.push, b), |
| 69 |
Event::Tag(t) => any(&self.tag, t), |
| 70 |
Event::Manual => self.manual, |
| 71 |
Event::Schedule => !self.schedule.is_empty(), |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
impl Pipeline { |
| 77 |
|
| 78 |
pub fn on_of(&self, job: &Job) -> On { |
| 79 |
job.on.clone().or_else(|| self.on.clone()).unwrap_or_else(On::default_on) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| e0918b88 Andy McCall |
83 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] |
| 84 |
#[serde(rename_all = "lowercase")] |
| 85 |
pub enum Runtime { |
| 86 |
|
| 87 |
Wasm, |
| 88 |
|
| 89 |
Syscall, |
| 90 |
|
| 91 |
Microvm, |
| 92 |
} |
| 93 |
|
| 94 |
impl Runtime { |
| 95 |
pub fn as_str(self) -> &'static str { |
| 96 |
match self { |
| 97 |
Runtime::Wasm => "wasm", |
| 98 |
Runtime::Syscall => "syscall", |
| 99 |
Runtime::Microvm => "microvm", |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 105 |
#[serde(rename_all = "lowercase")] |
| 106 |
pub enum Network { |
| 107 |
Isolated, |
| 108 |
Loopback, |
| 109 |
Egress, |
| 110 |
} |
| 111 |
|
| 112 |
impl Network { |
| 113 |
pub fn as_str(self) -> &'static str { |
| 114 |
match self { |
| 115 |
Network::Isolated => "isolated", |
| 116 |
Network::Loopback => "loopback", |
| 117 |
Network::Egress => "egress", |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 123 |
pub struct Sandbox { |
| 124 |
pub name: String, |
| 125 |
pub runtime: Runtime, |
| 126 |
|
| 127 |
pub rootfs: String, |
| 128 |
pub memory_mib: u32, |
| 129 |
pub cpus: u32, |
| 130 |
pub network: Network, |
| a72abeee Andy McCall ↶ |
131 |
|
| 132 |
|
| 133 |
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")] |
| 134 |
pub env: BTreeMap<String, String>, |
| e576e086 Andy McCall ↶ |
135 |
|
| 136 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 137 |
pub runs_on: Vec<String>, |
| ea81796b Andy McCall ↶ |
138 |
|
| 139 |
|
| 140 |
|
| 141 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 142 |
pub egress_allow: Vec<String>, |
| 02b127d2 Andy McCall ↶ |
143 |
|
| 144 |
|
| 145 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 146 |
pub timeout_s: Option<u64>, |
| 1abadd4a Andy McCall ↶ |
147 |
|
| 148 |
|
| 149 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 150 |
pub arch: Option<String>, |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
pub fn arch_ok(a: &str) -> bool { |
| 155 |
(2..=16).contains(&a.len()) && a.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_') |
| e0918b88 Andy McCall |
156 |
} |
| 157 |
|
| 158 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 159 |
pub struct Job { |
| 160 |
pub name: String, |
| 161 |
pub sandbox: String, |
| 162 |
|
| 163 |
pub scope: Vec<String>, |
| 164 |
|
| 165 |
pub depends_on: Vec<String>, |
| 166 |
pub env: BTreeMap<String, String>, |
| 167 |
|
| 168 |
pub exec: Vec<String>, |
| fc10d9e8 Andy McCall ↶ |
169 |
|
| 170 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 171 |
pub mounts: Vec<Mount>, |
| a67b97e9 Andy McCall ↶ |
172 |
|
| 173 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 174 |
pub outputs: Vec<Output>, |
| 46fe2c81 Andy McCall ↶ |
175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 179 |
pub inputs: Vec<Input>, |
| e2089721 Andy McCall ↶ |
180 |
|
| 181 |
|
| 182 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 183 |
pub approval: Option<Approval>, |
| f4e51e63 Andy McCall ↶ |
184 |
|
| 185 |
|
| 186 |
|
| 187 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 188 |
pub environment: Option<String>, |
| e2c12561 Andy McCall ↶ |
189 |
|
| 190 |
|
| 191 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 192 |
pub secrets: Vec<Secret>, |
| 720eb819 Andy McCall ↶ |
193 |
|
| 194 |
|
| 195 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 196 |
pub identities: Vec<Identity>, |
| e576e086 Andy McCall ↶ |
197 |
|
| 198 |
|
| 199 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 200 |
pub runs_on: Vec<String>, |
| 02b127d2 Andy McCall ↶ |
201 |
|
| 202 |
|
| 203 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 204 |
pub timeout_s: Option<u64>, |
| bf7bb1b8 Andy McCall ↶ |
205 |
|
| 206 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 207 |
pub on: Option<On>, |
| fc10d9e8 Andy McCall ↶ |
208 |
} |
| 209 |
|
| c4285535 Andy McCall ↶ |
210 |
impl Job { |
| a72abeee Andy McCall ↶ |
211 |
|
| 212 |
pub fn in_sandbox(&self, sandbox: &Sandbox) -> Job { |
| 213 |
let mut job = self.clone(); |
| 214 |
for (k, v) in &sandbox.env { |
| 215 |
job.env.entry(k.clone()).or_insert_with(|| v.clone()); |
| 216 |
} |
| e576e086 Andy McCall ↶ |
217 |
job.runs_on.extend(sandbox.runs_on.iter().cloned()); |
| 218 |
job.runs_on.sort(); |
| 219 |
job.runs_on.dedup(); |
| 02b127d2 Andy McCall ↶ |
220 |
job.timeout_s = job.timeout_s.or(sandbox.timeout_s); |
| a72abeee Andy McCall ↶ |
221 |
job |
| 222 |
} |
| 223 |
|
| c4285535 Andy McCall ↶ |
224 |
|
| 225 |
|
| 226 |
|
| 227 |
pub fn with_flow_env(&self, vars: &[(String, String)]) -> Job { |
| 228 |
let mut job = self.clone(); |
| 229 |
for (k, v) in vars { |
| 230 |
job.env.entry(k.clone()).or_insert_with(|| v.clone()); |
| 231 |
} |
| 232 |
job |
| 233 |
} |
| 234 |
} |
| 235 |
|
| fc10d9e8 Andy McCall ↶ |
236 |
|
| d60aac6c Andy McCall ↶ |
237 |
|
| 238 |
|
| 239 |
|
| fc10d9e8 Andy McCall ↶ |
240 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 241 |
pub struct Mount { |
| 242 |
pub source: MountSource, |
| 243 |
|
| 244 |
pub target: String, |
| 245 |
pub size_mib: u32, |
| d60aac6c Andy McCall ↶ |
246 |
|
| 247 |
|
| 248 |
#[serde(default, skip_serializing_if = "Option::is_none")] |
| 249 |
pub cache: Option<String>, |
| 250 |
|
| 251 |
#[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 252 |
pub key: Vec<String>, |
| fc10d9e8 Andy McCall ↶ |
253 |
} |
| 254 |
|
| 255 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 256 |
#[serde(rename_all = "lowercase")] |
| 257 |
pub enum MountSource { |
| 258 |
|
| 259 |
Tmpfs, |
| 260 |
|
| 261 |
Disk, |
| 262 |
} |
| 263 |
|
| 264 |
impl MountSource { |
| 265 |
pub fn as_str(self) -> &'static str { |
| 266 |
match self { |
| 267 |
MountSource::Tmpfs => "ephemeral://tmpfs", |
| 268 |
MountSource::Disk => "ephemeral://disk", |
| 269 |
} |
| 270 |
} |
| a67b97e9 Andy McCall ↶ |
271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 276 |
pub struct Output { |
| 277 |
|
| 278 |
pub name: String, |
| 279 |
|
| 280 |
pub path: String, |
| 9a6fac2b Andy McCall ↶ |
281 |
|
| 282 |
|
| 283 |
#[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 284 |
pub always: bool, |
| 6d53fb7d Andy McCall ↶ |
285 |
|
| 286 |
|
| 287 |
#[serde(default, skip_serializing_if = "OutputKind::is_artifact")] |
| 288 |
pub kind: OutputKind, |
| 46fe2c81 Andy McCall ↶ |
289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 294 |
pub struct Input { |
| 295 |
|
| 296 |
pub artifact: String, |
| 297 |
|
| 298 |
pub from: String, |
| 299 |
|
| 300 |
pub path: String, |
| e2089721 Andy McCall ↶ |
301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 306 |
pub struct Approval { |
| 307 |
|
| 308 |
pub by: String, |
| 309 |
|
| 310 |
#[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 311 |
pub others_only: bool, |
| 312 |
|
| 313 |
pub expires_s: u64, |
| 2be1fe4a Andy McCall ↶ |
314 |
|
| 315 |
|
| 316 |
|
| 317 |
#[serde(default, skip_serializing_if = "std::ops::Not::not")] |
| 318 |
pub sealed: bool, |
| e2c12561 Andy McCall ↶ |
319 |
} |
| 46fe2c81 Andy McCall ↶ |
320 |
|
| e2089721 Andy McCall ↶ |
321 |
|
| 322 |
const MAX_APPROVAL_S: u64 = 30 * 24 * 3600; |
| 323 |
|
| 46fe2c81 Andy McCall ↶ |
324 |
|
| 325 |
const MAX_INPUTS: usize = 8; |
| e2c12561 Andy McCall ↶ |
326 |
|
| 6d53fb7d Andy McCall ↶ |
327 |
|
| 328 |
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 329 |
#[serde(rename_all = "lowercase")] |
| 330 |
pub enum OutputKind { |
| 331 |
#[default] |
| 332 |
Artifact, |
| 333 |
Report, |
| 334 |
} |
| 335 |
|
| 336 |
impl OutputKind { |
| 337 |
pub fn is_artifact(&self) -> bool { |
| 338 |
*self == OutputKind::Artifact |
| 339 |
} |
| 340 |
|
| 341 |
pub fn word(&self) -> &'static str { |
| 342 |
match self { |
| 343 |
OutputKind::Artifact => "artifact", |
| 344 |
OutputKind::Report => "report", |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| e2c12561 Andy McCall ↶ |
349 |
|
| 350 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 351 |
pub struct Secret { |
| 352 |
|
| 353 |
pub name: String, |
| 354 |
|
| 355 |
|
| 356 |
pub mount_path: String, |
| a67b97e9 Andy McCall ↶ |
357 |
} |
| 358 |
|
| 720eb819 Andy McCall ↶ |
359 |
|
| 360 |
|
| 361 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 362 |
pub struct Identity { |
| 363 |
|
| 364 |
pub audience: String, |
| 365 |
|
| 366 |
|
| 367 |
pub mount_path: String, |
| 368 |
} |
| 369 |
|
| e2c12561 Andy McCall ↶ |
370 |
|
| 371 |
pub fn secret_path_ok(p: &str) -> bool { |
| 372 |
p.strip_prefix("/run/secrets/").is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..")) |
| 373 |
} |
| 374 |
|
| a67b97e9 Andy McCall ↶ |
375 |
|
| 376 |
pub const MAX_OUTPUTS: usize = 16; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
fn output_path_ok(p: &str) -> bool { |
| 381 |
p.strip_prefix("/src/").is_some_and(|rest| { |
| 382 |
!rest.is_empty() |
| 383 |
&& rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..") |
| 384 |
&& p.bytes().all(|b| b.is_ascii_graphic() && b != b'"' && b != b'\\') |
| 385 |
}) |
| fc10d9e8 Andy McCall ↶ |
386 |
} |
| 387 |
|
| 388 |
|
| 389 |
pub const MAX_MOUNTS: usize = 4; |
| e2c12561 Andy McCall ↶ |
390 |
|
| 391 |
pub const MAX_SECRETS: usize = 8; |
| 720eb819 Andy McCall ↶ |
392 |
|
| 393 |
pub const MAX_IDENTITIES: usize = 4; |
| fc10d9e8 Andy McCall ↶ |
394 |
|
| 395 |
pub const MIN_MOUNT_MIB: u64 = 16; |
| 396 |
pub const MAX_MOUNT_MIB: u64 = 64 * 1024; |
| 397 |
|
| 398 |
|
| 399 |
fn mount_target_ok(t: &str) -> bool { |
| 400 |
let under = t.strip_prefix("/src/").or_else(|| t.strip_prefix("/tmp/")); |
| 401 |
under.is_some_and(|rest| !rest.is_empty() && rest.split('/').all(|c| !c.is_empty() && c != "." && c != "..")) |
| e0918b88 Andy McCall |
402 |
} |
| 403 |
|
| 404 |
|
| 405 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 406 |
pub struct Problem { |
| 407 |
pub line: usize, |
| 408 |
pub message: String, |
| 409 |
} |
| 410 |
|
| 411 |
impl fmt::Display for Problem { |
| 412 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 413 |
write!(f, "line {}: {}", self.line, self.message) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)] |
| 419 |
pub struct Problems(pub Vec<Problem>); |
| 420 |
|
| 421 |
impl fmt::Display for Problems { |
| 422 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 423 |
let lines: Vec<String> = self.0.iter().map(|p| p.to_string()).collect(); |
| 424 |
f.write_str(&lines.join("\n")) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
const MIN_MEMORY_MIB: u64 = 64; |
| 429 |
const MAX_MEMORY_MIB: u64 = 16 * 1024; |
| 430 |
const MAX_CPUS: i128 = 16; |
| 431 |
|
| 432 |
|
| 433 |
const LATER_TOP: &[&str] = &["publish", "secret"]; |
| e2c12561 Andy McCall ↶ |
434 |
const LATER_JOB: &[&str] = &["attest"]; |
| e0918b88 Andy McCall |
435 |
|
| 436 |
struct Ctx<'a> { |
| 437 |
src: &'a str, |
| 438 |
problems: Vec<Problem>, |
| 439 |
} |
| 440 |
|
| 441 |
impl Ctx<'_> { |
| 442 |
fn line(&self, span: SourceSpan) -> usize { |
| 443 |
let at = span.offset().min(self.src.len()); |
| 444 |
self.src[..at].bytes().filter(|b| *b == b'\n').count() + 1 |
| 445 |
} |
| 446 |
|
| 447 |
fn err(&mut self, span: SourceSpan, message: impl Into<String>) { |
| 448 |
let line = self.line(span); |
| 449 |
self.problems.push(Problem { line, message: message.into() }); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
fn strings(&mut self, node: &KdlNode) -> Vec<String> { |
| 454 |
let mut out = Vec::new(); |
| 455 |
for e in node.entries() { |
| 456 |
if let Some(k) = e.name() { |
| 457 |
self.err(e.span(), format!("`{}` takes no property `{}`", node.name().value(), k.value())); |
| 458 |
continue; |
| 459 |
} |
| 460 |
match e.value() { |
| 461 |
KdlValue::String(s) => out.push(s.clone()), |
| 462 |
other => self.err(e.span(), format!("`{}` takes strings, not {}", node.name().value(), kind_of(other))), |
| 463 |
} |
| 464 |
} |
| 465 |
out |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
fn one_string(&mut self, node: &KdlNode) -> Option<String> { |
| 470 |
let v = self.strings(node); |
| 471 |
if v.len() != 1 { |
| 472 |
self.err(node.span(), format!("`{}` takes one string", node.name().value())); |
| 473 |
return None; |
| 474 |
} |
| 475 |
v.into_iter().next() |
| 476 |
} |
| 477 |
|
| 478 |
fn one_integer(&mut self, node: &KdlNode) -> Option<i128> { |
| 479 |
match node.entries() { |
| 480 |
[e] if e.name().is_none() => match e.value() { |
| 481 |
KdlValue::Integer(n) => Some(*n), |
| 482 |
other => { |
| 483 |
self.err(e.span(), format!("`{}` takes an integer, not {}", node.name().value(), kind_of(other))); |
| 484 |
None |
| 485 |
} |
| 486 |
}, |
| 487 |
_ => { |
| 488 |
self.err(node.span(), format!("`{}` takes one integer", node.name().value())); |
| 489 |
None |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
fn no_children(&mut self, node: &KdlNode) { |
| 495 |
if node.children().is_some() { |
| 496 |
self.err(node.span(), format!("`{}` takes no block", node.name().value())); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
fn name_ok(&mut self, span: SourceSpan, what: &str, name: &str) -> bool { |
| 501 |
let ok = !name.is_empty() && name.len() <= 64 && name.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)); |
| 502 |
if !ok { |
| 503 |
self.err(span, format!("{what} name {name:?} must be 1 to 64 of A-Z a-z 0-9 . _ -")); |
| 504 |
} |
| 505 |
ok |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
use kdl::{KdlDocument, KdlNode, KdlValue}; |
| 510 |
use miette::SourceSpan; |
| 511 |
|
| 512 |
fn kind_of(v: &KdlValue) -> &'static str { |
| 513 |
match v { |
| 514 |
KdlValue::String(_) => "a string", |
| 515 |
KdlValue::Integer(_) => "an integer", |
| 516 |
KdlValue::Float(_) => "a number", |
| 517 |
KdlValue::Bool(_) => "a boolean", |
| 518 |
KdlValue::Null => "null", |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
fn parse_memory(s: &str) -> Option<u64> { |
| 524 |
let (num, unit) = s.split_at(s.find(|c: char| !c.is_ascii_digit())?); |
| 525 |
let n: u64 = num.parse().ok()?; |
| 526 |
let mib = match unit { |
| 527 |
"KiB" => n.is_multiple_of(1024).then_some(n / 1024)?, |
| 528 |
"MiB" => n, |
| 529 |
"GiB" => n.checked_mul(1024)?, |
| 530 |
_ => return None, |
| 531 |
}; |
| 532 |
Some(mib) |
| 533 |
} |
| 534 |
|
| b647ab72 Andy McCall ↶ |
535 |
|
| 536 |
|
| 537 |
fn foreign_escape_line(src: &str) -> Option<usize> { |
| 538 |
src.lines() |
| 539 |
.position(|l| { |
| 540 |
let b = l.as_bytes(); |
| 541 |
b.windows(2).enumerate().any(|(i, w)| { |
| 542 |
w[0] == b'\\' |
| 543 |
&& (i == 0 || b[i - 1] != b'\\') |
| 544 |
&& (w[1] == b'e' || w[1] == b'x' || (w[1] == b'0' && b.get(i + 2).is_some_and(u8::is_ascii_digit))) |
| 545 |
}) |
| 546 |
}) |
| 547 |
.map(|i| i + 1) |
| 548 |
} |
| 549 |
|
| e0918b88 Andy McCall |
550 |
impl Pipeline { |
| 551 |
|
| 552 |
pub fn parse(src: &str) -> Result<Pipeline, Problems> { |
| 553 |
let doc = match KdlDocument::parse_v2(src) { |
| 554 |
Ok(d) => d, |
| 555 |
Err(e) => { |
| 556 |
let mut ctx = Ctx { src, problems: Vec::new() }; |
| 557 |
for d in &e.diagnostics { |
| 558 |
let msg = d.message.clone().or_else(|| d.label.clone()).unwrap_or_else(|| "not valid KDL 2.0".into()); |
| 559 |
ctx.err(d.span, msg); |
| 560 |
} |
| 561 |
if ctx.problems.is_empty() { |
| 562 |
ctx.problems.push(Problem { line: 1, message: "not valid KDL 2.0".into() }); |
| b647ab72 Andy McCall ↶ |
563 |
} |
| 564 |
if let Some(line) = foreign_escape_line(src) { |
| 565 |
ctx.problems.push(Problem { |
| 566 |
line, |
| 567 |
message: "KDL strings have no \\033, \\e or \\x escapes: write ESC (for ANSI colours) as \\u{1b}".into(), |
| 568 |
}); |
| e0918b88 Andy McCall |
569 |
} |
| 570 |
return Err(Problems(ctx.problems)); |
| 571 |
} |
| 572 |
}; |
| 573 |
let mut ctx = Ctx { src, problems: Vec::new() }; |
| 574 |
let mut schema = None; |
| 575 |
let mut sandboxes = BTreeMap::new(); |
| 576 |
let mut jobs: BTreeMap<String, (Job, SourceSpan)> = BTreeMap::new(); |
| bf7bb1b8 Andy McCall ↶ |
577 |
let mut on = None; |
| e0918b88 Andy McCall |
578 |
for node in doc.nodes() { |
| 579 |
match node.name().value() { |
| bf7bb1b8 Andy McCall ↶ |
580 |
"on" => { |
| 581 |
if on.is_some() { |
| 582 |
ctx.err(node.span(), "`on` is given twice"); |
| 583 |
} |
| 584 |
on = Some(parse_on(&mut ctx, node)); |
| 585 |
} |
| e0918b88 Andy McCall |
586 |
"schema-version" => { |
| 587 |
ctx.no_children(node); |
| 588 |
match ctx.one_integer(node) { |
| 589 |
Some(1) => schema = Some(1), |
| 590 |
Some(n) => ctx.err(node.span(), format!("schema-version {n} is not supported; this runner reads 1")), |
| 591 |
None => {} |
| 592 |
} |
| 593 |
} |
| 594 |
"sandbox" => { |
| 595 |
if let Some(sb) = parse_sandbox(&mut ctx, node) { |
| 596 |
if sandboxes.contains_key(&sb.name) { |
| 597 |
ctx.err(node.span(), format!("sandbox {:?} is declared twice", sb.name)); |
| 598 |
} else { |
| 599 |
sandboxes.insert(sb.name.clone(), sb); |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
"job" => { |
| 604 |
if let Some(job) = parse_job(&mut ctx, node) { |
| 605 |
if jobs.contains_key(&job.name) { |
| 606 |
ctx.err(node.span(), format!("job {:?} is declared twice", job.name)); |
| 607 |
} else { |
| 608 |
jobs.insert(job.name.clone(), (job, node.span())); |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
n if LATER_TOP.contains(&n) => ctx.err(node.span(), format!("`{n}` is not in M0 yet")), |
| bf7bb1b8 Andy McCall ↶ |
613 |
n => ctx.err(node.span(), format!("unknown node `{n}`; expected schema-version, on, sandbox or job")), |
| e0918b88 Andy McCall |
614 |
} |
| 615 |
} |
| 616 |
if schema.is_none() && !ctx.problems.iter().any(|p| p.message.contains("schema-version")) { |
| 617 |
ctx.problems.push(Problem { line: 1, message: "`schema-version 1` is required".into() }); |
| 618 |
} |
| 619 |
if jobs.is_empty() && ctx.problems.is_empty() { |
| 620 |
ctx.problems.push(Problem { line: 1, message: "a pipeline needs at least one job".into() }); |
| 621 |
} |
| 622 |
|
| 623 |
for (job, span) in jobs.values() { |
| 624 |
if !job.sandbox.is_empty() && !sandboxes.contains_key(&job.sandbox) { |
| 625 |
ctx.err(*span, format!("job {:?} uses sandbox {:?}, which is not declared", job.name, job.sandbox)); |
| 626 |
} |
| 627 |
for d in &job.depends_on { |
| 628 |
if !jobs.contains_key(d) { |
| 629 |
ctx.err(*span, format!("job {:?} depends on {:?}, which is not declared", job.name, d)); |
| 630 |
} else if d == &job.name { |
| 631 |
ctx.err(*span, format!("job {:?} depends on itself", job.name)); |
| 46fe2c81 Andy McCall ↶ |
632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
for i in &job.inputs { |
| 637 |
let Some((producer, _)) = jobs.get(&i.from) else { |
| 638 |
ctx.err(*span, format!("job {:?} takes an input from {:?}, which is not declared", job.name, i.from)); |
| 639 |
continue; |
| 640 |
}; |
| 641 |
if !job.depends_on.contains(&i.from) { |
| 642 |
ctx.err(*span, format!("job {:?} takes {:?} from {:?}: add depends-on \"{}\"", job.name, i.artifact, i.from, i.from)); |
| e0918b88 Andy McCall |
643 |
} |
| 46fe2c81 Andy McCall ↶ |
644 |
match producer.outputs.iter().find(|o| o.name == i.artifact) { |
| 645 |
Some(o) if o.kind == OutputKind::Artifact => {} |
| 646 |
Some(_) => ctx.err(*span, format!("{:?} of job {:?} is a report; inputs are artifacts", i.artifact, i.from)), |
| 647 |
None => ctx.err(*span, format!("job {:?} declares no artifact {:?}", i.from, i.artifact)), |
| 648 |
} |
| e0918b88 Andy McCall |
649 |
} |
| 650 |
} |
| 651 |
let graph: BTreeMap<&str, Vec<&str>> = |
| 652 |
jobs.values().map(|(j, _)| (j.name.as_str(), j.depends_on.iter().map(String::as_str).collect())).collect(); |
| 653 |
if let Some(cycle) = find_cycle(&graph) { |
| 654 |
let span = jobs[cycle[0]].1; |
| 655 |
ctx.err(span, format!("jobs depend on each other in a cycle: {}", cycle.join(" → "))); |
| 656 |
} |
| 657 |
if !ctx.problems.is_empty() { |
| 658 |
ctx.problems.sort_by_key(|p| p.line); |
| 659 |
return Err(Problems(ctx.problems)); |
| 660 |
} |
| bf7bb1b8 Andy McCall ↶ |
661 |
Ok(Pipeline { schema_version: 1, sandboxes, jobs: jobs.into_iter().map(|(k, (j, _))| (k, j)).collect(), on }) |
| e0918b88 Andy McCall |
662 |
} |
| 663 |
|
| 664 |
|
| 665 |
pub fn order(&self) -> Vec<&Job> { |
| 666 |
let mut done: BTreeSet<&str> = BTreeSet::new(); |
| 667 |
let mut out = Vec::with_capacity(self.jobs.len()); |
| 668 |
while out.len() < self.jobs.len() { |
| 669 |
let before = out.len(); |
| 670 |
for j in self.jobs.values() { |
| 671 |
if !done.contains(j.name.as_str()) && j.depends_on.iter().all(|d| done.contains(d.as_str())) { |
| 672 |
out.push(j); |
| 673 |
} |
| 674 |
} |
| 675 |
for j in &out[before..] { |
| 676 |
done.insert(j.name.as_str()); |
| 677 |
} |
| 678 |
if out.len() == before { |
| 679 |
break; |
| 680 |
} |
| 681 |
} |
| 682 |
out |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
pub fn depths(&self) -> BTreeMap<&str, usize> { |
| 687 |
let mut depth: BTreeMap<&str, usize> = BTreeMap::new(); |
| 688 |
for j in self.order() { |
| 689 |
let d = j.depends_on.iter().map(|x| depth.get(x.as_str()).copied().unwrap_or(0) + 1).max().unwrap_or(0); |
| 690 |
depth.insert(j.name.as_str(), d); |
| 691 |
} |
| 692 |
depth |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
fn find_cycle<'a>(graph: &BTreeMap<&'a str, Vec<&'a str>>) -> Option<Vec<&'a str>> { |
| 697 |
#[derive(Clone, Copy, PartialEq)] |
| 698 |
enum Mark { |
| 699 |
Visiting, |
| 700 |
Done, |
| 701 |
} |
| 702 |
fn visit<'a>( |
| 703 |
n: &'a str, |
| 704 |
graph: &BTreeMap<&'a str, Vec<&'a str>>, |
| 705 |
marks: &mut BTreeMap<&'a str, Mark>, |
| 706 |
stack: &mut Vec<&'a str>, |
| 707 |
) -> Option<Vec<&'a str>> { |
| 708 |
match marks.get(n) { |
| 709 |
Some(Mark::Done) => return None, |
| 710 |
Some(Mark::Visiting) => { |
| 711 |
let at = stack.iter().position(|s| *s == n).unwrap_or(0); |
| 712 |
let mut c = stack[at..].to_vec(); |
| 713 |
c.push(n); |
| 714 |
return Some(c); |
| 715 |
} |
| 716 |
None => {} |
| 717 |
} |
| 718 |
marks.insert(n, Mark::Visiting); |
| 719 |
stack.push(n); |
| 720 |
for d in graph.get(n).into_iter().flatten() { |
| 721 |
if graph.contains_key(d) && *d != n { |
| 722 |
if let Some(c) = visit(d, graph, marks, stack) { |
| 723 |
return Some(c); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
stack.pop(); |
| 728 |
marks.insert(n, Mark::Done); |
| 729 |
None |
| 730 |
} |
| 731 |
let mut marks = BTreeMap::new(); |
| 732 |
for n in graph.keys() { |
| 733 |
let mut stack = Vec::new(); |
| 734 |
if let Some(c) = visit(n, graph, &mut marks, &mut stack) { |
| 735 |
return Some(c); |
| 736 |
} |
| 737 |
} |
| 738 |
None |
| 739 |
} |
| 740 |
|
| 741 |
fn block<'a>(ctx: &mut Ctx, node: &'a KdlNode) -> &'a [KdlNode] { |
| 742 |
match node.children() { |
| 743 |
Some(d) => d.nodes(), |
| 744 |
None => { |
| 745 |
ctx.err(node.span(), format!("`{}` needs a block {{ … }}", node.name().value())); |
| 746 |
&[] |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
fn parse_sandbox(ctx: &mut Ctx, node: &KdlNode) -> Option<Sandbox> { |
| 752 |
let name = ctx.one_string(node)?; |
| 753 |
let named = ctx.name_ok(node.span(), "sandbox", &name); |
| 754 |
let (mut runtime, mut rootfs, mut memory, mut cpus, mut network) = |
| 755 |
(Some(Runtime::Microvm), None, Some(1024u32), Some(1u32), Some(Network::Isolated)); |
| 756 |
let mut seen = BTreeSet::new(); |
| a72abeee Andy McCall ↶ |
757 |
let mut env = BTreeMap::new(); |
| e576e086 Andy McCall ↶ |
758 |
let mut runs_on = Vec::new(); |
| 02b127d2 Andy McCall ↶ |
759 |
let mut timeout_s = None; |
| 1abadd4a Andy McCall ↶ |
760 |
let mut arch: Option<String> = None; |
| ea81796b Andy McCall ↶ |
761 |
let mut egress_allow = Vec::new(); |
| e0918b88 Andy McCall |
762 |
for c in block(ctx, node) { |
| 763 |
let key = c.name().value(); |
| e576e086 Andy McCall ↶ |
764 |
if key != "runs-on" && !seen.insert(key.to_string()) { |
| e0918b88 Andy McCall |
765 |
ctx.err(c.span(), format!("`{key}` is given twice")); |
| a72abeee Andy McCall ↶ |
766 |
} |
| 767 |
if key == "env" { |
| 768 |
parse_env(ctx, c, &mut env); |
| 769 |
continue; |
| e0918b88 Andy McCall |
770 |
} |
| e576e086 Andy McCall ↶ |
771 |
if key == "runs-on" { |
| 772 |
ctx.no_children(c); |
| 773 |
parse_labels(ctx, c, &mut runs_on); |
| 774 |
continue; |
| ea81796b Andy McCall ↶ |
775 |
} |
| 776 |
if key != "network" { |
| 777 |
ctx.no_children(c); |
| e576e086 Andy McCall ↶ |
778 |
} |
| e0918b88 Andy McCall |
779 |
match key { |
| 780 |
"runtime" => { |
| 781 |
runtime = match ctx.one_string(c).as_deref() { |
| 782 |
Some("microvm") => Some(Runtime::Microvm), |
| 783 |
Some("syscall") | Some("gvisor") => Some(Runtime::Syscall), |
| 28cc603f Andy McCall ↶ |
784 |
|
| 785 |
Some("wasm") => Some(Runtime::Wasm), |
| e0918b88 Andy McCall |
786 |
Some(other) => { |
| 787 |
ctx.err(c.span(), format!("runtime {other:?}: expected \"microvm\", \"syscall\" or \"wasm\"")); |
| 788 |
None |
| 789 |
} |
| 790 |
None => None, |
| 791 |
} |
| 792 |
} |
| 793 |
"rootfs" => { |
| 794 |
rootfs = match ctx.one_string(c) { |
| 795 |
Some(r) => match r.strip_prefix("cas://blob/") { |
| 796 |
Some(h) if h.len() == 64 && h.bytes().all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase()) => { |
| 797 |
Some(h.to_string()) |
| 798 |
} |
| 799 |
_ => { |
| 800 |
ctx.err(c.span(), format!("rootfs {r:?}: expected \"cas://blob/\" and 64 lower-case hex digits")); |
| 801 |
None |
| 802 |
} |
| 803 |
}, |
| 804 |
None => None, |
| 805 |
} |
| 806 |
} |
| 1abadd4a Andy McCall ↶ |
807 |
"arch" => { |
| 808 |
arch = match ctx.one_string(c) { |
| 809 |
Some(a) if arch_ok(&a) => Some(a), |
| 810 |
Some(a) => { |
| 811 |
ctx.err(c.span(), format!("arch {a:?} is not an architecture name such as x86_64, aarch64 or riscv64")); |
| 812 |
None |
| 813 |
} |
| 814 |
None => None, |
| 815 |
} |
| 816 |
} |
| e0918b88 Andy McCall |
817 |
"memory-limit" => { |
| 818 |
memory = match ctx.one_string(c) { |
| 819 |
Some(m) => match parse_memory(&m) { |
| 820 |
Some(mib) if (MIN_MEMORY_MIB..=MAX_MEMORY_MIB).contains(&mib) => Some(mib as u32), |
| 821 |
Some(_) => { |
| 822 |
ctx.err(c.span(), format!("memory-limit {m:?} must be from 64MiB to 16GiB")); |
| 823 |
None |
| 824 |
} |
| 825 |
None => { |
| 826 |
ctx.err(c.span(), format!("memory-limit {m:?}: expected a whole number of KiB, MiB or GiB, as \"4GiB\"")); |
| 827 |
None |
| 828 |
} |
| 829 |
}, |
| 830 |
None => None, |
| 831 |
} |
| 832 |
} |
| 833 |
"cpu-limit" => { |
| 834 |
cpus = match ctx.one_integer(c) { |
| 835 |
Some(n) if (1..=MAX_CPUS).contains(&n) => Some(n as u32), |
| 836 |
Some(n) => { |
| 837 |
ctx.err(c.span(), format!("cpu-limit {n} must be from 1 to {MAX_CPUS}")); |
| 838 |
None |
| 839 |
} |
| 840 |
None => None, |
| 841 |
} |
| 842 |
} |
| 843 |
"network" => { |
| ea81796b Andy McCall ↶ |
844 |
let kind = ctx.one_string(c); |
| 845 |
if kind.as_deref() != Some("egress") { |
| 846 |
ctx.no_children(c); |
| 847 |
} |
| 848 |
network = match kind.as_deref() { |
| e0918b88 Andy McCall |
849 |
Some("isolated") => Some(Network::Isolated), |
| eb5c4db6 Andy McCall ↶ |
850 |
|
| 851 |
|
| 852 |
Some("loopback") => Some(Network::Loopback), |
| ea81796b Andy McCall ↶ |
853 |
|
| 854 |
|
| 855 |
|
| eb5c4db6 Andy McCall ↶ |
856 |
Some("egress") => { |
| ea81796b Andy McCall ↶ |
857 |
egress_allow = parse_egress(ctx, c); |
| 858 |
Some(Network::Egress) |
| e0918b88 Andy McCall |
859 |
} |
| 860 |
Some(other) => { |
| 861 |
ctx.err(c.span(), format!("network {other:?}: expected \"isolated\", \"loopback\" or \"egress\"")); |
| 862 |
None |
| 863 |
} |
| 864 |
None => None, |
| 865 |
} |
| 866 |
} |
| 02b127d2 Andy McCall ↶ |
867 |
"timeout" => timeout_s = parse_timeout(ctx, c), |
| e0918b88 Andy McCall |
868 |
other => ctx.err(c.span(), format!("unknown sandbox setting `{other}`")), |
| 869 |
} |
| 870 |
} |
| 28cc603f Andy McCall ↶ |
871 |
|
| 872 |
|
| 873 |
let wasm = runtime == Some(Runtime::Wasm); |
| 874 |
if wasm { |
| 875 |
if seen.contains("rootfs") { |
| 876 |
ctx.err(node.span(), format!("sandbox {name:?}: runtime \"wasm\" takes no rootfs; its module is a file the job has")); |
| 877 |
} |
| 878 |
if network.is_some_and(|n| n != Network::Isolated) { |
| 879 |
ctx.err(node.span(), format!("sandbox {name:?}: runtime \"wasm\" has no network; leave out `network` or say \"isolated\"")); |
| 880 |
} |
| 881 |
rootfs = Some(String::new()); |
| 882 |
network = Some(Network::Isolated); |
| 883 |
} else if rootfs.is_none() && !seen.contains("rootfs") { |
| e0918b88 Andy McCall |
884 |
ctx.err(node.span(), format!("sandbox {name:?} needs a rootfs")); |
| 885 |
} |
| ea81796b Andy McCall ↶ |
886 |
if network == Some(Network::Egress) { |
| 887 |
if runtime == Some(Runtime::Syscall) { |
| 888 |
ctx.err(node.span(), format!("sandbox {name:?}: network \"egress\" needs runtime \"microvm\"")); |
| 889 |
} |
| 890 |
|
| 891 |
if !runs_on.iter().any(|l| l == EGRESS_LABEL) { |
| 892 |
runs_on.push(EGRESS_LABEL.to_string()); |
| 893 |
runs_on.sort(); |
| 894 |
} |
| 895 |
} |
| e0918b88 Andy McCall |
896 |
if !named { |
| 897 |
return None; |
| e576e086 Andy McCall ↶ |
898 |
} |
| 1abadd4a Andy McCall ↶ |
899 |
Some(Sandbox { |
| 900 |
name, |
| 901 |
runtime: runtime?, |
| 902 |
rootfs: rootfs?, |
| 903 |
memory_mib: memory?, |
| 904 |
cpus: cpus?, |
| 905 |
network: network?, |
| 906 |
env, |
| 907 |
runs_on, |
| ea81796b Andy McCall ↶ |
908 |
egress_allow, |
| 1abadd4a Andy McCall ↶ |
909 |
timeout_s, |
| 910 |
arch, |
| 911 |
}) |
| ea81796b Andy McCall ↶ |
912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
pub const EGRESS_LABEL: &str = "egress"; |
| 917 |
|
| 918 |
|
| 919 |
const MAX_EGRESS_HOSTS: usize = 32; |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
pub fn egress_pattern_ok(pattern: &str) -> bool { |
| 924 |
let name = pattern.strip_prefix("*.").unwrap_or(pattern); |
| 925 |
let labels: Vec<&str> = name.split('.').collect(); |
| 926 |
name.len() <= 253 |
| 927 |
&& labels.len() >= 2 |
| 928 |
&& labels.iter().all(|l| { |
| 929 |
!l.is_empty() |
| 930 |
&& l.len() <= 63 |
| 931 |
&& !l.starts_with('-') |
| 932 |
&& !l.ends_with('-') |
| 933 |
&& l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') |
| 934 |
}) |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
pub fn egress_allows(patterns: &[String], host: &str) -> bool { |
| 940 |
let host = host.trim_end_matches('.').to_ascii_lowercase(); |
| 941 |
patterns.iter().any(|p| match p.strip_prefix("*.") { |
| 942 |
Some(domain) => host.len() > domain.len() + 1 && host.ends_with(domain) && host[..host.len() - domain.len()].ends_with('.'), |
| 943 |
None => host == *p, |
| 944 |
}) |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
fn parse_egress(ctx: &mut Ctx, node: &KdlNode) -> Vec<String> { |
| 949 |
let mut out = Vec::new(); |
| 950 |
for c in block(ctx, node) { |
| 951 |
ctx.no_children(c); |
| 952 |
if c.name().value() != "allow" { |
| 953 |
ctx.err(c.span(), format!("unknown egress setting `{}`; egress takes `allow \"host\"...`", c.name().value())); |
| 954 |
continue; |
| 955 |
} |
| 956 |
for h in ctx.strings(c) { |
| 957 |
if egress_pattern_ok(&h) { |
| 958 |
out.push(h); |
| 959 |
} else { |
| 960 |
ctx.err( |
| 961 |
c.span(), |
| 962 |
format!("allow {h:?}: expected a lower-case host name such as \"registry.opentofu.org\", or \"*.\" and a domain"), |
| 963 |
); |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
out.sort(); |
| 968 |
out.dedup(); |
| 969 |
if out.is_empty() { |
| 970 |
ctx.err(node.span(), "network \"egress\" needs at least one `allow \"host\"`"); |
| 971 |
} else if out.len() > MAX_EGRESS_HOSTS { |
| 972 |
ctx.err(node.span(), format!("network \"egress\" may allow at most {MAX_EGRESS_HOSTS} hosts")); |
| 973 |
} |
| 974 |
out |
| bf7bb1b8 Andy McCall ↶ |
975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
fn parse_on(ctx: &mut Ctx, node: &KdlNode) -> On { |
| 980 |
let mut on = On::default(); |
| 981 |
if !node.entries().is_empty() { |
| 982 |
ctx.err(node.span(), "`on` takes a block: on { push \"main\"; tag \"v*\"; manual }"); |
| 983 |
} |
| 984 |
let globs = |ctx: &mut Ctx, c: &KdlNode| -> Vec<String> { |
| 985 |
let g = ctx.strings(c); |
| 986 |
for p in &g { |
| 987 |
if let Err(e) = globset::Glob::new(p) { |
| 988 |
ctx.err(c.span(), format!("{p:?}: {e}")); |
| 989 |
} |
| 990 |
} |
| 991 |
g |
| 992 |
}; |
| 993 |
for c in block(ctx, node) { |
| 994 |
ctx.no_children(c); |
| 995 |
match c.name().value() { |
| 996 |
"push" => on.push.get_or_insert_with(Vec::new).extend(globs(ctx, c)), |
| 997 |
"tag" => on.tag.get_or_insert_with(Vec::new).extend(globs(ctx, c)), |
| 998 |
"manual" => on.manual = true, |
| 999 |
"schedule" => { |
| 1000 |
for line in ctx.strings(c) { |
| 1001 |
if crate::cron::Cron::parse(&line).is_some() { |
| 1002 |
on.schedule.push(line); |
| 1003 |
} else { |
| 1004 |
ctx.err( |
| 1005 |
c.span(), |
| 1006 |
format!("schedule {line:?}: expected five cron fields (minute hour day month weekday), as \"0 3 * * *\""), |
| 1007 |
); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
other => ctx.err(c.span(), format!("unknown `on` event `{other}`; expected push, tag, manual or schedule")), |
| 1012 |
} |
| 1013 |
} |
| 1014 |
if on.push.is_none() && on.tag.is_none() && !on.manual && on.schedule.is_empty() { |
| 1015 |
ctx.err(node.span(), "`on` names no event, so nothing would run it"); |
| 1016 |
} |
| 1017 |
on |
| e576e086 Andy McCall ↶ |
1018 |
} |
| 1019 |
|
| 02b127d2 Andy McCall ↶ |
1020 |
|
| 1021 |
pub const MAX_TIMEOUT_S: u64 = 24 * 3600; |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
pub fn parse_duration(s: &str) -> Option<u64> { |
| 1026 |
let (mut total, mut num, mut last) = (0u64, String::new(), 0u8); |
| 1027 |
for ch in s.chars() { |
| 1028 |
if ch.is_ascii_digit() { |
| 1029 |
num.push(ch); |
| 1030 |
continue; |
| 1031 |
} |
| 1032 |
let (unit, mult) = match ch { |
| 1033 |
'h' => (1, 3600), |
| 1034 |
'm' => (2, 60), |
| 1035 |
's' => (3, 1), |
| 1036 |
_ => return None, |
| 1037 |
}; |
| 1038 |
|
| 1039 |
if num.is_empty() || unit <= last { |
| 1040 |
return None; |
| 1041 |
} |
| 1042 |
last = unit; |
| 1043 |
total = total.checked_add(num.parse::<u64>().ok()?.checked_mul(mult)?)?; |
| 1044 |
num.clear(); |
| 1045 |
} |
| 1046 |
(num.is_empty() && total > 0).then_some(total) |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
fn parse_timeout(ctx: &mut Ctx, c: &KdlNode) -> Option<u64> { |
| 1051 |
let t = ctx.one_string(c)?; |
| 1052 |
match parse_duration(&t) { |
| 1053 |
Some(secs) if (60..=MAX_TIMEOUT_S).contains(&secs) => Some(secs), |
| 1054 |
Some(_) => { |
| 1055 |
ctx.err(c.span(), format!("timeout {t:?} must be from 1m to 24h")); |
| 1056 |
None |
| 1057 |
} |
| 1058 |
None => { |
| 1059 |
ctx.err(c.span(), format!("timeout {t:?}: expected hours, minutes and seconds, as \"90m\" or \"1h30m\"")); |
| 1060 |
None |
| 1061 |
} |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| e576e086 Andy McCall ↶ |
1065 |
|
| 1066 |
|
| 1067 |
fn parse_labels(ctx: &mut Ctx, c: &KdlNode, into: &mut Vec<String>) { |
| 1068 |
let labels = ctx.strings(c); |
| 1069 |
if labels.is_empty() { |
| 1070 |
ctx.err(c.span(), "`runs-on` needs one or more labels, as runs-on \"riscv64\""); |
| a72abeee Andy McCall ↶ |
1071 |
} |
| e576e086 Andy McCall ↶ |
1072 |
for l in labels { |
| 1073 |
let ok = !l.is_empty() |
| 1074 |
&& l.len() <= 64 |
| 1075 |
&& l.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'-' | b'_' | b'.')); |
| 1076 |
if ok { |
| 1077 |
into.push(l); |
| 1078 |
} else { |
| 1079 |
ctx.err(c.span(), format!("runs-on {l:?}: a label is lower-case letters, digits, -, _ and ., at most 64")); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
into.sort(); |
| 1083 |
into.dedup(); |
| a72abeee Andy McCall ↶ |
1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
fn parse_env(ctx: &mut Ctx, c: &KdlNode, env: &mut BTreeMap<String, String>) { |
| 1088 |
if !c.entries().is_empty() { |
| 1089 |
ctx.err(c.span(), "`env` takes a block of NAME \"value\" lines"); |
| 1090 |
} |
| 1091 |
for e in block(ctx, c) { |
| 1092 |
let k = e.name().value().to_string(); |
| 1093 |
let valid = |
| 1094 |
!k.is_empty() && !k.starts_with(|c: char| c.is_ascii_digit()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'); |
| 1095 |
if !valid { |
| 1096 |
ctx.err(e.span(), format!("environment name {k:?} must be letters, digits and _")); |
| 1097 |
continue; |
| 1098 |
} |
| 1099 |
ctx.no_children(e); |
| 1100 |
if let Some(v) = ctx.one_string(e) { |
| 1101 |
if env.insert(k.clone(), v).is_some() { |
| 1102 |
ctx.err(e.span(), format!("environment {k} is set twice")); |
| 1103 |
} |
| 1104 |
} |
| e0918b88 Andy McCall |
1105 |
} |
| 1106 |
} |
| 1107 |
|
| fc10d9e8 Andy McCall ↶ |
1108 |
|
| 1109 |
fn parse_mount(ctx: &mut Ctx, node: &KdlNode) -> Option<Mount> { |
| 1110 |
let (mut source, mut target, mut size) = (None, None, None); |
| d60aac6c Andy McCall ↶ |
1111 |
let (mut cache, mut keys) = (None, Vec::new()); |
| fc10d9e8 Andy McCall ↶ |
1112 |
let mut seen = BTreeSet::new(); |
| 1113 |
for c in block(ctx, node) { |
| 1114 |
let key = c.name().value(); |
| 1115 |
if !seen.insert(key.to_string()) { |
| 1116 |
ctx.err(c.span(), format!("`{key}` is given twice")); |
| 1117 |
continue; |
| 1118 |
} |
| 1119 |
ctx.no_children(c); |
| 1120 |
match key { |
| 1121 |
"source" => { |
| 1122 |
source = match ctx.one_string(c).as_deref() { |
| 1123 |
Some("ephemeral://tmpfs") => Some(MountSource::Tmpfs), |
| 1124 |
Some("ephemeral://disk") => Some(MountSource::Disk), |
| 1125 |
Some(other) => { |
| 1126 |
ctx.err( |
| 1127 |
c.span(), |
| 1128 |
format!("mount source {other:?} is not in M0 yet; M0 mounts \"ephemeral://tmpfs\" or \"ephemeral://disk\""), |
| 1129 |
); |
| 1130 |
None |
| 1131 |
} |
| 1132 |
None => None, |
| 1133 |
} |
| 1134 |
} |
| 1135 |
"target" => { |
| 1136 |
target = match ctx.one_string(c) { |
| 1137 |
Some(t) if mount_target_ok(&t) => Some(t), |
| 1138 |
Some(t) => { |
| 1139 |
ctx.err( |
| 1140 |
c.span(), |
| 1141 |
format!("mount target {t:?} must be an absolute path under /src or /tmp, without . or .. or a trailing /"), |
| 1142 |
); |
| 1143 |
None |
| 1144 |
} |
| 1145 |
None => None, |
| 1146 |
} |
| 1147 |
} |
| 1148 |
"size" => { |
| 1149 |
size = match ctx.one_string(c) { |
| 1150 |
Some(m) => match parse_memory(&m) { |
| 1151 |
Some(mib) if (MIN_MOUNT_MIB..=MAX_MOUNT_MIB).contains(&mib) => Some(mib as u32), |
| 1152 |
Some(_) => { |
| 1153 |
ctx.err(c.span(), format!("mount size {m:?} must be from 16MiB to 64GiB")); |
| 1154 |
None |
| 1155 |
} |
| 1156 |
None => { |
| 1157 |
ctx.err(c.span(), format!("mount size {m:?}: expected a whole number of KiB, MiB or GiB, as \"8GiB\"")); |
| 1158 |
None |
| 1159 |
} |
| 1160 |
}, |
| 1161 |
None => None, |
| 1162 |
} |
| 1163 |
} |
| d60aac6c Andy McCall ↶ |
1164 |
"cache" => { |
| 1165 |
cache = match ctx.one_string(c) { |
| 1166 |
Some(n) if !n.is_empty() && n.len() <= 64 && n.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) => { |
| 1167 |
Some(n) |
| 1168 |
} |
| 1169 |
Some(n) => { |
| 1170 |
ctx.err(c.span(), format!("cache name {n:?}: letters, digits, '.', '_' and '-', at most 64")); |
| 1171 |
None |
| 1172 |
} |
| 1173 |
None => None, |
| 1174 |
} |
| 1175 |
} |
| 1176 |
"key" => { |
| 1177 |
keys = ctx.strings(c); |
| 1178 |
if keys.is_empty() { |
| 1179 |
ctx.err(c.span(), "`key` names one or more files, as \"Cargo.lock\""); |
| 1180 |
} |
| 1181 |
for k in &keys { |
| 1182 |
if k.starts_with('/') || k.split('/').any(|p| p.is_empty() || p == "." || p == "..") { |
| 1183 |
ctx.err(c.span(), format!("key file {k:?} must be a path in the repository, without . or ..")); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
} |
| fc10d9e8 Andy McCall ↶ |
1187 |
"readonly" | "include" => ctx.err(c.span(), format!("mount `{key}` is not in M0 yet")), |
| 1188 |
other => ctx.err(c.span(), format!("unknown mount setting `{other}`")), |
| 1189 |
} |
| 1190 |
} |
| 1191 |
for (name, missing) in [("source", source.is_none()), ("target", target.is_none()), ("size", size.is_none())] { |
| 1192 |
if missing && !seen.contains(name) { |
| 1193 |
ctx.err(node.span(), format!("a mount needs a {name}")); |
| 1194 |
} |
| d60aac6c Andy McCall ↶ |
1195 |
} |
| 1196 |
if source == Some(MountSource::Tmpfs) && cache.is_some() { |
| 1197 |
ctx.err(node.span(), "only an ephemeral://disk mount can be a cache"); |
| 1198 |
} |
| 1199 |
if !keys.is_empty() && cache.is_none() { |
| 1200 |
ctx.err(node.span(), "`key` goes with `cache`"); |
| fc10d9e8 Andy McCall ↶ |
1201 |
} |
| d60aac6c Andy McCall ↶ |
1202 |
Some(Mount { source: source?, target: target?, size_mib: size?, cache, key: keys }) |
| fc10d9e8 Andy McCall ↶ |
1203 |
} |
| 1204 |
|
| a67b97e9 Andy McCall ↶ |
1205 |
|
| e2c12561 Andy McCall ↶ |
1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
fn parse_secret(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1210 |
let Some(name) = ctx.one_string(node) else { |
| 1211 |
ctx.err(node.span(), "`secret` takes a name and a block with mount-path"); |
| 1212 |
return; |
| 1213 |
}; |
| 1214 |
if !ctx.name_ok(node.span(), "secret", &name) { |
| 1215 |
return; |
| 1216 |
} |
| 1217 |
let mut path = None; |
| 1218 |
for c in block(ctx, node) { |
| 1219 |
ctx.no_children(c); |
| 1220 |
match c.name().value() { |
| 1221 |
"mount-path" => match ctx.one_string(c) { |
| 1222 |
Some(p) if secret_path_ok(&p) => path = Some(p), |
| 1223 |
Some(p) => { |
| 1224 |
ctx.err(c.span(), format!("secret mount-path {p:?} must be an absolute path under /run/secrets/, without . or ..")) |
| 1225 |
} |
| 1226 |
None => {} |
| 1227 |
}, |
| 1228 |
"vault-ref" => ctx.err(c.span(), "`vault-ref` is not in M0; the hub supplies the secret of that name"), |
| 1229 |
other => ctx.err(c.span(), format!("unknown secret setting `{other}`")), |
| 1230 |
} |
| 1231 |
} |
| 1232 |
let Some(mount_path) = path else { |
| 1233 |
ctx.err(node.span(), format!("secret {name:?} needs a mount-path")); |
| 1234 |
return; |
| 1235 |
}; |
| 1236 |
if job.secrets.iter().any(|s| s.name == name) { |
| 1237 |
ctx.err(node.span(), format!("secret {name:?} is given twice")); |
| 720eb819 Andy McCall ↶ |
1238 |
} else if job.secrets.iter().any(|s| s.mount_path == mount_path) || job.identities.iter().any(|i| i.mount_path == mount_path) { |
| 1239 |
ctx.err(node.span(), format!("two files mount at {mount_path:?}")); |
| e2c12561 Andy McCall ↶ |
1240 |
} else if job.secrets.len() == MAX_SECRETS { |
| 1241 |
ctx.err(node.span(), format!("a job has at most {MAX_SECRETS} secrets")); |
| 1242 |
} else { |
| 1243 |
job.secrets.push(Secret { name, mount_path }); |
| 720eb819 Andy McCall ↶ |
1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
fn parse_identity(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1251 |
let Some(audience) = ctx.one_string(node) else { |
| 1252 |
ctx.err(node.span(), "`identity` takes an audience and a block with mount-path"); |
| 1253 |
return; |
| 1254 |
}; |
| 1255 |
let aud_ok = !audience.is_empty() && audience.len() <= 255 && !audience.chars().any(|c| c.is_whitespace() || c.is_control()); |
| 1256 |
if !aud_ok { |
| 1257 |
ctx.err(node.span(), format!("identity audience {audience:?} must be 1 to 255 characters without spaces")); |
| 1258 |
return; |
| 1259 |
} |
| 1260 |
let mut path = None; |
| 1261 |
for c in block(ctx, node) { |
| 1262 |
ctx.no_children(c); |
| 1263 |
match c.name().value() { |
| 1264 |
"mount-path" => match ctx.one_string(c) { |
| 1265 |
Some(p) if secret_path_ok(&p) => path = Some(p), |
| 1266 |
Some(p) => { |
| 1267 |
ctx.err(c.span(), format!("identity mount-path {p:?} must be an absolute path under /run/secrets/, without . or ..")) |
| 1268 |
} |
| 1269 |
None => {} |
| 1270 |
}, |
| 1271 |
other => ctx.err(c.span(), format!("unknown identity setting `{other}`")), |
| 1272 |
} |
| 1273 |
} |
| 1274 |
let Some(mount_path) = path else { |
| 1275 |
ctx.err(node.span(), format!("identity {audience:?} needs a mount-path")); |
| 1276 |
return; |
| 1277 |
}; |
| 1278 |
if job.identities.iter().any(|i| i.audience == audience) { |
| 1279 |
ctx.err(node.span(), format!("identity {audience:?} is given twice")); |
| 1280 |
} else if job.identities.iter().any(|i| i.mount_path == mount_path) || job.secrets.iter().any(|s| s.mount_path == mount_path) { |
| 1281 |
ctx.err(node.span(), format!("two files mount at {mount_path:?}")); |
| 1282 |
} else if job.identities.len() == MAX_IDENTITIES { |
| 1283 |
ctx.err(node.span(), format!("a job has at most {MAX_IDENTITIES} identities")); |
| 1284 |
} else { |
| 1285 |
job.identities.push(Identity { audience, mount_path }); |
| e2c12561 Andy McCall ↶ |
1286 |
} |
| 1287 |
} |
| 1288 |
|
| a67b97e9 Andy McCall ↶ |
1289 |
fn parse_output(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1290 |
if !node.entries().is_empty() { |
| 6d53fb7d Andy McCall ↶ |
1291 |
ctx.err(node.span(), "`output` takes a block of artifacts and reports"); |
| a67b97e9 Andy McCall ↶ |
1292 |
} |
| 1293 |
for a in block(ctx, node) { |
| 6d53fb7d Andy McCall ↶ |
1294 |
let kind = match a.name().value() { |
| 1295 |
"artifact" => OutputKind::Artifact, |
| 1296 |
"report" => OutputKind::Report, |
| 1297 |
other => { |
| 1298 |
ctx.err( |
| 1299 |
a.span(), |
| 1300 |
format!("unknown output `{other}`; outputs are `artifact \"name\" {{ path \"/src/file\" }}` and `report \"name\" {{ path \"/src/dir\" }}`"), |
| 1301 |
); |
| 1302 |
continue; |
| 1303 |
} |
| 1304 |
}; |
| 1305 |
let word = kind.word(); |
| a67b97e9 Andy McCall ↶ |
1306 |
let Some(name) = ctx.one_string(a) else { continue }; |
| 6d53fb7d Andy McCall ↶ |
1307 |
if !ctx.name_ok(a.span(), word, &name) { |
| a67b97e9 Andy McCall ↶ |
1308 |
continue; |
| 1309 |
} |
| 1310 |
let mut path = None; |
| 9a6fac2b Andy McCall ↶ |
1311 |
let mut always = false; |
| a67b97e9 Andy McCall ↶ |
1312 |
for c in block(ctx, a) { |
| 1313 |
ctx.no_children(c); |
| 1314 |
match c.name().value() { |
| 1315 |
"path" => match ctx.one_string(c) { |
| 1316 |
Some(p) if output_path_ok(&p) => path = Some(p), |
| 6d53fb7d Andy McCall ↶ |
1317 |
Some(p) => ctx |
| 1318 |
.err(c.span(), format!("{word} path {p:?} must be an absolute path under /src, without . or .., quotes or spaces")), |
| a67b97e9 Andy McCall ↶ |
1319 |
None => {} |
| 1320 |
}, |
| 6d53fb7d Andy McCall ↶ |
1321 |
"format" if kind == OutputKind::Artifact => { |
| a67b97e9 Andy McCall ↶ |
1322 |
if ctx.one_string(c).as_deref() != Some("blob") { |
| 1323 |
ctx.err(c.span(), "artifact format is \"blob\""); |
| 1324 |
} |
| 1325 |
} |
| 9a6fac2b Andy McCall ↶ |
1326 |
"when" => match ctx.one_string(c).as_deref() { |
| 1327 |
Some("passed") => always = false, |
| 1328 |
Some("always") => always = true, |
| 6d53fb7d Andy McCall ↶ |
1329 |
Some(_) => ctx.err(c.span(), format!("{word} `when` is \"passed\" (the default) or \"always\"")), |
| 9a6fac2b Andy McCall ↶ |
1330 |
None => {} |
| 1331 |
}, |
| 6d53fb7d Andy McCall ↶ |
1332 |
other => ctx.err(c.span(), format!("unknown {word} setting `{other}`")), |
| a67b97e9 Andy McCall ↶ |
1333 |
} |
| 1334 |
} |
| 1335 |
let Some(path) = path else { |
| 6d53fb7d Andy McCall ↶ |
1336 |
ctx.err(a.span(), format!("{word} {name:?} needs a path")); |
| a67b97e9 Andy McCall ↶ |
1337 |
continue; |
| 1338 |
}; |
| 1339 |
if job.outputs.iter().any(|o| o.name == name) { |
| 6d53fb7d Andy McCall ↶ |
1340 |
ctx.err(a.span(), format!("output {name:?} is given twice (artifacts and reports share names)")); |
| a67b97e9 Andy McCall ↶ |
1341 |
} else if job.outputs.len() == MAX_OUTPUTS { |
| 6d53fb7d Andy McCall ↶ |
1342 |
ctx.err(a.span(), format!("a job has at most {MAX_OUTPUTS} artifacts and reports")); |
| a67b97e9 Andy McCall ↶ |
1343 |
} else { |
| 6d53fb7d Andy McCall ↶ |
1344 |
job.outputs.push(Output { name, path, always, kind }); |
| e2089721 Andy McCall ↶ |
1345 |
} |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 2be1fe4a Andy McCall ↶ |
1351 |
|
| 1352 |
|
| 1353 |
fn parse_approval(ctx: &mut Ctx, node: &KdlNode) -> Option<(Approval, bool)> { |
| e2089721 Andy McCall ↶ |
1354 |
if !node.entries().is_empty() { |
| 1355 |
ctx.err(node.span(), "`approval` takes a block: approval { by \"maintain\"; others-only #true; expires \"24h\" }"); |
| 1356 |
} |
| 2be1fe4a Andy McCall ↶ |
1357 |
let mut a = Approval { by: "maintain".into(), others_only: false, expires_s: 24 * 3600, sealed: false }; |
| 1358 |
let mut sealed_said = false; |
| 1359 |
let Some(children) = node.children() else { return Some((a, false)) }; |
| e2089721 Andy McCall ↶ |
1360 |
for c in children.nodes() { |
| 1361 |
ctx.no_children(c); |
| 1362 |
match c.name().value() { |
| 1363 |
"by" => match ctx.one_string(c).as_deref() { |
| 1364 |
Some(r @ ("write" | "maintain" | "admin")) => a.by = r.to_string(), |
| 1365 |
Some(r) => ctx.err(c.span(), format!("approval by {r:?}: expected \"write\", \"maintain\" or \"admin\"")), |
| 1366 |
None => {} |
| 1367 |
}, |
| 1368 |
"others-only" => match c.entries() { |
| 1369 |
[e] if e.name().is_none() => match e.value() { |
| 1370 |
KdlValue::Bool(b) => a.others_only = *b, |
| 1371 |
_ => ctx.err(c.span(), "`others-only` takes #true or #false"), |
| 1372 |
}, |
| 1373 |
_ => ctx.err(c.span(), "`others-only` takes #true or #false"), |
| 2be1fe4a Andy McCall ↶ |
1374 |
}, |
| 1375 |
"sealed" => match c.entries() { |
| 1376 |
[e] if e.name().is_none() => match e.value() { |
| 1377 |
KdlValue::Bool(b) => { |
| 1378 |
a.sealed = *b; |
| 1379 |
sealed_said = true; |
| 1380 |
} |
| 1381 |
_ => ctx.err(c.span(), "`sealed` takes #true or #false"), |
| 1382 |
}, |
| 1383 |
_ => ctx.err(c.span(), "`sealed` takes #true or #false"), |
| e2089721 Andy McCall ↶ |
1384 |
}, |
| 1385 |
"expires" => { |
| 1386 |
let Some(t) = ctx.one_string(c) else { continue }; |
| 1387 |
match parse_duration(&t) { |
| 1388 |
Some(secs) if (60..=MAX_APPROVAL_S).contains(&secs) => a.expires_s = secs, |
| 1389 |
Some(_) => ctx.err(c.span(), format!("expires {t:?} must be from 1m to 720h")), |
| 1390 |
None => ctx.err(c.span(), format!("expires {t:?}: expected hours, minutes and seconds, as \"24h\"")), |
| 1391 |
} |
| 2be1fe4a Andy McCall ↶ |
1392 |
} |
| 1393 |
other => { |
| 1394 |
ctx.err(c.span(), format!("unknown approval setting `{other}`; approval takes `by`, `others-only`, `expires` and `sealed`")) |
| e2089721 Andy McCall ↶ |
1395 |
} |
| 46fe2c81 Andy McCall ↶ |
1396 |
} |
| 1397 |
} |
| 2be1fe4a Andy McCall ↶ |
1398 |
Some((a, sealed_said)) |
| 46fe2c81 Andy McCall ↶ |
1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
fn parse_input(ctx: &mut Ctx, node: &KdlNode, job: &mut Job) { |
| 1403 |
if !node.entries().is_empty() { |
| 1404 |
ctx.err(node.span(), "`input` takes a block: input { artifact \"tfplan\" from \"plan\"; path \"/src/tfplan\" }"); |
| 1405 |
} |
| 1406 |
let (mut artifact, mut from, mut path) = (None, None, None); |
| 1407 |
for c in block(ctx, node) { |
| 1408 |
ctx.no_children(c); |
| 1409 |
match c.name().value() { |
| 1410 |
"artifact" => match ctx.strings(c).as_slice() { |
| 1411 |
[name, word, producer] if word == "from" => { |
| 1412 |
if ctx.name_ok(c.span(), "artifact", name) && ctx.name_ok(c.span(), "job", producer) { |
| 1413 |
artifact = Some(name.clone()); |
| 1414 |
from = Some(producer.clone()); |
| 1415 |
} |
| 1416 |
} |
| 1417 |
_ => ctx.err(c.span(), "`artifact` names the artifact and its job: artifact \"tfplan\" from \"plan\""), |
| 1418 |
}, |
| 1419 |
"path" => match ctx.one_string(c) { |
| 1420 |
Some(p) if output_path_ok(&p) => path = Some(p), |
| 1421 |
Some(p) => { |
| 1422 |
ctx.err(c.span(), format!("input path {p:?} must be an absolute path under /src, without . or .., quotes or spaces")) |
| 1423 |
} |
| 1424 |
None => {} |
| 1425 |
}, |
| 1426 |
other => ctx.err(c.span(), format!("unknown input setting `{other}`; an input has `artifact` and `path`")), |
| 1427 |
} |
| 1428 |
} |
| 1429 |
match (artifact, from, path) { |
| 1430 |
(Some(artifact), Some(from), Some(path)) => { |
| 1431 |
if job.inputs.iter().any(|i| i.path == path) { |
| 1432 |
ctx.err(node.span(), format!("two inputs are put at {path:?}")); |
| 1433 |
} else if job.inputs.len() == MAX_INPUTS { |
| 1434 |
ctx.err(node.span(), format!("a job takes at most {MAX_INPUTS} inputs")); |
| 1435 |
} else { |
| 1436 |
job.inputs.push(Input { artifact, from, path }); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
(a, _, p) => { |
| 1440 |
if a.is_some() && p.is_none() { |
| 1441 |
ctx.err(node.span(), "an input needs a `path`"); |
| 1442 |
} else if a.is_none() && p.is_some() { |
| 1443 |
ctx.err(node.span(), "an input needs `artifact \"NAME\" from \"JOB\"`"); |
| 1444 |
} |
| a67b97e9 Andy McCall ↶ |
1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| e0918b88 Andy McCall |
1449 |
fn parse_job(ctx: &mut Ctx, node: &KdlNode) -> Option<Job> { |
| 1450 |
let name = ctx.one_string(node)?; |
| 1451 |
let named = ctx.name_ok(node.span(), "job", &name); |
| 2be1fe4a Andy McCall ↶ |
1452 |
let mut approval: Option<(Approval, bool)> = None; |
| fc10d9e8 Andy McCall ↶ |
1453 |
let mut job = Job { |
| 1454 |
name: name.clone(), |
| 1455 |
sandbox: String::new(), |
| 1456 |
scope: vec![], |
| 1457 |
depends_on: vec![], |
| 1458 |
env: BTreeMap::new(), |
| 1459 |
exec: vec![], |
| 1460 |
mounts: vec![], |
| e2c12561 Andy McCall ↶ |
1461 |
secrets: vec![], |
| 720eb819 Andy McCall ↶ |
1462 |
identities: vec![], |
| a67b97e9 Andy McCall ↶ |
1463 |
outputs: vec![], |
| 46fe2c81 Andy McCall ↶ |
1464 |
inputs: vec![], |
| e2089721 Andy McCall ↶ |
1465 |
approval: None, |
| f4e51e63 Andy McCall ↶ |
1466 |
environment: None, |
| e576e086 Andy McCall ↶ |
1467 |
runs_on: vec![], |
| 02b127d2 Andy McCall ↶ |
1468 |
timeout_s: None, |
| bf7bb1b8 Andy McCall ↶ |
1469 |
on: None, |
| fc10d9e8 Andy McCall ↶ |
1470 |
}; |
| e0918b88 Andy McCall |
1471 |
let mut seen = BTreeSet::new(); |
| a67b97e9 Andy McCall ↶ |
1472 |
let mut outputs_at = None; |
| e0918b88 Andy McCall |
1473 |
for c in block(ctx, node) { |
| 1474 |
let key = c.name().value(); |
| 720eb819 Andy McCall ↶ |
1475 |
if key != "scope" |
| 1476 |
&& key != "depends-on" |
| 1477 |
&& key != "mount" |
| 1478 |
&& key != "secret" |
| 1479 |
&& key != "identity" |
| 1480 |
&& key != "runs-on" |
| 1481 |
&& !seen.insert(key.to_string()) |
| 1482 |
{ |
| e0918b88 Andy McCall |
1483 |
ctx.err(c.span(), format!("`{key}` is given twice")); |
| 1484 |
} |
| 1485 |
match key { |
| 1486 |
"sandbox" => { |
| 1487 |
ctx.no_children(c); |
| 1488 |
job.sandbox = ctx.one_string(c).unwrap_or_default(); |
| 1489 |
} |
| 1490 |
"scope" => { |
| 1491 |
ctx.no_children(c); |
| 1492 |
for g in ctx.strings(c) { |
| 1493 |
match globset::Glob::new(&g) { |
| 1494 |
Ok(_) => job.scope.push(g), |
| 1495 |
Err(e) => ctx.err(c.span(), format!("scope {g:?}: {e}")), |
| 1496 |
} |
| 1497 |
} |
| 1498 |
} |
| 1499 |
"depends-on" => { |
| 1500 |
ctx.no_children(c); |
| 1501 |
job.depends_on.extend(ctx.strings(c)); |
| e576e086 Andy McCall ↶ |
1502 |
} |
| 1503 |
"runs-on" => { |
| 1504 |
ctx.no_children(c); |
| 1505 |
parse_labels(ctx, c, &mut job.runs_on); |
| 02b127d2 Andy McCall ↶ |
1506 |
} |
| 1507 |
"timeout" => { |
| 1508 |
ctx.no_children(c); |
| 1509 |
job.timeout_s = parse_timeout(ctx, c); |
| e0918b88 Andy McCall |
1510 |
} |
| bf7bb1b8 Andy McCall ↶ |
1511 |
"on" => job.on = Some(parse_on(ctx, c)), |
| a72abeee Andy McCall ↶ |
1512 |
"env" => parse_env(ctx, c, &mut job.env), |
| e0918b88 Andy McCall |
1513 |
"exec" => { |
| 1514 |
ctx.no_children(c); |
| 1515 |
job.exec = ctx.strings(c); |
| 1516 |
if job.exec.is_empty() { |
| 1517 |
ctx.err(c.span(), "`exec` needs a program and its arguments"); |
| 1518 |
} else if job.exec.iter().any(|a| a.contains('\0')) { |
| 1519 |
ctx.err(c.span(), "`exec` arguments cannot contain NUL"); |
| 1520 |
} |
| 1521 |
} |
| fc10d9e8 Andy McCall ↶ |
1522 |
"mount" => { |
| 1523 |
if !c.entries().is_empty() { |
| 1524 |
ctx.err(c.span(), "`mount` takes a block of source, target and size"); |
| 1525 |
} |
| 1526 |
if let Some(m) = parse_mount(ctx, c) { |
| 1527 |
if job.mounts.len() == MAX_MOUNTS { |
| 1528 |
ctx.err(c.span(), format!("a job has at most {MAX_MOUNTS} mounts")); |
| 1529 |
} else if let Some(other) = job.mounts.iter().find(|o| { |
| 1530 |
o.target == m.target |
| 1531 |
|| m.target.starts_with(&format!("{}/", o.target)) |
| 1532 |
|| o.target.starts_with(&format!("{}/", m.target)) |
| 1533 |
}) { |
| 1534 |
ctx.err(c.span(), format!("mount {:?} overlaps mount {:?}", m.target, other.target)); |
| 1535 |
} else { |
| 1536 |
job.mounts.push(m); |
| 1537 |
} |
| 1538 |
} |
| 1539 |
} |
| a67b97e9 Andy McCall ↶ |
1540 |
"output" => { |
| 1541 |
outputs_at = Some(c.span()); |
| 1542 |
parse_output(ctx, c, &mut job); |
| 1543 |
} |
| 46fe2c81 Andy McCall ↶ |
1544 |
"input" => parse_input(ctx, c, &mut job), |
| 2be1fe4a Andy McCall ↶ |
1545 |
"approval" => approval = parse_approval(ctx, c), |
| f4e51e63 Andy McCall ↶ |
1546 |
"environment" => { |
| 1547 |
ctx.no_children(c); |
| 1548 |
if let Some(e) = ctx.one_string(c) { |
| 1549 |
let ok = !e.is_empty() && e.len() <= 32 && e.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'); |
| 1550 |
if ok { |
| 1551 |
job.environment = Some(e); |
| 1552 |
} else { |
| 1553 |
ctx.err(c.span(), format!("environment {e:?}: 1 to 32 of a-z 0-9 -, as \"prod\"")); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
} |
| e2c12561 Andy McCall ↶ |
1557 |
"secret" => parse_secret(ctx, c, &mut job), |
| 720eb819 Andy McCall ↶ |
1558 |
"identity" => parse_identity(ctx, c, &mut job), |
| e0918b88 Andy McCall |
1559 |
n if LATER_JOB.contains(&n) => ctx.err(c.span(), format!("`{n}` is not in M0 yet")), |
| 1560 |
other => ctx.err(c.span(), format!("unknown job setting `{other}`")), |
| 1561 |
} |
| 1562 |
} |
| 2be1fe4a Andy McCall ↶ |
1563 |
|
| 1564 |
job.approval = approval.map(|(mut a, said)| { |
| 1565 |
if !said { |
| 1566 |
a.sealed = job.environment.is_some(); |
| 1567 |
} |
| 1568 |
a |
| 1569 |
}); |
| fc10d9e8 Andy McCall ↶ |
1570 |
job.mounts.sort_by(|a, b| a.target.cmp(&b.target)); |
| a67b97e9 Andy McCall ↶ |
1571 |
job.outputs.sort_by(|a, b| a.name.cmp(&b.name)); |
| 46fe2c81 Andy McCall ↶ |
1572 |
job.inputs.sort_by(|a, b| a.path.cmp(&b.path)); |
| e2c12561 Andy McCall ↶ |
1573 |
job.secrets.sort_by(|a, b| a.name.cmp(&b.name)); |
| 720eb819 Andy McCall ↶ |
1574 |
job.identities.sort_by(|a, b| a.audience.cmp(&b.audience)); |
| a67b97e9 Andy McCall ↶ |
1575 |
|
| 1576 |
for o in &job.outputs { |
| 1577 |
if let Some(m) = job.mounts.iter().find(|m| m.source == MountSource::Tmpfs && o.path.starts_with(&format!("{}/", m.target))) { |
| 1578 |
ctx.err( |
| 1579 |
outputs_at.unwrap_or(node.span()), |
| 1580 |
format!( |
| 1581 |
"artifact {:?} is on the tmpfs mount {:?}, which is gone when the job ends; use ephemeral://disk", |
| 1582 |
o.name, m.target |
| 1583 |
), |
| 1584 |
); |
| 1585 |
} |
| 1586 |
} |
| e0918b88 Andy McCall |
1587 |
if job.sandbox.is_empty() && !seen.contains("sandbox") { |
| 1588 |
ctx.err(node.span(), format!("job {name:?} needs a sandbox")); |
| 1589 |
} |
| 1590 |
if job.exec.is_empty() && !seen.contains("exec") { |
| 1591 |
ctx.err(node.span(), format!("job {name:?} needs an exec")); |
| 1592 |
} |
| 1593 |
job.depends_on.sort(); |
| 1594 |
job.depends_on.dedup(); |
| 1595 |
|
| 1596 |
|
| 1597 |
named.then_some(job) |
| 1598 |
} |