From bb77e651e09b088ee0c43e0ea98a134caae5afd7 Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Tue, 30 Jan 2024 18:34:47 -0800 Subject: [PATCH] hypha: add support for passing environment variables --- hypha/bin/controller.rs | 8 ++++++-- hypha/src/container/init.rs | 3 +++ hypha/src/ctl/mod.rs | 7 ++++--- hypha/src/shared/mod.rs | 1 + 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/hypha/bin/controller.rs b/hypha/bin/controller.rs index 39043bb..f0b70c4 100644 --- a/hypha/bin/controller.rs +++ b/hypha/bin/controller.rs @@ -30,6 +30,8 @@ enum Commands { mem: u64, #[arg(long)] config_bundle: Option, + #[arg[short, long]] + env: Option>, #[arg(allow_hyphen_values = true, trailing_var_arg = true)] run: Vec, }, @@ -68,20 +70,22 @@ fn main() -> Result<()> { cpus, mem, config_bundle, + env, run, } => { let kernel = map_kernel_path(&store_path, kernel); let initrd = map_initrd_path(&store_path, initrd); - let domid = controller.launch( + let (uuid, _domid) = controller.launch( &kernel, &initrd, config_bundle.as_deref(), &image, cpus, mem, + env, if run.is_empty() { None } else { Some(run) }, )?; - println!("launched domain: {}", domid); + println!("launched container: {}", uuid); } Commands::Destroy { domain } => { diff --git a/hypha/src/container/init.rs b/hypha/src/container/init.rs index 6755c0f..eeeff3b 100644 --- a/hypha/src/container/init.rs +++ b/hypha/src/container/init.rs @@ -288,6 +288,9 @@ impl ContainerInit { Some(value) => value.clone(), }; 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 cmd_cstr = ContainerInit::strings_as_cstrings(cmd)?; let env_cstr = ContainerInit::strings_as_cstrings(env)?; diff --git a/hypha/src/ctl/mod.rs b/hypha/src/ctl/mod.rs index 4c0368d..3773dcb 100644 --- a/hypha/src/ctl/mod.rs +++ b/hypha/src/ctl/mod.rs @@ -69,12 +69,13 @@ impl Controller { image: &str, vcpus: u32, mem: u64, + env: Option>, run: Option>, - ) -> Result { + ) -> Result<(Uuid, u32)> { let uuid = Uuid::new_v4(); let name = format!("hypha-{uuid}"); 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)?; cfgblk.build(&launch_config)?; @@ -134,7 +135,7 @@ impl Controller { ], }; match self.client.create(&config) { - Ok(domid) => Ok(domid), + Ok(domid) => Ok((uuid, domid)), Err(error) => { let _ = self.autoloop.unloop(&image_squashfs_loop.path); let _ = self.autoloop.unloop(&cfgblk_squashfs_loop.path); diff --git a/hypha/src/shared/mod.rs b/hypha/src/shared/mod.rs index 417c3f0..44ac6b5 100644 --- a/hypha/src/shared/mod.rs +++ b/hypha/src/shared/mod.rs @@ -2,5 +2,6 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] pub struct LaunchInfo { + pub env: Option>, pub run: Option>, }