mirror of
https://github.com/edera-dev/krata.git
synced 2025-08-03 13:11:31 +00:00
feat: implement oci image progress (#64)
* feat: oci progress events * feat: oci progress bars on launch
This commit is contained in:
@ -31,10 +31,6 @@ name = "kratart"
|
||||
[dev-dependencies]
|
||||
env_logger = { workspace = true }
|
||||
|
||||
[[example]]
|
||||
name = "kratart-squashify"
|
||||
path = "examples/squashify.rs"
|
||||
|
||||
[[example]]
|
||||
name = "kratart-channel"
|
||||
path = "examples/channel.rs"
|
||||
|
@ -1,29 +0,0 @@
|
||||
use std::{env::args, path::PathBuf};
|
||||
|
||||
use anyhow::Result;
|
||||
use env_logger::Env;
|
||||
use krataoci::{cache::ImageCache, compiler::ImageCompiler, name::ImageName};
|
||||
use tokio::fs;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
|
||||
let image = ImageName::parse(&args().nth(1).unwrap())?;
|
||||
let seed = args().nth(2).map(PathBuf::from);
|
||||
|
||||
let cache_dir = PathBuf::from("krata-cache");
|
||||
if !cache_dir.exists() {
|
||||
fs::create_dir(&cache_dir).await?;
|
||||
}
|
||||
|
||||
let cache = ImageCache::new(&cache_dir)?;
|
||||
let compiler = ImageCompiler::new(&cache, seed)?;
|
||||
let info = compiler.compile(&image).await?;
|
||||
println!(
|
||||
"generated squashfs of {} to {}",
|
||||
image,
|
||||
info.image_squashfs.to_string_lossy()
|
||||
);
|
||||
Ok(())
|
||||
}
|
@ -9,6 +9,7 @@ use ipnetwork::{IpNetwork, Ipv4Network};
|
||||
use krata::launchcfg::{
|
||||
LaunchInfo, LaunchNetwork, LaunchNetworkIpv4, LaunchNetworkIpv6, LaunchNetworkResolver,
|
||||
};
|
||||
use krataoci::progress::OciProgressContext;
|
||||
use tokio::sync::Semaphore;
|
||||
use uuid::Uuid;
|
||||
use xenclient::{DomainChannel, DomainConfig, DomainDisk, DomainNetworkInterface};
|
||||
@ -51,7 +52,14 @@ impl GuestLauncher {
|
||||
) -> Result<GuestInfo> {
|
||||
let uuid = request.uuid.unwrap_or_else(Uuid::new_v4);
|
||||
let xen_name = format!("krata-{uuid}");
|
||||
let image_info = self.compile(request.image, &context.image_cache).await?;
|
||||
let image_info = self
|
||||
.compile(
|
||||
&uuid.to_string(),
|
||||
request.image,
|
||||
&context.image_cache,
|
||||
&context.oci_progress_context,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut gateway_mac = MacAddr6::random();
|
||||
gateway_mac.set_local(true);
|
||||
@ -243,10 +251,16 @@ impl GuestLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
async fn compile(&self, image: &str, image_cache: &ImageCache) -> Result<ImageInfo> {
|
||||
async fn compile(
|
||||
&self,
|
||||
id: &str,
|
||||
image: &str,
|
||||
image_cache: &ImageCache,
|
||||
progress: &OciProgressContext,
|
||||
) -> Result<ImageInfo> {
|
||||
let image = ImageName::parse(image)?;
|
||||
let compiler = ImageCompiler::new(image_cache, None)?;
|
||||
compiler.compile(&image).await
|
||||
let compiler = ImageCompiler::new(image_cache, None, progress.clone())?;
|
||||
compiler.compile(id, &image).await
|
||||
}
|
||||
|
||||
async fn allocate_ipv4(&self, context: &RuntimeContext) -> Result<Ipv4Addr> {
|
||||
|
@ -17,7 +17,7 @@ use self::{
|
||||
autoloop::AutoLoop,
|
||||
launch::{GuestLaunchRequest, GuestLauncher},
|
||||
};
|
||||
use krataoci::cache::ImageCache;
|
||||
use krataoci::{cache::ImageCache, progress::OciProgressContext};
|
||||
|
||||
pub mod autoloop;
|
||||
pub mod cfgblk;
|
||||
@ -51,6 +51,7 @@ pub struct GuestInfo {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RuntimeContext {
|
||||
pub oci_progress_context: OciProgressContext,
|
||||
pub image_cache: ImageCache,
|
||||
pub autoloop: AutoLoop,
|
||||
pub xen: XenClient,
|
||||
@ -59,7 +60,7 @@ pub struct RuntimeContext {
|
||||
}
|
||||
|
||||
impl RuntimeContext {
|
||||
pub async fn new(store: String) -> Result<Self> {
|
||||
pub async fn new(oci_progress_context: OciProgressContext, store: String) -> Result<Self> {
|
||||
let mut image_cache_path = PathBuf::from(&store);
|
||||
image_cache_path.push("cache");
|
||||
fs::create_dir_all(&image_cache_path)?;
|
||||
@ -72,6 +73,7 @@ impl RuntimeContext {
|
||||
let initrd = RuntimeContext::detect_guest_file(&store, "initrd")?;
|
||||
|
||||
Ok(RuntimeContext {
|
||||
oci_progress_context,
|
||||
image_cache,
|
||||
autoloop: AutoLoop::new(LoopControl::open()?),
|
||||
xen,
|
||||
@ -252,15 +254,17 @@ impl RuntimeContext {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Runtime {
|
||||
oci_progress_context: OciProgressContext,
|
||||
store: Arc<String>,
|
||||
context: RuntimeContext,
|
||||
launch_semaphore: Arc<Semaphore>,
|
||||
}
|
||||
|
||||
impl Runtime {
|
||||
pub async fn new(store: String) -> Result<Self> {
|
||||
let context = RuntimeContext::new(store.clone()).await?;
|
||||
pub async fn new(oci_progress_context: OciProgressContext, store: String) -> Result<Self> {
|
||||
let context = RuntimeContext::new(oci_progress_context.clone(), store.clone()).await?;
|
||||
Ok(Self {
|
||||
oci_progress_context,
|
||||
store: Arc::new(store),
|
||||
context,
|
||||
launch_semaphore: Arc::new(Semaphore::new(1)),
|
||||
@ -324,7 +328,7 @@ impl Runtime {
|
||||
}
|
||||
|
||||
pub async fn dupe(&self) -> Result<Runtime> {
|
||||
Runtime::new((*self.store).clone()).await
|
||||
Runtime::new(self.oci_progress_context.clone(), (*self.store).clone()).await
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user