| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use flow::{facts, home, images, microvm, runner, syscall, wait, wasm, workspace}; |
| 5 | |
|
| 6 | | use anyhow::{bail, Context, Result}; |
| 7 | | use clap::{Parser, Subcommand}; |
| 8 | | use flow_core::api::Record; |
| 9 | | use flow_core::{Pipeline, Runtime, PIPELINE_PATH}; |
| 10 | | use home::Home; |
| 11 | | use std::collections::BTreeSet; |
| 12 | | use std::path::{Path, PathBuf}; |
| 13 | | use std::time::Duration; |
| 14 | |
|
| 15 | | #[derive(Parser)] |
| 16 | | #[command(name = "flow", version = env!("OXY_VERSION_LONG"), about = "OxygenFlow: hermetic pipelines for Oxygen repositories")] |
| 17 | | struct Cli { |
| 18 | | |
| 19 | | #[arg(long, global = true)] |
| 20 | | home: Option<PathBuf>, |
| 21 | | #[command(subcommand)] |
| 22 | | cmd: Cmd, |
| 23 | | } |
| 24 | |
|
| 25 | | #[derive(Subcommand)] |
| 26 | | enum RunnerCmd { |
| 27 | | |
| 28 | | Register { |
| 29 | | |
| 30 | | hub: String, |
| 31 | | |
| 32 | | #[arg(long)] |
| 33 | | token: Option<String>, |
| 34 | | |
| 35 | | #[arg(long)] |
| 36 | | name: Option<String>, |
| 37 | | |
| 38 | | #[arg(long)] |
| 39 | | ca: Option<PathBuf>, |
| 40 | | |
| 41 | | #[arg(long)] |
| 42 | | label: Vec<String>, |
| 43 | | }, |
| 44 | | |
| 45 | | |
| 46 | | Bench, |
| 47 | | } |
| 48 | |
|
| 49 | | #[derive(Subcommand)] |
| 50 | | enum Cmd { |
| 51 | | |
| 52 | | Check { |
| 53 | | |
| 54 | | file: Option<PathBuf>, |
| 55 | | }, |
| 56 | | |
| 57 | | Hash { file: PathBuf }, |
| 58 | | |
| 59 | | Rootfs { |
| 60 | | #[command(subcommand)] |
| 61 | | cmd: RootfsCmd, |
| 62 | | }, |
| 63 | | |
| 64 | | Run { |
| 65 | | |
| 66 | | jobs: Vec<String>, |
| 67 | | |
| 68 | | #[arg(long, default_value = ".")] |
| 69 | | dir: PathBuf, |
| 70 | | |
| 71 | | #[arg(long, default_value_t = 60)] |
| 72 | | timeout: u64, |
| 73 | | |
| 74 | | |
| 75 | | #[arg(long = "var", value_name = "NAME=VALUE")] |
| 76 | | vars: Vec<String>, |
| 77 | | |
| 78 | | |
| 79 | | #[arg(long = "secret", value_name = "NAME=VALUE")] |
| 80 | | secrets: Vec<String>, |
| 81 | | }, |
| 82 | | |
| 83 | | |
| 84 | | Runner { |
| 85 | | #[command(subcommand)] |
| 86 | | cmd: Option<RunnerCmd>, |
| 87 | | }, |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | Wait { |
| 92 | | |
| 93 | | url: String, |
| 94 | | |
| 95 | | state: String, |
| 96 | | |
| 97 | | #[arg(long, default_value_t = 60)] |
| 98 | | timeout: u64, |
| 99 | | }, |
| 100 | | |
| 101 | | |
| 102 | | Tiers, |
| 103 | | |
| 104 | | #[command(name = "__sandbox", hide = true)] |
| 105 | | Sandbox { spec: PathBuf }, |
| 106 | | |
| 107 | | #[command(name = "__sandbox-probe", hide = true)] |
| 108 | | SandboxProbe, |
| 109 | | |
| 110 | | #[command(name = "__wasm", hide = true)] |
| 111 | | Wasm { spec: PathBuf }, |
| 112 | | } |
| 113 | |
|
| 114 | | #[derive(Subcommand)] |
| 115 | | enum RootfsCmd { |
| 116 | | |
| 117 | | Import { |
| 118 | | tarball: PathBuf, |
| 119 | | |
| 120 | | #[arg(long)] |
| 121 | | sha256: Option<String>, |
| 122 | | |
| 123 | | |
| 124 | | |
| 125 | | #[arg(long = "env", value_name = "NAME=VALUE")] |
| 126 | | env: Vec<String>, |
| 127 | | }, |
| 128 | | |
| 129 | | List, |
| 130 | | |
| 131 | | |
| 132 | | |
| 133 | | Push { |
| 134 | | |
| 135 | | hash: String, |
| 136 | | |
| 137 | | #[arg(long)] |
| 138 | | hub: Option<String>, |
| 139 | | }, |
| 140 | | |
| 141 | | |
| 142 | | Pull { |
| 143 | | hash: String, |
| 144 | | #[arg(long)] |
| 145 | | hub: Option<String>, |
| 146 | | }, |
| 147 | | } |
| 148 | |
|
| 149 | 5× | fn main() { |
| 150 | 5× | let cli = Cli::parse(); |
| 151 | 5× | if let Err(e) = run(cli) { |
| 152 | 2× | eprintln!("flow: {e:#}"); |
| 153 | 2× | std::process::exit(1); |
| 154 | 3× | } |
| 155 | 3× | } |
| 156 | |
|
| 157 | 5× | fn run(cli: Cli) -> Result<()> { |
| 158 | 0× | match cli.cmd { |
| 159 | 0× | Cmd::Sandbox { spec } => std::process::exit(syscall::sandbox_main(&spec)), |
| 160 | 0× | Cmd::SandboxProbe => std::process::exit(syscall::probe_main()), |
| 161 | 0× | Cmd::Wasm { spec } => std::process::exit(wasm::wasm_main(&spec)), |
| 162 | 0× | Cmd::Check { file } => check(&file.unwrap_or_else(|| PathBuf::from(PIPELINE_PATH))), |
| 163 | 0× | Cmd::Hash { file } => { |
| 164 | 0× | println!("{}", home::hash_file(&file)?); |
| 165 | 0× | Ok(()) |
| 166 | | } |
| 167 | 0× | Cmd::Rootfs { cmd } => { |
| 168 | 0× | let home = Home::locate(cli.home.as_deref())?; |
| 169 | 0× | match cmd { |
| 170 | 0× | RootfsCmd::Import { tarball, sha256, env } => rootfs_import(&home, &tarball, sha256.as_deref(), &env), |
| 171 | 0× | RootfsCmd::Push { hash, hub } => { |
| 172 | 0× | let client = image_client(&home, hub)?; |
| 173 | 0× | eprintln!("Sending image {} …", &hash[..12.min(hash.len())]); |
| 174 | 0× | let m = client.push(&home, &hash)?; |
| 175 | 0× | println!( |
| 176 | | "Sent image {}: {} chunks the hub lacked, {} in {:.1} s", |
| 177 | 0× | &hash[..12], |
| 178 | | m.chunks, |
| 179 | 0× | images::human(m.bytes), |
| 180 | 0× | m.elapsed.as_secs_f64() |
| 181 | | ); |
| 182 | 0× | Ok(()) |
| 183 | | } |
| 184 | 0× | RootfsCmd::Pull { hash, hub } => { |
| 185 | 0× | let client = image_client(&home, hub)?; |
| 186 | 0× | match client.fetch(&home, &hash)? { |
| 187 | 0× | Some(m) => { |
| 188 | 0× | println!( |
| 189 | | "Fetched image {}: {} chunks, {} in {:.1} s", |
| 190 | 0× | &hash[..12], |
| 191 | | m.chunks, |
| 192 | 0× | images::human(m.bytes), |
| 193 | 0× | m.elapsed.as_secs_f64() |
| 194 | | ); |
| 195 | 0× | Ok(()) |
| 196 | | } |
| 197 | 0× | None => anyhow::bail!("the hub does not hold image {}; send it with flow rootfs push", &hash[..12.min(hash.len())]), |
| 198 | | } |
| 199 | | } |
| 200 | | RootfsCmd::List => { |
| 201 | 0× | for e in std::fs::read_dir(home.root.join("cas"))?.flatten() { |
| 202 | 0× | let size = e.metadata().map(|m| m.len()).unwrap_or(0); |
| 203 | 0× | if let Some(h) = e.file_name().to_string_lossy().strip_suffix(".ext4") { |
| 204 | 0× | println!("cas://blob/{h} {} MiB", size >> 20); |
| 205 | 0× | } |
| 206 | | } |
| 207 | 0× | Ok(()) |
| 208 | | } |
| 209 | | } |
| 210 | | } |
| 211 | 2× | Cmd::Run { jobs, dir, timeout, vars, secrets } => { |
| 212 | 2× | let home = Home::locate(cli.home.as_deref())?; |
| 213 | 4× | let pairs = |what: &str, list: &[String]| { |
| 214 | 4× | list.iter() |
| 215 | 4× | .map(|e| { |
| 216 | 0× | e.split_once('=') |
| 217 | 0× | .map(|(k, v)| (k.to_string(), v.to_string())) |
| 218 | 0× | .ok_or_else(|| anyhow::anyhow!("--{what} {e:?}: expected NAME=VALUE")) |
| 219 | 0× | }) |
| 220 | 4× | .collect::<Result<Vec<_>>>() |
| 221 | 4× | }; |
| 222 | 2× | let (vars, secrets) = (pairs("var", &vars)?, pairs("secret", &secrets)?); |
| 223 | 2× | run_local(&home, &dir, &jobs, Duration::from_secs(timeout * 60), &vars, &secrets) |
| 224 | | } |
| 225 | | Cmd::Runner { cmd: Some(RunnerCmd::Bench) } => { |
| 226 | 0× | let home = Home::locate(cli.home.as_deref())?; |
| 227 | 0× | let (speed, ms) = facts::bench(); |
| 228 | 0× | let f = facts::now(&home, Some(speed)); |
| 229 | 0× | println!("speed {speed} (class {}), benchmark {ms} ms", flow_core::api::Facts::class_of(speed)); |
| 230 | 0× | println!("free disk {} MiB at {}", f.free_disk_mib.unwrap_or(0), home.root.display()); |
| 231 | 0× | println!("load {:.2}", f.load_x100.unwrap_or(0) as f64 / 100.0); |
| 232 | 0× | if let Some(k) = f.kernel { |
| 233 | 0× | println!("kernel {k}"); |
| 234 | 0× | } |
| 235 | 0× | Ok(()) |
| 236 | | } |
| 237 | 0× | Cmd::Runner { cmd: Some(RunnerCmd::Register { hub, token, name, ca, label }) } => { |
| 238 | 0× | let home = Home::locate(cli.home.as_deref())?; |
| 239 | 0× | if std::env::var_os("OXY_HOME").is_none() { |
| 240 | 0× | std::env::set_var("OXY_HOME", home.root.join("oxy")); |
| 241 | 0× | } |
| 242 | 0× | let name = name.unwrap_or_else(|| { |
| 243 | 0× | std::fs::read_to_string("/etc/hostname").map(|h| h.trim().to_string()).unwrap_or_else(|_| "flow".into()) |
| 244 | 0× | }); |
| 245 | 0× | let token = match token { |
| 246 | 0× | Some(t) => t, |
| 247 | | None => { |
| 248 | 0× | eprintln!("Paste the registration token from the hub's Admin → Runners, then press Enter:"); |
| 249 | 0× | let mut line = String::new(); |
| 250 | 0× | std::io::stdin().read_line(&mut line)?; |
| 251 | 0× | line.trim().to_string() |
| 252 | | } |
| 253 | | }; |
| 254 | 0× | let got = runner::register(&home, &hub, &token, &name, ca, label)?; |
| 255 | 0× | println!("registered as {} (runner {}); start it with `flow runner`", got.name, got.id); |
| 256 | 0× | Ok(()) |
| 257 | | } |
| 258 | | Cmd::Runner { cmd: None } => { |
| 259 | 0× | let home = Home::locate(cli.home.as_deref())?; |
| 260 | | |
| 261 | 0× | if std::env::var_os("OXY_HOME").is_none() { |
| 262 | 0× | std::env::set_var("OXY_HOME", home.root.join("oxy")); |
| 263 | 0× | } |
| 264 | 0× | runner::serve(&home) |
| 265 | | } |
| 266 | | Cmd::Tiers => { |
| 267 | 3× | let home = Home::locate(cli.home.as_deref())?; |
| 268 | 3× | match runner::tiers(&home) { |
| 269 | 3× | Ok((have, missing)) => { |
| 270 | 3× | for t in have { |
| 271 | 3× | println!("{t}: available"); |
| 272 | 3× | } |
| 273 | 6× | for why in missing { |
| 274 | 6× | println!("{}", why.replacen(": ", ": unavailable: ", 1)); |
| 275 | 6× | } |
| 276 | | } |
| 277 | 0× | Err(why) => println!("{why}"), |
| 278 | | } |
| 279 | 3× | Ok(()) |
| 280 | | } |
| 281 | 0× | Cmd::Wait { url, state, timeout } => { |
| 282 | 0× | let token = std::env::var("OXY_TOKEN").ok(); |
| 283 | 0× | let ca = std::env::var_os("OXY_CA").map(PathBuf::from); |
| 284 | 0× | let code = |
| 285 | 0× | wait::wait(&url, &state, token.as_deref(), ca.as_deref(), Duration::from_secs(timeout * 60), Duration::from_secs(15))?; |
| 286 | 0× | std::process::exit(code); |
| 287 | | } |
| 288 | | } |
| 289 | 5× | } |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | 0× | fn image_client(home: &Home, hub: Option<String>) -> Result<images::Client> { |
| 296 | 0× | let runner = home.config.runner.clone(); |
| 297 | 0× | let hub = hub |
| 298 | 0× | .or_else(|| std::env::var("OH_HUB").ok().filter(|h| !h.is_empty())) |
| 299 | 0× | .or_else(|| runner.as_ref().map(|r| r.hub.clone())) |
| 300 | 0× | .context("name the hub with --hub https://HUB (or set OH_HUB)")?; |
| 301 | 0× | let (saved_token, saved_ca) = oxy_core::credentials::Credentials::user().lookup(&hub); |
| 302 | 0× | let token = std::env::var("OXY_TOKEN") |
| 303 | 0× | .ok() |
| 304 | 0× | .filter(|t| !t.is_empty()) |
| 305 | 0× | .or(saved_token) |
| 306 | 0× | .or_else(|| runner.as_ref().map(|r| r.token.clone())) |
| 307 | 0× | .with_context(|| format!("no token for {hub}: sign in with `oh auth login --hub {hub}`, or set OXY_TOKEN"))?; |
| 308 | 0× | let ca = std::env::var_os("OXY_CA").map(PathBuf::from).or(saved_ca).or_else(|| runner.as_ref().and_then(|r| r.ca.clone())); |
| 309 | 0× | images::Client::new(&hub, &token, ca.as_deref()) |
| 310 | 0× | } |
| 311 | |
|
| 312 | 2× | fn load(file: &Path) -> Result<Pipeline> { |
| 313 | 2× | let src = std::fs::read_to_string(file).with_context(|| format!("read {}", file.display()))?; |
| 314 | 2× | Pipeline::parse(&src).map_err(|p| anyhow::anyhow!("{}", not_valid(file, &p.to_string()))) |
| 315 | 2× | } |
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | 2× | fn not_valid(file: &Path, problems: &str) -> String { |
| 321 | 2× | if problems.contains("unknown ") { |
| 322 | 1× | format!( |
| 323 | | "{} uses something this flow ({}) does not know:\n{problems}\n\ |
| 324 | | If the pipeline was written for a newer OxygenFlow, update flow (build it from oxygenflow main); \ |
| 325 | | otherwise fix the lines named.", |
| 326 | 1× | file.display(), |
| 327 | | env!("OXY_VERSION_LONG") |
| 328 | | ) |
| 329 | | } else { |
| 330 | 1× | format!("{} is not valid:\n{problems}", file.display()) |
| 331 | | } |
| 332 | 2× | } |
| 333 | |
|
| 334 | 0× | fn check(file: &Path) -> Result<()> { |
| 335 | 0× | let p = load(file)?; |
| 336 | 0× | let depths = p.depths(); |
| 337 | 0× | for j in p.order() { |
| 338 | 0× | let sb = &p.sandboxes[&j.sandbox]; |
| 339 | 0× | let after = if j.depends_on.is_empty() { String::new() } else { format!(" after {}", j.depends_on.join(", ")) }; |
| 340 | 0× | println!("{}{} {} [{}]{after}", " ".repeat(depths[j.name.as_str()]), j.name, j.exec.join(" "), sb.runtime.as_str()); |
| 341 | | } |
| 342 | 0× | println!("pipeline {}", hex::encode(p.hash())); |
| 343 | 0× | Ok(()) |
| 344 | 0× | } |
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | 5× | fn image_env_file(env: &[String]) -> Result<String> { |
| 349 | 5× | let mut out = String::new(); |
| 350 | 6× | for e in env { |
| 351 | 6× | let Some((k, v)) = e.split_once('=') else { bail!("--env {e:?}: expected NAME=value") }; |
| 352 | 16× | let ok = !k.is_empty() && !k.starts_with(|c: char| c.is_ascii_digit()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_'); |
| 353 | 5× | if !ok || v.contains('\n') || v.contains('\0') { |
| 354 | 2× | bail!("--env {e:?}: a name is letters, digits and _, and a value is one line"); |
| 355 | 3× | } |
| 356 | 3× | out.push_str(&format!("{k}={v}\n")); |
| 357 | | } |
| 358 | 2× | Ok(out) |
| 359 | 5× | } |
| 360 | |
|
| 361 | |
|
| 362 | 0× | fn elf_arch(path: &Path) -> Option<&'static str> { |
| 363 | 0× | let mut head = [0u8; 20]; |
| 364 | 0× | std::io::Read::read_exact(&mut std::fs::File::open(path).ok()?, &mut head).ok()?; |
| 365 | 0× | jet::rootfs::elf_arch(&head) |
| 366 | 0× | } |
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | 0× | fn tree_arch(tree: &Path) -> Option<&'static str> { |
| 371 | 0× | for rel in ["bin/sh", "bin/busybox", "usr/bin/env", "bin/bash", "usr/bin/sh"] { |
| 372 | 0× | let p = tree.join(rel); |
| 373 | 0× | let p = std::fs::canonicalize(&p).unwrap_or(p); |
| 374 | 0× | if let Some(a) = elf_arch(&p) { |
| 375 | 0× | return Some(a); |
| 376 | 0× | } |
| 377 | | } |
| 378 | 0× | None |
| 379 | 0× | } |
| 380 | |
|
| 381 | 0× | fn rootfs_import(home: &Home, tarball: &Path, sha256: Option<&str>, env: &[String]) -> Result<()> { |
| 382 | 0× | let env_file = image_env_file(env)?; |
| 383 | 0× | if let Some(want) = sha256 { |
| 384 | 0× | let out = std::process::Command::new("sha256sum").arg(tarball).output().context("run sha256sum")?; |
| 385 | 0× | let got = String::from_utf8_lossy(&out.stdout).split_whitespace().next().unwrap_or("").to_string(); |
| 386 | 0× | if got != want.to_lowercase() { |
| 387 | 0× | bail!("{} has SHA-256 {got}, expected {want}", tarball.display()); |
| 388 | 0× | } |
| 389 | 0× | } |
| 390 | 0× | let tmp = home.root.join("work").join(format!("rootfs-{}", std::process::id())); |
| 391 | 0× | let _ = std::fs::remove_dir_all(&tmp); |
| 392 | 0× | let tree = tmp.join("tree"); |
| 393 | 0× | std::fs::create_dir_all(&tree)?; |
| 394 | 0× | let result = (|| -> Result<String> { |
| 395 | 0× | let status = std::process::Command::new("tar").arg("-xf").arg(tarball).arg("-C").arg(&tree).arg("--no-same-owner").status()?; |
| 396 | 0× | if !status.success() { |
| 397 | 0× | bail!("tar could not unpack {}", tarball.display()); |
| 398 | 0× | } |
| 399 | | |
| 400 | | |
| 401 | 0× | let arch = tree_arch(&tree).unwrap_or("unknown"); |
| 402 | 0× | if arch != "unknown" && arch != std::env::consts::ARCH { |
| 403 | 0× | bail!( |
| 404 | | "{} is a {arch} image; this runner is {}, and jobs here run no other architecture", |
| 405 | 0× | tarball.display(), |
| 406 | | std::env::consts::ARCH |
| 407 | | ); |
| 408 | 0× | } |
| 409 | | |
| 410 | 0× | for d in ["src", "tmp", "proc", "dev", "run"] { |
| 411 | 0× | std::fs::create_dir_all(tree.join(d))?; |
| 412 | | } |
| 413 | 0× | if !env_file.is_empty() { |
| 414 | 0× | std::fs::create_dir_all(tree.join("etc/flow"))?; |
| 415 | 0× | std::fs::write(tree.join("etc/flow/env"), &env_file)?; |
| 416 | 0× | } |
| 417 | 0× | let used: u64 = ignore::WalkBuilder::new(&tree) |
| 418 | 0× | .hidden(false) |
| 419 | 0× | .ignore(false) |
| 420 | 0× | .git_ignore(false) |
| 421 | 0× | .build() |
| 422 | 0× | .flatten() |
| 423 | 0× | .filter_map(|e| e.metadata().ok()) |
| 424 | 0× | .map(|m| m.len().max(4096)) |
| 425 | 0× | .sum(); |
| 426 | 0× | let mib = used * 13 / 10 / (1 << 20) + 64; |
| 427 | 0× | let img = tmp.join("rootfs.ext4"); |
| 428 | 0× | let status = std::process::Command::new("mkfs.ext4") |
| 429 | 0× | .args(["-q", "-F", "-L", "rootfs", "-E", "root_owner=0:0", "-d"]) |
| 430 | 0× | .arg(&tree) |
| 431 | 0× | .arg(&img) |
| 432 | 0× | .arg(format!("{mib}M")) |
| 433 | 0× | .status() |
| 434 | 0× | .context("run mkfs.ext4")?; |
| 435 | 0× | if !status.success() { |
| 436 | 0× | bail!("mkfs.ext4 failed"); |
| 437 | 0× | } |
| 438 | 0× | let hash = home::hash_file(&img)?; |
| 439 | 0× | std::fs::rename(&img, home.rootfs(&hash))?; |
| 440 | 0× | let manifest = serde_json::json!({ "arch": if arch == "unknown" { std::env::consts::ARCH } else { arch }, "imported_ms": std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0) }); |
| 441 | 0× | std::fs::write(home.image_manifest(&hash), manifest.to_string())?; |
| 442 | 0× | Ok(hash) |
| 443 | | })(); |
| 444 | 0× | let _ = std::fs::remove_dir_all(&tmp); |
| 445 | 0× | let hash = result?; |
| 446 | 0× | println!("rootfs \"cas://blob/{hash}\""); |
| 447 | 0× | Ok(()) |
| 448 | 0× | } |
| 449 | |
|
| 450 | 2× | fn run_local( |
| 451 | 2× | home: &Home, |
| 452 | 2× | dir: &Path, |
| 453 | 2× | only: &[String], |
| 454 | 2× | timeout: Duration, |
| 455 | 2× | vars: &[(String, String)], |
| 456 | 2× | secrets: &[(String, String)], |
| 457 | 2× | ) -> Result<()> { |
| 458 | 2× | let p = load(&dir.join(PIPELINE_PATH))?; |
| 459 | | |
| 460 | 2× | let mut wanted: BTreeSet<String> = only.iter().cloned().collect(); |
| 461 | 2× | for name in only { |
| 462 | 0× | if !p.jobs.contains_key(name) { |
| 463 | 0× | bail!("no job {name:?} in the pipeline"); |
| 464 | 0× | } |
| 465 | | } |
| 466 | 2× | if !only.is_empty() { |
| 467 | | loop { |
| 468 | 0× | let more: Vec<String> = wanted.iter().flat_map(|j| p.jobs[j].depends_on.clone()).filter(|d| !wanted.contains(d)).collect(); |
| 469 | 0× | if more.is_empty() { |
| 470 | 0× | break; |
| 471 | 0× | } |
| 472 | 0× | wanted.extend(more); |
| 473 | | } |
| 474 | 2× | } |
| 475 | 2× | let scratch = home.root.join("work").join(format!("local-{}", std::process::id())); |
| 476 | 2× | let _ = std::fs::remove_dir_all(&scratch); |
| 477 | 2× | std::fs::create_dir_all(scratch.join("tree"))?; |
| 478 | 2× | workspace::copy_tree(dir, &scratch.join("tree"))?; |
| 479 | 2× | let mut failed: BTreeSet<String> = BTreeSet::new(); |
| 480 | 2× | let mut worst = 0; |
| 481 | 12× | for job in p.order() { |
| 482 | 12× | if !only.is_empty() && !wanted.contains(&job.name) { |
| 483 | 0× | continue; |
| 484 | 12× | } |
| 485 | 12× | if let Some(d) = job.depends_on.iter().find(|d| failed.contains(*d)) { |
| 486 | 0× | println!("── {} skipped: {d} failed", job.name); |
| 487 | 0× | failed.insert(job.name.clone()); |
| 488 | 0× | continue; |
| 489 | 12× | } |
| 490 | 12× | let sb = &p.sandboxes[&job.sandbox]; |
| 491 | 12× | println!("── {} ({})", job.name, job.exec.join(" ")); |
| 492 | 12× | let js = scratch.join(&job.name); |
| 493 | 12× | std::fs::create_dir_all(&js)?; |
| 494 | 12× | let job = &job |
| 495 | 12× | .in_sandbox(sb) |
| 496 | 12× | .with_flow_env(&[("FLOW_JOB".into(), job.name.clone()), ("FLOW_RUN".into(), "local".into())]) |
| 497 | 12× | .with_flow_env(vars); |
| 498 | | |
| 499 | 12× | for i in &job.identities { |
| 500 | 0× | println!("── {}: identity {:?} is not available locally; {} will be missing", job.name, i.audience, i.mount_path); |
| 501 | 0× | } |
| 502 | | |
| 503 | 12× | let inputs = job |
| 504 | 12× | .secrets |
| 505 | 12× | .iter() |
| 506 | 12× | .map(|s| { |
| 507 | 0× | let value = secrets.iter().find(|(k, _)| *k == s.name).map(|(_, v)| v.clone()); |
| 508 | 0× | value |
| 509 | 0× | .map(|value| flow_core::api::SecretInput { name: s.name.clone(), path: s.mount_path.clone(), value }) |
| 510 | 0× | .ok_or_else(|| anyhow::anyhow!("job {}: secret {:?} is not given; pass --secret {}=VALUE", job.name, s.name, s.name)) |
| 511 | 0× | }) |
| 512 | 12× | .collect::<Result<Vec<_>>>()?; |
| 513 | | |
| 514 | 12× | let out = if sb.runtime == Runtime::Wasm { |
| 515 | 12× | workspace::copy_tree(&scratch.join("tree"), &js.join("tree"))?; |
| 516 | 17× | wasm::run(sb, job, &js.join("tree"), &js, timeout, &mut |r: Record| print_record(&r))? |
| 517 | 0× | } else if sb.runtime == Runtime::Syscall { |
| 518 | 0× | workspace::copy_tree(&scratch.join("tree"), &js.join("tree"))?; |
| 519 | 0× | let group = format!("local-{}-{}", std::process::id(), js.file_name().unwrap_or_default().to_string_lossy()); |
| 520 | 0× | syscall::run(home, sb, job, &inputs, &js.join("tree"), &js, timeout, &mut |r: Record| print_record(&r), &group)? |
| 521 | | } else { |
| 522 | 0× | workspace::image(&scratch.join("tree"), &js.join("workspace.ext4"))?; |
| 523 | 0× | microvm::run(home, sb, job, &inputs, &js.join("workspace.ext4"), &js, timeout, &mut |r: Record| print_record(&r))? |
| 524 | | }; |
| 525 | 12× | let _ = workspace::remove_tree(&js); |
| 526 | 12× | match out.exit_code { |
| 527 | 8× | Some(0) => println!("── {} passed in {} ms", job.name, out.duration_ms), |
| 528 | 4× | other => { |
| 529 | 4× | println!( |
| 530 | | "── {} failed ({}) in {} ms", |
| 531 | | job.name, |
| 532 | 4× | other.map(|c| format!("exit {c}")).unwrap_or_else(|| "no exit code".into()), |
| 533 | | out.duration_ms |
| 534 | | ); |
| 535 | 4× | failed.insert(job.name.clone()); |
| 536 | 4× | worst = 1; |
| 537 | | } |
| 538 | | } |
| 539 | | } |
| 540 | 2× | let _ = workspace::remove_tree(&scratch); |
| 541 | 2× | if worst != 0 { |
| 542 | 2× | std::process::exit(worst); |
| 543 | 0× | } |
| 544 | 0× | Ok(()) |
| 545 | 0× | } |
| 546 | |
|
| 547 | 17× | fn print_record(r: &Record) { |
| 548 | 17× | match r.stream.as_str() { |
| 549 | 17× | "out" => println!(" {}", r.text), |
| 550 | 13× | "err" => eprintln!(" {}", r.text), |
| 551 | 10× | s => eprintln!(" [{s}] {}", r.text), |
| 552 | | } |
| 553 | 17× | } |
| 554 | |
|
| 555 | | #[cfg(test)] |
| 556 | | mod tests { |
| 557 | | #[test] |
| 558 | 1× | fn a_pipeline_newer_than_flow_is_named_as_such() { |
| 559 | 1× | let m = super::not_valid(std::path::Path::new(".flow/pipeline.kdl"), "line 92: unknown output `sbom`; outputs are …"); |
| 560 | 1× | assert!(m.contains("does not know") && m.contains(env!("OXY_VERSION_LONG")) && m.contains("newer OxygenFlow"), "{m}"); |
| 561 | 1× | let m = super::not_valid(std::path::Path::new(".flow/pipeline.kdl"), "line 3: a job needs a sandbox"); |
| 562 | 1× | assert!(m.contains("is not valid") && !m.contains("newer"), "{m}"); |
| 563 | 1× | } |
| 564 | |
|
| 565 | | use super::image_env_file; |
| 566 | |
|
| 567 | | #[test] |
| 568 | 1× | fn image_environments_are_checked_lines() { |
| 569 | 1× | let f = image_env_file(&["PATH=/opt/bin:/usr/bin".into(), "X16EMU=/opt/x16emu".into(), "EMPTY=".into()]).unwrap(); |
| 570 | 1× | assert_eq!(f, "PATH=/opt/bin:/usr/bin\nX16EMU=/opt/x16emu\nEMPTY=\n"); |
| 571 | 1× | assert!(image_env_file(&["NOEQUALS".into()]).is_err()); |
| 572 | 1× | assert!(image_env_file(&["1X=y".into()]).is_err()); |
| 573 | 1× | assert!(image_env_file(&["X=two\nlines".into()]).is_err()); |
| 574 | 1× | assert_eq!(image_env_file(&[]).unwrap(), ""); |
| 575 | 1× | } |
| 576 | | } |