2024-01-30 02:15:03 -08:00
|
|
|
use anyhow::Result;
|
2024-04-29 10:02:20 -07:00
|
|
|
use backhand::compression::Compressor;
|
|
|
|
use backhand::{FilesystemCompressor, FilesystemWriter, NodeHeader};
|
2024-03-05 11:35:25 +00:00
|
|
|
use krata::launchcfg::LaunchInfo;
|
2024-04-16 01:53:44 -07:00
|
|
|
use krataoci::packer::OciPackedImage;
|
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;
|
|
|
|
|
2024-04-29 10:02:20 -07:00
|
|
|
pub struct ConfigBlock {
|
|
|
|
pub image: OciPackedImage,
|
2024-01-18 14:52:35 -08:00
|
|
|
pub file: PathBuf,
|
2024-01-21 00:13:05 -08:00
|
|
|
pub dir: PathBuf,
|
2024-01-18 14:52:35 -08:00
|
|
|
}
|
|
|
|
|
2024-04-29 10:02:20 -07:00
|
|
|
impl ConfigBlock {
|
|
|
|
pub fn new(uuid: &Uuid, image: OciPackedImage) -> Result<ConfigBlock> {
|
2024-01-21 00:13:05 -08:00
|
|
|
let mut dir = std::env::temp_dir().clone();
|
2024-02-21 20:57:46 +00:00
|
|
|
dir.push(format!("krata-cfg-{}", uuid));
|
2024-01-21 00:13:05 -08:00
|
|
|
fs::create_dir_all(&dir)?;
|
|
|
|
let mut file = dir.clone();
|
2024-01-18 14:52:35 -08:00
|
|
|
file.push("config.squashfs");
|
2024-04-15 10:24:14 -07:00
|
|
|
Ok(ConfigBlock { image, file, dir })
|
2024-01-18 14:52:35 -08:00
|
|
|
}
|
|
|
|
|
2024-01-22 05:30:02 -08:00
|
|
|
pub fn build(&self, launch_config: &LaunchInfo) -> Result<()> {
|
2024-02-29 12:52:44 +00:00
|
|
|
trace!("build launch_config={:?}", launch_config);
|
2024-04-16 01:53:44 -07:00
|
|
|
let config = self.image.config.raw();
|
2024-01-22 05:30:02 -08:00
|
|
|
let launch = serde_json::to_string(launch_config)?;
|
2024-01-18 14:52:35 -08:00
|
|
|
let mut writer = FilesystemWriter::default();
|
2024-04-29 10:02:20 -07:00
|
|
|
writer.set_compressor(FilesystemCompressor::new(Compressor::Gzip, None)?);
|
2024-01-18 14:52:35 -08:00
|
|
|
writer.push_dir(
|
|
|
|
"/image",
|
|
|
|
NodeHeader {
|
|
|
|
permissions: 384,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
mtime: 0,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
writer.push_file(
|
2024-04-16 01:53:44 -07:00
|
|
|
config,
|
2024-01-18 14:52:35 -08:00
|
|
|
"/image/config.json",
|
|
|
|
NodeHeader {
|
|
|
|
permissions: 384,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
mtime: 0,
|
|
|
|
},
|
|
|
|
)?;
|
2024-01-22 05:30:02 -08:00
|
|
|
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-02-29 12:52:44 +00:00
|
|
|
trace!("build write sqaushfs");
|
2024-01-18 14:52:35 -08:00
|
|
|
writer.write(&mut file)?;
|
2024-02-29 12:52:44 +00:00
|
|
|
trace!("build complete");
|
2024-01-18 14:52:35 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|