Skip to content

Commit

Permalink
feat!: [#878] extract database section in core config section
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 17, 2024
1 parent 77dd938 commit 2f94f6c
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 71 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_CORE__DB_DRIVER="Sqlite3"
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER}
ENV TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER:-MySQL}
- TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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
7 changes: 4 additions & 3 deletions 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_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_OVERRIDE_CORE__DATABASE__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 Expand Up @@ -243,8 +243,9 @@ podman run -it \
The docker-compose configuration includes the MySQL service configuration. If you want to use MySQL instead of SQLite you should verify the `/etc/torrust/tracker/tracker.toml` (i.e `./storage/tracker/etc/tracker.toml`) configuration:

```toml
db_driver = "MySQL"
db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
[core.database]
driver = "MySQL"
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
```

### Build and Run:
Expand Down
29 changes: 8 additions & 21 deletions packages/configuration/src/v1/core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::net::{IpAddr, Ipv4Addr};

use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
use torrust_tracker_primitives::TrackerMode;

use crate::v1::database::Database;
use crate::AnnouncePolicy;

#[allow(clippy::struct_excessive_bools)]
Expand All @@ -12,18 +13,9 @@ pub struct Core {
#[serde(default = "Core::default_mode")]
pub mode: TrackerMode,

// Database configuration
/// 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`.
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
/// example: `root:password@localhost:3306/torrust`.
#[serde(default = "Core::default_db_path")]
pub db_path: String,
// Database configuration.
#[serde(default = "Core::default_database")]
pub database: Database,

/// See [`AnnouncePolicy::interval`]
#[serde(default = "AnnouncePolicy::default_interval")]
Expand Down Expand Up @@ -87,8 +79,7 @@ impl Default for Core {

Self {
mode: Self::default_mode(),
db_driver: Self::default_db_driver(),
db_path: Self::default_db_path(),
database: Self::default_database(),
announce_interval: announce_policy.interval,
min_announce_interval: announce_policy.interval_min,
max_peer_timeout: Self::default_max_peer_timeout(),
Expand All @@ -107,12 +98,8 @@ impl Core {
TrackerMode::Public
}

fn default_db_driver() -> DatabaseDriver {
DatabaseDriver::Sqlite3
}

fn default_db_path() -> String {
String::from("./storage/tracker/lib/database/sqlite3.db")
fn default_database() -> Database {
Database::default()
}

fn default_on_reverse_proxy() -> bool {
Expand Down
38 changes: 38 additions & 0 deletions packages/configuration/src/v1/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use serde::{Deserialize, Serialize};
use torrust_tracker_primitives::DatabaseDriver;

#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Database {
// Database configuration
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
#[serde(default = "Database::default_driver")]
pub 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`.
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
/// example: `root:password@localhost:3306/torrust`.
#[serde(default = "Database::default_path")]
pub path: String,
}

impl Default for Database {
fn default() -> Self {
Self {
driver: Self::default_driver(),
path: Self::default_path(),
}
}
}

impl Database {
fn default_driver() -> DatabaseDriver {
DatabaseDriver::Sqlite3
}

fn default_path() -> String {
String::from("./storage/tracker/lib/database/sqlite3.db")
}
}
25 changes: 15 additions & 10 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@
//!
//! [core]
//! mode = "public"
//! db_driver = "Sqlite3"
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
//! announce_interval = 120
//! min_announce_interval = 120
//! on_reverse_proxy = false
Expand All @@ -210,6 +208,10 @@
//! inactive_peer_cleanup_interval = 600
//! remove_peerless_torrents = true
//!
//! [core.database]
//! driver = "Sqlite3"
//! path = "./storage/tracker/lib/database/sqlite3.db"
//!
//! [[udp_trackers]]
//! enabled = false
//! bind_address = "0.0.0.0:6969"
Expand All @@ -234,6 +236,7 @@
//! bind_address = "127.0.0.1:1313"
//!```
pub mod core;
pub mod database;
pub mod health_check_api;
pub mod http_tracker;
pub mod logging;
Expand Down Expand Up @@ -382,8 +385,6 @@ mod tests {
[core]
mode = "public"
db_driver = "Sqlite3"
db_path = "./storage/tracker/lib/database/sqlite3.db"
announce_interval = 120
min_announce_interval = 120
on_reverse_proxy = false
Expand All @@ -394,6 +395,10 @@ mod tests {
inactive_peer_cleanup_interval = 600
remove_peerless_torrents = true
[core.database]
driver = "Sqlite3"
path = "./storage/tracker/lib/database/sqlite3.db"
[[udp_trackers]]
enabled = false
bind_address = "0.0.0.0:6969"
Expand Down Expand Up @@ -490,8 +495,8 @@ 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"
[core.database]
path = "OVERWRITTEN DEFAULT DB PATH"
"#
.to_string();

Expand All @@ -502,7 +507,7 @@ mod tests {

let configuration = Configuration::load(&info).expect("Could not load configuration from file");

assert_eq!(configuration.core.db_path, "OVERWRITTEN DEFAULT DB PATH".to_string());
assert_eq!(configuration.core.database.path, "OVERWRITTEN DEFAULT DB PATH".to_string());

Ok(())
});
Expand All @@ -514,8 +519,8 @@ mod tests {
jail.create_file(
"tracker.toml",
r#"
[core]
db_path = "OVERWRITTEN DEFAULT DB PATH"
[core.database]
path = "OVERWRITTEN DEFAULT DB PATH"
"#,
)?;

Expand All @@ -526,7 +531,7 @@ mod tests {

let configuration = Configuration::load(&info).expect("Could not load configuration from file");

assert_eq!(configuration.core.db_path, "OVERWRITTEN DEFAULT DB PATH".to_string());
assert_eq!(configuration.core.database.path, "OVERWRITTEN DEFAULT DB PATH".to_string());

Ok(())
});
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 @@ -54,7 +54,7 @@ pub fn ephemeral() -> Configuration {
let temp_directory = env::temp_dir();
let random_db_id = random::string(16);
let temp_file = temp_directory.join(format!("data_{random_db_id}.db"));
temp_file.to_str().unwrap().clone_into(&mut config.core.db_path);
temp_file.to_str().unwrap().clone_into(&mut config.core.database.path);

config
}
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_CORE__DB_DRIVER" ]; then
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER" "Sqlite3"; then
if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" ]; then
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER" "MySQL"; then
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER\"."
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER\"."
echo "Please Note: Supported Database Types: \"Sqlite3\", \"MySQL\"."
exit 1
fi
else
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER\" was not set!"; exit 1;
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER\" was not set!"; exit 1;
fi

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

[[http_trackers]]
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"
Expand Down
4 changes: 2 additions & 2 deletions share/default/config/tracker.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[core]
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
[core.database]
path = "/var/lib/torrust/tracker/database/sqlite3.db"

[[http_trackers]]
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"
Expand Down
4 changes: 2 additions & 2 deletions share/default/config/tracker.e2e.container.sqlite3.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[core]
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
[core.database]
path = "/var/lib/torrust/tracker/database/sqlite3.db"

[[udp_trackers]]
enabled = true
Expand Down
18 changes: 2 additions & 16 deletions src/console/ci/e2e/logs_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,7 @@ mod tests {

#[test]
fn it_should_parse_from_logs_with_valid_logs() {
let logs = r#"
Loading configuration from environment variable db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
[[udp_trackers]]
enabled = true
[[http_trackers]]
enabled = true
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"
ssl_key_path = "/var/lib/torrust/tracker/tls/localhost.key"
[http_api]
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"
ssl_key_path = "/var/lib/torrust/tracker/tls/localhost.key"
let logs = r"
Loading configuration from default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: logging initialized.
2024-06-10T16:07:39.990244Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6969
Expand All @@ -139,7 +125,7 @@ mod tests {
2024-06-10T16:07:39.990565Z INFO API: Started on http://127.0.0.1:1212
2024-06-10T16:07:39.990577Z INFO HEALTH CHECK API: Starting on: http://127.0.0.1:1313
2024-06-10T16:07:39.990638Z INFO HEALTH CHECK API: Started on: http://127.0.0.1:1313
"#;
";

let running_services = RunningServices::parse_from_logs(logs);

Expand Down
8 changes: 5 additions & 3 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,6 @@
//!
//! [core]
//! mode = "public"
//! db_driver = "Sqlite3"
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
//! announce_interval = 120
//! min_announce_interval = 120
//! max_peer_timeout = 900
Expand All @@ -328,6 +326,10 @@
//! persistent_torrent_completed_stat = true
//! inactive_peer_cleanup_interval = 600
//! remove_peerless_torrents = false
//!
//! [core.database]
//! driver = "Sqlite3"
//! path = "./storage/tracker/lib/database/sqlite3.db"
//! ```
//!
//! Refer to the [`configuration` module documentation](https://docs.rs/torrust-tracker-configuration) to get more information about all options.
Expand Down Expand Up @@ -548,7 +550,7 @@ impl Tracker {
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
stats_repository: statistics::Repo,
) -> Result<Tracker, databases::error::Error> {
let database = Arc::new(databases::driver::build(&config.db_driver, &config.db_path)?);
let database = Arc::new(databases::driver::build(&config.database.driver, &config.database.path)?);

let mode = config.mode.clone();

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@
//!
//! [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
//! max_peer_timeout = 900
Expand All @@ -184,6 +182,10 @@
//! remove_peerless_torrents = true
//! tracker_usage_statistics = true
//!
//! [core.database]
//! driver = "Sqlite3"
//! path = "./storage/tracker/lib/database/sqlite3.db"
//!
//! [[udp_trackers]]
//! bind_address = "0.0.0.0:6969"
//! enabled = false
Expand Down

0 comments on commit 2f94f6c

Please sign in to comment.