From 6236074a5239c63cb1b97a255499b0b5b9df4f2c Mon Sep 17 00:00:00 2001 From: Mihail Kirov Date: Wed, 3 Jul 2024 22:23:18 +0300 Subject: [PATCH 1/2] feat: Programatically adds a docker volume on to the docker-compose to be used for storing prometheus data --- .gitignore | 3 +++ crates/cli/src/docker_init.rs | 5 ++++- prometheus_data/.gitkeep | 0 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 prometheus_data/.gitkeep diff --git a/.gitignore b/.gitignore index 1dfbe15..79d16af 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ Cargo.lock *.docker-compose.yml targets.json .idea/ + +prometheus_data/* +!prometheus_data/.gitkeep \ No newline at end of file diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 06fbbe9..337b635 100644 --- a/crates/cli/src/docker_init.rs +++ b/crates/cli/src/docker_init.rs @@ -22,6 +22,7 @@ pub(super) const CB_CONFIG_FILE: &str = "cb-config.toml"; pub(super) const CB_COMPOSE_FILE: &str = "cb.docker-compose.yml"; pub(super) const CB_ENV_FILE: &str = ".cb.env"; pub(super) const CB_TARGETS_FILE: &str = "targets.json"; // needs to match prometheus.yml +pub(super) const PROMETHEUS_DATA_DIR: &str = "prometheus_data"; const METRICS_NETWORK: &str = "monitoring_network"; const SIGNER_NETWORK: &str = "signer_network"; @@ -253,10 +254,12 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> let targets_volume = Volumes::Simple(format!("./{}:/etc/prometheus/targets.json", CB_TARGETS_FILE)); + let data_volume = Volumes::Simple(format!("./{}:/prometheus", PROMETHEUS_DATA_DIR)); + let prometheus_service = Service { container_name: Some("cb_prometheus".to_owned()), image: Some("prom/prometheus:latest".to_owned()), - volumes: vec![prom_volume, targets_volume], + volumes: vec![prom_volume, targets_volume, data_volume], // to inspect prometheus from localhost ports: Ports::Short(vec!["9090:9090".to_owned()]), networks: Networks::Simple(vec![METRICS_NETWORK.to_owned()]), diff --git a/prometheus_data/.gitkeep b/prometheus_data/.gitkeep new file mode 100644 index 0000000..e69de29 From 22c94b9fbfbab576c2ea09bf2d06401468412b9d Mon Sep 17 00:00:00 2001 From: ltitanb Date: Fri, 2 Aug 2024 14:02:14 +0100 Subject: [PATCH 2/2] use docker volume --- .gitignore | 3 --- crates/cli/src/docker_init.rs | 21 +++++++++++++++++---- prometheus_data/.gitkeep | 0 3 files changed, 17 insertions(+), 7 deletions(-) delete mode 100644 prometheus_data/.gitkeep diff --git a/.gitignore b/.gitignore index 79d16af..1dfbe15 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,3 @@ Cargo.lock *.docker-compose.yml targets.json .idea/ - -prometheus_data/* -!prometheus_data/.gitkeep \ No newline at end of file diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 337b635..51638b9 100644 --- a/crates/cli/src/docker_init.rs +++ b/crates/cli/src/docker_init.rs @@ -11,8 +11,8 @@ use cb_common::{ utils::random_jwt, }; use docker_compose_types::{ - Compose, DependsOnOptions, Environment, LoggingParameters, MapOrEmpty, NetworkSettings, - Networks, Ports, Service, Services, SingleValue, Volumes, + Compose, ComposeVolume, DependsOnOptions, Environment, Labels, LoggingParameters, MapOrEmpty, + NetworkSettings, Networks, Ports, Service, Services, SingleValue, TopLevelVolumes, Volumes, }; use eyre::Result; use indexmap::IndexMap; @@ -22,7 +22,7 @@ pub(super) const CB_CONFIG_FILE: &str = "cb-config.toml"; pub(super) const CB_COMPOSE_FILE: &str = "cb.docker-compose.yml"; pub(super) const CB_ENV_FILE: &str = ".cb.env"; pub(super) const CB_TARGETS_FILE: &str = "targets.json"; // needs to match prometheus.yml -pub(super) const PROMETHEUS_DATA_DIR: &str = "prometheus_data"; +pub(super) const PROMETHEUS_DATA_VOLUME: &str = "prometheus-data"; const METRICS_NETWORK: &str = "monitoring_network"; const SIGNER_NETWORK: &str = "signer_network"; @@ -37,6 +37,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> let cb_config = CommitBoostConfig::from_file(&config_path)?; let mut services = IndexMap::new(); + let mut volumes = IndexMap::new(); // config volume to pass to all services let config_volume = Volumes::Simple(format!("./{}:{}:ro", config_path, CB_CONFIG_NAME)); @@ -254,7 +255,18 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> let targets_volume = Volumes::Simple(format!("./{}:/etc/prometheus/targets.json", CB_TARGETS_FILE)); - let data_volume = Volumes::Simple(format!("./{}:/prometheus", PROMETHEUS_DATA_DIR)); + let data_volume = Volumes::Simple(format!("{}:/prometheus", PROMETHEUS_DATA_VOLUME)); + + volumes.insert( + PROMETHEUS_DATA_VOLUME.to_owned(), + MapOrEmpty::Map(ComposeVolume { + driver: Some("local".to_owned()), + driver_opts: IndexMap::default(), + external: None, + labels: Labels::default(), + name: None, + }), + ); let prometheus_service = Service { container_name: Some("cb_prometheus".to_owned()), @@ -285,6 +297,7 @@ pub fn handle_docker_init(config_path: String, output_dir: String) -> Result<()> } compose.services = Services(services); + compose.volumes = TopLevelVolumes(volumes); // write compose to file let compose_str = serde_yaml::to_string(&compose)?; diff --git a/prometheus_data/.gitkeep b/prometheus_data/.gitkeep deleted file mode 100644 index e69de29..0000000