Skip to content

Commit

Permalink
feat(docker): add docker feature to disable modifying location to con…
Browse files Browse the repository at this point in the history
…fig.toml

If the application is run using the container, the config.toml config
file location is set to /etc/stream-rust-test/, otherwise it can be
modified by using the ASTARTE_CONFIG_PATH env variable

Signed-off-by: Riccardo Gallo <[email protected]>
  • Loading branch information
rgallor committed Dec 13, 2024
1 parent 07ea282 commit e978875
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ toml = "0.8.12"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.0", features = ["env-filter"]}
uuid = { version = "1.10.0", features = ["v4", "serde"] }

[features]
docker = []
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,11 @@ To run the container with your configuration file:
1. Ensure you have defined your astarte configuration file `config.toml`
2. Run the Docker container, mounting the configuration file:
```sh
docker run -v /path/to/your/config.toml:<MOUNT_TO_THIS_PATH> -e ASTARTE_CONFIG_PATH="<MOUNT_TO_THIS_PATH>" stream-rust-test:latest
docker run -v /path/to/your/config.toml:/etc/stream-rust-test/ stream-rust-test:latest
```

Replace `/path/to/your/config.toml` with the actual path to your configuration file.

Note: `MOUNT_TO_THIS_PATH` must be an absolute path.

#### Run the container with environment variables

You can configure the application with environment variables by exporting them (e.g. configuring
Expand Down
2 changes: 1 addition & 1 deletion astarte-device-conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ device_id = "DEVICE_ID_HERE"
pairing_url = "PAIRING_URL_HERE"
credentials_secret = "CREDENTIALS_SECRET_HERE"
#pairing_token = "PAIRING_TOKEN_HERE"
astarte_ignore_ssl = false
ignore_ssl_errors = false

# ####################################################
# Use the following to connect through gRPC to Astarte
Expand Down
3 changes: 2 additions & 1 deletion scripts/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY . /stream-rust-test

WORKDIR /stream-rust-test

RUN cargo build --release
RUN cargo build --features "docker" --release

FROM alpine:3.20

Expand All @@ -20,5 +20,6 @@ COPY scripts/docker/entrypoint.sh /entrypoint.sh
COPY --from=build /stream-rust-test/target/release/stream-rust-test /usr/bin/

RUN mkdir -p /tmp/stream-rust-test/store/
RUN mkdir /etc/stream-rust-test/

CMD ["/entrypoint.sh"]
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
use crate::math::MathFunction;
use clap::Parser;
use std::path::PathBuf;

/// Configuration for the values to be sent to Astarte
#[derive(Debug, Clone, Parser)]
#[clap(version, about)]
pub struct Config {
#[cfg(not(feature = "docker"))]
/// Path to the directory containing the Astarte configuration file config.toml
///
/// First, the Astarte configuration is taken from ENV vars, then from the config.toml if the
/// path has been specified
#[clap(short, long, env = "ASTARTE_CONFIG_PATH")]
pub astarte_config_path: Option<PathBuf>,
pub astarte_config_path: Option<std::path::PathBuf>,
/// Math function the device will use to send data to Astarte
#[clap(short, long, default_value = "default", env = "MATH_FUNCTION")]
pub math_function: MathFunction,
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ async fn main() -> eyre::Result<()> {
if let Err(err) = astarte_cfg_builder.try_from_env() {
warn!("failed to retrieve Astarte connection config from ENV: {err}");

#[cfg(feature = "docker")]
{
let path = std::path::PathBuf::from("/etc/stream-rust-test/config.toml");
astarte_cfg_builder.from_toml(path).await;
}

#[cfg(not(feature = "docker"))]
if let Some(path) = &cli_cfg.astarte_config_path {
let path = path.join("config.toml");
info!("retrieve Astarte connection config from {}", path.display());
Expand Down

0 comments on commit e978875

Please sign in to comment.