Skip to content

Commit

Permalink
WIP: pretty status general framework
Browse files Browse the repository at this point in the history
The current proposed framework for including the human-readable status
is to create a new `PrettyBootEntry` struct that returns a subset of the
BootEntry fields. Both the original `BootEntry` and new `PrettyBootEntry`
structs will be returned from the `boot_entry_from_deployment()`
function and a conditional argument will be used to determine which
of the two to use. We've opted to create a new Struct since it provides
more flexiblity if we decide to change what fields the human-readable
status includes. It also allows us to have the conditional check for
the `pretty` flag to be outside the `boot_entry_from_deployment()`
function.

Co-authored-by: Joseph Marrero <jmarrero@redhat.com>
Co-authored-by: Huijing Hei <hhei@redhat.com>
Co-authored-by: Yasmin de Souza <ydesouza@redhat.com>
Co-authored-by: Steven Presti <spresti@redhat.com>

Signed-off-by: Luke Yang <luyang@redhat.com>
  • Loading branch information
lukewarmtemp committed Jun 13, 2024
1 parent 1c5ad72 commit fe49f2f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ pub(crate) struct StatusOpts {
/// Only display status for the booted deployment.
#[clap(long)]
pub(crate) booted: bool,

/// Condense status into most important info, to reduce eye fatigue
#[clap(long)]
pub(crate) pretty: bool,

}

/// Options for internal testing
Expand Down Expand Up @@ -731,7 +736,8 @@ fn test_parse_opts() {
Opt::parse_including_static(["bootc", "status"]),
Opt::Status(StatusOpts {
json: false,
booted: false
booted: false,
pretty: false
})
));
}
Expand Down
31 changes: 31 additions & 0 deletions lib/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ pub struct ImageStatus {
pub image_digest: String,
}

/// The status of the booted image
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct PrettyImageStatus {
/// The currently booted image
pub image: ImageReference,
/// The version string, if any
pub version: Option<String>,
/// The digest of the fetched image (e.g. sha256:a0...);
pub image_digest: String,
}

/// A bootable entry
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "camelCase")]
Expand All @@ -116,6 +128,25 @@ pub struct BootEntry {
pub ostree: Option<BootEntryOstree>,
}

/// A bootable entry
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct BootEntrys {
///
pub boot_entry: Option<BootEntry>,

///
pub pretty_boot_entry: Option<PrettyBootEntry>
}

/// A bootable entry
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct PrettyBootEntry {
/// The image reference
pub image: Option<ImageStatus>
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
Expand Down
60 changes: 57 additions & 3 deletions lib/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,52 @@ fn boot_entry_from_deployment(
Ok(r)
}

// Add a human readable output for bootc status
// image name
fn pretty_boot_entry_from_deployment(
sysroot: &SysrootLock,
deployment: &ostree::Deployment,
) -> Result<BootEntry> {
let repo = &sysroot.repo();
let (image, cached_update, incompatible) = if let Some(origin) = deployment.origin().as_ref() {
let incompatible = crate::utils::origin_has_rpmostree_stuff(origin);
let (image, cached) = if incompatible {
// If there are local changes, we can't represent it as a bootc compatible image.
(None, None)
} else if let Some(image) = get_image_origin(origin)? {
let image = ImageReference::from(image);
let csum = deployment.csum();
let imgstate = ostree_container::store::query_image_commit(repo, &csum)?;
let cached = imgstate.cached_update.map(|cached| {
create_imagestatus(image.clone(), &cached.manifest_digest, &cached.config)
});
let imagestatus =
create_imagestatus(image, &imgstate.manifest_digest, &imgstate.configuration);
// We found a container-image based deployment
(Some(imagestatus), cached)
} else {
// The deployment isn't using a container image
(None, None)
};
(image, cached, incompatible)
} else {
// The deployment has no origin at all (this generally shouldn't happen)
(None, None, false)
};
let r = BootEntry {
image,
cached_update,
incompatible,
pinned: deployment.is_pinned(),
ostree: Some(crate::spec::BootEntryOstree {
checksum: deployment.csum().into(),
// SAFETY: The deployserial is really unsigned
deploy_serial: deployment.deployserial().try_into().unwrap(),
}),
};
Ok(r)
}

impl BootEntry {
/// Given a boot entry, find its underlying ostree container image
pub(crate) fn query_image(
Expand All @@ -204,7 +250,7 @@ pub(crate) fn get_status_require_booted(
sysroot: &SysrootLock,
) -> Result<(ostree::Deployment, Deployments, Host)> {
let booted_deployment = sysroot.require_booted_deployment()?;
let (deployments, host) = get_status(sysroot, Some(&booted_deployment))?;
let (deployments, host) = get_status(sysroot, Some(&booted_deployment), false)?;
Ok((booted_deployment, deployments, host))
}

Expand All @@ -214,6 +260,7 @@ pub(crate) fn get_status_require_booted(
pub(crate) fn get_status(
sysroot: &SysrootLock,
booted_deployment: Option<&ostree::Deployment>,
pretty : bool
) -> Result<(Deployments, Host)> {
let stateroot = booted_deployment.as_ref().map(|d| d.osname());
let (mut related_deployments, other_deployments) = sysroot
Expand Down Expand Up @@ -258,7 +305,14 @@ pub(crate) fn get_status(
.context("Staged deployment")?;
let booted = booted_deployment
.as_ref()
.map(|d| boot_entry_from_deployment(sysroot, d))
.map(|d| {
let t = boot_entry_from_deployment(sysroot, d);
if pretty {
return t//bleh
}
return t //otherbleh

} )
.transpose()
.context("Booted deployment")?;
let rollback = deployments
Expand Down Expand Up @@ -308,7 +362,7 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
crate::cli::prepare_for_write().await?;
let sysroot = super::cli::get_locked_sysroot().await?;
let booted_deployment = sysroot.booted_deployment();
let (_deployments, host) = get_status(&sysroot, booted_deployment.as_ref())?;
let (_deployments, host) = get_status(&sysroot, booted_deployment.as_ref(), opts.pretty)?;
host
};

Expand Down

0 comments on commit fe49f2f

Please sign in to comment.