From 0d37a967af6fdad28421bb2670e59f87d7d0ec19 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Wed, 11 Sep 2024 14:34:24 +0200 Subject: [PATCH 1/4] feat: support environment variables in module config --- config.example.toml | 7 +++++++ crates/cli/src/docker_init.rs | 18 +++++++++++++++--- crates/common/src/config/module.rs | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/config.example.toml b/config.example.toml index 01e2102a..9c21f479 100644 --- a/config.example.toml +++ b/config.example.toml @@ -110,6 +110,13 @@ id = "DA_COMMIT" type = "commit" # Docker image of the module docker_image = "test_da_commit" +# Environment file for the module +env_file = ".cb.env" + +# Other environment variables for the module +[modules.env] +SOME_ENV_VAR = "some_value" + # Additional config needed by the business logic of the module should also be set here. # See also `examples/da_commit/src/main.rs` for more information sleep_secs = 5 diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 6c705007..3b37d576 100644 --- a/crates/cli/src/docker_init.rs +++ b/crates/cli/src/docker_init.rs @@ -12,8 +12,9 @@ use cb_common::{ utils::random_jwt, }; use docker_compose_types::{ - Compose, ComposeVolume, DependsOnOptions, Environment, Labels, LoggingParameters, MapOrEmpty, - NetworkSettings, Networks, Ports, Service, Services, SingleValue, TopLevelVolumes, Volumes, + Compose, ComposeVolume, DependsOnOptions, EnvFile, Environment, Labels, LoggingParameters, + MapOrEmpty, NetworkSettings, Networks, Ports, Service, Services, SingleValue, TopLevelVolumes, + Volumes, }; use eyre::Result; use indexmap::IndexMap; @@ -34,7 +35,6 @@ const SIGNER_NETWORK: &str = "signer_network"; pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> { println!("Initializing Commit-Boost with config file: {}", config_path); - let cb_config = CommitBoostConfig::from_file(&config_path)?; let metrics_enabled = cb_config.metrics.is_some(); @@ -106,6 +106,17 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> get_env_interp(MODULE_JWT_ENV, &jwt_name), get_env_val(SIGNER_SERVER_ENV, &signer_server), ]); + + // Pass on the env variables + if let Some(envs) = module.env { + for (k, v) in envs { + module_envs.insert(k, Some(SingleValue::String(v.to_string()))); + } + } + + // Set environment file + let env_file = module.env_file.map(EnvFile::Simple); + if metrics_enabled { let (key, val) = get_env_uval(METRICS_SERVER_ENV, metrics_port as u64); module_envs.insert(key, val); @@ -133,6 +144,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> volumes: module_volumes, environment: Environment::KvPair(module_envs), depends_on: DependsOnOptions::Simple(vec!["cb_signer".to_owned()]), + env_file, ..Service::default() } } diff --git a/crates/common/src/config/module.rs b/crates/common/src/config/module.rs index 872d141a..ffe2299b 100644 --- a/crates/common/src/config/module.rs +++ b/crates/common/src/config/module.rs @@ -1,5 +1,6 @@ use eyre::{ContextCompat, Result}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use std::collections::HashMap; use toml::Table; use crate::{ @@ -28,6 +29,10 @@ pub struct StaticModuleConfig { pub id: ModuleId, /// Docker image of the module pub docker_image: String, + /// Environment variables for the module + pub env: Option>, + /// Environment file for the module + pub env_file: Option, /// Type of the module #[serde(rename = "type")] pub kind: ModuleKind, From 66f45a19b0c3a52068187975c40cb9dd1d0f5b35 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Wed, 11 Sep 2024 14:39:21 +0200 Subject: [PATCH 2/4] chore: fmt --- crates/common/src/config/module.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/common/src/config/module.rs b/crates/common/src/config/module.rs index ffe2299b..43789cd8 100644 --- a/crates/common/src/config/module.rs +++ b/crates/common/src/config/module.rs @@ -1,6 +1,7 @@ +use std::collections::HashMap; + use eyre::{ContextCompat, Result}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use std::collections::HashMap; use toml::Table; use crate::{ From 84f575d358e14162310f56e65524282102a32419 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Wed, 11 Sep 2024 14:41:30 +0200 Subject: [PATCH 3/4] fix: rm redundant to_string --- crates/cli/src/docker_init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 3b37d576..2a40edde 100644 --- a/crates/cli/src/docker_init.rs +++ b/crates/cli/src/docker_init.rs @@ -110,7 +110,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> // Pass on the env variables if let Some(envs) = module.env { for (k, v) in envs { - module_envs.insert(k, Some(SingleValue::String(v.to_string()))); + module_envs.insert(k, Some(SingleValue::String(v))); } } From ec83a22951f32f78954b8c3411b294cd85374293 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Thu, 12 Sep 2024 09:54:56 +0200 Subject: [PATCH 4/4] fix(config): fix configuration ordering --- config.example.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config.example.toml b/config.example.toml index 9c21f479..49b50dca 100644 --- a/config.example.toml +++ b/config.example.toml @@ -113,13 +113,14 @@ docker_image = "test_da_commit" # Environment file for the module env_file = ".cb.env" +# Additional config needed by the business logic of the module should also be set here. +# See also `examples/da_commit/src/main.rs` for more information +sleep_secs = 5 + # Other environment variables for the module [modules.env] SOME_ENV_VAR = "some_value" -# Additional config needed by the business logic of the module should also be set here. -# See also `examples/da_commit/src/main.rs` for more information -sleep_secs = 5 # Configuration for how metrics should be collected and scraped # OPTIONAL, skip metrics collection if missing