Skip to content

Commit

Permalink
ostree-ext: Add image clear-cached-update
Browse files Browse the repository at this point in the history
This is effectively an internals command for us to aid
debugging.

At some point it may make sense to expose this
at the bootc level too.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Feb 3, 2025
1 parent 63f49d3 commit 71f8346
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ostree-ext/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ pub(crate) enum ContainerImageOpts {
config: bool,
},

/// Remove metadata for a cached update.
ClearCachedUpdate {
/// Path to the repository
#[clap(long, value_parser)]
repo: Utf8PathBuf,

/// Container image reference, e.g. registry:quay.io/exampleos/exampleos:latest
#[clap(value_parser = parse_base_imgref)]
imgref: ImageReference,
},

/// Copy a pulled container image from one repo to another.
Copy {
/// Path to the source repository
Expand Down Expand Up @@ -1132,6 +1143,11 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
stdout.flush()?;
Ok(())
}
ContainerImageOpts::ClearCachedUpdate { repo, imgref } => {
let repo = parse_repo(&repo)?;
crate::container::store::clear_cached_update(&repo, &imgref)?;
Ok(())
}
ContainerImageOpts::Remove {
repo,
imgrefs,
Expand Down
34 changes: 33 additions & 1 deletion ostree-ext/src/container/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ use futures_util::TryFutureExt;
use oci_spec::image::{
self as oci_image, Arch, Descriptor, Digest, History, ImageConfiguration, ImageManifest,
};
use ostree::glib::FromVariant;
use ostree::prelude::{Cast, FileEnumeratorExt, FileExt, ToVariant};
use ostree::{gio, glib};
use std::collections::{BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::iter::FromIterator;
use tokio::sync::mpsc::{Receiver, Sender};

Expand Down Expand Up @@ -1196,6 +1197,37 @@ fn parse_cached_update(meta: &glib::VariantDict) -> Result<Option<CachedImageUpd
}))
}

/// Remove any cached
#[context("Clearing cached update {imgref}")]
pub fn clear_cached_update(repo: &ostree::Repo, imgref: &ImageReference) -> Result<()> {
let cancellable = gio::Cancellable::NONE;
let ostree_ref = ref_for_image(imgref)?;
let rev = repo.require_rev(&ostree_ref)?;
let Some(commitmeta) = repo.read_commit_detached_metadata(&rev, cancellable)? else {
return Ok(());
};

// SAFETY: We know this is an a{sv}
let mut commitmeta: BTreeMap<String, glib::Variant> =
BTreeMap::from_variant(&commitmeta).unwrap();
let mut changed = false;
for key in [
ImageImporter::CACHED_KEY_CONFIG,
ImageImporter::CACHED_KEY_MANIFEST,
ImageImporter::CACHED_KEY_MANIFEST_DIGEST,
] {
if commitmeta.remove(key).is_some() {
changed = true;
}
}
if !changed {
return Ok(());
}
let commitmeta = glib::Variant::from(commitmeta);
repo.write_commit_detached_metadata(&rev, Some(&commitmeta), cancellable)?;
Ok(())
}

/// Query metadata for a pulled image via an OSTree commit digest.
/// The digest must refer to a pulled container image's merge commit.
pub fn query_image_commit(repo: &ostree::Repo, commit: &str) -> Result<Box<LayeredImageState>> {
Expand Down

0 comments on commit 71f8346

Please sign in to comment.