Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #499 from lukewarmtemp/add-previous-state-manifest
Browse files Browse the repository at this point in the history
Add previous container image state
  • Loading branch information
cgwalters authored Jul 27, 2023
2 parents 002e92f + c7ae1a0 commit ea853c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
6 changes: 5 additions & 1 deletion lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tokio::sync::mpsc::Receiver;

use crate::commit::container_commit;
use crate::container::store::{ImportProgress, LayerProgress, PreparedImport};
use crate::container::{self as ostree_container};
use crate::container::{self as ostree_container, ManifestDiff};
use crate::container::{Config, ImageReference, OstreeImageReference};
use crate::sysroot::SysrootLock;
use ostree_container::store::{ImageImporter, PrepareResult};
Expand Down Expand Up @@ -666,6 +666,10 @@ async fn container_store(
if let Some(warning) = prep.deprecated_warning() {
print_deprecated_warning(warning).await;
}
if let Some(previous_state) = prep.previous_state.as_ref() {
let diff = ManifestDiff::new(&previous_state.manifest, &prep.manifest);
diff.print();
}
print_layer_status(&prep);
let printer = (!quiet).then(|| {
let layer_progress = imp.request_progress();
Expand Down
51 changes: 37 additions & 14 deletions lib/src/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ pub struct ManifestDiff<'a> {
pub removed: Vec<&'a oci_spec::image::Descriptor>,
/// Layers which are present in the new image but not the old image.
pub added: Vec<&'a oci_spec::image::Descriptor>,
/// Total number of packages
pub total: u64,
/// Size of total number of packages.
pub total_size: u64,
/// Number of packages removed
pub n_removed: u64,
/// Size of the number of packages removed
pub removed_size: u64,
/// Number of packages added
pub n_added: u64,
/// Size of the number of packages added
pub added_size: u64,
}

impl<'a> ManifestDiff<'a> {
Expand Down Expand Up @@ -314,32 +326,43 @@ impl<'a> ManifestDiff<'a> {
}
}
added.sort_by(|a, b| a.digest().cmp(b.digest()));

fn layersum<'a, I: Iterator<Item = &'a oci_spec::image::Descriptor>>(layers: I) -> u64 {
layers.map(|layer| layer.size() as u64).sum()
}
let total = dest_layers.len() as u64;
let total_size = layersum(dest.layers().iter());
let n_removed = removed.len() as u64;
let n_added = added.len() as u64;
let removed_size = layersum(removed.iter().copied());
let added_size = layersum(added.iter().copied());
ManifestDiff {
from: src,
to: dest,
removed,
added,
total,
total_size,
n_removed,
removed_size,
n_added,
added_size,
}
}
}

impl<'a> ManifestDiff<'a> {
/// Prints the total, removed and added content between two OCI images
pub fn print(&self) {
fn layersum<'a, I: Iterator<Item = &'a oci_spec::image::Descriptor>>(layers: I) -> u64 {
layers.map(|layer| layer.size() as u64).sum()
}
let new_total = self.to.layers().len();
let new_total_size = glib::format_size(layersum(self.to.layers().iter()));
let n_removed = self.removed.len();
let n_added = self.added.len();
let removed_size = layersum(self.removed.iter().copied());
let removed_size_str = glib::format_size(removed_size);
let added_size = layersum(self.added.iter().copied());
let added_size_str = glib::format_size(added_size);
println!("Total new layers: {new_total:<4} Size: {new_total_size}");
println!("Removed layers: {n_removed:<4} Size: {removed_size_str}");
println!("Added layers: {n_added:<4} Size: {added_size_str}");
let print_total = self.total;
let print_total_size = glib::format_size(self.total_size);
let print_n_removed = self.n_removed;
let print_removed_size = glib::format_size(self.removed_size);
let print_n_added = self.n_added;
let print_added_size = glib::format_size(self.added_size);
println!("Total new layers: {print_total:<4} Size: {print_total_size}");
println!("Removed layers: {print_n_removed:<4} Size: {print_removed_size}");
println!("Added layers: {print_n_added:<4} Size: {print_added_size}");
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/src/container/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ pub struct PreparedImport {
pub manifest: oci_image::ImageManifest,
/// The deserialized configuration.
pub config: oci_image::ImageConfiguration,
/// The previous manifest
pub previous_state: Option<Box<LayeredImageState>>,
/// The previously stored manifest digest.
pub previous_manifest_digest: Option<String>,
/// The previously stored image ID.
Expand Down Expand Up @@ -535,7 +537,7 @@ impl ImageImporter {

// Query for previous stored state

let (previous_manifest_digest, previous_imageid) =
let (previous_state, previous_imageid) =
if let Some(previous_state) = try_query_image_ref(&self.repo, &self.imgref.imgref)? {
// If the manifest digests match, we're done.
if previous_state.manifest_digest == manifest_digest {
Expand All @@ -546,10 +548,8 @@ impl ImageImporter {
if previous_imageid == new_imageid {
return Ok(PrepareResult::AlreadyPresent(previous_state));
}
(
Some(previous_state.manifest_digest),
Some(previous_imageid.to_string()),
)
let previous_imageid = previous_imageid.to_string();
(Some(previous_state), Some(previous_imageid))
} else {
(None, None)
};
Expand All @@ -570,10 +570,12 @@ impl ImageImporter {
.map(query)
.collect::<Result<Vec<_>>>()?;

let previous_manifest_digest = previous_state.as_ref().map(|s| s.manifest_digest.clone());
let imp = PreparedImport {
manifest,
manifest_digest,
config,
previous_state,
previous_manifest_digest,
previous_imageid,
ostree_layers: component_layers,
Expand Down

0 comments on commit ea853c0

Please sign in to comment.