| e0918b88 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use crate::pipeline::{Job, Pipeline, Sandbox}; |
| 7 |
use oxy_core::cbor::{encode, Value}; |
| 8 |
|
| 9 |
|
| 10 |
pub type Hash = [u8; 32]; |
| 11 |
|
| 12 |
|
| 13 |
pub fn object_hash(ty: &str, body: &[u8]) -> Hash { |
| 14 |
let mut h = blake3::Hasher::new(); |
| 15 |
h.update(format!("oxy1 {ty} {}\0", body.len()).as_bytes()); |
| 16 |
h.update(body); |
| 17 |
*h.finalize().as_bytes() |
| 18 |
} |
| 19 |
|
| 20 |
fn text(s: &str) -> Value { |
| 21 |
Value::Text(s.to_string()) |
| 22 |
} |
| 23 |
|
| 24 |
fn texts(v: &[String]) -> Value { |
| 25 |
Value::Array(v.iter().map(|s| text(s)).collect()) |
| 26 |
} |
| 27 |
|
| 28 |
fn hex32(s: &str) -> Value { |
| 29 |
Value::Bytes(hex::decode(s).unwrap_or_default()) |
| 30 |
} |
| 31 |
|
| 32 |
pub fn sandbox_value(s: &Sandbox) -> Value { |
| a72abeee Andy McCall ↶ |
33 |
let mut fields = vec![ |
| e0918b88 Andy McCall |
34 |
(0, text(&s.name)), |
| 35 |
(1, text(s.runtime.as_str())), |
| 36 |
(2, hex32(&s.rootfs)), |
| 37 |
(3, Value::Uint(s.memory_mib as u64)), |
| 38 |
(4, Value::Uint(s.cpus as u64)), |
| 39 |
(5, text(s.network.as_str())), |
| a72abeee Andy McCall ↶ |
40 |
]; |
| 41 |
|
| 42 |
|
| 43 |
if !s.env.is_empty() { |
| 44 |
fields.push((6, Value::Array(s.env.iter().map(|(k, v)| Value::Array(vec![text(k), text(v)])).collect()))); |
| 45 |
} |
| 46 |
Value::map(fields) |
| e0918b88 Andy McCall |
47 |
} |
| 48 |
|
| 49 |
pub fn job_value(j: &Job) -> Value { |
| fc10d9e8 Andy McCall ↶ |
50 |
let mut fields = vec![ |
| e0918b88 Andy McCall |
51 |
(0, text(&j.name)), |
| 52 |
(1, text(&j.sandbox)), |
| 53 |
(2, texts(&j.scope)), |
| 54 |
(3, texts(&j.depends_on)), |
| 55 |
|
| 56 |
(4, Value::Array(j.env.iter().map(|(k, v)| Value::Array(vec![text(k), text(v)])).collect())), |
| 57 |
(5, texts(&j.exec)), |
| fc10d9e8 Andy McCall ↶ |
58 |
]; |
| 59 |
|
| 60 |
|
| 61 |
if !j.mounts.is_empty() { |
| 62 |
fields.push(( |
| 63 |
6, |
| 64 |
Value::Array( |
| 65 |
j.mounts |
| 66 |
.iter() |
| 67 |
.map(|m| Value::Array(vec![text(m.source.as_str()), text(&m.target), Value::Uint(m.size_mib as u64)])) |
| 68 |
.collect(), |
| 69 |
), |
| 70 |
)); |
| 71 |
} |
| a67b97e9 Andy McCall ↶ |
72 |
|
| 73 |
if !j.outputs.is_empty() { |
| 74 |
fields.push((7, Value::Array(j.outputs.iter().map(|o| Value::Array(vec![text(&o.name), text(&o.path)])).collect()))); |
| 75 |
} |
| fc10d9e8 Andy McCall ↶ |
76 |
Value::map(fields) |
| e0918b88 Andy McCall |
77 |
} |
| 78 |
|
| 79 |
impl Pipeline { |
| 80 |
|
| 81 |
pub fn canonical(&self) -> Vec<u8> { |
| 82 |
encode(&Value::map([ |
| 83 |
(0, Value::Uint(self.schema_version as u64)), |
| 84 |
(1, Value::Array(self.sandboxes.values().map(sandbox_value).collect())), |
| 85 |
(2, Value::Array(self.jobs.values().map(job_value).collect())), |
| 86 |
])) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
pub fn hash(&self) -> Hash { |
| 91 |
object_hash("pipeline", &self.canonical()) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
impl Job { |
| 96 |
|
| 97 |
|
| 98 |
pub fn hash(&self, sandbox: &Sandbox) -> Hash { |
| 99 |
object_hash("job", &encode(&Value::Array(vec![job_value(self), sandbox_value(sandbox)]))) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub fn scope_tree_hash<'a>(scope: &[String], files: impl IntoIterator<Item = (&'a str, Hash)>) -> Hash { |
| 106 |
let set = { |
| 107 |
let mut b = globset::GlobSetBuilder::new(); |
| 108 |
for g in scope { |
| 109 |
if let Ok(g) = globset::Glob::new(g) { |
| 110 |
b.add(g); |
| 111 |
} |
| 112 |
} |
| 113 |
b.build().unwrap_or_else(|_| globset::GlobSet::empty()) |
| 114 |
}; |
| 115 |
let mut picked: Vec<(&str, Hash)> = files.into_iter().filter(|(p, _)| scope.is_empty() || set.is_match(p)).collect(); |
| 116 |
picked.sort(); |
| 117 |
let body = encode(&Value::Array(picked.iter().map(|(p, h)| Value::Array(vec![text(p), Value::Bytes(h.to_vec())])).collect())); |
| 118 |
object_hash("scope", &body) |
| 119 |
} |