Skip to content

Commit

Permalink
feat: [#937] add version to configration file following semver.
Browse files Browse the repository at this point in the history
Add version and namespace to the configuration. It will fail if the provided version is not supported.

```toml
version = "2"
```

It only supports the exact match '2'.
  • Loading branch information
josecelano committed Jul 2, 2024
1 parent ddd73ca commit 60c6876
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
62 changes: 61 additions & 1 deletion packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::sync::Arc;
use std::time::Duration;

use camino::Utf8PathBuf;
use derive_more::Constructor;
use derive_more::{Constructor, Display};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use thiserror::Error;
Expand Down Expand Up @@ -45,6 +45,63 @@ pub type Threshold = v2::logging::Threshold;

pub type AccessTokens = HashMap<String, String>;

pub const LATEST_VERSION: &str = "2";

/// Info about the configuration specification.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display)]
pub struct Metadata {
#[serde(default = "Metadata::default_version")]
#[serde(flatten)]
version: Version,
}

impl Default for Metadata {
fn default() -> Self {
Self {
version: Self::default_version(),
}
}
}

impl Metadata {
fn default_version() -> Version {
Version::latest()
}
}

/// The configuration version.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display)]
pub struct Version {
#[serde(default = "Version::default_semver")]
version: String,
}

impl Default for Version {
fn default() -> Self {
Self {
version: Self::default_semver(),

Check warning on line 82 in packages/configuration/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/lib.rs#L80-L82

Added lines #L80 - L82 were not covered by tests
}
}

Check warning on line 84 in packages/configuration/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/lib.rs#L84

Added line #L84 was not covered by tests
}

impl Version {
fn new(semver: &str) -> Self {
Self {
version: semver.to_owned(),
}
}

fn latest() -> Self {
Self {
version: LATEST_VERSION.to_string(),
}
}

fn default_semver() -> String {
LATEST_VERSION.to_string()
}

Check warning on line 102 in packages/configuration/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/lib.rs#L100-L102

Added lines #L100 - L102 were not covered by tests
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Constructor)]
pub struct TrackerPolicy {
// Cleanup job configuration
Expand Down Expand Up @@ -208,6 +265,9 @@ pub enum Error {

#[error("The error for errors that can never happen.")]
Infallible,

#[error("Unsupported configuration version: {version}")]
UnsupportedVersion { version: Version },

Check warning on line 270 in packages/configuration/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/lib.rs#L269-L270

Added lines #L269 - L270 were not covered by tests
}

impl From<figment::Error> for Error {
Expand Down
20 changes: 18 additions & 2 deletions packages/configuration/src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,24 @@ use self::health_check_api::HealthCheckApi;
use self::http_tracker::HttpTracker;
use self::tracker_api::HttpApi;
use self::udp_tracker::UdpTracker;
use crate::{Error, Info};
use crate::{Error, Info, Metadata, Version};

/// This configuration version
const VERSION_2: &str = "2";

/// Prefix for env vars that overwrite configuration options.
const CONFIG_OVERRIDE_PREFIX: &str = "TORRUST_TRACKER_CONFIG_OVERRIDE_";

/// Path separator in env var names for nested values in configuration.
const CONFIG_OVERRIDE_SEPARATOR: &str = "__";

/// Core configuration for the tracker.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default)]
pub struct Configuration {
/// Configuration metadata.
#[serde(flatten)]
pub metadata: Metadata,

/// Logging configuration
pub logging: Logging,

Expand Down Expand Up @@ -326,6 +334,12 @@ impl Configuration {

let config: Configuration = figment.extract()?;

if config.metadata.version != Version::new(VERSION_2) {
return Err(Error::UnsupportedVersion {
version: config.metadata.version,

Check warning on line 339 in packages/configuration/src/v2/mod.rs

View check run for this annotation

Codecov / codecov/patch

packages/configuration/src/v2/mod.rs#L338-L339

Added lines #L338 - L339 were not covered by tests
});
}

Ok(config)
}

Expand Down Expand Up @@ -378,7 +392,9 @@ mod tests {

#[cfg(test)]
fn default_config_toml() -> String {
let config = r#"[logging]
let config = r#"version = "2"
[logging]
threshold = "info"
[core]
Expand Down
2 changes: 2 additions & 0 deletions share/default/config/tracker.container.mysql.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version = "2"

[core.database]
driver = "MySQL"
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
Expand Down
2 changes: 2 additions & 0 deletions share/default/config/tracker.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version = "2"

[core.database]
path = "/var/lib/torrust/tracker/database/sqlite3.db"

Expand Down
2 changes: 2 additions & 0 deletions share/default/config/tracker.development.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version = "2"

[[udp_trackers]]
bind_address = "0.0.0.0:6969"

Expand Down
2 changes: 2 additions & 0 deletions share/default/config/tracker.e2e.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version = "2"

[core.database]
path = "/var/lib/torrust/tracker/database/sqlite3.db"

Expand Down
2 changes: 2 additions & 0 deletions share/default/config/tracker.udp.benchmarking.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version = "2"

[logging]
threshold = "error"

Expand Down

0 comments on commit 60c6876

Please sign in to comment.