hypha: implement image cache

This commit is contained in:
Alex Zenla 2024-01-18 00:02:21 -08:00
parent 9c438e8e57
commit 1c92ba54f3
No known key found for this signature in database
GPG Key ID: 067B238899B51269
6 changed files with 149 additions and 25 deletions

View File

@ -12,7 +12,11 @@ log = "0.4.20"
env_logger = "0.10.1" env_logger = "0.10.1"
flate2 = "1.0.28" flate2 = "1.0.28"
tar = "0.4.40" tar = "0.4.40"
directories = "5.0.1"
walkdir = "2" walkdir = "2"
serde = "1.0.195"
serde_json = "1.0.111"
sha256 = "1.5.0"
[dependencies.clap] [dependencies.clap]
version = "4.4.18" version = "4.4.18"

View File

@ -1,6 +1,6 @@
use clap::Parser; use clap::Parser;
use hypha::ctl::Controller; use hypha::ctl::Controller;
use hypha::error::Result; use hypha::error::{HyphaError, Result};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about)] #[command(version, about)]
@ -19,16 +19,38 @@ struct ControllerArgs {
#[arg(short, long, default_value_t = 512)] #[arg(short, long, default_value_t = 512)]
mem: u64, mem: u64,
#[arg(short = 'C', long, default_value = "auto")]
cache: String,
} }
fn main() -> Result<()> { fn main() -> Result<()> {
env_logger::init(); env_logger::init();
let args = ControllerArgs::parse(); let args = ControllerArgs::parse();
let mut controller = let cache_path = if args.cache == "auto" {
Controller::new(args.kernel, args.initrd, args.image, args.cpus, args.mem)?; default_cache_path()
controller.compile()?; .ok_or_else(|| HyphaError::new("unable to determine default cache path"))
} else {
Ok(args.cache)
}?;
let mut controller = Controller::new(
cache_path,
args.kernel,
args.initrd,
args.image,
args.cpus,
args.mem,
)?;
let domid = controller.launch()?; let domid = controller.launch()?;
println!("launched domain: {}", domid); println!("launched domain: {}", domid);
Ok(()) Ok(())
} }
fn default_cache_path() -> Option<String> {
let user_dirs = directories::UserDirs::new()?;
let mut path = user_dirs.home_dir().to_path_buf();
path.push(".hypha/cache");
Some(path.to_str()?.to_string())
}

View File

@ -1,44 +1,56 @@
use crate::error::Result; use crate::error::Result;
use crate::image::ImageCompiler; use crate::image::cache::ImageCache;
use crate::image::{ImageCompiler, ImageInfo};
use ocipkg::ImageName; use ocipkg::ImageName;
use std::fs;
use std::path::PathBuf;
use xenclient::{DomainConfig, XenClient}; use xenclient::{DomainConfig, XenClient};
pub struct Controller { pub struct Controller {
image_cache: ImageCache,
image: String,
client: XenClient, client: XenClient,
kernel_path: String, kernel_path: String,
initrd_path: String, initrd_path: String,
vcpus: u32, vcpus: u32,
mem: u64, mem: u64,
image: String,
} }
impl Controller { impl Controller {
pub fn new( pub fn new(
cache_path: String,
kernel_path: String, kernel_path: String,
initrd_path: String, initrd_path: String,
image: String, image: String,
vcpus: u32, vcpus: u32,
mem: u64, mem: u64,
) -> Result<Controller> { ) -> Result<Controller> {
fs::create_dir_all(&cache_path)?;
let client = XenClient::open()?; let client = XenClient::open()?;
let mut image_cache_path = PathBuf::from(cache_path);
image_cache_path.push("image");
fs::create_dir_all(&image_cache_path)?;
let image_cache = ImageCache::new(&image_cache_path)?;
Ok(Controller { Ok(Controller {
image_cache,
image,
client, client,
kernel_path, kernel_path,
initrd_path, initrd_path,
image,
vcpus, vcpus,
mem, mem,
}) })
} }
pub fn compile(&mut self) -> Result<()> { fn compile(&mut self) -> Result<ImageInfo> {
let image = ImageName::parse(&self.image)?; let image = ImageName::parse(&self.image)?;
let compiler = ImageCompiler::new()?; let compiler = ImageCompiler::new(&self.image_cache)?;
let _squashfs = compiler.compile(&image)?; compiler.compile(&image)
Ok(())
} }
pub fn launch(&mut self) -> Result<u32> { pub fn launch(&mut self) -> Result<u32> {
let _image_info = self.compile()?;
let config = DomainConfig { let config = DomainConfig {
max_vcpus: self.vcpus, max_vcpus: self.vcpus,
mem_mb: self.mem, mem_mb: self.mem,

View File

@ -66,3 +66,9 @@ impl From<BackhandError> for HyphaError {
HyphaError::new(value.to_string().as_str()) HyphaError::new(value.to_string().as_str())
} }
} }
impl From<serde_json::Error> for HyphaError {
fn from(value: serde_json::Error) -> Self {
HyphaError::new(value.to_string().as_str())
}
}

51
hypha/src/image/cache.rs Normal file
View File

@ -0,0 +1,51 @@
use crate::image::{ImageInfo, Result};
use log::debug;
use oci_spec::image::ImageManifest;
use std::fs;
use std::path::{Path, PathBuf};
pub struct ImageCache {
cache_dir: PathBuf,
}
impl ImageCache {
pub fn new(cache_dir: &Path) -> Result<ImageCache> {
Ok(ImageCache {
cache_dir: cache_dir.to_path_buf(),
})
}
pub fn recall(&self, digest: &str) -> Result<Option<ImageInfo>> {
let mut squashfs_path = self.cache_dir.clone();
let mut manifest_path = self.cache_dir.clone();
squashfs_path.push(format!("{}.squashfs", digest));
manifest_path.push(format!("{}.json", digest));
Ok(if squashfs_path.exists() && manifest_path.exists() {
let squashfs_metadata = fs::metadata(&squashfs_path)?;
let manifest_metadata = fs::metadata(&manifest_path)?;
if squashfs_metadata.is_file() && manifest_metadata.is_file() {
let manifest_text = fs::read_to_string(&manifest_path)?;
let manifest: ImageManifest = serde_json::from_str(manifest_text.as_str())?;
debug!("cache hit digest={}", digest);
Some(ImageInfo::new(squashfs_path.clone(), manifest)?)
} else {
None
}
} else {
debug!("cache miss digest={}", digest);
None
})
}
pub fn store(&self, digest: &str, info: &ImageInfo) -> Result<ImageInfo> {
debug!("cache store digest={}", digest);
let mut squashfs_path = self.cache_dir.clone();
let mut manifest_path = self.cache_dir.clone();
squashfs_path.push(format!("{}.squashfs", digest));
manifest_path.push(format!("{}.json", digest));
fs::copy(&info.squashfs, &squashfs_path)?;
let manifest_text = serde_json::to_string_pretty(&info.manifest)?;
fs::write(&manifest_path, manifest_text)?;
ImageInfo::new(squashfs_path.clone(), info.manifest.clone())
}
}

View File

@ -1,7 +1,10 @@
pub mod cache;
use crate::error::{HyphaError, Result}; use crate::error::{HyphaError, Result};
use crate::image::cache::ImageCache;
use backhand::{FilesystemWriter, NodeHeader}; use backhand::{FilesystemWriter, NodeHeader};
use log::{debug, trace}; use log::{debug, trace};
use oci_spec::image::MediaType; use oci_spec::image::{ImageManifest, MediaType};
use ocipkg::distribution::Client; use ocipkg::distribution::Client;
use ocipkg::error::Error; use ocipkg::error::Error;
use ocipkg::{Digest, ImageName}; use ocipkg::{Digest, ImageName};
@ -13,14 +16,27 @@ use std::path::PathBuf;
use uuid::Uuid; use uuid::Uuid;
use walkdir::WalkDir; use walkdir::WalkDir;
pub struct ImageCompiler {} pub struct ImageInfo {
pub squashfs: PathBuf,
pub manifest: ImageManifest,
}
impl ImageCompiler { impl ImageInfo {
pub fn new() -> Result<ImageCompiler> { fn new(squashfs: PathBuf, manifest: ImageManifest) -> Result<ImageInfo> {
Ok(ImageCompiler {}) Ok(ImageInfo { squashfs, manifest })
}
}
pub struct ImageCompiler<'a> {
cache: &'a ImageCache,
}
impl ImageCompiler<'_> {
pub fn new(cache: &ImageCache) -> Result<ImageCompiler> {
Ok(ImageCompiler { cache })
} }
pub fn compile(&self, image: &ImageName) -> Result<String> { pub fn compile(&self, image: &ImageName) -> Result<ImageInfo> {
debug!("ImageCompiler compile image={image}"); debug!("ImageCompiler compile image={image}");
let mut tmp_dir = std::env::temp_dir().clone(); let mut tmp_dir = std::env::temp_dir().clone();
tmp_dir.push(format!("hypha-compile-{}", Uuid::new_v4())); tmp_dir.push(format!("hypha-compile-{}", Uuid::new_v4()));
@ -29,11 +45,17 @@ impl ImageCompiler {
fs::create_dir_all(&image_dir)?; fs::create_dir_all(&image_dir)?;
let mut squash_file = tmp_dir.clone(); let mut squash_file = tmp_dir.clone();
squash_file.push("image.squashfs"); squash_file.push("image.squashfs");
self.download(image, &image_dir)?; let info = self.download_and_compile(image, &image_dir, &squash_file)?;
self.squash(&image_dir, &squash_file) fs::remove_dir_all(tmp_dir)?;
Ok(info)
} }
fn download(&self, image: &ImageName, image_dir: &PathBuf) -> Result<()> { fn download_and_compile(
&self,
image: &ImageName,
image_dir: &PathBuf,
squash_file: &PathBuf,
) -> Result<ImageInfo> {
debug!( debug!(
"ImageCompiler download image={image}, image_dir={}", "ImageCompiler download image={image}, image_dir={}",
image_dir.to_str().unwrap() image_dir.to_str().unwrap()
@ -43,12 +65,18 @@ impl ImageCompiler {
} = image; } = image;
let mut client = Client::new(image.registry_url()?, name.clone())?; let mut client = Client::new(image.registry_url()?, name.clone())?;
let manifest = client.get_manifest(reference)?; let manifest = client.get_manifest(reference)?;
let manifest_serialized = serde_json::to_string(&manifest)?;
let manifest_digest = sha256::digest(manifest_serialized);
if let Some(cached) = self.cache.recall(&manifest_digest)? {
return Ok(cached);
}
for layer in manifest.layers() { for layer in manifest.layers() {
debug!( debug!(
"ImageCompiler download start digest={} size={}", "ImageCompiler download start digest={} size={}",
layer.digest(), layer.digest(),
layer.size() layer.size()
); );
let blob = client.get_blob(&Digest::new(layer.digest())?)?; let blob = client.get_blob(&Digest::new(layer.digest())?)?;
match layer.media_type() { match layer.media_type() {
MediaType::ImageLayerGzip => {} MediaType::ImageLayerGzip => {}
@ -71,12 +99,14 @@ impl ImageCompiler {
layer.digest(), layer.digest(),
layer.size() layer.size()
); );
return Ok(()); self.squash(image_dir, squash_file)?;
let info = ImageInfo::new(squash_file.clone(), manifest.clone())?;
return self.cache.store(&manifest_digest, &info);
} }
Err(Error::MissingLayer.into()) Err(Error::MissingLayer.into())
} }
fn squash(&self, image_dir: &PathBuf, squash_file: &PathBuf) -> Result<String> { fn squash(&self, image_dir: &PathBuf, squash_file: &PathBuf) -> Result<()> {
let mut writer = FilesystemWriter::default(); let mut writer = FilesystemWriter::default();
let walk = WalkDir::new(image_dir).follow_links(false); let walk = WalkDir::new(image_dir).follow_links(false);
for entry in walk { for entry in walk {
@ -137,9 +167,8 @@ impl ImageCompiler {
.ok_or_else(|| HyphaError::new("failed to convert squashfs string"))?; .ok_or_else(|| HyphaError::new("failed to convert squashfs string"))?;
let mut out = File::create(squash_file)?; let mut out = File::create(squash_file)?;
trace!("ImageCompiler squash generateI : {}", squash_file_path); trace!("ImageCompiler squash generate: {}", squash_file_path);
writer.write(&mut out)?; writer.write(&mut out)?;
Ok(())
Ok(squash_file_path.to_string())
} }
} }