Skip to content

Commit

Permalink
Merge pull request #371 from chainbound/lore/fix/docker-again
Browse files Browse the repository at this point in the history
fix(holesky/docker): add environment variable replacement in entrypoint
  • Loading branch information
thedevbirb authored Nov 11, 2024
2 parents 3173638 + 8161a72 commit e2bc4e1
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 37 deletions.
28 changes: 18 additions & 10 deletions bolt-sidecar/bin/sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::Parser;
use eyre::{bail, Result};
use eyre::bail;
use tracing::info;

use bolt_sidecar::{
config::{strip_empty_envs, Opts},
config::{remove_empty_envs, Opts},
telemetry::init_telemetry_stack,
SidecarDriver,
};
Expand All @@ -17,16 +17,12 @@ const BOLT: &str = r#"
╚═════╝ ╚═════╝ ╚══════╝╚═╝ "#;

#[tokio::main]
async fn main() -> Result<()> {
if dotenvy::dotenv().is_ok() {
strip_empty_envs()?;
info!("Loaded .env file");
}
async fn main() -> eyre::Result<()> {
read_env_file()?;

let opts = Opts::parse();

if let Err(err) = init_telemetry_stack(opts.telemetry.metrics_port()) {
bail!("Failed to initialize telemetry stack: {:?}", err)
}
init_telemetry_stack(opts.telemetry.metrics_port())?;

println!("{BOLT}");

Expand Down Expand Up @@ -55,3 +51,15 @@ async fn main() -> Result<()> {
}
}
}

fn read_env_file() -> eyre::Result<()> {
match dotenvy::dotenv() {
// It means the .env file hasn't been found but it's okay since it's optional
Err(dotenvy::Error::Io(_)) => (),
Err(err) => bail!("Failed to load .env file: {:?}", err),
Ok(path) => println!("Loaded environment variables from path: {:?}", path),
};

remove_empty_envs()?;
Ok(())
}
1 change: 1 addition & 0 deletions bolt-sidecar/src/config/constraint_signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl fmt::Debug for ConstraintSigningOpts {
.field("keystore_password", &"********") // Hides the actual password
.field("keystore_path", &self.keystore_path)
.field("keystore_secrets_path", &self.keystore_secrets_path)
.field("delegations_path", &self.delegations_path)
.finish()
}
}
16 changes: 10 additions & 6 deletions bolt-sidecar/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::env;

use alloy::primitives::Address;
use clap::Parser;
use reqwest::Url;
Expand All @@ -18,6 +20,7 @@ use telemetry::TelemetryOpts;
/// Operating limits for commitments and constraints.
pub mod limits;
use limits::LimitsOpts;
use tracing::debug;

use crate::common::{BlsSecretKeyWrapper, EcdsaSecretKeyWrapper, JwtSecretConfig};

Expand Down Expand Up @@ -103,10 +106,11 @@ pub struct Opts {
/// It removes environment variables that are set as empty strings, i.e. like `MY_VAR=`. This is
/// useful to avoid unexpected edge cases and because we don't have options that make sense with an
/// empty string value.
pub fn strip_empty_envs() -> eyre::Result<()> {
for item in dotenvy::dotenv_iter()? {
let (key, val) = item?;
if val.is_empty() {
pub fn remove_empty_envs() -> eyre::Result<()> {
for item in env::vars() {
let (key, val) = item;
if val.trim().is_empty() {
debug!("removing empty env var: {}", key);
std::env::remove_var(key)
}
}
Expand All @@ -122,9 +126,9 @@ mod tests {

#[test]
#[ignore = "Doesn't need to run in CI, only for local development"]
fn test_strip_empty_envs() {
fn test_remove_empty_envs() {
let _ = dotenv().expect("to load .env file");
strip_empty_envs().expect("to strip empty envs");
remove_empty_envs().expect("to remove empty envs");
let opts = Opts::parse();
println!("{:#?}", opts);
}
Expand Down
6 changes: 2 additions & 4 deletions bolt-sidecar/src/telemetry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ pub use metrics::ApiMetrics;
///
/// **This function should be called at the beginning of the program.**
pub fn init_telemetry_stack(metrics_port: Option<u16>) -> Result<()> {
// 1. Initialize tracing to stdout
let std_layer = FmtLayer::default().with_writer(std::io::stdout).with_filter(
EnvFilter::builder()
.with_default_directive("bolt_sidecar=info".parse()?)
.from_env_lossy()
.add_directive("reqwest=error".parse()?)
.add_directive("alloy_transport_http=error".parse()?),
);
Registry::default().with(std_layer).try_init()?;

// 2. Initialize metrics recorder and start the Prometheus server
Registry::default().with(std_layer).try_init()?;
if let Some(metrics_port) = metrics_port {
let prometheus_addr = SocketAddr::from(([0, 0, 0, 0], metrics_port));
let builder = PrometheusBuilder::new().with_http_listener(prometheus_addr);

if let Err(e) = builder.install() {
bail!("failed to install Prometheus recorder: {:?}", e);
bail!("failed to init telemetry stack. Error installing Prometheus recorder: {:?}", e);
} else {
info!(
"Telemetry initialized. Serving Prometheus metrics at: http://{}",
Expand Down
2 changes: 1 addition & 1 deletion scripts/kurtosis_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mev_params:
# instead of MEV-Boost by Flashbots
bolt_boost_image: ghcr.io/chainbound/bolt-boost:0.1.0
bolt_sidecar_image: ghcr.io/chainbound/bolt-sidecar:0.1.0
helix_relay_image: ghcr.io/chainbound/helix:v0.3.0-alpha.rc2
helix_relay_image: ghcr.io/chainbound/helix:v0.3.0-alpha.rc3
mev_relay_image: ghcr.io/chainbound/bolt-relay:0.1.0
mev_builder_image: ghcr.io/chainbound/bolt-builder:0.1.0
mev_boost_image: ghcr.io/chainbound/bolt-mev-boost:0.1.0
Expand Down
2 changes: 1 addition & 1 deletion testnets/holesky/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ Once the configuration files are in place, make sure you are in the
`testnets/holesky` directory and then run:
```bash
docker compose up -d --env-file bolt-sidecar.env
docker compose --env-file bolt-sidecar.env up -d
```
The docker compose setup comes with various observability tools, such as
Expand Down
24 changes: 9 additions & 15 deletions testnets/holesky/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@ services:
container_name: bolt-sidecar-holesky
restart: unless-stopped
ports:
- "${BOLT_SIDECAR_PORT:-8017}:${BOLT_SIDECAR_PORT:-8017}" # Bolt RPC port (this should be opened on your firewall!)
# NOTE: to read these envs, it is necessary to provide them via `--env-file` or having them already set.
- "${BOLT_SIDECAR_PORT:-8017}:${BOLT_SIDECAR_PORT:-8017}" # This port should be opened on your firewall!
- "${BOLT_SIDECAR_CONSTRAINTS_PROXY_PORT:-18550}:${BOLT_SIDECAR_CONSTRAINTS_PROXY_PORT:-18550}"
entrypoint: /bin/sh -c /usr/local/bin/bolt-sidecar
env_file: ./bolt-sidecar.env
environment:
# The "+" syntax replaces the environment variable with the alternate valuee only if set
# Reference: https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/#interpolation-syntax
#
# NOTE: These enviroment variables take precedence over the ones provided via `env_file`.
# Reference: https://docs.docker.com/compose/how-tos/environment-variables/envvars-precedence/
BOLT_SIDECAR_DELEGATIONS_PATH: "${BOLT_SIDECAR_DELEGATIONS_PATH+/etc/delegations.json}"
BOLT_SIDECAR_KEYSTORE_PATH: "${BOLT_SIDECAR_KEYSTORE_PATH+/etc/keystore}"
BOLT_SIDECAR_KEYSTORE_SECRETS_PATH: "${BOLT_SIDECAR_KEYSTORE_SECRETS_PATH+/etc/secrets}"
entrypoint: /usr/local/bin/entrypoint.sh
volumes:
- ${BOLT_SIDECAR_DELEGATIONS_PATH:-/dev/null:/etc/delegations.json}
- ${BOLT_SIDECAR_KEYSTORE_PATH:-/dev/null:/etc/keystores}
- ${BOLT_SIDECAR_KEYSTORE_SECRETS_PATH:-/dev/null:/etc/secrets}
- ./entrypoint.sh:/usr/local/bin/entrypoint.sh
- ./bolt-sidecar.env:/usr/local/bin/.env
# NOTE: to read these envs, it is necessary to provide them via `--env-file` or having them already set.
- ${BOLT_SIDECAR_DELEGATIONS_PATH:-/dev/null}:/etc/delegations.json
- ${BOLT_SIDECAR_KEYSTORE_PATH:-/dev/null}:/etc/keystores
- ${BOLT_SIDECAR_KEYSTORE_SECRETS_PATH:-/dev/null}:/etc/secrets
networks:
- bolt-default

Expand Down
19 changes: 19 additions & 0 deletions testnets/holesky/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Exit immediately if a command exits with a non-zero status.
set -e

# Override the environment variables provided by the user.
#
# The "+" syntax replaces the environment variable with the alternate valuee
# only if set.
# Reference: https://docs.docker.com/compose/how-tos/environment-variables/variable-interpolation/#interpolation-syntax

# Ensure these environment variables are either empty or set with the
# alternative values, overriding what's provided with the `--env-file` flag in
# the Docker Compose file and matching the volume mounts.
export BOLT_SIDECAR_DELEGATIONS_PATH="${BOLT_SIDECAR_DELEGATIONS_PATH+/etc/delegations.json}"
export BOLT_SIDECAR_KEYSTORE_PATH="${BOLT_SIDECAR_KEYSTORE_PATH+/etc/keystore}"
export BOLT_SIDECAR_KEYSTORE_SECRETS_PATH="${BOLT_SIDECAR_KEYSTORE_SECRETS_PATH+/etc/secrets}"

/usr/local/bin/bolt-sidecar

0 comments on commit e2bc4e1

Please sign in to comment.