diff --git a/config.example.toml b/config.example.toml index 01e2102a..49b50dca 100644 --- a/config.example.toml +++ b/config.example.toml @@ -110,10 +110,18 @@ id = "DA_COMMIT" type = "commit" # Docker image of the module 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" + + # Configuration for how metrics should be collected and scraped # OPTIONAL, skip metrics collection if missing [metrics] diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 6c705007..2a40edde 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))); + } + } + + // 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..43789cd8 100644 --- a/crates/common/src/config/module.rs +++ b/crates/common/src/config/module.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use eyre::{ContextCompat, Result}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use toml::Table; @@ -28,6 +30,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,