| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use super::{AppState, Json, PromoteBody, Result}; |
| 7 |
|
| 8 |
pub(super) async fn promote_inner( |
| 9 |
s: AppState, |
| 10 |
tier: String, |
| 11 |
body: PromoteBody, |
| 12 |
) -> Result<Json<serde_json::Value>> { |
| 13 |
|
| 14 |
|
| 15 |
let _deploy_guard = s.deploy_lock.lock().await; |
| 16 |
let tier = crate::domain::TierId::new(tier); |
| 17 |
let idx = s |
| 18 |
.topo |
| 19 |
.tiers |
| 20 |
.iter() |
| 21 |
.position(|t| t.name == tier) |
| 22 |
.ok_or(crate::error::Error::NotFound)?; |
| 23 |
if idx == 0 { |
| 24 |
return Err(crate::error::Error::GateBlocked( |
| 25 |
"cannot /promote to the first tier; use /rebuild".into(), |
| 26 |
)); |
| 27 |
} |
| 28 |
let target = &s.topo.tiers[idx]; |
| 29 |
let source = &s.topo.tiers[idx - 1]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
if !target.provisioned { |
| 36 |
return Err(crate::error::Error::GateBlocked(format!( |
| 37 |
"tier {} is not provisioned (no nodes); promoting to it would record a version it never received", |
| 38 |
target.name, |
| 39 |
))); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
let source_build: Option<(Option<i64>, Option<String>, Option<String>, Option<String>)> = |
| 49 |
sqlx::query_as( |
| 50 |
"SELECT ts.current_build_id, br.version, br.staged_path, br.platform |
| 51 |
FROM tier_state ts |
| 52 |
LEFT JOIN build_runs br ON br.id = ts.current_build_id |
| 53 |
WHERE ts.app = ? AND ts.tier = ?", |
| 54 |
) |
| 55 |
.bind(&s.cfg.id) |
| 56 |
.bind(&source.name) |
| 57 |
.fetch_optional(&s.pool) |
| 58 |
.await |
| 59 |
.map_err(crate::error::Error::Db)?; |
| 60 |
|
| 61 |
let (build_id, version_str, staged_dir, source_platform) = match source_build { |
| 62 |
|
| 63 |
Some((Some(bid), ver, staged_path, platform)) => { |
| 64 |
let version_str = ver.ok_or_else(|| { |
| 65 |
crate::error::Error::Other(anyhow::anyhow!( |
| 66 |
"source build {bid} on tier {} has no recorded version", |
| 67 |
source.name |
| 68 |
)) |
| 69 |
})?; |
| 70 |
let staged_path = staged_path.ok_or_else(|| { |
| 71 |
crate::error::Error::Other(anyhow::anyhow!( |
| 72 |
"source build {bid} ({version_str}) has no staged_path; cannot promote" |
| 73 |
)) |
| 74 |
})?; |
| 75 |
if let Some(req) = &body.version |
| 76 |
&& req != &version_str |
| 77 |
{ |
| 78 |
return Err(crate::error::Error::GateBlocked(format!( |
| 79 |
"tier {} is running build {bid} ({version_str}); refusing to promote an \ |
| 80 |
explicit version {req} that is not the build the tier vouched for", |
| 81 |
source.name |
| 82 |
))); |
| 83 |
} |
| 84 |
let platform = platform |
| 85 |
.as_deref() |
| 86 |
.map(crate::domain::Platform::parse) |
| 87 |
.transpose() |
| 88 |
.map_err(|e| crate::error::Error::Other(anyhow::anyhow!(e)))?; |
| 89 |
( |
| 90 |
Some(bid), |
| 91 |
version_str, |
| 92 |
std::path::PathBuf::from(staged_path), |
| 93 |
platform, |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
_ => { |
| 100 |
let version_str = match body.version.clone() { |
| 101 |
Some(v) => v, |
| 102 |
None => sqlx::query_scalar::<_, Option<String>>( |
| 103 |
"SELECT current_version FROM tier_state WHERE app = ? AND tier = ?", |
| 104 |
) |
| 105 |
.bind(&s.cfg.id) |
| 106 |
.bind(&source.name) |
| 107 |
.fetch_optional(&s.pool) |
| 108 |
.await |
| 109 |
.map_err(crate::error::Error::Db)? |
| 110 |
.flatten() |
| 111 |
.ok_or_else(|| { |
| 112 |
crate::error::Error::GateBlocked(format!( |
| 113 |
"no version specified and tier {} has no current_version", |
| 114 |
source.name |
| 115 |
)) |
| 116 |
})?, |
| 117 |
}; |
| 118 |
let bin: Option<(String,)> = |
| 119 |
sqlx::query_as("SELECT artifact_path FROM versions WHERE app = ? AND version = ?") |
| 120 |
.bind(&s.cfg.id) |
| 121 |
.bind(&version_str) |
| 122 |
.fetch_optional(&s.pool) |
| 123 |
.await |
| 124 |
.map_err(crate::error::Error::Db)?; |
| 125 |
let Some((bin,)) = bin else { |
| 126 |
return Err(crate::error::Error::NotFound); |
| 127 |
}; |
| 128 |
|
| 129 |
let staged_dir = std::path::PathBuf::from(&bin) |
| 130 |
.parent() |
| 131 |
.ok_or_else(|| { |
| 132 |
crate::error::Error::Other(anyhow::anyhow!("artifact_path has no parent")) |
| 133 |
})? |
| 134 |
.to_path_buf(); |
| 135 |
(None, version_str, staged_dir, None) |
| 136 |
} |
| 137 |
}; |
| 138 |
let version = crate::domain::Version::parse(&version_str) |
| 139 |
.map_err(|e| crate::error::Error::Other(anyhow::anyhow!(e)))?; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
let mut effective_gates = source.gates.clone(); |
| 153 |
if body.bears_migration |
| 154 |
&& !effective_gates |
| 155 |
.iter() |
| 156 |
.any(|g| matches!(g, crate::topology::Gate::ManualConfirm)) |
| 157 |
{ |
| 158 |
effective_gates.push(crate::topology::Gate::ManualConfirm); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
let target_nodes: Vec<&crate::topology::Node> = target.nodes.iter().collect(); |
| 167 |
let bundles = bundles_for_nodes( |
| 168 |
&s, |
| 169 |
&version_str, |
| 170 |
&target_nodes, |
| 171 |
&staged_dir, |
| 172 |
source_platform.as_ref(), |
| 173 |
build_id, |
| 174 |
) |
| 175 |
.await?; |
| 176 |
let mut promoted_builds = distinct_builds(&bundles); |
| 177 |
if promoted_builds.is_empty() { |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
promoted_builds.push(PromotedBuild { |
| 186 |
platform: source_platform.clone(), |
| 187 |
build_id, |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
let pending = unsatisfied_gates( |
| 192 |
&s.pool, |
| 193 |
&s.cfg.id, |
| 194 |
&source.name, |
| 195 |
&effective_gates, |
| 196 |
&version_str, |
| 197 |
&promoted_builds, |
| 198 |
body.hotfix, |
| 199 |
) |
| 200 |
.await?; |
| 201 |
if !pending.is_empty() { |
| 202 |
return Err(crate::error::Error::GateBlocked(format!( |
| 203 |
"{} gate(s) not satisfied on tier {}: {}", |
| 204 |
pending.len(), |
| 205 |
source.name, |
| 206 |
pending.join(", "), |
| 207 |
))); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
let prev_version: Option<String> = |
| 213 |
sqlx::query_scalar("SELECT current_version FROM tier_state WHERE app = ? AND tier = ?") |
| 214 |
.bind(&s.cfg.id) |
| 215 |
.bind(&target.name) |
| 216 |
.fetch_optional(&s.pool) |
| 217 |
.await |
| 218 |
.map_err(crate::error::Error::Db)? |
| 219 |
.flatten(); |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
let mut deployed: Vec<&crate::topology::Node> = Vec::new(); |
| 226 |
|
| 227 |
|
| 228 |
for (node, node_bundle, node_bundle_platform, _) in &bundles { |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
let placement = |
| 233 |
crate::deploy::Placement::check(node, node_bundle, node_bundle_platform.as_ref()) |
| 234 |
.map_err(|e| crate::error::Error::GateBlocked(e.to_string()))?; |
| 235 |
let started = chrono::Utc::now().to_rfc3339(); |
| 236 |
crate::events::emit( |
| 237 |
&s.events, |
| 238 |
crate::events::Event::DeployStart { |
| 239 |
tier: target.name.clone(), |
| 240 |
node: node.name.clone(), |
| 241 |
version: version.clone(), |
| 242 |
}, |
| 243 |
); |
| 244 |
let executor = s |
| 245 |
.executors |
| 246 |
.get(&node.name) |
| 247 |
.cloned() |
| 248 |
.unwrap_or_else(|| crate::state::build_executor(node)); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
let deploy_id: i64 = sqlx::query_scalar( |
| 258 |
"INSERT INTO deploys (app, version, tier, node, started_at, outcome, hotfix, reset_burn_in, build_id) |
| 259 |
VALUES (?, ?, ?, ?, ?, 'in_progress', ?, ?, ?) RETURNING id", |
| 260 |
) |
| 261 |
.bind(&s.cfg.id) |
| 262 |
.bind(&version).bind(&target.name).bind(&node.name) |
| 263 |
.bind(&started) |
| 264 |
.bind(body.hotfix as i64).bind(body.reset_burn_in as i64) |
| 265 |
.bind(build_id) |
| 266 |
.fetch_one(&s.pool).await.map_err(crate::error::Error::Db)?; |
| 267 |
let result = crate::deploy::deploy_node( |
| 268 |
executor.as_ref(), |
| 269 |
placement, |
| 270 |
&version_str, |
| 271 |
s.cfg.primary_bin(), |
| 272 |
) |
| 273 |
.await; |
| 274 |
let finished = chrono::Utc::now().to_rfc3339(); |
| 275 |
let (outcome_obj, err_for_propagation) = match result { |
| 276 |
Ok(_) => (crate::outcome::DeployOutcome::ok(), None), |
| 277 |
Err(e) => { |
| 278 |
let msg = format!("{e:#}"); |
| 279 |
let kind = crate::classify::classify_deploy_error(&msg); |
| 280 |
(crate::outcome::DeployOutcome::failed(kind), Some(e)) |
| 281 |
} |
| 282 |
}; |
| 283 |
let outcome_json = serde_json::to_string(&outcome_obj) |
| 284 |
.unwrap_or_else(|e| format!("{{\"_serialize_error\":{e:?}}}")); |
| 285 |
sqlx::query( |
| 286 |
"UPDATE deploys SET finished_at = ?, outcome = ?, outcome_json = ? WHERE id = ?", |
| 287 |
) |
| 288 |
.bind(&finished) |
| 289 |
.bind(outcome_obj.status_str()) |
| 290 |
.bind(&outcome_json) |
| 291 |
.bind(deploy_id) |
| 292 |
.execute(&s.pool) |
| 293 |
.await |
| 294 |
.map_err(crate::error::Error::Db)?; |
| 295 |
if let Some(e) = err_for_propagation { |
| 296 |
let crate::outcome::DeployStatus::Failed { failure } = outcome_obj.status else { |
| 297 |
unreachable!("err_for_propagation is Some iff status is Failed"); |
| 298 |
}; |
| 299 |
tracing::error!( |
| 300 |
tier = %target.name, node = %node.name, version = %version, |
| 301 |
failure = failure.summary(), |
| 302 |
"deploy failed; current symlink left intact, tier_state not advanced" |
| 303 |
); |
| 304 |
crate::events::emit( |
| 305 |
&s.events, |
| 306 |
crate::events::Event::DeployFailed { |
| 307 |
tier: target.name.clone(), |
| 308 |
node: node.name.clone(), |
| 309 |
version: version.clone(), |
| 310 |
failure, |
| 311 |
}, |
| 312 |
); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
deployed.push(node); |
| 321 |
let touched = deployed.len(); |
| 322 |
match prev_version.as_deref() { |
| 323 |
Some(prev) => { |
| 324 |
let report = rollback_deployed_nodes(&s, &target.name, &deployed, prev).await; |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if report.is_consistent() { |
| 330 |
tracing::warn!( |
| 331 |
tier = %target.name, |
| 332 |
restored = report.restored, |
| 333 |
already_on_previous = report.already_on_previous, |
| 334 |
of = report.touched(), |
| 335 |
from = %version, to = prev, |
| 336 |
"canary failed mid-rollout; every touched node is on the previous \ |
| 337 |
version and the tier is consistent", |
| 338 |
); |
| 339 |
} else { |
| 340 |
tracing::error!( |
| 341 |
tier = %target.name, |
| 342 |
restored = report.restored, |
| 343 |
already_on_previous = report.already_on_previous, |
| 344 |
indeterminate = report.indeterminate, |
| 345 |
of = report.touched(), |
| 346 |
from = %version, to = prev, |
| 347 |
"canary failed mid-rollout and the tier is NOT consistent; some nodes \ |
| 348 |
have an indeterminate version", |
| 349 |
); |
| 350 |
} |
| 351 |
if report.restored > 0 |
| 352 |
&& let Ok(prev_v) = crate::domain::Version::parse(prev) |
| 353 |
{ |
| 354 |
crate::events::emit( |
| 355 |
&s.events, |
| 356 |
crate::events::Event::Rollback { |
| 357 |
tier: target.name.clone(), |
| 358 |
from: version.clone(), |
| 359 |
to: prev_v, |
| 360 |
}, |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
if report.is_consistent() { |
| 369 |
clear_partial(&s, &target.name).await; |
| 370 |
} else { |
| 371 |
set_partial(&s, &target.name, &format!( |
| 372 |
"canary rollback incomplete: {indeterminate} of {touched} node(s) have an \ |
| 373 |
indeterminate version and may be on {version}; {restored} restored to \ |
| 374 |
{prev}, {already} were never swapped — manual check needed", |
| 375 |
touched = report.touched(), |
| 376 |
indeterminate = report.indeterminate, |
| 377 |
restored = report.restored, |
| 378 |
already = report.already_on_previous, |
| 379 |
)).await; |
| 380 |
} |
| 381 |
} |
| 382 |
None => { |
| 383 |
tracing::error!( |
| 384 |
tier = %target.name, count = touched, version = %version, |
| 385 |
"canary failed on a first deploy (no previous version to restore to); \ |
| 386 |
touched nodes remain on the new version — manual cleanup needed", |
| 387 |
); |
| 388 |
set_partial( |
| 389 |
&s, |
| 390 |
&target.name, |
| 391 |
&format!( |
| 392 |
"first-deploy canary failed: {touched} node(s) left on {version}, \ |
| 393 |
no prior version to restore — manual cleanup needed", |
| 394 |
), |
| 395 |
) |
| 396 |
.await; |
| 397 |
} |
| 398 |
} |
| 399 |
return Err(crate::error::Error::Other(e)); |
| 400 |
} |
| 401 |
deployed.push(node); |
| 402 |
crate::events::emit( |
| 403 |
&s.events, |
| 404 |
crate::events::Event::DeployOk { |
| 405 |
tier: target.name.clone(), |
| 406 |
node: node.name.clone(), |
| 407 |
version: version.clone(), |
| 408 |
}, |
| 409 |
); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
let post_deploy: Vec<crate::topology::Gate> = target |
| 431 |
.gates |
| 432 |
.iter() |
| 433 |
.filter(|g| g.runs_post_deploy()) |
| 434 |
.cloned() |
| 435 |
.collect(); |
| 436 |
let mut post_deploy_failure: Option<String> = None; |
| 437 |
if !post_deploy.is_empty() { |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
let nodes: Vec<crate::gates::NodeProbe> = target |
| 444 |
.nodes |
| 445 |
.iter() |
| 446 |
.filter_map(|n| { |
| 447 |
s.executors |
| 448 |
.get(&n.name) |
| 449 |
.map(|exec| crate::gates::NodeProbe { |
| 450 |
node: n.name.clone(), |
| 451 |
service: n.service_name.clone(), |
| 452 |
health_url: n.health_url.clone(), |
| 453 |
executor: exec.clone(), |
| 454 |
}) |
| 455 |
}) |
| 456 |
.collect(); |
| 457 |
let ctx = crate::gates::GateCtx { |
| 458 |
pool: s.pool.clone(), |
| 459 |
cfg: s.cfg.clone(), |
| 460 |
tier: target.name.clone(), |
| 461 |
version: version.clone(), |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
worktree: None, |
| 466 |
bundle: None, |
| 467 |
events: s.events.clone(), |
| 468 |
nodes, |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
build_id, |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public_url: target.public_url.clone(), |
| 478 |
|
| 479 |
aux_dirs: std::collections::HashMap::new(), |
| 480 |
}; |
| 481 |
post_deploy_failure = match crate::gates::run_all(&ctx, &post_deploy).await { |
| 482 |
Ok(failed) if failed.is_empty() => None, |
| 483 |
Ok(failed) => { |
| 484 |
let names = failed |
| 485 |
.iter() |
| 486 |
.map(|k| k.as_str()) |
| 487 |
.collect::<Vec<_>>() |
| 488 |
.join(", "); |
| 489 |
tracing::warn!( |
| 490 |
tier = %target.name, version = %version, gates = %names, |
| 491 |
"post-deploy gate(s) failed; tier advanced but promotion to the next tier is blocked", |
| 492 |
); |
| 493 |
Some(format!( |
| 494 |
"post-deploy gate(s) failed on {version}: {names}; \ |
| 495 |
the tier is serving {version} but cannot promote onward until they pass" |
| 496 |
)) |
| 497 |
} |
| 498 |
Err(e) => { |
| 499 |
tracing::error!( |
| 500 |
tier = %target.name, version = %version, error = %e, |
| 501 |
"post-deploy gate execution errored; promotion to the next tier is blocked", |
| 502 |
); |
| 503 |
Some(format!( |
| 504 |
"post-deploy gate execution errored on {version}: {e}; \ |
| 505 |
the tier is serving {version} but cannot promote onward until the gates pass" |
| 506 |
)) |
| 507 |
} |
| 508 |
}; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
crate::runs::advance_tier(&s.pool, &s.cfg.id, target.name.as_str(), &version, build_id) |
| 518 |
.await |
| 519 |
.map_err(crate::error::Error::Db)?; |
| 520 |
|
| 521 |
if body.reset_burn_in { |
| 522 |
sqlx::query("UPDATE tier_state SET burn_in_started_at = NULL WHERE app = ? AND tier = ?") |
| 523 |
.bind(&s.cfg.id) |
| 524 |
.bind(&source.name) |
| 525 |
.execute(&s.pool) |
| 526 |
.await |
| 527 |
.map_err(crate::error::Error::Db)?; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
if let Some(reason) = post_deploy_failure { |
| 536 |
set_partial(&s, &target.name, &reason).await; |
| 537 |
return Err(crate::error::Error::GateBlocked(reason)); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
clear_partial(&s, &target.name).await; |
| 542 |
|
| 543 |
crate::events::emit( |
| 544 |
&s.events, |
| 545 |
crate::events::Event::PromoteComplete { |
| 546 |
tier: target.name.clone(), |
| 547 |
version: version.clone(), |
| 548 |
}, |
| 549 |
); |
| 550 |
tracing::info!( |
| 551 |
version = %version, tier = %target.name, |
| 552 |
hotfix = body.hotfix, reset_burn_in = body.reset_burn_in, |
| 553 |
"promote complete", |
| 554 |
); |
| 555 |
|
| 556 |
Ok(Json(serde_json::json!({ |
| 557 |
"tier": target.name, |
| 558 |
"version": version, |
| 559 |
"nodes_deployed": target.nodes.iter().map(|n| n.name.clone()).collect::<Vec<_>>(), |
| 560 |
}))) |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 571 |
pub(super) struct RollbackReport { |
| 572 |
|
| 573 |
pub(super) restored: usize, |
| 574 |
|
| 575 |
|
| 576 |
pub(super) already_on_previous: usize, |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
pub(super) indeterminate: usize, |
| 581 |
} |
| 582 |
|
| 583 |
impl RollbackReport { |
| 584 |
|
| 585 |
pub(super) fn touched(self) -> usize { |
| 586 |
self.restored + self.already_on_previous + self.indeterminate |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
pub(super) fn is_consistent(self) -> bool { |
| 593 |
self.indeterminate == 0 |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
pub(super) type NodeBundle<'a> = ( |
| 606 |
&'a crate::topology::Node, |
| 607 |
std::path::PathBuf, |
| 608 |
Option<crate::domain::Platform>, |
| 609 |
Option<i64>, |
| 610 |
); |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 619 |
pub(super) struct PromotedBuild { |
| 620 |
|
| 621 |
|
| 622 |
pub platform: Option<crate::domain::Platform>, |
| 623 |
|
| 624 |
|
| 625 |
pub build_id: Option<i64>, |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
pub(super) async fn bundles_for_nodes<'a>( |
| 640 |
s: &AppState, |
| 641 |
version: &str, |
| 642 |
nodes: &[&'a crate::topology::Node], |
| 643 |
fallback: &std::path::Path, |
| 644 |
fallback_platform: Option<&crate::domain::Platform>, |
| 645 |
fallback_build_id: Option<i64>, |
| 646 |
) -> Result<Vec<NodeBundle<'a>>> { |
| 647 |
let mut out = Vec::with_capacity(nodes.len()); |
| 648 |
for node in nodes.iter().copied() { |
| 649 |
let resolved = match &node.platform { |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
None => ( |
| 654 |
fallback.to_path_buf(), |
| 655 |
fallback_platform.cloned(), |
| 656 |
fallback_build_id, |
| 657 |
), |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
Some(want) if fallback_platform == Some(want) => ( |
| 662 |
fallback.to_path_buf(), |
| 663 |
fallback_platform.cloned(), |
| 664 |
fallback_build_id, |
| 665 |
), |
| 666 |
Some(want) => { |
| 667 |
let (build_id, path) = bundle_for_platform(s, version, want).await?; |
| 668 |
(path, Some(want.clone()), build_id) |
| 669 |
} |
| 670 |
}; |
| 671 |
out.push((node, resolved.0, resolved.1, resolved.2)); |
| 672 |
} |
| 673 |
Ok(out) |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
pub(super) fn distinct_builds(bundles: &[NodeBundle<'_>]) -> Vec<PromotedBuild> { |
| 684 |
let mut out: Vec<PromotedBuild> = Vec::new(); |
| 685 |
for (_, _, platform, build_id) in bundles { |
| 686 |
let entry = PromotedBuild { |
| 687 |
platform: platform.clone(), |
| 688 |
build_id: *build_id, |
| 689 |
}; |
| 690 |
if !out.contains(&entry) { |
| 691 |
out.push(entry); |
| 692 |
} |
| 693 |
} |
| 694 |
out |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
async fn bundle_for_platform( |
| 709 |
s: &AppState, |
| 710 |
version: &str, |
| 711 |
platform: &crate::domain::Platform, |
| 712 |
) -> Result<(Option<i64>, std::path::PathBuf)> { |
| 713 |
let row: Option<(i64, Option<String>)> = sqlx::query_as( |
| 714 |
"SELECT id, staged_path FROM build_runs |
| 715 |
WHERE app = ? AND version = ? AND platform = ? AND result = 'passed' |
| 716 |
ORDER BY id DESC LIMIT 1", |
| 717 |
) |
| 718 |
.bind(&s.cfg.id) |
| 719 |
.bind(version) |
| 720 |
.bind(platform.to_string()) |
| 721 |
.fetch_optional(&s.pool) |
| 722 |
.await |
| 723 |
.map_err(crate::error::Error::Db)?; |
| 724 |
|
| 725 |
match row { |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
Some((id, Some(path))) => Ok((Some(id), std::path::PathBuf::from(path))), |
| 731 |
Some((_, None)) => Err(crate::error::Error::Other(anyhow::anyhow!( |
| 732 |
"the {platform} build of {version} has no staged_path; cannot promote it" |
| 733 |
))), |
| 734 |
None => Err(crate::error::Error::GateBlocked(format!( |
| 735 |
"no green {platform} bundle recorded for {version}. Each architecture is \ |
| 736 |
a separate artifact with its own evidence, so this one has to be built \ |
| 737 |
and accepted before a {platform} node can take this version" |
| 738 |
))), |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
pub(super) async fn rollback_deployed_nodes( |
| 754 |
s: &AppState, |
| 755 |
tier: &crate::domain::TierId, |
| 756 |
nodes: &[&crate::topology::Node], |
| 757 |
prev_version: &str, |
| 758 |
) -> RollbackReport { |
| 759 |
let bin: Option<(String,)> = |
| 760 |
match sqlx::query_as("SELECT artifact_path FROM versions WHERE app = ? AND version = ?") |
| 761 |
.bind(&s.cfg.id) |
| 762 |
.bind(prev_version) |
| 763 |
.fetch_optional(&s.pool) |
| 764 |
.await |
| 765 |
{ |
| 766 |
Ok(b) => b, |
| 767 |
Err(e) => { |
| 768 |
tracing::error!(tier = %tier, prev = prev_version, error = %e, |
| 769 |
"canary rollback: looking up the previous artifact failed; no rollback attempted"); |
| 770 |
return RollbackReport { |
| 771 |
indeterminate: nodes.len(), |
| 772 |
..Default::default() |
| 773 |
}; |
| 774 |
} |
| 775 |
}; |
| 776 |
let Some((bin,)) = bin else { |
| 777 |
tracing::error!(tier = %tier, prev = prev_version, nodes = nodes.len(), |
| 778 |
"canary rollback: previous version has no artifact_path; no rollback attempted"); |
| 779 |
return RollbackReport { |
| 780 |
indeterminate: nodes.len(), |
| 781 |
..Default::default() |
| 782 |
}; |
| 783 |
}; |
| 784 |
let Some(staged_dir) = std::path::PathBuf::from(&bin) |
| 785 |
.parent() |
| 786 |
.map(std::path::Path::to_path_buf) |
| 787 |
else { |
| 788 |
tracing::error!(tier = %tier, prev = prev_version, |
| 789 |
"canary rollback: previous artifact_path has no parent dir; no rollback attempted"); |
| 790 |
return RollbackReport { |
| 791 |
indeterminate: nodes.len(), |
| 792 |
..Default::default() |
| 793 |
}; |
| 794 |
}; |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
let bundles = match bundles_for_nodes(s, prev_version, nodes, &staged_dir, None, None).await { |
| 807 |
Ok(b) => b, |
| 808 |
Err(e) => { |
| 809 |
tracing::error!(tier = %tier, prev = prev_version, error = %e, |
| 810 |
"canary rollback: could not resolve a previous bundle per node; no rollback attempted"); |
| 811 |
return RollbackReport { |
| 812 |
indeterminate: nodes.len(), |
| 813 |
..Default::default() |
| 814 |
}; |
| 815 |
} |
| 816 |
}; |
| 817 |
|
| 818 |
let mut report = RollbackReport::default(); |
| 819 |
for (node, node_bundle, node_bundle_platform, _) in &bundles { |
| 820 |
let executor = s |
| 821 |
.executors |
| 822 |
.get(&node.name) |
| 823 |
.cloned() |
| 824 |
.unwrap_or_else(|| crate::state::build_executor(node)); |
| 825 |
let placement = |
| 826 |
match crate::deploy::Placement::check(node, node_bundle, node_bundle_platform.as_ref()) |
| 827 |
{ |
| 828 |
Ok(p) => p, |
| 829 |
Err(e) => { |
| 830 |
report.indeterminate += 1; |
| 831 |
tracing::error!(tier = %tier, node = %node.name, error = %e, |
| 832 |
"canary rollback: refused to place the previous bundle on this node"); |
| 833 |
continue; |
| 834 |
} |
| 835 |
}; |
| 836 |
match crate::deploy::deploy_node( |
| 837 |
executor.as_ref(), |
| 838 |
placement, |
| 839 |
prev_version, |
| 840 |
s.cfg.primary_bin(), |
| 841 |
) |
| 842 |
.await |
| 843 |
{ |
| 844 |
Ok(_) => { |
| 845 |
report.restored += 1; |
| 846 |
tracing::warn!(tier = %tier, node = %node.name, version = prev_version, |
| 847 |
"canary rollback: node restored to the previous version"); |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
Err(e) => match crate::deploy::stage_of(&e) { |
| 855 |
Some(crate::deploy::FailureStage::BeforeSwap) => { |
| 856 |
report.already_on_previous += 1; |
| 857 |
tracing::warn!( |
| 858 |
tier = %tier, node = %node.name, version = prev_version, |
| 859 |
error = %format!("{e:#}"), |
| 860 |
"canary rollback did not run, and did not need to: it failed before the \ |
| 861 |
symlink swap, so the node is already on the previous version", |
| 862 |
); |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
stage => { |
| 867 |
report.indeterminate += 1; |
| 868 |
tracing::error!( |
| 869 |
tier = %tier, node = %node.name, version = prev_version, |
| 870 |
error = %format!("{e:#}"), |
| 871 |
stage = ?stage, |
| 872 |
"canary rollback FAILED for node at or after the symlink swap; its version \ |
| 873 |
is indeterminate — manual intervention needed", |
| 874 |
); |
| 875 |
} |
| 876 |
}, |
| 877 |
} |
| 878 |
} |
| 879 |
report |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
pub(super) async fn set_partial(s: &AppState, tier: &crate::domain::TierId, reason: &str) { |
| 887 |
if let Err(e) = |
| 888 |
sqlx::query("UPDATE tier_state SET partial_reason = ? WHERE app = ? AND tier = ?") |
| 889 |
.bind(reason) |
| 890 |
.bind(&s.cfg.id) |
| 891 |
.bind(tier) |
| 892 |
.execute(&s.pool) |
| 893 |
.await |
| 894 |
{ |
| 895 |
tracing::error!(tier = %tier, reason, error = %e, |
| 896 |
"failed to record tier partial state; the fleet may be inconsistent without a /state flag"); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
pub(super) async fn clear_partial(s: &AppState, tier: &crate::domain::TierId) { |
| 905 |
if let Err(e) = |
| 906 |
sqlx::query("UPDATE tier_state SET partial_reason = NULL WHERE app = ? AND tier = ?") |
| 907 |
.bind(&s.cfg.id) |
| 908 |
.bind(tier) |
| 909 |
.execute(&s.pool) |
| 910 |
.await |
| 911 |
{ |
| 912 |
tracing::warn!(tier = %tier, error = %e, "failed to clear tier partial flag"); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
pub(super) async fn unsatisfied_gates( |
| 953 |
pool: &sqlx::SqlitePool, |
| 954 |
app: &crate::domain::AppId, |
| 955 |
tier: &crate::domain::TierId, |
| 956 |
gates: &[crate::topology::Gate], |
| 957 |
version: &str, |
| 958 |
builds: &[PromotedBuild], |
| 959 |
hotfix: bool, |
| 960 |
) -> std::result::Result<Vec<String>, crate::error::Error> { |
| 961 |
use crate::topology::Gate; |
| 962 |
let mut bad = Vec::new(); |
| 963 |
for gate in gates { |
| 964 |
let kind = gate.kind(); |
| 965 |
match gate { |
| 966 |
Gate::BurnIn { hours } => { |
| 967 |
if hotfix { |
| 968 |
continue; |
| 969 |
} |
| 970 |
let ok = crate::gates::burn_in_satisfied(pool, app, tier, *hours) |
| 971 |
.await |
| 972 |
.map_err(crate::error::Error::Other)?; |
| 973 |
if !ok { |
| 974 |
bad.push(kind.as_str().to_string()); |
| 975 |
} |
| 976 |
} |
| 977 |
Gate::ManualConfirm => { |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
let confirmed_at: Option<String> = sqlx::query_scalar( |
| 986 |
"SELECT finished_at FROM gate_runs |
| 987 |
WHERE app = ?1 AND tier = ?2 AND version = ?3 |
| 988 |
AND gate_kind = 'manual_confirm' AND status = 'passed' |
| 989 |
ORDER BY id DESC LIMIT 1", |
| 990 |
) |
| 991 |
.bind(app) |
| 992 |
.bind(tier.as_str()) |
| 993 |
.bind(version) |
| 994 |
.fetch_optional(pool) |
| 995 |
.await |
| 996 |
.map_err(crate::error::Error::Db)? |
| 997 |
.flatten(); |
| 998 |
let landed_at: Option<String> = sqlx::query_scalar( |
| 999 |
"SELECT burn_in_started_at FROM tier_state WHERE app = ? AND tier = ?", |
| 1000 |
) |
| 1001 |
.bind(app) |
| 1002 |
.bind(tier.as_str()) |
| 1003 |
.fetch_optional(pool) |
| 1004 |
.await |
| 1005 |
.map_err(crate::error::Error::Db)? |
| 1006 |
.flatten(); |
| 1007 |
let fresh = match (confirmed_at, landed_at) { |
| 1008 |
(Some(c), Some(l)) => { |
| 1009 |
match ( |
| 1010 |
chrono::DateTime::parse_from_rfc3339(&c), |
| 1011 |
chrono::DateTime::parse_from_rfc3339(&l), |
| 1012 |
) { |
| 1013 |
(Ok(cd), Ok(ld)) => cd >= ld, |
| 1014 |
_ => false, |
| 1015 |
} |
| 1016 |
} |
| 1017 |
_ => false, |
| 1018 |
}; |
| 1019 |
if !fresh { |
| 1020 |
bad.push(kind.as_str().to_string()); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
Gate::CargoTest |
| 1030 |
| Gate::HardeningTest |
| 1031 |
| Gate::Clippy |
| 1032 |
| Gate::Fmt |
| 1033 |
| Gate::CargoAudit |
| 1034 |
| Gate::CargoDeny |
| 1035 |
| Gate::MigrationDryRun |
| 1036 |
| Gate::CodeSmoke |
| 1037 |
| Gate::BootSmoke |
| 1038 |
| Gate::NodeHealth |
| 1039 |
|
| 1040 |
|
| 1041 |
| Gate::PageSmoke => { |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
let lookups: &[PromotedBuild] = if builds.is_empty() { |
| 1048 |
&[PromotedBuild { |
| 1049 |
platform: None, |
| 1050 |
build_id: None, |
| 1051 |
}] |
| 1052 |
} else { |
| 1053 |
builds |
| 1054 |
}; |
| 1055 |
let qualify = lookups.len() > 1; |
| 1056 |
for b in lookups { |
| 1057 |
|
| 1058 |
|
| 1059 |
let status: Option<String> = match b.build_id { |
| 1060 |
Some(bid) => sqlx::query_scalar( |
| 1061 |
"SELECT status FROM gate_runs |
| 1062 |
WHERE app = ?1 AND tier = ?2 AND build_id = ?3 AND gate_kind = ?4 |
| 1063 |
ORDER BY id DESC LIMIT 1", |
| 1064 |
) |
| 1065 |
.bind(app) |
| 1066 |
.bind(tier.as_str()) |
| 1067 |
.bind(bid) |
| 1068 |
.bind(kind.as_str()), |
| 1069 |
None => sqlx::query_scalar( |
| 1070 |
"SELECT status FROM gate_runs |
| 1071 |
WHERE app = ?1 AND tier = ?2 AND version = ?3 AND gate_kind = ?4 |
| 1072 |
ORDER BY id DESC LIMIT 1", |
| 1073 |
) |
| 1074 |
.bind(app) |
| 1075 |
.bind(tier.as_str()) |
| 1076 |
.bind(version) |
| 1077 |
.bind(kind.as_str()), |
| 1078 |
} |
| 1079 |
.fetch_optional(pool) |
| 1080 |
.await |
| 1081 |
.map_err(crate::error::Error::Db)? |
| 1082 |
.flatten(); |
| 1083 |
if status.as_deref() != Some("passed") { |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
let name = match (&b.platform, qualify) { |
| 1088 |
(Some(p), true) => format!("{} ({p})", kind.as_str()), |
| 1089 |
_ => kind.as_str().to_string(), |
| 1090 |
}; |
| 1091 |
if !bad.contains(&name) { |
| 1092 |
bad.push(name); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} |
| 1099 |
Ok(bad) |
| 1100 |
} |
| 1101 |
|