Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit 94d278b

Browse files
committed
feat: [#14] implement Phase 1 of 12-Factor App refactor - configuration management
- Add environment-based configuration system with local and production environments - Create configuration templates for tracker.toml and prometheus.yml with variable substitution - Implement configure-env.sh script for processing environment files and generating configs - Add validate-config.sh script for comprehensive configuration validation - Support colored logging output consistent with existing project scripts - Include comprehensive validation for TOML/YAML syntax and environment-specific settings - Remove staging environment references (only local and production needed) - Update yamllint configuration to exclude application/storage directory from linting - Update documentation and migration guide to reflect current implementation This establishes the foundation for environment-based configuration management as outlined in the 12-Factor App methodology.
1 parent b0327d6 commit 94d278b

File tree

9 files changed

+634
-54
lines changed

9 files changed

+634
-54
lines changed

.yamllint-ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
extends: default
22

3+
ignore: |
4+
application/storage/
5+
36
rules:
47
line-length:
58
max: 120 # More reasonable for infrastructure code
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Infrastructure Configuration
2+
INFRASTRUCTURE_PROVIDER=local
3+
INFRASTRUCTURE_VM_NAME=torrust-tracker-demo
4+
INFRASTRUCTURE_VM_MEMORY=2048
5+
INFRASTRUCTURE_VM_CPUS=2
6+
7+
# Torrust Tracker Core Configuration
8+
TORRUST_TRACKER_MODE=public
9+
TORRUST_TRACKER_LOG_LEVEL=debug
10+
TORRUST_TRACKER_LISTED=false
11+
TORRUST_TRACKER_PRIVATE=false
12+
TORRUST_TRACKER_STATS=true
13+
14+
# Database Configuration
15+
TORRUST_TRACKER_DATABASE_DRIVER=mysql
16+
TORRUST_TRACKER_DATABASE_PATH=./storage/tracker/lib/database/sqlite3.db
17+
18+
# Network Configuration
19+
TORRUST_TRACKER_EXTERNAL_IP=0.0.0.0
20+
TORRUST_TRACKER_ON_REVERSE_PROXY=false
21+
22+
# Tracker Policy
23+
TORRUST_TRACKER_CLEANUP_INTERVAL=600
24+
TORRUST_TRACKER_MAX_PEER_TIMEOUT=900
25+
TORRUST_TRACKER_PERSISTENT_COMPLETED_STAT=false
26+
TORRUST_TRACKER_REMOVE_PEERLESS=true
27+
28+
# Announce Policy
29+
TORRUST_TRACKER_ANNOUNCE_INTERVAL=120
30+
TORRUST_TRACKER_ANNOUNCE_INTERVAL_MIN=120
31+
32+
# Port Configuration
33+
TORRUST_TRACKER_UDP_6868_ENABLED=true
34+
TORRUST_TRACKER_UDP_6969_ENABLED=true
35+
TORRUST_TRACKER_HTTP_ENABLED=true
36+
TORRUST_TRACKER_HTTP_PORT=7070
37+
TORRUST_TRACKER_API_PORT=1212
38+
TORRUST_TRACKER_HEALTH_CHECK_PORT=1313
39+
40+
# API Authentication
41+
TORRUST_TRACKER_API_TOKEN=local-dev-token
42+
43+
# Service Configuration
44+
GRAFANA_ADMIN_PASSWORD=admin
45+
PROMETHEUS_RETENTION_TIME=7d
46+
47+
# Docker Configuration
48+
USER_ID=1000
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Infrastructure Configuration
2+
INFRASTRUCTURE_PROVIDER=hetzner
3+
INFRASTRUCTURE_REGION=fsn1
4+
INFRASTRUCTURE_INSTANCE_TYPE=cx21
5+
6+
# Torrust Tracker Core Configuration
7+
TORRUST_TRACKER_MODE=private
8+
TORRUST_TRACKER_LOG_LEVEL=info
9+
TORRUST_TRACKER_LISTED=false
10+
TORRUST_TRACKER_PRIVATE=true
11+
TORRUST_TRACKER_STATS=true
12+
13+
# Database Configuration (MySQL for production)
14+
TORRUST_TRACKER_DATABASE_DRIVER=mysql
15+
TORRUST_TRACKER_DATABASE_URL=${TORRUST_PROD_DATABASE_URL}
16+
17+
# Network Configuration
18+
TORRUST_TRACKER_EXTERNAL_IP=${PRODUCTION_EXTERNAL_IP}
19+
TORRUST_TRACKER_ON_REVERSE_PROXY=true
20+
21+
# Tracker Policy (production optimized)
22+
TORRUST_TRACKER_CLEANUP_INTERVAL=300
23+
TORRUST_TRACKER_MAX_PEER_TIMEOUT=1800
24+
TORRUST_TRACKER_PERSISTENT_COMPLETED_STAT=true
25+
TORRUST_TRACKER_REMOVE_PEERLESS=false
26+
27+
# Announce Policy (production optimized)
28+
TORRUST_TRACKER_ANNOUNCE_INTERVAL=600
29+
TORRUST_TRACKER_ANNOUNCE_INTERVAL_MIN=300
30+
31+
# Port Configuration
32+
TORRUST_TRACKER_UDP_6868_ENABLED=true
33+
TORRUST_TRACKER_UDP_6969_ENABLED=true
34+
TORRUST_TRACKER_HTTP_ENABLED=true
35+
TORRUST_TRACKER_HTTP_PORT=7070
36+
TORRUST_TRACKER_API_PORT=1212
37+
TORRUST_TRACKER_HEALTH_CHECK_PORT=1313
38+
39+
# API Authentication (from secrets)
40+
TORRUST_TRACKER_API_TOKEN=${TORRUST_PROD_API_TOKEN}
41+
42+
# Service Configuration
43+
GRAFANA_ADMIN_PASSWORD=${GRAFANA_PROD_PASSWORD}
44+
PROMETHEUS_RETENTION_TIME=30d
45+
46+
# Security Configuration
47+
SSH_PUBLIC_KEY=${SSH_PUBLIC_KEY}
48+
DOMAIN_NAME=tracker.torrust-demo.com
49+
SSL_EMAIL=${SSL_EMAIL}
50+
51+
# Docker Configuration
52+
USER_ID=1000
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
global:
3+
scrape_interval: 15s
4+
evaluation_interval: 15s
5+
6+
rule_files:
7+
# - "first_rules.yml"
8+
# - "second_rules.yml"
9+
10+
scrape_configs:
11+
- job_name: 'prometheus'
12+
static_configs:
13+
- targets: ['localhost:9090']
14+
15+
- job_name: 'torrust-tracker'
16+
static_configs:
17+
- targets: ['tracker:${TORRUST_TRACKER_API_PORT}']
18+
metrics_path: '/metrics'
19+
scrape_interval: 30s
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[logging]
2+
threshold = "${TORRUST_TRACKER_LOG_LEVEL}"
3+
4+
[core]
5+
inactive_peer_cleanup_interval = ${TORRUST_TRACKER_CLEANUP_INTERVAL}
6+
listed = ${TORRUST_TRACKER_LISTED}
7+
private = ${TORRUST_TRACKER_PRIVATE}
8+
tracker_usage_statistics = ${TORRUST_TRACKER_STATS}
9+
10+
[core.announce_policy]
11+
interval = ${TORRUST_TRACKER_ANNOUNCE_INTERVAL}
12+
interval_min = ${TORRUST_TRACKER_ANNOUNCE_INTERVAL_MIN}
13+
14+
[core.database]
15+
driver = "${TORRUST_TRACKER_DATABASE_DRIVER}"
16+
# Database connection will be determined by driver type
17+
# For MySQL: uses environment variable or falls back to default MySQL settings
18+
# For SQLite: uses path specified in TORRUST_TRACKER_DATABASE_PATH
19+
20+
[core.net]
21+
external_ip = "${TORRUST_TRACKER_EXTERNAL_IP}"
22+
on_reverse_proxy = ${TORRUST_TRACKER_ON_REVERSE_PROXY}
23+
24+
[core.tracker_policy]
25+
max_peer_timeout = ${TORRUST_TRACKER_MAX_PEER_TIMEOUT}
26+
persistent_torrent_completed_stat = ${TORRUST_TRACKER_PERSISTENT_COMPLETED_STAT}
27+
remove_peerless_torrents = ${TORRUST_TRACKER_REMOVE_PEERLESS}
28+
29+
# Health check API (separate from main API)
30+
[health_check_api]
31+
bind_address = "127.0.0.1:${TORRUST_TRACKER_HEALTH_CHECK_PORT}"
32+
33+
# Main HTTP API
34+
[http_api]
35+
bind_address = "0.0.0.0:${TORRUST_TRACKER_API_PORT}"
36+
37+
[http_api.access_tokens]
38+
admin = "${TORRUST_TRACKER_API_TOKEN}"
39+
40+
# UDP Trackers - Port 6868
41+
[[udp_trackers]]
42+
bind_address = "0.0.0.0:6868"
43+
44+
# UDP Trackers - Port 6969
45+
[[udp_trackers]]
46+
bind_address = "0.0.0.0:6969"
47+
48+
# HTTP Trackers
49+
[[http_trackers]]
50+
bind_address = "0.0.0.0:${TORRUST_TRACKER_HTTP_PORT}"

infrastructure/docs/refactoring/twelve-factor-refactor/migration-guide.md

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ make apply
318318
The new system uses environment-specific configuration:
319319

320320
- `infrastructure/config/environments/local.env` - Local development
321-
- `infrastructure/config/environments/staging.env` - Staging environment
322321
- `infrastructure/config/environments/production.env` - Production environment
323322

324323
Process configuration before deployment:
@@ -348,30 +347,6 @@ make destroy
348347

349348
#### 4.1 Create Environment Variations
350349

351-
**Staging configuration** (`infrastructure/config/environments/staging.env`):
352-
353-
```bash
354-
# Infrastructure Configuration
355-
INFRASTRUCTURE_PROVIDER=hetzner
356-
INFRASTRUCTURE_REGION=fsn1
357-
INFRASTRUCTURE_INSTANCE_TYPE=cx11
358-
359-
# Application Configuration
360-
TORRUST_TRACKER_MODE=private
361-
TORRUST_TRACKER_LOG_LEVEL=info
362-
TORRUST_TRACKER_DATABASE_DRIVER=sqlite3
363-
TORRUST_TRACKER_API_TOKEN=${TORRUST_STAGING_API_TOKEN}
364-
365-
# Service Configuration
366-
GRAFANA_ADMIN_PASSWORD=${GRAFANA_STAGING_PASSWORD}
367-
PROMETHEUS_RETENTION_TIME=15d
368-
369-
# Security Configuration
370-
SSH_PUBLIC_KEY=${SSH_PUBLIC_KEY}
371-
DOMAIN_NAME=staging.torrust-demo.com
372-
SSL_EMAIL=${SSL_EMAIL}
373-
```
374-
375350
**Production configuration** (`infrastructure/config/environments/production.env`):
376351

377352
```bash
@@ -393,7 +368,7 @@ PROMETHEUS_RETENTION_TIME=30d
393368
394369
# Security Configuration
395370
SSH_PUBLIC_KEY=${SSH_PUBLIC_KEY}
396-
DOMAIN_NAME=torrust-demo.com
371+
DOMAIN_NAME=tracker.torrust-demo.com
397372
SSL_EMAIL=${SSL_EMAIL}
398373
```
399374

@@ -498,7 +473,7 @@ direnv allow
498473

499474
### Week 3: Environment Support
500475

501-
- [ ] Create staging and production configurations
476+
- [ ] Create production configurations
502477
- [ ] Implement environment-specific logic
503478
- [ ] Test multi-environment deployment
504479
- [ ] Validate configuration for all environments

infrastructure/docs/refactoring/twelve-factor-refactor/phase-1-implementation.md

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ mkdir -p application/config/templates
2121
**Files to create:**
2222

2323
- [ ] `infrastructure/config/environments/local.env`
24-
- [ ] `infrastructure/config/environments/staging.env`
2524
- [ ] `infrastructure/config/environments/production.env`
2625
- [ ] `infrastructure/config/templates/tracker.toml.tpl`
2726
- [ ] `infrastructure/config/templates/prometheus.yml.tpl`
@@ -81,30 +80,6 @@ PROMETHEUS_RETENTION_TIME=7d
8180
USER_ID=1000
8281
```
8382

84-
**Staging Environment (`staging.env`):**
85-
86-
```bash
87-
# Infrastructure
88-
INFRASTRUCTURE_PROVIDER=hetzner
89-
INFRASTRUCTURE_REGION=fsn1
90-
INFRASTRUCTURE_INSTANCE_TYPE=cx11
91-
92-
# Application
93-
TORRUST_TRACKER_MODE=private
94-
TORRUST_TRACKER_LOG_LEVEL=info
95-
TORRUST_TRACKER_DATABASE_DRIVER=sqlite3
96-
TORRUST_TRACKER_API_TOKEN=${TORRUST_STAGING_API_TOKEN}
97-
98-
# Services
99-
GRAFANA_ADMIN_PASSWORD=${GRAFANA_STAGING_PASSWORD}
100-
PROMETHEUS_RETENTION_TIME=15d
101-
102-
# Security
103-
SSH_PUBLIC_KEY=${SSH_PUBLIC_KEY}
104-
DOMAIN_NAME=staging.torrust-demo.com
105-
SSL_EMAIL=${SSL_EMAIL}
106-
```
107-
10883
**Production Environment (`production.env`):**
10984

11085
```bash
@@ -155,7 +130,7 @@ PROMETHEUS_RETENTION_TIME=30d
155130

156131
# Security Configuration
157132
SSH_PUBLIC_KEY=${SSH_PUBLIC_KEY}
158-
DOMAIN_NAME=torrust-demo.com
133+
DOMAIN_NAME=tracker.torrust-demo.com
159134
SSL_EMAIL=${SSL_EMAIL}
160135

161136
# Docker Configuration
@@ -498,7 +473,7 @@ main() {
498473
local failed=0
499474

500475
# Validate environment files
501-
for env in local staging production; do
476+
for env in local production; do
502477
env_file="${CONFIG_DIR}/environments/${env}.env"
503478
if ! validate_env_file "${env_file}" "${env}"; then
504479
failed=1

0 commit comments

Comments
 (0)