Skip to content

Commit

Permalink
feat!: [#878] extract logging and core section in toml config files
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 17, 2024
1 parent aa8b787 commit ef9461a
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ RUN ["/busybox/cp", "-sp", "/busybox/sh","/busybox/cat","/busybox/ls","/busybox/
COPY --from=gcc --chmod=0555 /usr/local/bin/su-exec /bin/su-exec

ARG TORRUST_TRACKER_CONFIG_TOML_PATH="/etc/torrust/tracker/tracker.toml"
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER="Sqlite3"
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER="Sqlite3"
ARG USER_ID=1000
ARG UDP_PORT=6969
ARG HTTP_PORT=7070
ARG API_PORT=1212
ARG HEALTH_CHECK_API_PORT=1313

ENV TORRUST_TRACKER_CONFIG_TOML_PATH=${TORRUST_TRACKER_CONFIG_TOML_PATH}
ENV TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER}
ENV TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER}
ENV USER_ID=${USER_ID}
ENV UDP_PORT=${UDP_PORT}
ENV HTTP_PORT=${HTTP_PORT}
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
image: torrust-tracker:release
tty: true
environment:
- TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER:-MySQL}
- TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER:-MySQL}
- TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN=${TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN:-MyAccessToken}
networks:
- server_side
Expand Down
2 changes: 2 additions & 0 deletions docs/benchmarking.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cargo build --release -p aquatic_udp_load_test
Run the tracker with UDP service enabled and other services disabled and set log level to `error`.

```toml
[logging]
log_level = "error"

[[udp_trackers]]
Expand Down Expand Up @@ -163,6 +164,7 @@ Announce responses per info hash:
Run the tracker with UDP service enabled and other services disabled and set log level to `error`.

```toml
[logging]
log_level = "error"

[[udp_trackers]]
Expand Down
2 changes: 1 addition & 1 deletion docs/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ The following environmental variables can be set:

- `TORRUST_TRACKER_CONFIG_TOML_PATH` - The in-container path to the tracker configuration file, (default: `"/etc/torrust/tracker/tracker.toml"`).
- `TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN` - Override of the admin token. If set, this value overrides any value set in the config.
- `TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER` - The database type used for the container, (options: `Sqlite3`, `MySQL`, default `Sqlite3`). Please Note: This dose not override the database configuration within the `.toml` config file.
- `TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER` - The database type used for the container, (options: `Sqlite3`, `MySQL`, default `Sqlite3`). Please Note: This dose not override the database configuration within the `.toml` config file.
- `TORRUST_TRACKER_CONFIG_TOML` - Load config from this environmental variable instead from a file, (i.e: `TORRUST_TRACKER_CONFIG_TOML=$(cat tracker-tracker.toml)`).
- `USER_ID` - The user id for the runtime crated `torrust` user. Please Note: This user id should match the ownership of the host-mapped volumes, (default `1000`).
- `UDP_PORT` - The port for the UDP tracker. This should match the port used in the configuration, (default `6969`).
Expand Down
19 changes: 8 additions & 11 deletions packages/configuration/src/v1/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ use std::net::{IpAddr, Ipv4Addr};
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};

use crate::{AnnouncePolicy, LogLevel};
use crate::AnnouncePolicy;

#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Core {
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
/// `Debug` and `Trace`. Default is `Info`.
#[serde(default = "Core::default_log_level")]
pub log_level: Option<LogLevel>,
/// Tracker mode. See [`TrackerMode`] for more information.
#[serde(default = "Core::default_mode")]
pub mode: TrackerMode,
Expand All @@ -20,6 +16,7 @@ pub struct Core {
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
#[serde(default = "Core::default_db_driver")]
pub db_driver: DatabaseDriver,

/// Database connection string. The format depends on the database driver.
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
/// `./storage/tracker/lib/database/sqlite3.db`.
Expand All @@ -35,25 +32,29 @@ pub struct Core {
/// See [`AnnouncePolicy::interval_min`]
#[serde(default = "AnnouncePolicy::default_interval_min")]
pub min_announce_interval: u32,

/// Weather the tracker is behind a reverse proxy or not.
/// If the tracker is behind a reverse proxy, the `X-Forwarded-For` header
/// sent from the proxy will be used to get the client's IP address.
#[serde(default = "Core::default_on_reverse_proxy")]
pub on_reverse_proxy: bool,

/// The external IP address of the tracker. If the client is using a
/// loopback IP address, this IP address will be used instead. If the peer
/// is using a loopback IP address, the tracker assumes that the peer is
/// in the same network as the tracker and will use the tracker's IP
/// address instead.
#[serde(default = "Core::default_external_ip")]
pub external_ip: Option<IpAddr>,

/// Weather the tracker should collect statistics about tracker usage.
/// If enabled, the tracker will collect statistics like the number of
/// connections handled, the number of announce requests handled, etc.
/// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more
/// information about the collected metrics.
#[serde(default = "Core::default_tracker_usage_statistics")]
pub tracker_usage_statistics: bool,

/// If enabled the tracker will persist the number of completed downloads.
/// That's how many times a torrent has been downloaded completely.
#[serde(default = "Core::default_persistent_torrent_completed_stat")]
Expand All @@ -65,10 +66,12 @@ pub struct Core {
/// time, it will be removed from the torrent peer list.
#[serde(default = "Core::default_max_peer_timeout")]
pub max_peer_timeout: u32,

/// Interval in seconds that the cleanup job will run to remove inactive
/// peers from the torrent peer list.
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
pub inactive_peer_cleanup_interval: u64,

/// If enabled, the tracker will remove torrents that have no peers.
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
/// seconds and it removes inactive peers. Eventually, the peer list of a
Expand All @@ -83,7 +86,6 @@ impl Default for Core {
let announce_policy = AnnouncePolicy::default();

Self {
log_level: Self::default_log_level(),
mode: Self::default_mode(),
db_driver: Self::default_db_driver(),
db_path: Self::default_db_path(),
Expand All @@ -101,11 +103,6 @@ impl Default for Core {
}

impl Core {
#[allow(clippy::unnecessary_wraps)]
fn default_log_level() -> Option<LogLevel> {
Some(LogLevel::Info)
}

fn default_mode() -> TrackerMode {
TrackerMode::Public
}
Expand Down
27 changes: 27 additions & 0 deletions packages/configuration/src/v1/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use serde::{Deserialize, Serialize};

use crate::LogLevel;

#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Logging {
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
/// `Debug` and `Trace`. Default is `Info`.
#[serde(default = "Logging::default_log_level")]
pub log_level: Option<LogLevel>,
}

impl Default for Logging {
fn default() -> Self {
Self {
log_level: Self::default_log_level(),
}
}
}

impl Logging {
#[allow(clippy::unnecessary_wraps)]
fn default_log_level() -> Option<LogLevel> {
Some(LogLevel::Info)
}
}
21 changes: 19 additions & 2 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@
//! The default configuration is:
//!
//! ```toml
//! [logging]
//! log_level = "info"
//!
//! [core]
//! mode = "public"
//! db_driver = "Sqlite3"
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
Expand Down Expand Up @@ -233,6 +236,7 @@
pub mod core;
pub mod health_check_api;
pub mod http_tracker;
pub mod logging;
pub mod tracker_api;
pub mod udp_tracker;

Expand All @@ -241,6 +245,7 @@ use std::net::IpAddr;

use figment::providers::{Env, Format, Serialized, Toml};
use figment::Figment;
use logging::Logging;
use serde::{Deserialize, Serialize};

use self::core::Core;
Expand All @@ -258,26 +263,33 @@ const CONFIG_OVERRIDE_SEPARATOR: &str = "__";
/// Core configuration for the tracker.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub struct Configuration {
/// Logging configuration
pub logging: Logging,

/// Core configuration.
#[serde(flatten)]
pub core: Core,

/// The list of UDP trackers the tracker is running. Each UDP tracker
/// represents a UDP server that the tracker is running and it has its own
/// configuration.
pub udp_trackers: Vec<UdpTracker>,

/// The list of HTTP trackers the tracker is running. Each HTTP tracker
/// represents a HTTP server that the tracker is running and it has its own
/// configuration.
pub http_trackers: Vec<HttpTracker>,

/// The HTTP API configuration.
pub http_api: HttpApi,

/// The Health Check API configuration.
pub health_check_api: HealthCheckApi,
}

impl Default for Configuration {
fn default() -> Self {
Self {
logging: Logging::default(),
core: Core::default(),
udp_trackers: vec![UdpTracker::default()],
http_trackers: vec![HttpTracker::default()],
Expand Down Expand Up @@ -365,7 +377,10 @@ mod tests {

#[cfg(test)]
fn default_config_toml() -> String {
let config = r#"log_level = "info"
let config = r#"[logging]
log_level = "info"
[core]
mode = "public"
db_driver = "Sqlite3"
db_path = "./storage/tracker/lib/database/sqlite3.db"
Expand Down Expand Up @@ -475,6 +490,7 @@ mod tests {
fn default_configuration_could_be_overwritten_from_a_single_env_var_with_toml_contents() {
figment::Jail::expect_with(|_jail| {
let config_toml = r#"
[core]
db_path = "OVERWRITTEN DEFAULT DB PATH"
"#
.to_string();
Expand All @@ -498,6 +514,7 @@ mod tests {
jail.create_file(
"tracker.toml",
r#"
[core]
db_path = "OVERWRITTEN DEFAULT DB PATH"
"#,
)?;
Expand Down
2 changes: 1 addition & 1 deletion packages/test-helpers/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn ephemeral() -> Configuration {

let mut config = Configuration::default();

config.core.log_level = Some(LogLevel::Off); // Change to `debug` for tests debugging
config.logging.log_level = Some(LogLevel::Off); // Change to `debug` for tests debugging

// Ephemeral socket address for API
let api_port = 0u16;
Expand Down
10 changes: 5 additions & 5 deletions share/container/entry_script_sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ chmod -R 2770 /var/lib/torrust /var/log/torrust /etc/torrust


# Install the database and config:
if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" ]; then
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" "Sqlite3"; then
if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER" ]; then
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER" "Sqlite3"; then

# Select Sqlite3 empty database
default_database="/usr/share/torrust/default/database/tracker.sqlite3.db"

# Select Sqlite3 default configuration
default_config="/usr/share/torrust/default/config/tracker.container.sqlite3.toml"

elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" "MySQL"; then
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER" "MySQL"; then

# (no database file needed for MySQL)

# Select default MySQL configuration
default_config="/usr/share/torrust/default/config/tracker.container.mysql.toml"

else
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER\"."
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER\"."
echo "Please Note: Supported Database Types: \"Sqlite3\", \"MySQL\"."
exit 1
fi
else
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER\" was not set!"; exit 1;
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER\" was not set!"; exit 1;
fi

install_config="/etc/torrust/tracker/tracker.toml"
Expand Down
1 change: 1 addition & 0 deletions share/default/config/tracker.container.mysql.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[core]
db_driver = "MySQL"
db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"

Expand Down
1 change: 1 addition & 0 deletions share/default/config/tracker.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[core]
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"

[[http_trackers]]
Expand Down
1 change: 1 addition & 0 deletions share/default/config/tracker.e2e.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[core]
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"

[[udp_trackers]]
Expand Down
3 changes: 3 additions & 0 deletions share/default/config/tracker.udp.benchmarking.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[logging]
log_level = "error"

[core]
remove_peerless_torrents = false
tracker_usage_statistics = false

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static INIT: Once = Once::new();

/// It redirects the log info to the standard output with the log level defined in the configuration
pub fn setup(cfg: &Configuration) {
let tracing_level = config_level_or_default(&cfg.core.log_level);
let tracing_level = config_level_or_default(&cfg.logging.log_level);

if tracing_level == LevelFilter::OFF {
return;
Expand Down
3 changes: 3 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@
//! You can control the behavior of this module with the module settings:
//!
//! ```toml
//! [logging]
//! log_level = "debug"
//!
//! [core]
//! mode = "public"
//! db_driver = "Sqlite3"
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,15 @@
//! The default configuration is:
//!
//! ```toml
//! [logging]
//! log_level = "info"
//!
//! [core]
//! announce_interval = 120
//! db_driver = "Sqlite3"
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
//! external_ip = "0.0.0.0"
//! inactive_peer_cleanup_interval = 600
//! log_level = "info"
//! max_peer_timeout = 900
//! min_announce_interval = 120
//! mode = "public"
Expand Down

0 comments on commit ef9461a

Please sign in to comment.