diff --git a/Containerfile b/Containerfile index 79fae692..cdd70e33 100644 --- a/Containerfile +++ b/Containerfile @@ -96,7 +96,7 @@ 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 @@ -104,7 +104,7 @@ 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} diff --git a/compose.yaml b/compose.yaml index 1d425c74..a02302a2 100644 --- a/compose.yaml +++ b/compose.yaml @@ -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 diff --git a/docs/benchmarking.md b/docs/benchmarking.md index 1758e0de..67b680fd 100644 --- a/docs/benchmarking.md +++ b/docs/benchmarking.md @@ -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]] @@ -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]] diff --git a/docs/containers.md b/docs/containers.md index a0ba59d4..ff15cd7c 100644 --- a/docs/containers.md +++ b/docs/containers.md @@ -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`). diff --git a/packages/configuration/src/v1/core.rs b/packages/configuration/src/v1/core.rs index 5d00c67a..ae66f54f 100644 --- a/packages/configuration/src/v1/core.rs +++ b/packages/configuration/src/v1/core.rs @@ -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, /// Tracker mode. See [`TrackerMode`] for more information. #[serde(default = "Core::default_mode")] pub mode: TrackerMode, @@ -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`. @@ -35,11 +32,13 @@ 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 @@ -47,6 +46,7 @@ pub struct Core { /// address instead. #[serde(default = "Core::default_external_ip")] pub external_ip: Option, + /// 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. @@ -54,6 +54,7 @@ pub struct Core { /// 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")] @@ -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 @@ -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(), @@ -101,11 +103,6 @@ impl Default for Core { } impl Core { - #[allow(clippy::unnecessary_wraps)] - fn default_log_level() -> Option { - Some(LogLevel::Info) - } - fn default_mode() -> TrackerMode { TrackerMode::Public } diff --git a/packages/configuration/src/v1/logging.rs b/packages/configuration/src/v1/logging.rs new file mode 100644 index 00000000..c85564a0 --- /dev/null +++ b/packages/configuration/src/v1/logging.rs @@ -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, +} + +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 { + Some(LogLevel::Info) + } +} diff --git a/packages/configuration/src/v1/mod.rs b/packages/configuration/src/v1/mod.rs index 8d45270b..80997050 100644 --- a/packages/configuration/src/v1/mod.rs +++ b/packages/configuration/src/v1/mod.rs @@ -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" @@ -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; @@ -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; @@ -258,19 +263,25 @@ 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, + /// 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, + /// The HTTP API configuration. pub http_api: HttpApi, + /// The Health Check API configuration. pub health_check_api: HealthCheckApi, } @@ -278,6 +289,7 @@ pub struct Configuration { impl Default for Configuration { fn default() -> Self { Self { + logging: Logging::default(), core: Core::default(), udp_trackers: vec![UdpTracker::default()], http_trackers: vec![HttpTracker::default()], @@ -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" @@ -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(); @@ -498,6 +514,7 @@ mod tests { jail.create_file( "tracker.toml", r#" + [core] db_path = "OVERWRITTEN DEFAULT DB PATH" "#, )?; diff --git a/packages/test-helpers/src/configuration.rs b/packages/test-helpers/src/configuration.rs index 15ecd528..c35d0a85 100644 --- a/packages/test-helpers/src/configuration.rs +++ b/packages/test-helpers/src/configuration.rs @@ -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; diff --git a/share/container/entry_script_sh b/share/container/entry_script_sh index 8c704ea6..51df717c 100644 --- a/share/container/entry_script_sh +++ b/share/container/entry_script_sh @@ -26,8 +26,8 @@ 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" @@ -35,7 +35,7 @@ if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" ]; then # 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) @@ -43,12 +43,12 @@ if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_DB_DRIVER" ]; then 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" diff --git a/share/default/config/tracker.container.mysql.toml b/share/default/config/tracker.container.mysql.toml index 7678327a..61745056 100644 --- a/share/default/config/tracker.container.mysql.toml +++ b/share/default/config/tracker.container.mysql.toml @@ -1,3 +1,4 @@ +[core] db_driver = "MySQL" db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker" diff --git a/share/default/config/tracker.container.sqlite3.toml b/share/default/config/tracker.container.sqlite3.toml index da825928..01ca655c 100644 --- a/share/default/config/tracker.container.sqlite3.toml +++ b/share/default/config/tracker.container.sqlite3.toml @@ -1,3 +1,4 @@ +[core] db_path = "/var/lib/torrust/tracker/database/sqlite3.db" [[http_trackers]] diff --git a/share/default/config/tracker.e2e.container.sqlite3.toml b/share/default/config/tracker.e2e.container.sqlite3.toml index 767b5611..60d7a798 100644 --- a/share/default/config/tracker.e2e.container.sqlite3.toml +++ b/share/default/config/tracker.e2e.container.sqlite3.toml @@ -1,3 +1,4 @@ +[core] db_path = "/var/lib/torrust/tracker/database/sqlite3.db" [[udp_trackers]] diff --git a/share/default/config/tracker.udp.benchmarking.toml b/share/default/config/tracker.udp.benchmarking.toml index 00f62628..cd193c40 100644 --- a/share/default/config/tracker.udp.benchmarking.toml +++ b/share/default/config/tracker.udp.benchmarking.toml @@ -1,4 +1,7 @@ +[logging] log_level = "error" + +[core] remove_peerless_torrents = false tracker_usage_statistics = false diff --git a/src/bootstrap/app.rs b/src/bootstrap/app.rs index bdd51a58..d8242557 100644 --- a/src/bootstrap/app.rs +++ b/src/bootstrap/app.rs @@ -42,7 +42,7 @@ fn map_to_tracing_level_filter(log_level: &LogLevel) -> LevelFilter { pub fn config() -> (Configuration, LevelFilter) { let config = initialize_configuration(); - let level: LevelFilter = match &config.core.log_level { + let level: LevelFilter = match &config.logging.log_level { None => LevelFilter::INFO, Some(level) => map_to_tracing_level_filter(level), }; diff --git a/src/core/mod.rs b/src/core/mod.rs index effabb55..6da58e23 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 39d0b5b3..2ed88a68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"