Files
krata/controller/src/ctl/cfgblk.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2024-01-18 14:52:35 -08:00
use crate::image::ImageInfo;
use anyhow::Result;
2024-01-18 14:52:35 -08:00
use backhand::{FilesystemWriter, NodeHeader};
use hypha::LaunchInfo;
2024-01-22 21:28:14 -08:00
use log::trace;
2024-01-18 14:52:35 -08:00
use std::fs;
use std::fs::File;
use std::path::PathBuf;
use uuid::Uuid;
pub struct ConfigBlock<'a> {
pub image_info: &'a ImageInfo,
pub file: PathBuf,
pub dir: PathBuf,
2024-01-18 14:52:35 -08:00
}
impl ConfigBlock<'_> {
pub fn new<'a>(uuid: &Uuid, image_info: &'a ImageInfo) -> Result<ConfigBlock<'a>> {
let mut dir = std::env::temp_dir().clone();
dir.push(format!("hypha-cfg-{}", uuid));
fs::create_dir_all(&dir)?;
let mut file = dir.clone();
2024-01-18 14:52:35 -08:00
file.push("config.squashfs");
Ok(ConfigBlock {
image_info,
file,
dir,
})
2024-01-18 14:52:35 -08:00
}
pub fn build(&self, launch_config: &LaunchInfo) -> Result<()> {
2024-01-22 21:28:14 -08:00
trace!("ConfigBlock build launch_config={:?}", launch_config);
2024-01-18 14:52:35 -08:00
let manifest = self.image_info.config.to_string()?;
let launch = serde_json::to_string(launch_config)?;
2024-01-18 14:52:35 -08:00
let mut writer = FilesystemWriter::default();
writer.push_dir(
"/image",
NodeHeader {
permissions: 384,
uid: 0,
gid: 0,
mtime: 0,
},
)?;
writer.push_file(
manifest.as_bytes(),
"/image/config.json",
NodeHeader {
permissions: 384,
uid: 0,
gid: 0,
mtime: 0,
},
)?;
writer.push_file(
launch.as_bytes(),
"/launch.json",
NodeHeader {
permissions: 384,
uid: 0,
gid: 0,
mtime: 0,
},
)?;
2024-01-18 14:52:35 -08:00
let mut file = File::create(&self.file)?;
2024-01-22 21:28:14 -08:00
trace!("ConfigBlock build write sqaushfs");
2024-01-18 14:52:35 -08:00
writer.write(&mut file)?;
2024-01-22 21:28:14 -08:00
trace!("ConfigBlock build complete");
2024-01-18 14:52:35 -08:00
Ok(())
}
}