| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use crate::domain::{AppId, Target, Version}; |
| 22 |
use crate::engine::RecipeCtx; |
| 23 |
use crate::state::AppState; |
| 24 |
use chrono::{DateTime, Utc}; |
| 25 |
use ops_artifact::{ArtifactRecord, GateRecord, Manifest, Provenance, Scope, Verdict}; |
| 26 |
use ops_exec::{Action, DiscardSink, Step as OpStep}; |
| 27 |
use std::path::PathBuf; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub fn record_path( |
| 35 |
dist_root: &std::path::Path, |
| 36 |
app: &AppId, |
| 37 |
version: &Version, |
| 38 |
target: Target, |
| 39 |
) -> std::path::PathBuf { |
| 40 |
crate::archive::target_dir(dist_root, app, version, target).join(RECORD_FILE) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
pub const RECORD_FILE: &str = "record.json"; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
pub async fn emit(state: &AppState, ctx: &RecipeCtx, pinned_sha: &str) -> Option<PathBuf> { |
| 54 |
if pinned_sha.is_empty() { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
tracing::debug!(app = %ctx.app, target = %ctx.target, "release pinning off, no record written"); |
| 60 |
return None; |
| 61 |
} |
| 62 |
|
| 63 |
let hashes = ctx.artifact_hashes(); |
| 64 |
if hashes.is_empty() { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
tracing::debug!(app = %ctx.app, target = %ctx.target, "no artifacts collected, no record written"); |
| 69 |
return None; |
| 70 |
} |
| 71 |
|
| 72 |
let manifest = match Manifest::new(hashes) { |
| 73 |
Ok(m) => m, |
| 74 |
Err(e) => { |
| 75 |
tracing::error!(app = %ctx.app, target = %ctx.target, error = %e, "could not build artifact manifest"); |
| 76 |
return None; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
let provenance = Provenance { |
| 81 |
app: ctx.app.to_string(), |
| 82 |
version: ctx.version.to_string(), |
| 83 |
tag: ctx.tag.clone(), |
| 84 |
git_sha: pinned_sha.to_string(), |
| 85 |
target: ctx.target.to_string(), |
| 86 |
build_host: ctx.build_host.clone(), |
| 87 |
toolchain: toolchain_of(state, &ctx.build_host).await, |
| 88 |
built_at: Utc::now(), |
| 89 |
}; |
| 90 |
|
| 91 |
let gates = gates_for(state, ctx.target_run_id).await; |
| 92 |
|
| 93 |
let record = match ArtifactRecord::new("bento", manifest, provenance, gates) { |
| 94 |
Ok(r) => r, |
| 95 |
Err(e) => { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
tracing::error!(app = %ctx.app, target = %ctx.target, error = %e, "assembled an invalid artifact record"); |
| 100 |
return None; |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
let path = record_path(&state.cfg.dist_root, &ctx.app, &ctx.version, ctx.target); |
| 105 |
if let Err(e) = tokio::fs::write(&path, record.to_json()).await { |
| 106 |
tracing::error!(path = %path.display(), error = %e, "could not write artifact record"); |
| 107 |
return None; |
| 108 |
} |
| 109 |
tracing::info!( |
| 110 |
app = %ctx.app, target = %ctx.target, digest = %record.digest.short(), |
| 111 |
"wrote artifact record" |
| 112 |
); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
if let Some(dir) = path.parent() |
| 120 |
&& let Err(e) = |
| 121 |
crate::archive::deposit(&state.cfg, dir, &ctx.app, &ctx.version, ctx.target).await |
| 122 |
{ |
| 123 |
tracing::error!(app = %ctx.app, target = %ctx.target, error = %e, "could not archive the artifact record"); |
| 124 |
} |
| 125 |
|
| 126 |
Some(path) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
async fn toolchain_of(state: &AppState, host: &str) -> String { |
| 138 |
const UNKNOWN: &str = "unknown"; |
| 139 |
let Some(exec) = state.executors.get(host) else { |
| 140 |
return UNKNOWN.to_string(); |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
let step = OpStep::shell( |
| 145 |
Action::Build, |
| 146 |
"bash -lc 'rustc --version' 2>/dev/null".to_string(), |
| 147 |
); |
| 148 |
let mut sink = DiscardSink; |
| 149 |
match exec.run_streaming(&step, &mut sink).await { |
| 150 |
Ok(out) if out.status.success() => { |
| 151 |
let v = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 152 |
if v.is_empty() { UNKNOWN.to_string() } else { v } |
| 153 |
} |
| 154 |
_ => UNKNOWN.to_string(), |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
async fn gates_for(state: &AppState, target_run_id: i64) -> Vec<GateRecord> { |
| 165 |
let rows: Vec<(String, String, Option<String>, String, Option<String>)> = sqlx::query_as( |
| 166 |
"SELECT step, status, log_ref, started_at, finished_at |
| 167 |
FROM step_runs WHERE target_run_id = ? ORDER BY id", |
| 168 |
) |
| 169 |
.bind(target_run_id) |
| 170 |
.fetch_all(&state.pool) |
| 171 |
.await |
| 172 |
.unwrap_or_default(); |
| 173 |
|
| 174 |
rows.into_iter() |
| 175 |
.map(|(step, status, log_ref, started_at, finished_at)| { |
| 176 |
let ran_at = parse_ts(&started_at); |
| 177 |
let (verdict, summary) = |
| 178 |
verdict_of(&step, &status, &started_at, finished_at.as_deref()); |
| 179 |
let mut g = GateRecord::new(step, Scope::Artifact, verdict, summary, ran_at); |
| 180 |
if let Some(r) = log_ref { |
| 181 |
g = g.with_log_ref(r); |
| 182 |
} |
| 183 |
g |
| 184 |
}) |
| 185 |
.collect() |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
fn verdict_of( |
| 195 |
step: &str, |
| 196 |
status: &str, |
| 197 |
started_at: &str, |
| 198 |
finished_at: Option<&str>, |
| 199 |
) -> (Verdict, String) { |
| 200 |
let secs = duration_secs(started_at, finished_at); |
| 201 |
match status { |
| 202 |
"ok" => ( |
| 203 |
Verdict::Passed, |
| 204 |
match secs { |
| 205 |
Some(s) => format!("{step} passed in {s}s"), |
| 206 |
None => format!("{step} passed"), |
| 207 |
}, |
| 208 |
), |
| 209 |
"failed" => ( |
| 210 |
Verdict::Failed, |
| 211 |
match secs { |
| 212 |
Some(s) => format!("{step} failed after {s}s"), |
| 213 |
None => format!("{step} failed"), |
| 214 |
}, |
| 215 |
), |
| 216 |
other => ( |
| 217 |
Verdict::Blocked, |
| 218 |
format!("{step} never finished (left `{other}`); the run was superseded or aborted"), |
| 219 |
), |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
fn parse_ts(s: &str) -> DateTime<Utc> { |
| 224 |
DateTime::parse_from_rfc3339(s).map_or_else(|_| Utc::now(), |t| t.with_timezone(&Utc)) |
| 225 |
} |
| 226 |
|
| 227 |
fn duration_secs(started_at: &str, finished_at: Option<&str>) -> Option<i64> { |
| 228 |
let start = DateTime::parse_from_rfc3339(started_at).ok()?; |
| 229 |
let end = DateTime::parse_from_rfc3339(finished_at?).ok()?; |
| 230 |
Some((end - start).num_seconds().max(0)) |
| 231 |
} |
| 232 |
|
| 233 |
#[cfg(test)] |
| 234 |
mod tests { |
| 235 |
use super::*; |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn the_record_sits_in_its_own_targets_directory() { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
let root = std::path::Path::new("/dist"); |
| 243 |
let linux = record_path( |
| 244 |
root, |
| 245 |
&AppId::new("goingson"), |
| 246 |
&"0.4.1".parse().unwrap(), |
| 247 |
"linux/x86_64".parse().unwrap(), |
| 248 |
); |
| 249 |
let macos = record_path( |
| 250 |
root, |
| 251 |
&AppId::new("goingson"), |
| 252 |
&"0.4.1".parse().unwrap(), |
| 253 |
"macos/aarch64".parse().unwrap(), |
| 254 |
); |
| 255 |
assert_ne!(linux, macos); |
| 256 |
assert_eq!( |
| 257 |
linux, |
| 258 |
std::path::Path::new("/dist/goingson/0.4.1/linux-x86_64/record.json") |
| 259 |
); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn a_passing_step_reports_its_duration() { |
| 264 |
let (v, s) = verdict_of( |
| 265 |
"prebuild", |
| 266 |
"ok", |
| 267 |
"2026-08-06T12:00:00Z", |
| 268 |
Some("2026-08-06T12:02:30Z"), |
| 269 |
); |
| 270 |
assert_eq!(v, Verdict::Passed); |
| 271 |
assert_eq!(s, "prebuild passed in 150s"); |
| 272 |
} |
| 273 |
|
| 274 |
#[test] |
| 275 |
fn a_failed_step_is_a_failed_gate() { |
| 276 |
let (v, s) = verdict_of( |
| 277 |
"sign", |
| 278 |
"failed", |
| 279 |
"2026-08-06T12:00:00Z", |
| 280 |
Some("2026-08-06T12:00:04Z"), |
| 281 |
); |
| 282 |
assert_eq!(v, Verdict::Failed); |
| 283 |
assert!(s.contains("failed after 4s"), "{s}"); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn a_superseded_step_is_blocked_rather_than_failed() { |
| 288 |
|
| 289 |
|
| 290 |
let (v, s) = verdict_of("build", "running", "2026-08-06T12:00:00Z", None); |
| 291 |
assert_eq!(v, Verdict::Blocked); |
| 292 |
assert!(s.contains("never finished"), "{s}"); |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn an_unparseable_timestamp_costs_the_duration_and_nothing_else() { |
| 297 |
let (v, s) = verdict_of("build", "ok", "not-a-timestamp", Some("also-not")); |
| 298 |
assert_eq!(v, Verdict::Passed); |
| 299 |
assert_eq!(s, "build passed"); |
| 300 |
} |
| 301 |
} |
| 302 |
|