feat: image pull is now internally explicit

This commit is contained in:
Alex Zenla
2024-04-15 06:50:53 +00:00
parent 5511fb2fa1
commit fdc8f4d634
23 changed files with 532 additions and 386 deletions

View File

@ -38,9 +38,9 @@ async fn main() -> Result<()> {
}
});
let context = OciProgressContext::new(sender);
let service = OciPackerService::new(seed, &cache_dir, OciPlatform::current(), context)?;
let service = OciPackerService::new(seed, &cache_dir, OciPlatform::current())?;
let packed = service
.pack("cli", image.clone(), OciPackedFormat::Squashfs)
.request(image.clone(), OciPackedFormat::Squashfs, context)
.await?;
println!(
"generated squashfs of {} to {}",

View File

@ -1,7 +1,4 @@
use crate::{
fetch::OciResolvedImage,
packer::{OciImagePacked, OciPackedFormat},
};
use crate::packer::{OciImagePacked, OciPackedFormat};
use anyhow::Result;
use log::debug;
@ -23,15 +20,15 @@ impl OciPackerCache {
pub async fn recall(
&self,
resolved: &OciResolvedImage,
digest: &str,
format: OciPackedFormat,
) -> Result<Option<OciImagePacked>> {
let mut fs_path = self.cache_dir.clone();
let mut config_path = self.cache_dir.clone();
let mut manifest_path = self.cache_dir.clone();
fs_path.push(format!("{}.{}", resolved.digest, format.extension()));
manifest_path.push(format!("{}.manifest.json", resolved.digest));
config_path.push(format!("{}.config.json", resolved.digest));
fs_path.push(format!("{}.{}", digest, format.extension()));
manifest_path.push(format!("{}.manifest.json", digest));
config_path.push(format!("{}.config.json", digest));
Ok(
if fs_path.exists() && manifest_path.exists() && config_path.exists() {
let image_metadata = fs::metadata(&fs_path).await?;
@ -45,9 +42,9 @@ impl OciPackerCache {
let manifest: ImageManifest = serde_json::from_str(&manifest_text)?;
let config_text = fs::read_to_string(&config_path).await?;
let config: ImageConfiguration = serde_json::from_str(&config_text)?;
debug!("cache hit digest={}", resolved.digest);
debug!("cache hit digest={}", digest);
Some(OciImagePacked::new(
resolved.digest.clone(),
digest.to_string(),
fs_path.clone(),
format,
config,
@ -57,7 +54,7 @@ impl OciPackerCache {
None
}
} else {
debug!("cache miss digest={}", resolved.digest);
debug!("cache miss digest={}", digest);
None
},
)

View File

@ -17,7 +17,6 @@ pub struct OciPackerService {
seed: Option<PathBuf>,
platform: OciPlatform,
cache: OciPackerCache,
progress: OciProgressContext,
}
impl OciPackerService {
@ -25,28 +24,34 @@ impl OciPackerService {
seed: Option<PathBuf>,
cache_dir: &Path,
platform: OciPlatform,
progress: OciProgressContext,
) -> Result<OciPackerService> {
Ok(OciPackerService {
seed,
cache: OciPackerCache::new(cache_dir)?,
platform,
progress,
})
}
pub async fn pack(
pub async fn recall(
&self,
digest: &str,
format: OciPackedFormat,
) -> Result<Option<OciImagePacked>> {
self.cache.recall(digest, format).await
}
pub async fn request(
&self,
id: &str,
name: ImageName,
format: OciPackedFormat,
progress_context: OciProgressContext,
) -> Result<OciImagePacked> {
let progress = OciProgress::new(id);
let progress = OciBoundProgress::new(self.progress.clone(), progress);
let progress = OciProgress::new();
let progress = OciBoundProgress::new(progress_context.clone(), progress);
let fetcher =
OciImageFetcher::new(self.seed.clone(), self.platform.clone(), progress.clone());
let resolved = fetcher.resolve(name).await?;
if let Some(cached) = self.cache.recall(&resolved, format).await? {
if let Some(cached) = self.cache.recall(&resolved.digest, format).await? {
return Ok(cached);
}
let assembler =

View File

@ -5,17 +5,21 @@ use tokio::sync::{broadcast::Sender, Mutex};
#[derive(Clone, Debug)]
pub struct OciProgress {
pub id: String,
pub phase: OciProgressPhase,
pub layers: IndexMap<String, OciProgressLayer>,
pub value: u64,
pub total: u64,
}
impl Default for OciProgress {
fn default() -> Self {
Self::new()
}
}
impl OciProgress {
pub fn new(id: &str) -> Self {
pub fn new() -> Self {
OciProgress {
id: id.to_string(),
phase: OciProgressPhase::Resolving,
layers: IndexMap::new(),
value: 0,