Skip to content

Commit 73dec71

Browse files
josecelanoda2ce7
authored andcommitted
feat!: [torrust#878] extract logging and core section in toml config files
1 parent 4f2e23b commit 73dec71

16 files changed

+80
-25
lines changed

Containerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ RUN ["/busybox/cp", "-sp", "/busybox/sh","/busybox/cat","/busybox/ls","/busybox/
9696
COPY --from=gcc --chmod=0555 /usr/local/bin/su-exec /bin/su-exec
9797

9898
ARG TORRUST_TRACKER_CONFIG_TOML_PATH="/etc/torrust/tracker/tracker.toml"
99-
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER="Sqlite3"
99+
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER="Sqlite3"
100100
ARG USER_ID=1000
101101
ARG UDP_PORT=6969
102102
ARG HTTP_PORT=7070
103103
ARG API_PORT=1212
104104
ARG HEALTH_CHECK_API_PORT=1313
105105

106106
ENV TORRUST_TRACKER_CONFIG_TOML_PATH=${TORRUST_TRACKER_CONFIG_TOML_PATH}
107-
ENV TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER}
107+
ENV TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER}
108108
ENV USER_ID=${USER_ID}
109109
ENV UDP_PORT=${UDP_PORT}
110110
ENV HTTP_PORT=${HTTP_PORT}

compose.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
image: torrust-tracker:release
55
tty: true
66
environment:
7-
- TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER:-MySQL}
7+
- TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER:-MySQL}
88
- TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN=${TORRUST_TRACKER_CONFIG_OVERRIDE_HTTP_API__ACCESS_TOKENS__ADMIN:-MyAccessToken}
99
networks:
1010
- server_side

docs/benchmarking.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cargo build --release -p aquatic_udp_load_test
2929
Run the tracker with UDP service enabled and other services disabled and set log level to `error`.
3030

3131
```toml
32+
[logging]
3233
log_level = "error"
3334

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

165166
```toml
167+
[logging]
166168
log_level = "error"
167169

168170
[[udp_trackers]]

docs/containers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ The following environmental variables can be set:
149149

150150
- `TORRUST_TRACKER_CONFIG_TOML_PATH` - The in-container path to the tracker configuration file, (default: `"/etc/torrust/tracker/tracker.toml"`).
151151
- `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.
152-
- `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.
152+
- `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.
153153
- `TORRUST_TRACKER_CONFIG_TOML` - Load config from this environmental variable instead from a file, (i.e: `TORRUST_TRACKER_CONFIG_TOML=$(cat tracker-tracker.toml)`).
154154
- `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`).
155155
- `UDP_PORT` - The port for the UDP tracker. This should match the port used in the configuration, (default `6969`).

packages/configuration/src/v1/core.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ use std::net::{IpAddr, Ipv4Addr};
33
use serde::{Deserialize, Serialize};
44
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
55

6-
use crate::{AnnouncePolicy, LogLevel};
6+
use crate::AnnouncePolicy;
77

88
#[allow(clippy::struct_excessive_bools)]
99
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
1010
pub struct Core {
11-
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
12-
/// `Debug` and `Trace`. Default is `Info`.
13-
#[serde(default = "Core::default_log_level")]
14-
pub log_level: Option<LogLevel>,
1511
/// Tracker mode. See [`TrackerMode`] for more information.
1612
#[serde(default = "Core::default_mode")]
1713
pub mode: TrackerMode,
@@ -20,6 +16,7 @@ pub struct Core {
2016
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
2117
#[serde(default = "Core::default_db_driver")]
2218
pub db_driver: DatabaseDriver,
19+
2320
/// Database connection string. The format depends on the database driver.
2421
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
2522
/// `./storage/tracker/lib/database/sqlite3.db`.
@@ -35,25 +32,29 @@ pub struct Core {
3532
/// See [`AnnouncePolicy::interval_min`]
3633
#[serde(default = "AnnouncePolicy::default_interval_min")]
3734
pub min_announce_interval: u32,
35+
3836
/// Weather the tracker is behind a reverse proxy or not.
3937
/// If the tracker is behind a reverse proxy, the `X-Forwarded-For` header
4038
/// sent from the proxy will be used to get the client's IP address.
4139
#[serde(default = "Core::default_on_reverse_proxy")]
4240
pub on_reverse_proxy: bool,
41+
4342
/// The external IP address of the tracker. If the client is using a
4443
/// loopback IP address, this IP address will be used instead. If the peer
4544
/// is using a loopback IP address, the tracker assumes that the peer is
4645
/// in the same network as the tracker and will use the tracker's IP
4746
/// address instead.
4847
#[serde(default = "Core::default_external_ip")]
4948
pub external_ip: Option<IpAddr>,
49+
5050
/// Weather the tracker should collect statistics about tracker usage.
5151
/// If enabled, the tracker will collect statistics like the number of
5252
/// connections handled, the number of announce requests handled, etc.
5353
/// Refer to the [`Tracker`](https://docs.rs/torrust-tracker) for more
5454
/// information about the collected metrics.
5555
#[serde(default = "Core::default_tracker_usage_statistics")]
5656
pub tracker_usage_statistics: bool,
57+
5758
/// If enabled the tracker will persist the number of completed downloads.
5859
/// That's how many times a torrent has been downloaded completely.
5960
#[serde(default = "Core::default_persistent_torrent_completed_stat")]
@@ -65,10 +66,12 @@ pub struct Core {
6566
/// time, it will be removed from the torrent peer list.
6667
#[serde(default = "Core::default_max_peer_timeout")]
6768
pub max_peer_timeout: u32,
69+
6870
/// Interval in seconds that the cleanup job will run to remove inactive
6971
/// peers from the torrent peer list.
7072
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
7173
pub inactive_peer_cleanup_interval: u64,
74+
7275
/// If enabled, the tracker will remove torrents that have no peers.
7376
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
7477
/// seconds and it removes inactive peers. Eventually, the peer list of a
@@ -83,7 +86,6 @@ impl Default for Core {
8386
let announce_policy = AnnouncePolicy::default();
8487

8588
Self {
86-
log_level: Self::default_log_level(),
8789
mode: Self::default_mode(),
8890
db_driver: Self::default_db_driver(),
8991
db_path: Self::default_db_path(),
@@ -101,11 +103,6 @@ impl Default for Core {
101103
}
102104

103105
impl Core {
104-
#[allow(clippy::unnecessary_wraps)]
105-
fn default_log_level() -> Option<LogLevel> {
106-
Some(LogLevel::Info)
107-
}
108-
109106
fn default_mode() -> TrackerMode {
110107
TrackerMode::Public
111108
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
use crate::LogLevel;
4+
5+
#[allow(clippy::struct_excessive_bools)]
6+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
7+
pub struct Logging {
8+
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
9+
/// `Debug` and `Trace`. Default is `Info`.
10+
#[serde(default = "Logging::default_log_level")]
11+
pub log_level: Option<LogLevel>,
12+
}
13+
14+
impl Default for Logging {
15+
fn default() -> Self {
16+
Self {
17+
log_level: Self::default_log_level(),
18+
}
19+
}
20+
}
21+
22+
impl Logging {
23+
#[allow(clippy::unnecessary_wraps)]
24+
fn default_log_level() -> Option<LogLevel> {
25+
Some(LogLevel::Info)
26+
}
27+
}

packages/configuration/src/v1/mod.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@
193193
//! The default configuration is:
194194
//!
195195
//! ```toml
196+
//! [logging]
196197
//! log_level = "info"
198+
//!
199+
//! [core]
197200
//! mode = "public"
198201
//! db_driver = "Sqlite3"
199202
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
@@ -233,6 +236,7 @@
233236
pub mod core;
234237
pub mod health_check_api;
235238
pub mod http_tracker;
239+
pub mod logging;
236240
pub mod tracker_api;
237241
pub mod udp_tracker;
238242

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

242246
use figment::providers::{Env, Format, Serialized, Toml};
243247
use figment::Figment;
248+
use logging::Logging;
244249
use serde::{Deserialize, Serialize};
245250

246251
use self::core::Core;
@@ -258,26 +263,33 @@ const CONFIG_OVERRIDE_SEPARATOR: &str = "__";
258263
/// Core configuration for the tracker.
259264
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
260265
pub struct Configuration {
266+
/// Logging configuration
267+
pub logging: Logging,
268+
261269
/// Core configuration.
262-
#[serde(flatten)]
263270
pub core: Core,
271+
264272
/// The list of UDP trackers the tracker is running. Each UDP tracker
265273
/// represents a UDP server that the tracker is running and it has its own
266274
/// configuration.
267275
pub udp_trackers: Vec<UdpTracker>,
276+
268277
/// The list of HTTP trackers the tracker is running. Each HTTP tracker
269278
/// represents a HTTP server that the tracker is running and it has its own
270279
/// configuration.
271280
pub http_trackers: Vec<HttpTracker>,
281+
272282
/// The HTTP API configuration.
273283
pub http_api: HttpApi,
284+
274285
/// The Health Check API configuration.
275286
pub health_check_api: HealthCheckApi,
276287
}
277288

278289
impl Default for Configuration {
279290
fn default() -> Self {
280291
Self {
292+
logging: Logging::default(),
281293
core: Core::default(),
282294
udp_trackers: vec![UdpTracker::default()],
283295
http_trackers: vec![HttpTracker::default()],
@@ -365,7 +377,10 @@ mod tests {
365377

366378
#[cfg(test)]
367379
fn default_config_toml() -> String {
368-
let config = r#"log_level = "info"
380+
let config = r#"[logging]
381+
log_level = "info"
382+
383+
[core]
369384
mode = "public"
370385
db_driver = "Sqlite3"
371386
db_path = "./storage/tracker/lib/database/sqlite3.db"
@@ -475,6 +490,7 @@ mod tests {
475490
fn default_configuration_could_be_overwritten_from_a_single_env_var_with_toml_contents() {
476491
figment::Jail::expect_with(|_jail| {
477492
let config_toml = r#"
493+
[core]
478494
db_path = "OVERWRITTEN DEFAULT DB PATH"
479495
"#
480496
.to_string();
@@ -498,6 +514,7 @@ mod tests {
498514
jail.create_file(
499515
"tracker.toml",
500516
r#"
517+
[core]
501518
db_path = "OVERWRITTEN DEFAULT DB PATH"
502519
"#,
503520
)?;

packages/test-helpers/src/configuration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn ephemeral() -> Configuration {
2929

3030
let mut config = Configuration::default();
3131

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

3434
// Ephemeral socket address for API
3535
let api_port = 0u16;

share/container/entry_script_sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,29 @@ chmod -R 2770 /var/lib/torrust /var/log/torrust /etc/torrust
2626

2727

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

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

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

38-
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" "MySQL"; then
38+
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER" "MySQL"; then
3939

4040
# (no database file needed for MySQL)
4141

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

4545
else
46-
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER\"."
46+
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER\"."
4747
echo "Please Note: Supported Database Types: \"Sqlite3\", \"MySQL\"."
4848
exit 1
4949
fi
5050
else
51-
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER\" was not set!"; exit 1;
51+
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER\" was not set!"; exit 1;
5252
fi
5353

5454
install_config="/etc/torrust/tracker/tracker.toml"

share/default/config/tracker.container.mysql.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[core]
12
db_driver = "MySQL"
23
db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
34

share/default/config/tracker.container.sqlite3.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[core]
12
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
23

34
[[http_trackers]]

share/default/config/tracker.e2e.container.sqlite3.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[core]
12
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
23

34
[[udp_trackers]]

share/default/config/tracker.udp.benchmarking.toml

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
[logging]
12
log_level = "error"
3+
4+
[core]
25
remove_peerless_torrents = false
36
tracker_usage_statistics = false
47

src/bootstrap/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn map_to_tracing_level_filter(log_level: &LogLevel) -> LevelFilter {
4242
pub fn config() -> (Configuration, LevelFilter) {
4343
let config = initialize_configuration();
4444

45-
let level: LevelFilter = match &config.core.log_level {
45+
let level: LevelFilter = match &config.logging.log_level {
4646
None => LevelFilter::INFO,
4747
Some(level) => map_to_tracing_level_filter(level),
4848
};

src/core/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,10 @@
312312
//! You can control the behavior of this module with the module settings:
313313
//!
314314
//! ```toml
315+
//! [logging]
315316
//! log_level = "debug"
317+
//!
318+
//! [core]
316319
//! mode = "public"
317320
//! db_driver = "Sqlite3"
318321
//! db_path = "./storage/tracker/lib/database/sqlite3.db"

src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,15 @@
167167
//! The default configuration is:
168168
//!
169169
//! ```toml
170+
//! [logging]
171+
//! log_level = "info"
172+
//!
173+
//! [core]
170174
//! announce_interval = 120
171175
//! db_driver = "Sqlite3"
172176
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
173177
//! external_ip = "0.0.0.0"
174178
//! inactive_peer_cleanup_interval = 600
175-
//! log_level = "info"
176179
//! max_peer_timeout = 900
177180
//! min_announce_interval = 120
178181
//! mode = "public"

0 commit comments

Comments
 (0)