Skip to content

Commit c5f80c9

Browse files
authored
Merge pull request #798 from cgwalters/status-tweaks
Status tweaks
2 parents 9f2e0fc + 11508eb commit c5f80c9

File tree

2 files changed

+89
-30
lines changed

2 files changed

+89
-30
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: org.containers.bootc/v1alpha1
2+
kind: BootcHost
3+
metadata:
4+
name: host
5+
spec:
6+
image:
7+
image: /var/mnt/osupdate:latest
8+
transport: oci
9+
bootOrder: default
10+
status:
11+
staged: null
12+
booted:
13+
image:
14+
image:
15+
image: /var/mnt/osupdate
16+
transport: oci
17+
version: stream9.20240807.0
18+
timestamp: null
19+
imageDigest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38
20+
cachedUpdate: null
21+
incompatible: false
22+
pinned: false
23+
ostree:
24+
checksum: 439f6bd2e2361bee292c1f31840d798c5ac5ba76483b8021dc9f7b0164ac0f48
25+
deploySerial: 0
26+
rollback: null
27+
rollbackQueued: false
28+
type: bootcHost

lib/src/status.rs

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::collections::VecDeque;
23
use std::io::IsTerminal;
34
use std::io::Write;
@@ -324,40 +325,62 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
324325
Ok(())
325326
}
326327

328+
/// Write the data for a container image based status.
329+
fn human_render_imagestatus(
330+
mut out: impl Write,
331+
slot_name: &str,
332+
image: &crate::spec::ImageStatus,
333+
) -> Result<()> {
334+
let transport = &image.image.transport;
335+
let imagename = &image.image.image;
336+
// Registry is the default, so don't show that
337+
let imageref = if transport == "registry" {
338+
Cow::Borrowed(imagename)
339+
} else {
340+
// But for non-registry we include the transport
341+
Cow::Owned(format!("{transport}:{imagename}"))
342+
};
343+
writeln!(out, "Current {slot_name} image: {imageref}")?;
344+
345+
let version = image
346+
.version
347+
.as_deref()
348+
.unwrap_or("No image version defined");
349+
let timestamp = image
350+
.timestamp
351+
.as_ref()
352+
.map(|t| t.to_string())
353+
.unwrap_or_else(|| "No timestamp present".to_owned());
354+
let digest = &image.image_digest;
355+
356+
writeln!(out, " Image version: {version} ({timestamp})")?;
357+
writeln!(out, " Image digest: {digest}")?;
358+
Ok(())
359+
}
360+
361+
fn human_render_ostree(mut out: impl Write, slot_name: &str, _ostree_commit: &str) -> Result<()> {
362+
// TODO consider rendering more ostree stuff here like rpm-ostree status does
363+
writeln!(out, "Current {slot_name} state is native ostree")?;
364+
Ok(())
365+
}
366+
367+
/// Implementation of rendering our host structure in a "human readable" way.
327368
fn human_readable_output(mut out: impl Write, host: &Host) -> Result<()> {
328-
for (status_string, status) in [
369+
for (slot_name, status) in [
329370
("staged", &host.status.staged),
330371
("booted", &host.status.booted),
331372
("rollback", &host.status.rollback),
332373
] {
333374
if let Some(host_status) = status {
334375
if let Some(image) = &host_status.image {
335-
writeln!(
336-
out,
337-
"Current {} image: {}",
338-
status_string, image.image.image
339-
)?;
340-
341-
let version = image
342-
.version
343-
.as_deref()
344-
.unwrap_or("No image version defined");
345-
let timestamp = image
346-
.timestamp
347-
.as_ref()
348-
.map(|t| t.to_string())
349-
.unwrap_or_else(|| "No timestamp present".to_owned());
350-
let transport = &image.image.transport;
351-
let digest = &image.image_digest;
352-
353-
writeln!(out, " Image version: {version} ({timestamp})")?;
354-
writeln!(out, " Image transport: {transport}")?;
355-
writeln!(out, " Image digest: {digest}")?;
376+
human_render_imagestatus(&mut out, slot_name, image)?;
377+
} else if let Some(ostree) = host_status.ostree.as_ref() {
378+
human_render_ostree(&mut out, slot_name, &ostree.checksum)?;
356379
} else {
357-
writeln!(out, "Current {status_string} state is native ostree")?;
380+
writeln!(out, "Current {slot_name} state is unknown")?;
358381
}
359382
} else {
360-
writeln!(out, "No {status_string} image present")?;
383+
writeln!(out, "No {slot_name} image present")?;
361384
}
362385
}
363386
Ok(())
@@ -383,11 +406,9 @@ mod tests {
383406
let expected = indoc::indoc! { r"
384407
Current staged image: quay.io/example/someimage:latest
385408
Image version: nightly (2023-10-14 19:22:15 UTC)
386-
Image transport: registry
387409
Image digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566
388410
Current booted image: quay.io/example/someimage:latest
389411
Image version: nightly (2023-09-30 19:22:16 UTC)
390-
Image transport: registry
391412
Image digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34
392413
No rollback image present
393414
"};
@@ -417,7 +438,6 @@ mod tests {
417438
let expected = indoc::indoc! { r"
418439
Current staged image: quay.io/centos-bootc/centos-bootc:stream9
419440
Image version: stream9.20240807.0 (No timestamp present)
420-
Image transport: registry
421441
Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38
422442
Current booted state is native ostree
423443
No rollback image present
@@ -434,7 +454,6 @@ mod tests {
434454
No staged image present
435455
Current booted image: quay.io/centos-bootc/centos-bootc:stream9
436456
Image version: stream9.20240807.0 (No timestamp present)
437-
Image transport: registry
438457
Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38
439458
No rollback image present
440459
"};
@@ -449,17 +468,29 @@ mod tests {
449468
let expected = indoc::indoc! { r"
450469
Current staged image: quay.io/example/someimage:latest
451470
Image version: nightly (2023-10-14 19:22:15 UTC)
452-
Image transport: registry
453471
Image digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566
454472
No booted image present
455473
Current rollback image: quay.io/example/someimage:latest
456474
Image version: nightly (2023-09-30 19:22:16 UTC)
457-
Image transport: registry
458475
Image digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34
459476
"};
460477
similar_asserts::assert_eq!(w, expected);
461478
}
462479

480+
#[test]
481+
fn test_via_oci() {
482+
let w = human_status_from_spec_fixture(include_str!("fixtures/spec-via-local-oci.yaml"))
483+
.unwrap();
484+
let expected = indoc::indoc! { r"
485+
No staged image present
486+
Current booted image: oci:/var/mnt/osupdate
487+
Image version: stream9.20240807.0 (No timestamp present)
488+
Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38
489+
No rollback image present
490+
"};
491+
similar_asserts::assert_eq!(w, expected);
492+
}
493+
463494
#[test]
464495
fn test_convert_signatures() {
465496
use std::str::FromStr;

0 commit comments

Comments
 (0)