Skip to content

Commit 2f94f6c

Browse files
committedJun 17, 2024··
feat!: [torrust#878] extract database section in core config section
1 parent 77dd938 commit 2f94f6c

File tree

14 files changed

+92
-71
lines changed

14 files changed

+92
-71
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_CORE__DB_DRIVER="Sqlite3"
99+
ARG TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER}
107+
ENV TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER:-MySQL}
7+
- TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER=${TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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/containers.md

+4-3
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_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.
152+
- `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.
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`).
@@ -243,8 +243,9 @@ podman run -it \
243243
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:
244244

245245
```toml
246-
db_driver = "MySQL"
247-
db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
246+
[core.database]
247+
driver = "MySQL"
248+
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
248249
```
249250

250251
### Build and Run:

‎packages/configuration/src/v1/core.rs

+8-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::net::{IpAddr, Ipv4Addr};
22

33
use serde::{Deserialize, Serialize};
4-
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};
4+
use torrust_tracker_primitives::TrackerMode;
55

6+
use crate::v1::database::Database;
67
use crate::AnnouncePolicy;
78

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

15-
// Database configuration
16-
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
17-
#[serde(default = "Core::default_db_driver")]
18-
pub db_driver: DatabaseDriver,
19-
20-
/// Database connection string. The format depends on the database driver.
21-
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
22-
/// `./storage/tracker/lib/database/sqlite3.db`.
23-
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
24-
/// example: `root:password@localhost:3306/torrust`.
25-
#[serde(default = "Core::default_db_path")]
26-
pub db_path: String,
16+
// Database configuration.
17+
#[serde(default = "Core::default_database")]
18+
pub database: Database,
2719

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

8880
Self {
8981
mode: Self::default_mode(),
90-
db_driver: Self::default_db_driver(),
91-
db_path: Self::default_db_path(),
82+
database: Self::default_database(),
9283
announce_interval: announce_policy.interval,
9384
min_announce_interval: announce_policy.interval_min,
9485
max_peer_timeout: Self::default_max_peer_timeout(),
@@ -107,12 +98,8 @@ impl Core {
10798
TrackerMode::Public
10899
}
109100

110-
fn default_db_driver() -> DatabaseDriver {
111-
DatabaseDriver::Sqlite3
112-
}
113-
114-
fn default_db_path() -> String {
115-
String::from("./storage/tracker/lib/database/sqlite3.db")
101+
fn default_database() -> Database {
102+
Database::default()
116103
}
117104

118105
fn default_on_reverse_proxy() -> bool {
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use serde::{Deserialize, Serialize};
2+
use torrust_tracker_primitives::DatabaseDriver;
3+
4+
#[allow(clippy::struct_excessive_bools)]
5+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
6+
pub struct Database {
7+
// Database configuration
8+
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
9+
#[serde(default = "Database::default_driver")]
10+
pub driver: DatabaseDriver,
11+
12+
/// Database connection string. The format depends on the database driver.
13+
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
14+
/// `./storage/tracker/lib/database/sqlite3.db`.
15+
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
16+
/// example: `root:password@localhost:3306/torrust`.
17+
#[serde(default = "Database::default_path")]
18+
pub path: String,
19+
}
20+
21+
impl Default for Database {
22+
fn default() -> Self {
23+
Self {
24+
driver: Self::default_driver(),
25+
path: Self::default_path(),
26+
}
27+
}
28+
}
29+
30+
impl Database {
31+
fn default_driver() -> DatabaseDriver {
32+
DatabaseDriver::Sqlite3
33+
}
34+
35+
fn default_path() -> String {
36+
String::from("./storage/tracker/lib/database/sqlite3.db")
37+
}
38+
}

‎packages/configuration/src/v1/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@
198198
//!
199199
//! [core]
200200
//! mode = "public"
201-
//! db_driver = "Sqlite3"
202-
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
203201
//! announce_interval = 120
204202
//! min_announce_interval = 120
205203
//! on_reverse_proxy = false
@@ -210,6 +208,10 @@
210208
//! inactive_peer_cleanup_interval = 600
211209
//! remove_peerless_torrents = true
212210
//!
211+
//! [core.database]
212+
//! driver = "Sqlite3"
213+
//! path = "./storage/tracker/lib/database/sqlite3.db"
214+
//!
213215
//! [[udp_trackers]]
214216
//! enabled = false
215217
//! bind_address = "0.0.0.0:6969"
@@ -234,6 +236,7 @@
234236
//! bind_address = "127.0.0.1:1313"
235237
//!```
236238
pub mod core;
239+
pub mod database;
237240
pub mod health_check_api;
238241
pub mod http_tracker;
239242
pub mod logging;
@@ -382,8 +385,6 @@ mod tests {
382385
383386
[core]
384387
mode = "public"
385-
db_driver = "Sqlite3"
386-
db_path = "./storage/tracker/lib/database/sqlite3.db"
387388
announce_interval = 120
388389
min_announce_interval = 120
389390
on_reverse_proxy = false
@@ -394,6 +395,10 @@ mod tests {
394395
inactive_peer_cleanup_interval = 600
395396
remove_peerless_torrents = true
396397
398+
[core.database]
399+
driver = "Sqlite3"
400+
path = "./storage/tracker/lib/database/sqlite3.db"
401+
397402
[[udp_trackers]]
398403
enabled = false
399404
bind_address = "0.0.0.0:6969"
@@ -490,8 +495,8 @@ mod tests {
490495
fn default_configuration_could_be_overwritten_from_a_single_env_var_with_toml_contents() {
491496
figment::Jail::expect_with(|_jail| {
492497
let config_toml = r#"
493-
[core]
494-
db_path = "OVERWRITTEN DEFAULT DB PATH"
498+
[core.database]
499+
path = "OVERWRITTEN DEFAULT DB PATH"
495500
"#
496501
.to_string();
497502

@@ -502,7 +507,7 @@ mod tests {
502507

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

505-
assert_eq!(configuration.core.db_path, "OVERWRITTEN DEFAULT DB PATH".to_string());
510+
assert_eq!(configuration.core.database.path, "OVERWRITTEN DEFAULT DB PATH".to_string());
506511

507512
Ok(())
508513
});
@@ -514,8 +519,8 @@ mod tests {
514519
jail.create_file(
515520
"tracker.toml",
516521
r#"
517-
[core]
518-
db_path = "OVERWRITTEN DEFAULT DB PATH"
522+
[core.database]
523+
path = "OVERWRITTEN DEFAULT DB PATH"
519524
"#,
520525
)?;
521526

@@ -526,7 +531,7 @@ mod tests {
526531

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

529-
assert_eq!(configuration.core.db_path, "OVERWRITTEN DEFAULT DB PATH".to_string());
534+
assert_eq!(configuration.core.database.path, "OVERWRITTEN DEFAULT DB PATH".to_string());
530535

531536
Ok(())
532537
});

‎packages/test-helpers/src/configuration.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn ephemeral() -> Configuration {
5454
let temp_directory = env::temp_dir();
5555
let random_db_id = random::string(16);
5656
let temp_file = temp_directory.join(format!("data_{random_db_id}.db"));
57-
temp_file.to_str().unwrap().clone_into(&mut config.core.db_path);
57+
temp_file.to_str().unwrap().clone_into(&mut config.core.database.path);
5858

5959
config
6060
}

‎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_CORE__DB_DRIVER" ]; then
30-
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER" "Sqlite3"; then
29+
if [ -n "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER" ]; then
30+
if cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER" "MySQL"; then
38+
elif cmp_lc "$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__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_CORE__DB_DRIVER\"."
46+
echo "Error: Unsupported Database Type: \"$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER\"."
4747
echo "Please Note: Supported Database Types: \"Sqlite3\", \"MySQL\"."
4848
exit 1
4949
fi
5050
else
51-
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DB_DRIVER\" was not set!"; exit 1;
51+
echo "Error: \"\$TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__DATABASE__DRIVER\" was not set!"; exit 1;
5252
fi
5353

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[core]
2-
db_driver = "MySQL"
3-
db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
1+
[core.database]
2+
driver = "MySQL"
3+
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"
44

55
[[http_trackers]]
66
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[core]
2-
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
1+
[core.database]
2+
path = "/var/lib/torrust/tracker/database/sqlite3.db"
33

44
[[http_trackers]]
55
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[core]
2-
db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
1+
[core.database]
2+
path = "/var/lib/torrust/tracker/database/sqlite3.db"
33

44
[[udp_trackers]]
55
enabled = true

‎src/console/ci/e2e/logs_parser.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,7 @@ mod tests {
112112

113113
#[test]
114114
fn it_should_parse_from_logs_with_valid_logs() {
115-
let logs = r#"
116-
Loading configuration from environment variable db_path = "/var/lib/torrust/tracker/database/sqlite3.db"
117-
118-
[[udp_trackers]]
119-
enabled = true
120-
121-
[[http_trackers]]
122-
enabled = true
123-
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"
124-
ssl_key_path = "/var/lib/torrust/tracker/tls/localhost.key"
125-
126-
[http_api]
127-
ssl_cert_path = "/var/lib/torrust/tracker/tls/localhost.crt"
128-
ssl_key_path = "/var/lib/torrust/tracker/tls/localhost.key"
129-
115+
let logs = r"
130116
Loading configuration from default configuration file: `./share/default/config/tracker.development.sqlite3.toml` ...
131117
2024-06-10T16:07:39.989540Z INFO torrust_tracker::bootstrap::logging: logging initialized.
132118
2024-06-10T16:07:39.990244Z INFO UDP TRACKER: Starting on: udp://0.0.0.0:6969
@@ -139,7 +125,7 @@ mod tests {
139125
2024-06-10T16:07:39.990565Z INFO API: Started on http://127.0.0.1:1212
140126
2024-06-10T16:07:39.990577Z INFO HEALTH CHECK API: Starting on: http://127.0.0.1:1313
141127
2024-06-10T16:07:39.990638Z INFO HEALTH CHECK API: Started on: http://127.0.0.1:1313
142-
"#;
128+
";
143129

144130
let running_services = RunningServices::parse_from_logs(logs);
145131

‎src/core/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@
317317
//!
318318
//! [core]
319319
//! mode = "public"
320-
//! db_driver = "Sqlite3"
321-
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
322320
//! announce_interval = 120
323321
//! min_announce_interval = 120
324322
//! max_peer_timeout = 900
@@ -328,6 +326,10 @@
328326
//! persistent_torrent_completed_stat = true
329327
//! inactive_peer_cleanup_interval = 600
330328
//! remove_peerless_torrents = false
329+
//!
330+
//! [core.database]
331+
//! driver = "Sqlite3"
332+
//! path = "./storage/tracker/lib/database/sqlite3.db"
331333
//! ```
332334
//!
333335
//! Refer to the [`configuration` module documentation](https://docs.rs/torrust-tracker-configuration) to get more information about all options.
@@ -548,7 +550,7 @@ impl Tracker {
548550
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
549551
stats_repository: statistics::Repo,
550552
) -> Result<Tracker, databases::error::Error> {
551-
let database = Arc::new(databases::driver::build(&config.db_driver, &config.db_path)?);
553+
let database = Arc::new(databases::driver::build(&config.database.driver, &config.database.path)?);
552554

553555
let mode = config.mode.clone();
554556

‎src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@
172172
//!
173173
//! [core]
174174
//! announce_interval = 120
175-
//! db_driver = "Sqlite3"
176-
//! db_path = "./storage/tracker/lib/database/sqlite3.db"
177175
//! external_ip = "0.0.0.0"
178176
//! inactive_peer_cleanup_interval = 600
179177
//! max_peer_timeout = 900
@@ -184,6 +182,10 @@
184182
//! remove_peerless_torrents = true
185183
//! tracker_usage_statistics = true
186184
//!
185+
//! [core.database]
186+
//! driver = "Sqlite3"
187+
//! path = "./storage/tracker/lib/database/sqlite3.db"
188+
//!
187189
//! [[udp_trackers]]
188190
//! bind_address = "0.0.0.0:6969"
189191
//! enabled = false

0 commit comments

Comments
 (0)
Please sign in to comment.