Skip to content

Commit aed00e8

Browse files
author
Jonas Bostoen
authored
feat: support environment variables in module config (#115)
* feat: support environment variables in module config * chore: fmt * fix: rm redundant to_string * fix(config): fix configuration ordering
1 parent ab78990 commit aed00e8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

config.example.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,18 @@ id = "DA_COMMIT"
110110
type = "commit"
111111
# Docker image of the module
112112
docker_image = "test_da_commit"
113+
# Environment file for the module
114+
env_file = ".cb.env"
115+
113116
# Additional config needed by the business logic of the module should also be set here.
114117
# See also `examples/da_commit/src/main.rs` for more information
115118
sleep_secs = 5
116119

120+
# Other environment variables for the module
121+
[modules.env]
122+
SOME_ENV_VAR = "some_value"
123+
124+
117125
# Configuration for how metrics should be collected and scraped
118126
# OPTIONAL, skip metrics collection if missing
119127
[metrics]

crates/cli/src/docker_init.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use cb_common::{
1212
utils::random_jwt,
1313
};
1414
use docker_compose_types::{
15-
Compose, ComposeVolume, DependsOnOptions, Environment, Labels, LoggingParameters, MapOrEmpty,
16-
NetworkSettings, Networks, Ports, Service, Services, SingleValue, TopLevelVolumes, Volumes,
15+
Compose, ComposeVolume, DependsOnOptions, EnvFile, Environment, Labels, LoggingParameters,
16+
MapOrEmpty, NetworkSettings, Networks, Ports, Service, Services, SingleValue, TopLevelVolumes,
17+
Volumes,
1718
};
1819
use eyre::Result;
1920
use indexmap::IndexMap;
@@ -34,7 +35,6 @@ const SIGNER_NETWORK: &str = "signer_network";
3435

3536
pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> {
3637
println!("Initializing Commit-Boost with config file: {}", config_path);
37-
3838
let cb_config = CommitBoostConfig::from_file(&config_path)?;
3939

4040
let metrics_enabled = cb_config.metrics.is_some();
@@ -106,6 +106,17 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
106106
get_env_interp(MODULE_JWT_ENV, &jwt_name),
107107
get_env_val(SIGNER_SERVER_ENV, &signer_server),
108108
]);
109+
110+
// Pass on the env variables
111+
if let Some(envs) = module.env {
112+
for (k, v) in envs {
113+
module_envs.insert(k, Some(SingleValue::String(v)));
114+
}
115+
}
116+
117+
// Set environment file
118+
let env_file = module.env_file.map(EnvFile::Simple);
119+
109120
if metrics_enabled {
110121
let (key, val) = get_env_uval(METRICS_SERVER_ENV, metrics_port as u64);
111122
module_envs.insert(key, val);
@@ -133,6 +144,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()>
133144
volumes: module_volumes,
134145
environment: Environment::KvPair(module_envs),
135146
depends_on: DependsOnOptions::Simple(vec!["cb_signer".to_owned()]),
147+
env_file,
136148
..Service::default()
137149
}
138150
}

crates/common/src/config/module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashMap;
2+
13
use eyre::{ContextCompat, Result};
24
use serde::{de::DeserializeOwned, Deserialize, Serialize};
35
use toml::Table;
@@ -28,6 +30,10 @@ pub struct StaticModuleConfig {
2830
pub id: ModuleId,
2931
/// Docker image of the module
3032
pub docker_image: String,
33+
/// Environment variables for the module
34+
pub env: Option<HashMap<String, String>>,
35+
/// Environment file for the module
36+
pub env_file: Option<String>,
3137
/// Type of the module
3238
#[serde(rename = "type")]
3339
pub kind: ModuleKind,

0 commit comments

Comments
 (0)