| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use super::DEPLOY_STAGING_ROOT; |
| 5 |
use super::RecipeCtx; |
| 6 |
use super::collect::ensure_glob_safe; |
| 7 |
use super::git::expand_tilde; |
| 8 |
use crate::topology::{DeployTarget, Kind}; |
| 9 |
use anyhow::{Context as _, Result}; |
| 10 |
use ops_exec::{Action, SyncOpts}; |
| 11 |
use std::path::{Path, PathBuf}; |
| 12 |
use std::sync::Arc; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
pub(super) fn max_glibc_symbol(objdump_out: &str) -> Option<(u64, u64)> { |
| 24 |
objdump_out |
| 25 |
.split(|c: char| !(c.is_ascii_digit() || c == '.' || c == '_' || c.is_ascii_alphabetic())) |
| 26 |
.filter_map(|tok| tok.strip_prefix("GLIBC_")) |
| 27 |
.filter_map(parse_glibc_version) |
| 28 |
.max() |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
pub(super) fn parse_glibc_version(s: &str) -> Option<(u64, u64)> { |
| 33 |
let mut parts = s.split('.'); |
| 34 |
let major = parts.next()?.parse().ok()?; |
| 35 |
let minor = parts.next()?.parse().ok()?; |
| 36 |
Some((major, minor)) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
pub(super) fn glibc_from_ldd(ldd_out: &str) -> Option<(u64, u64)> { |
| 43 |
let first = ldd_out.lines().find(|l| !l.trim().is_empty())?; |
| 44 |
parse_glibc_version(first.split_whitespace().last()?) |
| 45 |
} |
| 46 |
|
| 47 |
impl RecipeCtx { |
| 48 |
|
| 49 |
pub(super) fn deploy_target(&self) -> Result<&DeployTarget> { |
| 50 |
self.deploy.as_ref().ok_or_else(|| { |
| 51 |
anyhow::anyhow!( |
| 52 |
"no deploy destination for {} {}: the app is `kind = \"{}\"`, and only a \ |
| 53 |
service declares [[deploy]] entries", |
| 54 |
self.app, |
| 55 |
self.target, |
| 56 |
match self.kind { |
| 57 |
Kind::App => "app", |
| 58 |
Kind::Library => "library", |
| 59 |
Kind::Service => "service", |
| 60 |
} |
| 61 |
) |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pub(super) fn glibc_check(self: &Arc<Self>, binary: &str) -> Result<(String, String)> { |
| 68 |
let d = self.deploy_target()?.clone(); |
| 69 |
|
| 70 |
|
| 71 |
let (code, out) = self.run( |
| 72 |
&self.build_host.clone(), |
| 73 |
&format!( |
| 74 |
"objdump -T {binary} 2>/dev/null | grep -o 'GLIBC_[0-9.]*' | sort -uV || true" |
| 75 |
), |
| 76 |
)?; |
| 77 |
anyhow::ensure!(code == 0, "reading glibc symbols from {binary} failed"); |
| 78 |
let Some(needs) = max_glibc_symbol(&out) else { |
| 79 |
return Ok(("none".into(), "n/a".into())); |
| 80 |
}; |
| 81 |
let (code, ldd) = self.run(&d.host, "ldd --version")?; |
| 82 |
anyhow::ensure!( |
| 83 |
code == 0, |
| 84 |
"could not read glibc version on service host `{}`", |
| 85 |
d.host |
| 86 |
); |
| 87 |
let has = glibc_from_ldd(&ldd).ok_or_else(|| { |
| 88 |
anyhow::anyhow!( |
| 89 |
"could not parse glibc version from `ldd --version` on `{}`", |
| 90 |
d.host |
| 91 |
) |
| 92 |
})?; |
| 93 |
anyhow::ensure!( |
| 94 |
needs <= has, |
| 95 |
"binary needs glibc {}.{} but `{}` has {}.{} — it would fail to exec after the \ |
| 96 |
unit restarted onto it. Build on a host no newer than the service host.", |
| 97 |
needs.0, |
| 98 |
needs.1, |
| 99 |
d.host, |
| 100 |
has.0, |
| 101 |
has.1, |
| 102 |
); |
| 103 |
Ok(( |
| 104 |
format!("{}.{}", needs.0, needs.1), |
| 105 |
format!("{}.{}", has.0, has.1), |
| 106 |
)) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
pub(super) fn deploy(self: &Arc<Self>, binary: &str) -> Result<String> { |
| 124 |
anyhow::ensure!( |
| 125 |
!self.is_cancelled(), |
| 126 |
"build superseded by a newer request; refusing to deploy" |
| 127 |
); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
let failed = self.failed_steps_snapshot(); |
| 132 |
anyhow::ensure!( |
| 133 |
failed.is_empty(), |
| 134 |
"refusing to deploy {} {}: {} failed earlier in this run", |
| 135 |
self.app, |
| 136 |
self.version, |
| 137 |
failed |
| 138 |
.iter() |
| 139 |
.map(ToString::to_string) |
| 140 |
.collect::<Vec<_>>() |
| 141 |
.join(", "), |
| 142 |
); |
| 143 |
let d = self.deploy_target()?.clone(); |
| 144 |
ensure_glob_safe(binary)?; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
let staged = format!("{DEPLOY_STAGING_ROOT}/{}", self.app); |
| 149 |
let staged_bin = format!("{staged}/{}", self.app); |
| 150 |
let deploy_exec = self.exec(&d.host)?; |
| 151 |
anyhow::ensure!( |
| 152 |
deploy_exec.capabilities().permits(&Action::Deploy), |
| 153 |
"service host `{}` is not granted the `deploy` capability", |
| 154 |
d.host |
| 155 |
); |
| 156 |
|
| 157 |
self.run_ok(&d.host, &format!("mkdir -p {staged}"))?; |
| 158 |
if self.build_host_ssh == d.host { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
self.run_ok(&d.host, &format!("cp -f {binary} {staged_bin}"))?; |
| 163 |
} else { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
let tmp = tempfile::tempdir().context("staging dir for deploy")?; |
| 168 |
let local = tmp.path().join(self.app.as_str()); |
| 169 |
self.pull_for_deploy(binary, &local)?; |
| 170 |
let (dest, opts) = (PathBuf::from(&staged), SyncOpts::default()); |
| 171 |
let dir = tmp.path().to_path_buf(); |
| 172 |
self.run_bounded(&format!("stage {} on `{}`", self.app, d.host), async move { |
| 173 |
deploy_exec.push_dir(&dir, &dest, &opts).await |
| 174 |
}) |
| 175 |
.with_context(|| format!("staging {} onto `{}`", self.app, d.host))?; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
self.run_ok( |
| 181 |
&d.host, |
| 182 |
&format!( |
| 183 |
"{} {staged_bin} {} {}", |
| 184 |
self.cfg.deploy_installer, d.install_path, d.service |
| 185 |
), |
| 186 |
)?; |
| 187 |
Ok(format!( |
| 188 |
"{} {} installed at {} on `{}`; {} restarted", |
| 189 |
self.app, self.version, d.install_path, d.host, d.service |
| 190 |
)) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
fn pull_for_deploy(self: &Arc<Self>, remote: &str, local: &Path) -> Result<()> { |
| 202 |
let host = self.build_host.clone(); |
| 203 |
let remote_path = expand_tilde(remote); |
| 204 |
if self.build_host_ssh == "local" || self.build_host_ssh.is_empty() { |
| 205 |
std::fs::copy(&remote_path, local).with_context(|| { |
| 206 |
format!("staging {} from the daemon host", remote_path.display()) |
| 207 |
})?; |
| 208 |
return Ok(()); |
| 209 |
} |
| 210 |
let sync = self.host_sync(&host)?; |
| 211 |
let (src, dst, opts) = (remote_path, local.to_path_buf(), SyncOpts::default()); |
| 212 |
self.run_bounded(&format!("fetch {remote} from `{host}`"), async move { |
| 213 |
sync.pull_file(&src, &dst, &opts).await |
| 214 |
}) |
| 215 |
.with_context(|| format!("fetching {remote} from `{host}` to deploy")) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
fn run_ok(self: &Arc<Self>, host: &str, cmd: &str) -> Result<String> { |
| 221 |
let (code, tail) = self.run(host, cmd)?; |
| 222 |
if code != 0 { |
| 223 |
self.fail_current_step(); |
| 224 |
anyhow::bail!("command on `{host}` exited {code}: {cmd}\n{tail}"); |
| 225 |
} |
| 226 |
Ok(tail) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#[cfg(test)] |
| 231 |
mod tests { |
| 232 |
use super::super::action_for; |
| 233 |
use super::super::build_engine; |
| 234 |
use super::*; |
| 235 |
use crate::config::Config; |
| 236 |
use crate::domain::{AppId, Status, Step, Version}; |
| 237 |
use crate::ota::OtaRegistry; |
| 238 |
use std::sync::atomic::AtomicBool; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
#[test] |
| 246 |
fn glibc_versions_parse_from_what_the_tools_actually_print() { |
| 247 |
|
| 248 |
|
| 249 |
let objdump = "GLIBC_2.2.5\nGLIBC_2.34\nGLIBC_2.9\nGLIBC_2.17\n"; |
| 250 |
assert_eq!(max_glibc_symbol(objdump), Some((2, 34))); |
| 251 |
|
| 252 |
assert_eq!(max_glibc_symbol(""), None); |
| 253 |
|
| 254 |
|
| 255 |
assert_eq!( |
| 256 |
glibc_from_ldd("ldd (Ubuntu GLIBC 2.39-0ubuntu8.8) 2.39\nCopyright...\n"), |
| 257 |
Some((2, 39)) |
| 258 |
); |
| 259 |
assert_eq!( |
| 260 |
glibc_from_ldd("ldd (GNU libc) 2.41\nCopyright (C) 2025\n"), |
| 261 |
Some((2, 41)) |
| 262 |
); |
| 263 |
assert_eq!(glibc_from_ldd(""), None); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
#[test] |
| 270 |
fn glibc_requirement_is_satisfied_by_equal_or_newer_only() { |
| 271 |
let needs = max_glibc_symbol("GLIBC_2.41").unwrap(); |
| 272 |
assert!(needs > glibc_from_ldd("ldd (Ubuntu GLIBC 2.39) 2.39").unwrap()); |
| 273 |
assert!(needs <= glibc_from_ldd("ldd (GNU libc) 2.41").unwrap()); |
| 274 |
assert!(needs <= glibc_from_ldd("ldd (GNU libc) 2.42").unwrap()); |
| 275 |
assert!(needs <= glibc_from_ldd("ldd (GNU libc) 3.0").unwrap()); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
#[tokio::test] |
| 283 |
async fn deploy_host_fns_explain_a_missing_destination_by_kind() { |
| 284 |
let dir = tempfile::tempdir().unwrap(); |
| 285 |
let cfg = Arc::new(Config::for_tests(dir.path())); |
| 286 |
let pool = crate::db::open(&cfg.db_path).await.unwrap(); |
| 287 |
let ctx = Arc::new(RecipeCtx::new( |
| 288 |
AppId::new("demo"), |
| 289 |
Version::parse("0.1.0").unwrap(), |
| 290 |
"linux/x86_64".parse().unwrap(), |
| 291 |
"fw13".into(), |
| 292 |
"local".into(), |
| 293 |
"v0.1.0".into(), |
| 294 |
"/tmp".into(), |
| 295 |
vec![], |
| 296 |
Kind::Library, |
| 297 |
1, |
| 298 |
Arc::new(std::collections::HashMap::new()), |
| 299 |
Arc::new(std::collections::HashMap::new()), |
| 300 |
None, |
| 301 |
pool, |
| 302 |
crate::events::channel(), |
| 303 |
cfg, |
| 304 |
Arc::new(OtaRegistry::standard("https://makenot.work")), |
| 305 |
tokio::runtime::Handle::current(), |
| 306 |
Arc::new(AtomicBool::new(false)), |
| 307 |
None, |
| 308 |
)); |
| 309 |
let engine = build_engine(&ctx); |
| 310 |
for call in [ |
| 311 |
"deploy_host()", |
| 312 |
"service_name()", |
| 313 |
"install_path()", |
| 314 |
"health_url()", |
| 315 |
r#"deploy("/tmp/x")"#, |
| 316 |
] { |
| 317 |
let err = engine.eval::<String>(call).unwrap_err().to_string(); |
| 318 |
assert!( |
| 319 |
err.contains("library") && err.contains("no deploy destination"), |
| 320 |
"`{call}` must fail on the kind, got: {err}" |
| 321 |
); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
#[tokio::test] |
| 335 |
async fn a_service_host_is_addressed_on_the_deploy_plane_in_any_step() { |
| 336 |
let dir = tempfile::tempdir().unwrap(); |
| 337 |
let cfg = Arc::new(Config::for_tests(dir.path())); |
| 338 |
let pool = crate::db::open(&cfg.db_path).await.unwrap(); |
| 339 |
sqlx::query( |
| 340 |
"INSERT INTO builds (id, app, version, status, created_at) \ |
| 341 |
VALUES (1, 'demo', '0.1.0', 'running', '2026-07-30T00:00:00Z')", |
| 342 |
) |
| 343 |
.execute(&pool) |
| 344 |
.await |
| 345 |
.unwrap(); |
| 346 |
sqlx::query( |
| 347 |
"INSERT INTO target_runs (id, build_id, app, version, target, status, started_at) \ |
| 348 |
VALUES (1, 1, 'demo', '0.1.0', 'linux/x86_64', 'running', '2026-07-30T00:00:00Z')", |
| 349 |
) |
| 350 |
.execute(&pool) |
| 351 |
.await |
| 352 |
.unwrap(); |
| 353 |
|
| 354 |
let deploy = crate::topology::DeployTarget { |
| 355 |
target: "linux/x86_64".parse().unwrap(), |
| 356 |
host: "local".into(), |
| 357 |
port: None, |
| 358 |
install_path: "/usr/local/bin/demo".into(), |
| 359 |
service: "demo.service".into(), |
| 360 |
health_url: None, |
| 361 |
}; |
| 362 |
let mut execs: crate::state::ExecutorMap = std::collections::HashMap::new(); |
| 363 |
execs.insert("local".into(), crate::state::build_deploy_executor(&deploy)); |
| 364 |
|
| 365 |
|
| 366 |
assert!(!execs["local"].capabilities().permits(&Action::Build)); |
| 367 |
assert!(execs["local"].capabilities().permits(&Action::Deploy)); |
| 368 |
|
| 369 |
let ctx = Arc::new(RecipeCtx::new( |
| 370 |
AppId::new("demo"), |
| 371 |
Version::parse("0.1.0").unwrap(), |
| 372 |
"linux/x86_64".parse().unwrap(), |
| 373 |
"fw13".into(), |
| 374 |
"local".into(), |
| 375 |
"v0.1.0".into(), |
| 376 |
"/tmp".into(), |
| 377 |
vec![], |
| 378 |
Kind::Service, |
| 379 |
1, |
| 380 |
Arc::new(execs), |
| 381 |
Arc::new(std::collections::HashMap::new()), |
| 382 |
Some(deploy), |
| 383 |
pool, |
| 384 |
crate::events::channel(), |
| 385 |
cfg, |
| 386 |
Arc::new(OtaRegistry::standard("https://makenot.work")), |
| 387 |
tokio::runtime::Handle::current(), |
| 388 |
Arc::new(AtomicBool::new(false)), |
| 389 |
None, |
| 390 |
)); |
| 391 |
|
| 392 |
let ctx_blocking = ctx.clone(); |
| 393 |
tokio::task::spawn_blocking(move || { |
| 394 |
|
| 395 |
|
| 396 |
ctx_blocking.begin_step(Step::Verify).unwrap(); |
| 397 |
assert_eq!( |
| 398 |
action_for(Step::Verify, Kind::Service), |
| 399 |
Action::Build, |
| 400 |
"the step's own action is the one that would be denied", |
| 401 |
); |
| 402 |
let (code, out) = ctx_blocking |
| 403 |
.run("local", "echo reached-the-service-host") |
| 404 |
.expect("a service host must be reachable during a verify step"); |
| 405 |
assert_eq!(code, 0, "{out}"); |
| 406 |
assert!(out.contains("reached-the-service-host"), "{out}"); |
| 407 |
}) |
| 408 |
.await |
| 409 |
.unwrap(); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
#[tokio::test] |
| 418 |
async fn a_failed_step_bars_the_deploy() { |
| 419 |
let dir = tempfile::tempdir().unwrap(); |
| 420 |
let cfg = Arc::new(Config::for_tests(dir.path())); |
| 421 |
let pool = crate::db::open(&cfg.db_path).await.unwrap(); |
| 422 |
let deploy = crate::topology::DeployTarget { |
| 423 |
target: "linux/x86_64".parse().unwrap(), |
| 424 |
host: "local".into(), |
| 425 |
port: None, |
| 426 |
install_path: "/usr/local/bin/demo".into(), |
| 427 |
service: "demo.service".into(), |
| 428 |
health_url: None, |
| 429 |
}; |
| 430 |
|
| 431 |
|
| 432 |
sqlx::query( |
| 433 |
"INSERT INTO builds (id, app, version, status, created_at) \ |
| 434 |
VALUES (1, 'demo', '0.1.0', 'running', '2026-07-30T00:00:00Z')", |
| 435 |
) |
| 436 |
.execute(&pool) |
| 437 |
.await |
| 438 |
.unwrap(); |
| 439 |
sqlx::query( |
| 440 |
"INSERT INTO target_runs (id, build_id, app, version, target, status, started_at) \ |
| 441 |
VALUES (1, 1, 'demo', '0.1.0', 'linux/x86_64', 'running', '2026-07-30T00:00:00Z')", |
| 442 |
) |
| 443 |
.execute(&pool) |
| 444 |
.await |
| 445 |
.unwrap(); |
| 446 |
|
| 447 |
let mut execs: crate::state::ExecutorMap = std::collections::HashMap::new(); |
| 448 |
execs.insert("local".into(), crate::state::build_deploy_executor(&deploy)); |
| 449 |
let ctx = Arc::new(RecipeCtx::new( |
| 450 |
AppId::new("demo"), |
| 451 |
Version::parse("0.1.0").unwrap(), |
| 452 |
"linux/x86_64".parse().unwrap(), |
| 453 |
"fw13".into(), |
| 454 |
"local".into(), |
| 455 |
"v0.1.0".into(), |
| 456 |
"/tmp".into(), |
| 457 |
vec![], |
| 458 |
Kind::Service, |
| 459 |
1, |
| 460 |
Arc::new(execs), |
| 461 |
Arc::new(std::collections::HashMap::new()), |
| 462 |
Some(deploy), |
| 463 |
pool, |
| 464 |
crate::events::channel(), |
| 465 |
cfg, |
| 466 |
Arc::new(OtaRegistry::standard("https://makenot.work")), |
| 467 |
tokio::runtime::Handle::current(), |
| 468 |
Arc::new(AtomicBool::new(false)), |
| 469 |
None, |
| 470 |
)); |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
let ctx_blocking = ctx.clone(); |
| 475 |
tokio::task::spawn_blocking(move || { |
| 476 |
ctx_blocking.begin_step(Step::Prebuild).unwrap(); |
| 477 |
ctx_blocking.fail_current_step(); |
| 478 |
ctx_blocking.finish_step(Status::Ok).unwrap(); |
| 479 |
|
| 480 |
let err = ctx_blocking.deploy("/tmp/demo").unwrap_err().to_string(); |
| 481 |
assert!( |
| 482 |
err.contains("refusing to deploy") && err.contains("prebuild"), |
| 483 |
"must refuse and name the failed step, got: {err}" |
| 484 |
); |
| 485 |
}) |
| 486 |
.await |
| 487 |
.unwrap(); |
| 488 |
} |
| 489 |
} |
| 490 |
|