hypha: add support for passing environment variables

This commit is contained in:
Alex Zenla 2024-01-30 18:34:47 -08:00
parent 5e21d32295
commit bb77e651e0
No known key found for this signature in database
GPG Key ID: 067B238899B51269
4 changed files with 14 additions and 5 deletions

View File

@ -30,6 +30,8 @@ enum Commands {
mem: u64, mem: u64,
#[arg(long)] #[arg(long)]
config_bundle: Option<String>, config_bundle: Option<String>,
#[arg[short, long]]
env: Option<Vec<String>>,
#[arg(allow_hyphen_values = true, trailing_var_arg = true)] #[arg(allow_hyphen_values = true, trailing_var_arg = true)]
run: Vec<String>, run: Vec<String>,
}, },
@ -68,20 +70,22 @@ fn main() -> Result<()> {
cpus, cpus,
mem, mem,
config_bundle, config_bundle,
env,
run, run,
} => { } => {
let kernel = map_kernel_path(&store_path, kernel); let kernel = map_kernel_path(&store_path, kernel);
let initrd = map_initrd_path(&store_path, initrd); let initrd = map_initrd_path(&store_path, initrd);
let domid = controller.launch( let (uuid, _domid) = controller.launch(
&kernel, &kernel,
&initrd, &initrd,
config_bundle.as_deref(), config_bundle.as_deref(),
&image, &image,
cpus, cpus,
mem, mem,
env,
if run.is_empty() { None } else { Some(run) }, if run.is_empty() { None } else { Some(run) },
)?; )?;
println!("launched domain: {}", domid); println!("launched container: {}", uuid);
} }
Commands::Destroy { domain } => { Commands::Destroy { domain } => {

View File

@ -288,6 +288,9 @@ impl ContainerInit {
Some(value) => value.clone(), Some(value) => value.clone(),
}; };
env.push("HYPHA_CONTAINER=1".to_string()); env.push("HYPHA_CONTAINER=1".to_string());
if let Some(extra_env) = &launch.env {
env.extend_from_slice(extra_env.as_slice());
}
let path_cstr = CString::new(path)?; let path_cstr = CString::new(path)?;
let cmd_cstr = ContainerInit::strings_as_cstrings(cmd)?; let cmd_cstr = ContainerInit::strings_as_cstrings(cmd)?;
let env_cstr = ContainerInit::strings_as_cstrings(env)?; let env_cstr = ContainerInit::strings_as_cstrings(env)?;

View File

@ -69,12 +69,13 @@ impl Controller {
image: &str, image: &str,
vcpus: u32, vcpus: u32,
mem: u64, mem: u64,
env: Option<Vec<String>>,
run: Option<Vec<String>>, run: Option<Vec<String>>,
) -> Result<u32> { ) -> Result<(Uuid, u32)> {
let uuid = Uuid::new_v4(); let uuid = Uuid::new_v4();
let name = format!("hypha-{uuid}"); let name = format!("hypha-{uuid}");
let image_info = self.compile(image)?; let image_info = self.compile(image)?;
let launch_config = LaunchInfo { run }; let launch_config = LaunchInfo { env, run };
let cfgblk = ConfigBlock::new(&uuid, &image_info, config_bundle_path)?; let cfgblk = ConfigBlock::new(&uuid, &image_info, config_bundle_path)?;
cfgblk.build(&launch_config)?; cfgblk.build(&launch_config)?;
@ -134,7 +135,7 @@ impl Controller {
], ],
}; };
match self.client.create(&config) { match self.client.create(&config) {
Ok(domid) => Ok(domid), Ok(domid) => Ok((uuid, domid)),
Err(error) => { Err(error) => {
let _ = self.autoloop.unloop(&image_squashfs_loop.path); let _ = self.autoloop.unloop(&image_squashfs_loop.path);
let _ = self.autoloop.unloop(&cfgblk_squashfs_loop.path); let _ = self.autoloop.unloop(&cfgblk_squashfs_loop.path);

View File

@ -2,5 +2,6 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct LaunchInfo { pub struct LaunchInfo {
pub env: Option<Vec<String>>,
pub run: Option<Vec<String>>, pub run: Option<Vec<String>>,
} }