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 <riccardo.gallo@secomind.com>
  • Loading branch information
rgallor committed Jan 7, 2025
1 parent a60d71b commit baedef3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 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 = []
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ A detailed description of the fields is depicted below:
- `endpoint`: the endpoint where the Astarte Message Hub instance is listening for new connections.
- `node_id`: UUID of the Node to connect to the Astarte Message Hub (optional).

Since the application can be configured with a CLI, when [running the application](#build-and-run) you can specify the
type of connection (`mqtt` or `grpc`) and the path to the `config.toml` file with the `--astarte_connection` (`-c`) and
`--astarte_config_path` options, respectively.

### Build and run

Build the application using following commands:
Expand Down Expand Up @@ -136,13 +132,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"]
5 changes: 2 additions & 3 deletions src/astarte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ impl ConnectionConfigBuilder {
let endpoint = env::var("ASTARTE_MSGHUB_ENDPOINT")?;

let node_id = match env::var("ASTARTE_MSGHUB_NODE_ID") {
Ok(uuid) => {
Uuid::parse_str(&uuid).wrap_err("invalid ASTARTE_MSGHUB_NODE_ID {uuid}")?
}
Ok(uuid) => Uuid::parse_str(&uuid)
.wrap_err(format!("invalid ASTARTE_MSGHUB_NODE_ID {uuid}"))?,

Check warning on line 115 in src/astarte.rs

View check run for this annotation

Codecov / codecov/patch

src/astarte.rs#L114-L115

Added lines #L114 - L115 were not covered by tests
Err(VarError::NotPresent) => DEFAULT_STREAM_NODE_ID,
Err(VarError::NotUnicode(s)) => {
bail!("non unicode ASTARTE_MSGHUB_NODE_ID {s:?}")
Expand Down
7 changes: 0 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@
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 {
/// 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>,
/// 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
30 changes: 25 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,32 @@ 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}");

if let Some(path) = &cli_cfg.astarte_config_path {
let path = path.join("config.toml");
info!("retrieve Astarte connection config from {}", path.display());
// default path where to search for config.toml
let path_str = "/etc/stream-rust-test".to_string();

#[cfg(not(feature = "docker"))]
// if we are not using containers, we try to retrieve the config file from a specific location
// set by the user in the ASTARTE_CONFIG_PATH environment variable
let path_str = match std::env::var("ASTARTE_CONFIG_PATH") {
Ok(path) => {
debug!("retrieve Astarte connection config from path: {path}");
path
}
Err(std::env::VarError::NotPresent) => {
debug!(
"retrieve Astarte connection config from default path (/etc/stream-rust-test)"
);
path_str
}
Err(err) => {
error!("failed to retrieve Astarte connection config: {err}");
return Err(color_eyre::Report::new(err));
}
};

astarte_cfg_builder.from_toml(path).await;
}
let path = std::path::PathBuf::from(path_str).join("config.toml");

astarte_cfg_builder.from_toml(path).await;
};

let (client, connection) = astarte_cfg_builder.build().await?;
Expand Down

0 comments on commit baedef3

Please sign in to comment.