| @@ -1,13 +1,15 @@ |
| 1 | 1 | //! `louise self update`: replace the running compiler, and the runtime |
| 2 | 2 | //! archives next to it, with a release's prebuilt ones (issue #284). |
| 3 | 3 | //! |
| 4 | | −//! Until the forge has real releases (toboggan#94), a "release" is the |
| 5 | | −//! artifact of the latest successful `release` workflow run on a tag |
| 6 | | −//! (`.toboggan/workflows/release.yml`): an artifact `louise-<tag>-<platform>` |
| 7 | | −//! holding `louise-<tag>-<platform>.tar.xz` (the compiler, the three |
| 8 | | −//! runtime archives and the lto archive's LLVM version) and the tarball's |
| 9 | | −//! sha256. [`discover`] is the one function that knows this shape; real |
| 10 | | −//! releases swap in by replacing it, the rest stands. |
| 4 | +//! A release is read from the forge's releases API (toboggan#94): |
| 5 | +//! `releases/latest` (or `releases/tag/<tag>` for `--version`), the asset |
| 6 | +//! `louise-<tag>-<platform>.tar.xz` (the compiler, the three runtime |
| 7 | +//! archives and the lto archive's LLVM version) with its server-counted |
| 8 | +//! sha256. A forge that predates releases (or a tag without one) falls |
| 9 | +//! back to the interim shape: the artifact of the latest successful |
| 10 | +//! `release` workflow run on a tag (`.toboggan/workflows/release.yml`), |
| 11 | +//! which holds the tarball zipped. [`discover`] is the one function that |
| 12 | +//! knows the two shapes; the rest stands. |
| 11 | 13 | //! |
| 12 | 14 | //! The forge's API is MessagePack over HTTPS with a bearer token |
| 13 | 15 | //! (toboggan's docs/api.md); `tob`'s `src/api.rs` is the reference client |
| @@ -82,7 +84,7 @@ fn go(check: bool, version: Option<&str>, dry_run: bool, force: bool) -> Result< |
| 82 | 84 | |
| 83 | 85 | if check { |
| 84 | 86 | println!("installed: louise {current}"); |
| 85 | | − println!("latest: {} (run {})", release.tag, release.run); |
| 87 | + println!("latest: {} ({})", release.tag, release.label()); |
| 86 | 88 | match wanted.cmp(¤t) { |
| 87 | 89 | std::cmp::Ordering::Greater => { |
| 88 | 90 | println!("an update is available: {current} → {wanted}; run `louise self update`") |
| @@ -116,26 +118,67 @@ fn go(check: bool, version: Option<&str>, dry_run: bool, force: bool) -> Result< |
| 116 | 118 | let staging = Staging::new(dir)?; |
| 117 | 119 | |
| 118 | 120 | // Download, verify and extract before anything is replaced. |
| 119 | | − let zip = staging.path().join("artifact.zip"); |
| 120 | | − eprintln!( |
| 121 | | − "downloading {} (run {}, {} bytes)…", |
| 122 | | − release.artifact.name, release.run, release.artifact.size |
| 123 | | − ); |
| 124 | | − client.download(&release.download_path(&targets), &zip)?; |
| 125 | | − if let Some(expected) = &release.artifact.sha256 { |
| 126 | | − let got = sha256_file(&zip)?; |
| 127 | | − if !got.eq_ignore_ascii_case(expected) { |
| 128 | | − return Err(format!( |
| 129 | | − "the artifact's sha256 differs from the server's ({got} ≠ {expected}); not installing it" |
| 130 | | − )); |
| 121 | + let (members, dir_name) = match &release.source { |
| 122 | + Source::Asset { name, size, sha256 } => { |
| 123 | + let asset = format!("{name}.tar.xz"); |
| 124 | + let tarball_path = staging.path().join(&asset); |
| 125 | + eprintln!( |
| 126 | + "downloading {asset} (release {}, {size} bytes)…", |
| 127 | + release.tag |
| 128 | + ); |
| 129 | + client.download( |
| 130 | + &format!( |
| 131 | + "/{}/releases/tag/{}/assets/{}", |
| 132 | + targets.repo, release.tag, asset |
| 133 | + ), |
| 134 | + &tarball_path, |
| 135 | + )?; |
| 136 | + let got = sha256_file(&tarball_path)?; |
| 137 | + if !got.eq_ignore_ascii_case(sha256) { |
| 138 | + return Err(format!( |
| 139 | + "the asset's sha256 differs from the server's ({got} ≠ {sha256}); not installing it" |
| 140 | + )); |
| 141 | + } |
| 142 | + let tarball = fs::read(&tarball_path) |
| 143 | + .map_err(|e| format!("cannot read {}: {e}", tarball_path.display()))?; |
| 144 | + ( |
| 145 | + Members { |
| 146 | + tarball, |
| 147 | + sha256: sha256.clone(), |
| 148 | + }, |
| 149 | + name.clone(), |
| 150 | + ) |
| 131 | 151 | } |
| 132 | | − } |
| 133 | | − let tarball = format!("{}.tar.xz", release.artifact.name); |
| 134 | | − let members = unzip(&zip, &tarball)?; |
| 152 | + Source::Artifact { run, artifact } => { |
| 153 | + let zip = staging.path().join("artifact.zip"); |
| 154 | + eprintln!( |
| 155 | + "downloading {} (run {}, {} bytes)…", |
| 156 | + artifact.name, run, artifact.size |
| 157 | + ); |
| 158 | + client.download( |
| 159 | + &format!( |
| 160 | + "/{}/actions/runs/{}/artifacts/{}", |
| 161 | + targets.repo, run, artifact.id |
| 162 | + ), |
| 163 | + &zip, |
| 164 | + )?; |
| 165 | + if let Some(expected) = &artifact.sha256 { |
| 166 | + let got = sha256_file(&zip)?; |
| 167 | + if !got.eq_ignore_ascii_case(expected) { |
| 168 | + return Err(format!( |
| 169 | + "the artifact's sha256 differs from the server's ({got} ≠ {expected}); not installing it" |
| 170 | + )); |
| 171 | + } |
| 172 | + } |
| 173 | + let tarball = format!("{}.tar.xz", artifact.name); |
| 174 | + let members = unzip(&zip, &tarball)?; |
| 175 | + (members, artifact.name.clone()) |
| 176 | + } |
| 177 | + }; |
| 135 | 178 | verify_sha256(&members.tarball, &members.sha256)?; |
| 136 | 179 | let unpacked = staging.path().join("unpacked"); |
| 137 | 180 | untar(&members.tarball, &unpacked)?; |
| 138 | | − let staged = find_staged(&unpacked, &release.artifact.name)?; |
| 181 | + let staged = find_staged(&unpacked, &dir_name)?; |
| 139 | 182 | sanity_check(&staged, &wanted)?; |
| 140 | 183 | |
| 141 | 184 | if dry_run { |
| @@ -315,28 +358,67 @@ struct Problem { |
| 315 | 358 | message: String, |
| 316 | 359 | } |
| 317 | 360 | |
| 318 | | −/// A release: the run that built it and the artifact that is it. |
| 361 | +/// A release as the releases API answers it (toboggan#94): the parts |
| 362 | +/// `update` reads. Keys absent on an older server get their defaults. |
| 363 | +#[derive(Debug, Deserialize)] |
| 364 | +struct ReleasePayload { |
| 365 | + tag: String, |
| 366 | + #[serde(default)] |
| 367 | + assets: Vec<AssetPayload>, |
| 368 | +} |
| 369 | + |
| 370 | +#[derive(Debug, Deserialize)] |
| 371 | +struct AssetPayload { |
| 372 | + name: String, |
| 373 | + size: u64, |
| 374 | + sha256: String, |
| 375 | +} |
| 376 | + |
| 377 | +/// A release: its tag, and where its bytes live. |
| 319 | 378 | #[derive(Debug)] |
| 320 | 379 | struct Release { |
| 321 | | − /// The tag the run ran on (`v0.1.0`). |
| 380 | + /// The tag (`v0.1.0`). |
| 322 | 381 | tag: String, |
| 323 | | − run: u64, |
| 324 | | − artifact: Artifact, |
| 382 | + source: Source, |
| 383 | +} |
| 384 | + |
| 385 | +/// Where a release's bytes live: a real release's asset (toboggan#94), |
| 386 | +/// or the interim CI artifact of the tag's release-workflow run. |
| 387 | +#[derive(Debug)] |
| 388 | +enum Source { |
| 389 | + /// The release's `louise-<tag>-<platform>.tar.xz` asset, raw bytes |
| 390 | + /// (never zipped), the sha256 the server counted on upload. |
| 391 | + Asset { |
| 392 | + name: String, |
| 393 | + size: u64, |
| 394 | + sha256: String, |
| 395 | + }, |
| 396 | + /// The run's `louise-<tag>-<platform>` artifact: the tarball, zipped. |
| 397 | + Artifact { run: u64, artifact: Artifact }, |
| 325 | 398 | } |
| 326 | 399 | |
| 327 | 400 | impl Release { |
| 328 | | − fn download_path(&self, targets: &Targets) -> String { |
| 329 | | − format!( |
| 330 | | − "/{}/actions/runs/{}/artifacts/{}", |
| 331 | | − targets.repo, self.run, self.artifact.id |
| 332 | | − ) |
| 401 | + /// The label `--check` prints: the run for an artifact, "release" for |
| 402 | + /// a real one. |
| 403 | + fn label(&self) -> String { |
| 404 | + match &self.source { |
| 405 | + Source::Asset { .. } => "release".to_string(), |
| 406 | + Source::Artifact { run, .. } => format!("run {run}"), |
| 407 | + } |
| 333 | 408 | } |
| 334 | 409 | } |
| 335 | 410 | |
| 411 | +/// The platform's asset base name in a release payload: |
| 412 | +/// `louise-<tag>-<platform>` (the asset adds `.tar.xz`). |
| 413 | +fn asset_for<'a>(payload: &'a ReleasePayload, platform: &str) -> Option<&'a AssetPayload> { |
| 414 | + let wanted = format!("louise-{}-{platform}.tar.xz", payload.tag); |
| 415 | + payload.assets.iter().find(|a| a.name == wanted) |
| 416 | +} |
| 417 | + |
| 336 | 418 | /// The release `version` names (a tag, with or without the `v`), else the |
| 337 | | −/// newest: the artifact of the latest successful `release` run on a |
| 338 | | −/// version tag. This is the interim, CI-artifact discovery; toboggan#94's |
| 339 | | −/// real releases replace this function alone. |
| 419 | +/// newest. The releases API (toboggan#94) answers first; a forge without |
| 420 | +/// it (or a tag with none published) falls back to the interim shape: the |
| 421 | +/// artifact of the latest successful `release` run on a version tag. |
| 340 | 422 | fn discover( |
| 341 | 423 | client: &Client, |
| 342 | 424 | targets: &Targets, |
| @@ -344,6 +426,55 @@ fn discover( |
| 344 | 426 | platform: &str, |
| 345 | 427 | ) -> Result<Release, String> { |
| 346 | 428 | let wanted = version.map(normalize_tag); |
| 429 | + if let Some(release) = discover_release(client, targets, wanted.as_deref(), platform)? { |
| 430 | + return Ok(release); |
| 431 | + } |
| 432 | + discover_artifact(client, targets, wanted, platform) |
| 433 | +} |
| 434 | + |
| 435 | +/// The releases API: `releases/latest`, or `releases/tag/<tag>` for |
| 436 | +/// `--version`. `None` when the route is a 404 — a forge that predates |
| 437 | +/// releases, or no release on that tag (the artifact flow may still know |
| 438 | +/// one) — and when the release has no asset for this platform. |
| 439 | +fn discover_release( |
| 440 | + client: &Client, |
| 441 | + targets: &Targets, |
| 442 | + wanted: Option<&str>, |
| 443 | + platform: &str, |
| 444 | +) -> Result<Option<Release>, String> { |
| 445 | + let path = match wanted { |
| 446 | + None => format!("/{}/releases/latest", targets.repo), |
| 447 | + Some(tag) => format!("/{}/releases/tag/{}", targets.repo, tag), |
| 448 | + }; |
| 449 | + let Some(payload) = client.get_optional::<ReleasePayload>(&path, &[])? else { |
| 450 | + return Ok(None); |
| 451 | + }; |
| 452 | + let Some(asset) = asset_for(&payload, platform) else { |
| 453 | + return Ok(None); |
| 454 | + }; |
| 455 | + let (name, size, sha256) = ( |
| 456 | + asset |
| 457 | + .name |
| 458 | + .strip_suffix(".tar.xz") |
| 459 | + .expect("asset_for matched the suffix") |
| 460 | + .to_string(), |
| 461 | + asset.size, |
| 462 | + asset.sha256.clone(), |
| 463 | + ); |
| 464 | + Ok(Some(Release { |
| 465 | + tag: payload.tag, |
| 466 | + source: Source::Asset { name, size, sha256 }, |
| 467 | + })) |
| 468 | +} |
| 469 | + |
| 470 | +/// The interim discovery: the artifact of the newest successful |
| 471 | +/// `release` run on a version tag (`wanted`, when given). |
| 472 | +fn discover_artifact( |
| 473 | + client: &Client, |
| 474 | + targets: &Targets, |
| 475 | + wanted: Option<String>, |
| 476 | + platform: &str, |
| 477 | +) -> Result<Release, String> { |
| 347 | 478 | let mut candidates = Vec::new(); |
| 348 | 479 | for page in 1..=10 { |
| 349 | 480 | let runs: Runs = client.get( |
| @@ -372,11 +503,11 @@ fn discover( |
| 372 | 503 | }); |
| 373 | 504 | let run = candidates.first().ok_or_else(|| match &wanted { |
| 374 | 505 | Some(tag) => format!( |
| 375 | | − "no successful `{}` run on tag {tag} (tags a release workflow ran on create one)", |
| 506 | + "no release on tag {tag}, and no successful `{}` run on it either (tags a release workflow ran on create one)", |
| 376 | 507 | targets.workflow |
| 377 | 508 | ), |
| 378 | 509 | None => format!( |
| 379 | | − "no release found: no successful `{}` run on a tag yet (the first tag creates one)", |
| 510 | + "no release found: none published, and no successful `{}` run on a tag yet (the first tag creates one)", |
| 380 | 511 | targets.workflow |
| 381 | 512 | ), |
| 382 | 513 | })?; |
| @@ -394,8 +525,10 @@ fn discover( |
| 394 | 525 | .ok_or_else(|| format!("run {} has no artifact {name}", run.number))?; |
| 395 | 526 | Ok(Release { |
| 396 | 527 | tag, |
| 397 | | − run: run.number, |
| 398 | | − artifact, |
| 528 | + source: Source::Artifact { |
| 529 | + run: run.number, |
| 530 | + artifact, |
| 531 | + }, |
| 399 | 532 | }) |
| 400 | 533 | } |
| 401 | 534 | |
| @@ -450,6 +583,31 @@ impl Client { |
| 450 | 583 | path: &str, |
| 451 | 584 | query: &[(&str, String)], |
| 452 | 585 | ) -> Result<T, String> { |
| 586 | + let (status, body) = self.get_raw(path, query)?; |
| 587 | + self.problems(status, &body)?; |
| 588 | + rmp_serde::from_slice(&body) |
| 589 | + .map_err(|e| format!("the answer to GET {path} is not what the API describes: {e}")) |
| 590 | + } |
| 591 | + |
| 592 | + /// A GET that may not exist: `None` on 404 (a forge that predates the |
| 593 | + /// route, or nothing there), an error on anything else non-2xx. |
| 594 | + fn get_optional<T: serde::de::DeserializeOwned>( |
| 595 | + &self, |
| 596 | + path: &str, |
| 597 | + query: &[(&str, String)], |
| 598 | + ) -> Result<Option<T>, String> { |
| 599 | + let (status, body) = self.get_raw(path, query)?; |
| 600 | + if status == 404 { |
| 601 | + return Ok(None); |
| 602 | + } |
| 603 | + self.problems(status, &body)?; |
| 604 | + rmp_serde::from_slice(&body) |
| 605 | + .map(Some) |
| 606 | + .map_err(|e| format!("the answer to GET {path} is not what the API describes: {e}")) |
| 607 | + } |
| 608 | + |
| 609 | + /// A GET's status and body, problems unchecked. |
| 610 | + fn get_raw(&self, path: &str, query: &[(&str, String)]) -> Result<(u16, Vec<u8>), String> { |
| 453 | 611 | let mut request = self.agent.get(format!("{}{path}", self.base)); |
| 454 | 612 | if let Some(token) = &self.token { |
| 455 | 613 | request = request.header("Authorization", format!("Bearer {token}")); |
| @@ -467,9 +625,7 @@ impl Client { |
| 467 | 625 | .limit(MAX_ANSWER_BYTES) |
| 468 | 626 | .read_to_vec() |
| 469 | 627 | .map_err(|e| format!("cannot read the answer to GET {path}: {e}"))?; |
| 470 | | − self.problems(status, &body)?; |
| 471 | | − rmp_serde::from_slice(&body) |
| 472 | | − .map_err(|e| format!("the answer to GET {path} is not what the API describes: {e}")) |
| 628 | + Ok((status, body)) |
| 473 | 629 | } |
| 474 | 630 | |
| 475 | 631 | /// `path`'s bytes to `dest`, through `dest.part` renamed once whole, |
| @@ -548,7 +704,9 @@ impl Client { |
| 548 | 704 | |
| 549 | 705 | // ------------------------------------------------------------------ the archive |
| 550 | 706 | |
| 551 | | −/// A release artifact's two members, read out of its zip. |
| 707 | +/// A release's two members: the tarball, and its sha256 (a file's text |
| 708 | +/// `<hex> <name>` from the artifact zip; the server-counted hex alone |
| 709 | +/// for a release asset). |
| 552 | 710 | struct Members { |
| 553 | 711 | /// `louise-<tag>-<platform>.tar.xz`. |
| 554 | 712 | tarball: Vec<u8>, |
| @@ -1133,14 +1291,8 @@ protocol = "http" |
| 1133 | 1291 | let artifact = full.artifacts.iter().find(|a| a.id == id).unwrap(); |
| 1134 | 1292 | let dest = |
| 1135 | 1293 | std::env::temp_dir().join(format!("louise-live-download-{}", std::process::id())); |
| 1136 | | − let release = Release { |
| 1137 | | − tag: "test".into(), |
| 1138 | | − run, |
| 1139 | | − artifact: artifact.clone(), |
| 1140 | | − }; |
| 1141 | | − let size = client |
| 1142 | | − .download(&release.download_path(&targets), &dest) |
| 1143 | | − .unwrap(); |
| 1294 | + let path = format!("/{}/actions/runs/{run}/artifacts/{id}", targets.repo); |
| 1295 | + let size = client.download(&path, &dest).unwrap(); |
| 1144 | 1296 | assert_eq!(size, artifact.size, "the byte count is the server's"); |
| 1145 | 1297 | if let Some(expected) = &artifact.sha256 { |
| 1146 | 1298 | assert_eq!(&sha256_file(&dest).unwrap(), expected); |
| @@ -1149,4 +1301,93 @@ protocol = "http" |
| 1149 | 1301 | eprintln!("{}: {} bytes to {}", artifact.name, size, dest.display()); |
| 1150 | 1302 | fs::remove_file(&dest).unwrap(); |
| 1151 | 1303 | } |
| 1304 | + |
| 1305 | + /// A releases-API payload (toboggan#94) decodes, and the platform's |
| 1306 | + /// asset is found by name. |
| 1307 | + #[test] |
| 1308 | + fn release_payload_decodes_and_selects() { |
| 1309 | + #[derive(serde::Serialize)] |
| 1310 | + struct Asset { |
| 1311 | + name: &'static str, |
| 1312 | + mime_type: &'static str, |
| 1313 | + size: u64, |
| 1314 | + sha256: &'static str, |
| 1315 | + } |
| 1316 | + #[derive(serde::Serialize)] |
| 1317 | + struct Payload { |
| 1318 | + tag: &'static str, |
| 1319 | + title: &'static str, |
| 1320 | + notes: &'static str, |
| 1321 | + draft: bool, |
| 1322 | + prerelease: bool, |
| 1323 | + created_at: &'static str, |
| 1324 | + updated_at: &'static str, |
| 1325 | + path: &'static str, |
| 1326 | + assets: Vec<Asset>, |
| 1327 | + } |
| 1328 | + let payload = Payload { |
| 1329 | + tag: "v0.2.0", |
| 1330 | + title: "v0.2.0", |
| 1331 | + notes: "", |
| 1332 | + draft: false, |
| 1333 | + prerelease: false, |
| 1334 | + created_at: "2026-10-19T18:54:00Z", |
| 1335 | + updated_at: "2026-10-19T18:54:00Z", |
| 1336 | + path: "/gabla108/louise/releases/tag/v0.2.0", |
| 1337 | + assets: vec![Asset { |
| 1338 | + name: "louise-v0.2.0-x86_64-linux.tar.xz", |
| 1339 | + mime_type: "application/x-xz", |
| 1340 | + size: 10_029_824, |
| 1341 | + sha256: "cafe", |
| 1342 | + }], |
| 1343 | + }; |
| 1344 | + let bytes = rmp_serde::to_vec_named(&payload).unwrap(); |
| 1345 | + let decoded: ReleasePayload = rmp_serde::from_slice(&bytes).unwrap(); |
| 1346 | + assert_eq!(decoded.tag, "v0.2.0"); |
| 1347 | + let asset = asset_for(&decoded, "x86_64-linux").unwrap(); |
| 1348 | + assert_eq!(asset.size, 10_029_824); |
| 1349 | + assert!(asset_for(&decoded, "aarch64-linux").is_none()); |
| 1350 | + } |
| 1351 | + |
| 1352 | + /// A release payload without assets (an older server, or a release |
| 1353 | + /// published without them) selects nothing, so discovery falls back. |
| 1354 | + #[test] |
| 1355 | + fn release_payload_without_assets_selects_nothing() { |
| 1356 | + let bytes = rmp_serde::to_vec_named(&{ |
| 1357 | + let mut m = std::collections::BTreeMap::new(); |
| 1358 | + m.insert("tag", "v0.0.9"); |
| 1359 | + m |
| 1360 | + }) |
| 1361 | + .unwrap(); |
| 1362 | + let decoded: ReleasePayload = rmp_serde::from_slice(&bytes).unwrap(); |
| 1363 | + assert!(asset_for(&decoded, "x86_64-linux").is_none()); |
| 1364 | + } |
| 1365 | + |
| 1366 | + /// The `--check` label names the run for an artifact, "release" for a |
| 1367 | + /// real one. |
| 1368 | + #[test] |
| 1369 | + fn release_labels() { |
| 1370 | + let artifact = Release { |
| 1371 | + tag: "v0.1.0".into(), |
| 1372 | + source: Source::Artifact { |
| 1373 | + run: 497, |
| 1374 | + artifact: Artifact { |
| 1375 | + id: 1, |
| 1376 | + name: "louise-v0.1.0-x86_64-linux".into(), |
| 1377 | + size: 10, |
| 1378 | + sha256: None, |
| 1379 | + }, |
| 1380 | + }, |
| 1381 | + }; |
| 1382 | + assert_eq!(artifact.label(), "run 497"); |
| 1383 | + let asset = Release { |
| 1384 | + tag: "v0.1.0".into(), |
| 1385 | + source: Source::Asset { |
| 1386 | + name: "louise-v0.1.0-x86_64-linux".into(), |
| 1387 | + size: 10, |
| 1388 | + sha256: "cafe".into(), |
| 1389 | + }, |
| 1390 | + }; |
| 1391 | + assert_eq!(asset.label(), "release"); |
| 1392 | + } |
| 1152 | 1393 | } |