krata: rewrite all repo infrastructure

This commit is contained in:
Alex Zenla
2024-03-07 09:04:05 +00:00
parent baefe0dceb
commit 670e140682
39 changed files with 419 additions and 181 deletions

View File

@ -1,4 +1,9 @@
use std::{fs, path::PathBuf, str::FromStr, sync::Arc};
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};
use anyhow::{anyhow, Result};
use ipnetwork::IpNetwork;
@ -59,8 +64,9 @@ impl RuntimeContext {
image_cache_path.push("image");
fs::create_dir_all(&image_cache_path)?;
let image_cache = ImageCache::new(&image_cache_path)?;
let kernel = format!("{}/default/kernel", store);
let initrd = format!("{}/default/initrd", store);
let kernel = RuntimeContext::detect_guest_file(&store, "kernel")?;
let initrd = RuntimeContext::detect_guest_file(&store, "initrd")?;
Ok(RuntimeContext {
image_cache,
autoloop: AutoLoop::new(LoopControl::open()?),
@ -70,6 +76,15 @@ impl RuntimeContext {
})
}
fn detect_guest_file(store: &str, name: &str) -> Result<String> {
let path = PathBuf::from(format!("{}/{}", store, name));
if path.is_file() {
return path_as_string(&path);
}
Ok(format!("/usr/share/krata/guest/{}", name))
}
pub async fn list(&mut self) -> Result<Vec<GuestInfo>> {
let mut guests: Vec<GuestInfo> = Vec::new();
for domid_candidate in self.xen.store.list("/local/domain").await? {
@ -270,3 +285,9 @@ impl Runtime {
Runtime::new((*self.store).clone()).await
}
}
fn path_as_string(path: &Path) -> Result<String> {
path.to_str()
.ok_or_else(|| anyhow!("unable to convert path to string"))
.map(|x| x.to_string())
}