Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support environment variables in module config #115

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
18 changes: 15 additions & 3 deletions crates/cli/src/docker_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/common/src/config/module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use eyre::{ContextCompat, Result};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use toml::Table;
Expand Down Expand Up @@ -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<HashMap<String, String>>,
/// Environment file for the module
pub env_file: Option<String>,
/// Type of the module
#[serde(rename = "type")]
pub kind: ModuleKind,
Expand Down
Loading