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

Commit 9b1b78f

Browse files
committed
fix: [#28] add mandatory PROVIDER parameter to all infrastructure scripts
- Updated all infrastructure scripts to require PROVIDER parameter without defaults - Added provider auto-detection logic to e2e test script based on environment - Modified scripts: provision-infrastructure.sh, deploy-app.sh, health-check.sh, configure-env.sh, validate-config.sh - Updated Makefile to provide defaults only for development workflows (dev-* targets) - Fixed e2e test to include PROVIDER parameter in all make commands - Renamed config files to explicit provider format (development-libvirt.env, production-hetzner.env) - All scripts now fail appropriately when required parameters are missing - Development workflows maintain convenience with automatic defaults Changes eliminate ambiguity about which provider is being used and ensure explicit provider specification for all infrastructure operations.
1 parent d140fd1 commit 9b1b78f

File tree

10 files changed

+170
-72
lines changed

10 files changed

+170
-72
lines changed

Makefile

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
# Default variables
1313
VM_NAME ?= torrust-tracker-demo
14-
# Default values
15-
ENVIRONMENT ?= development
16-
PROVIDER ?= libvirt
14+
# Defaults for quick development workflows only
15+
DEV_ENVIRONMENT ?= development
16+
DEV_PROVIDER ?= libvirt
1717
TERRAFORM_DIR = infrastructure/terraform
1818
INFRA_TESTS_DIR = infrastructure/tests
1919
TESTS_DIR = tests
@@ -62,8 +62,9 @@ help: ## Show this help message
6262
@awk 'BEGIN {FS = ":.*?## "} /^(install-deps|clean).*:.*?## / {printf " %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
6363
@echo ""
6464
@echo "Development examples:"
65-
@echo " make dev-deploy ENVIRONMENT=development PROVIDER=libvirt"
65+
@echo " make dev-deploy # Uses defaults: development + libvirt"
6666
@echo " make infra-apply ENVIRONMENT=development PROVIDER=libvirt"
67+
@echo " make infra-apply ENVIRONMENT=production PROVIDER=hetzner"
6768
@echo " make app-deploy ENVIRONMENT=development"
6869

6970
install-deps: ## Install required dependencies (Ubuntu/Debian)
@@ -252,10 +253,10 @@ dev-setup: ## Complete development setup
252253
@make install-deps
253254

254255
dev-deploy: ## Full deployment workflow (infra + app)
255-
@echo "Running full deployment workflow for $(ENVIRONMENT)..."
256-
@make infra-apply ENVIRONMENT=$(ENVIRONMENT)
257-
@make app-deploy ENVIRONMENT=$(ENVIRONMENT)
258-
@make app-health-check ENVIRONMENT=$(ENVIRONMENT)
256+
@echo "Running full deployment workflow for $(DEV_ENVIRONMENT) with $(DEV_PROVIDER)..."
257+
@make infra-apply ENVIRONMENT=$(DEV_ENVIRONMENT) PROVIDER=$(DEV_PROVIDER)
258+
@make app-deploy ENVIRONMENT=$(DEV_ENVIRONMENT)
259+
@make app-health-check ENVIRONMENT=$(DEV_ENVIRONMENT)
259260
@echo "✅ Development deployment complete"
260261

261262
dev-test: ## Quick validation (syntax + unit tests)
@@ -266,7 +267,7 @@ dev-test: ## Quick validation (syntax + unit tests)
266267

267268
dev-clean: ## Complete cleanup
268269
@echo "Cleaning up development environment..."
269-
@make infra-destroy ENVIRONMENT=$(ENVIRONMENT) || true
270+
@make infra-destroy ENVIRONMENT=$(DEV_ENVIRONMENT) PROVIDER=$(DEV_PROVIDER) || true
270271
@make clean
271272
@echo "✅ Development environment cleaned"
272273

@@ -276,7 +277,7 @@ dev-clean: ## Complete cleanup
276277

277278
test-e2e: ## Run comprehensive end-to-end test (follows integration guide)
278279
@echo "Running comprehensive end-to-end test..."
279-
$(TESTS_DIR)/test-e2e.sh $(ENVIRONMENT)
280+
$(TESTS_DIR)/test-e2e.sh $(DEV_ENVIRONMENT)
280281

281282
test-ci: ## Run project-wide CI tests (global concerns)
282283
@echo "Running project-wide CI tests..."

infrastructure/scripts/configure-env.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ CONFIG_DIR="${PROJECT_ROOT}/infrastructure/config"
1313
# shellcheck source=../../scripts/shell-utils.sh
1414
source "${PROJECT_ROOT}/scripts/shell-utils.sh"
1515

16-
# Default values
17-
ENVIRONMENT="${1:-development}"
16+
# Parse arguments - NO DEFAULTS
17+
if [ $# -lt 1 ]; then
18+
echo "ERROR: ENVIRONMENT parameter is required"
19+
echo "Usage: $0 <ENVIRONMENT>"
20+
echo "Example: $0 development"
21+
exit 1
22+
fi
23+
24+
ENVIRONMENT="$1"
1825
VERBOSE="${VERBOSE:-false}"
1926

2027
# Source shared shell utilities

infrastructure/scripts/deploy-app.sh

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1010
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
1111
TERRAFORM_DIR="${PROJECT_ROOT}/infrastructure/terraform"
1212

13-
# Default values
14-
ENVIRONMENT="${1:-development}"
13+
# Source shared shell utilities first
14+
# shellcheck source=../../scripts/shell-utils.sh
15+
source "${PROJECT_ROOT}/scripts/shell-utils.sh"
16+
17+
# Parse arguments - NO DEFAULTS
18+
if [ $# -lt 1 ]; then
19+
echo "ERROR: ENVIRONMENT parameter is required"
20+
echo "Usage: $0 <ENVIRONMENT> [VM_IP]"
21+
echo "Example: $0 development"
22+
exit 1
23+
fi
24+
25+
ENVIRONMENT="$1"
1526
VM_IP="${2:-}"
1627
SKIP_HEALTH_CHECK="${SKIP_HEALTH_CHECK:-false}"
1728
SKIP_WAIT="${SKIP_WAIT:-false}" # New parameter for skipping waiting
1829
ENABLE_HTTPS="${ENABLE_SSL:-true}" # Enable HTTPS with self-signed certificates by default
1930

20-
# Source shared shell utilities
21-
# shellcheck source=../../scripts/shell-utils.sh
22-
source "${PROJECT_ROOT}/scripts/shell-utils.sh"
23-
2431
# Get VM IP from Terraform output or parameter
2532
get_vm_ip() {
2633
if [[ -n "${VM_IP}" ]]; then
@@ -299,8 +306,38 @@ generate_nginx_http_config() {
299306
exit 1
300307
fi
301308

302-
# Load environment variables from the generated config
303-
local env_file="${PROJECT_ROOT}/infrastructure/config/environments/${ENVIRONMENT}.env"
309+
# Load environment variables from the provider-specific config
310+
# Try to auto-detect provider-specific config file
311+
local env_file=""
312+
local config_dir="${PROJECT_ROOT}/infrastructure/config/environments"
313+
314+
# Look for provider-specific config files for this environment
315+
local available_configs=()
316+
while IFS= read -r -d '' file; do
317+
if [[ "$(basename "$file")" =~ ^${ENVIRONMENT}-.*\.env$ ]]; then
318+
available_configs+=("$file")
319+
fi
320+
done < <(find "${config_dir}" -name "${ENVIRONMENT}-*.env" -type f -print0 2>/dev/null)
321+
322+
if [[ ${#available_configs[@]} -eq 0 ]]; then
323+
log_error "No provider-specific configuration found for environment: ${ENVIRONMENT}"
324+
log_error "Expected format: ${config_dir}/${ENVIRONMENT}-<provider>.env"
325+
log_info "Available files:"
326+
find "${config_dir}" -name "*.env" -type f 2>/dev/null || echo "No .env files found"
327+
exit 1
328+
elif [[ ${#available_configs[@]} -eq 1 ]]; then
329+
env_file="${available_configs[0]}"
330+
log_info "Found configuration: ${env_file}"
331+
else
332+
# Multiple configs found - need provider specification
333+
log_error "Multiple provider configurations found for environment: ${ENVIRONMENT}"
334+
for config in "${available_configs[@]}"; do
335+
log_error " - $(basename "$config")"
336+
done
337+
log_error "Please specify provider in the call or ensure only one config exists"
338+
exit 1
339+
fi
340+
304341
if [[ -f "${env_file}" ]]; then
305342
log_info "Loading environment variables from ${env_file}"
306343
# Export variables for envsubst, filtering out comments and empty lines
@@ -310,7 +347,6 @@ generate_nginx_http_config() {
310347
set +a # stop auto-exporting
311348
else
312349
log_error "Environment file not found: ${env_file}"
313-
log_error "Run 'make infra-config ENVIRONMENT=${ENVIRONMENT}' first"
314350
exit 1
315351
fi
316352

infrastructure/scripts/generate-secrets.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ set -euo pipefail
77
echo "=== Torrust Tracker Secret Generator ==="
88
echo ""
99
echo "Generating secure random secrets for production deployment..."
10-
echo "Copy these values into your infrastructure/config/environments/production.env file:"
10+
echo "Copy these values into your provider-specific production environment file:"
11+
echo "Example: infrastructure/config/environments/production-hetzner.env"
1112
echo ""
1213

1314
echo "# === GENERATED SECRETS ==="

infrastructure/scripts/health-check.sh

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,50 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1010
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
1111
TERRAFORM_DIR="${PROJECT_ROOT}/infrastructure/terraform"
1212

13-
# Default values
14-
ENVIRONMENT="${1:-development}"
15-
VM_IP="${2:-}"
16-
VERBOSE="${VERBOSE:-false}"
17-
18-
# Source shared shell utilities
13+
# Source shared shell utilities first
1914
# shellcheck source=../../scripts/shell-utils.sh
2015
source "${PROJECT_ROOT}/scripts/shell-utils.sh"
2116

17+
# Parse arguments - NO DEFAULTS
18+
if [ $# -lt 1 ]; then
19+
echo "ERROR: ENVIRONMENT parameter is required"
20+
echo "Usage: $0 <ENVIRONMENT> [VM_IP]"
21+
echo "Example: $0 development"
22+
exit 1
23+
fi
24+
25+
ENVIRONMENT="$1"
26+
VM_IP="${2:-}"
27+
VERBOSE="${VERBOSE:-false}"
28+
2229
# Load environment variables
2330
load_environment() {
24-
local env_file="${PROJECT_ROOT}/infrastructure/config/environments/${ENVIRONMENT}.env"
31+
# Try to auto-detect provider-specific config file
32+
local config_dir="${PROJECT_ROOT}/infrastructure/config/environments"
33+
34+
# Look for provider-specific config files for this environment
35+
local available_configs=()
36+
while IFS= read -r -d '' file; do
37+
if [[ "$(basename "$file")" =~ ^${ENVIRONMENT}-.*\.env$ ]]; then
38+
available_configs+=("$file")
39+
fi
40+
done < <(find "${config_dir}" -name "${ENVIRONMENT}-*.env" -type f -print0 2>/dev/null)
41+
42+
if [[ ${#available_configs[@]} -eq 0 ]]; then
43+
log_warning "No provider-specific configuration found for environment: ${ENVIRONMENT}"
44+
log_warning "Expected format: ${config_dir}/${ENVIRONMENT}-<provider>.env"
45+
log_warning "Some tests may fail without proper configuration"
46+
return 1
47+
elif [[ ${#available_configs[@]} -eq 1 ]]; then
48+
local env_file="${available_configs[0]}"
49+
log_info "Found configuration: ${env_file}"
50+
else
51+
# Multiple configs found - use the first one but warn
52+
local env_file="${available_configs[0]}"
53+
log_warning "Multiple provider configurations found for environment: ${ENVIRONMENT}"
54+
log_warning "Using: $(basename "$env_file")"
55+
fi
56+
2557
if [[ -f "${env_file}" ]]; then
2658
log_info "Loading environment variables from ${env_file}"
2759
# Export variables for use in tests, filtering out comments and empty lines
@@ -32,7 +64,7 @@ load_environment() {
3264
else
3365
log_warning "Environment file not found: ${env_file}"
3466
log_warning "Some tests may fail without proper configuration"
35-
log_info "To create environment file: make infra-config ENVIRONMENT=${ENVIRONMENT}"
67+
return 1
3668
fi
3769
}
3870

infrastructure/scripts/provision-infrastructure.sh

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,43 @@ PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
1111
TERRAFORM_DIR="${PROJECT_ROOT}/infrastructure/terraform"
1212
CONFIG_DIR="${PROJECT_ROOT}/infrastructure/config"
1313

14-
# Parse arguments with provider support
15-
ENVIRONMENT="${1:-development}"
16-
PROVIDER="${2:-libvirt}" # New: Provider parameter
17-
ACTION="${3:-apply}" # Shifted due to provider parameter
18-
SKIP_WAIT="${SKIP_WAIT:-false}"
19-
20-
# Source shared shell utilities
14+
# Source shared shell utilities first
2115
# shellcheck source=../../scripts/shell-utils.sh
2216
source "${PROJECT_ROOT}/scripts/shell-utils.sh"
2317

18+
# Parse arguments with provider support - NO DEFAULTS
19+
if [ $# -lt 2 ]; then
20+
echo "ERROR: Missing required parameters"
21+
echo "Usage: $0 <ENVIRONMENT> <PROVIDER> [ACTION]"
22+
echo "Example: $0 development libvirt apply"
23+
echo "Available providers: libvirt, hetzner"
24+
exit 1
25+
fi
26+
27+
ENVIRONMENT="$1"
28+
PROVIDER="$2"
29+
ACTION="${3:-apply}" # Only ACTION has a default
30+
SKIP_WAIT="${SKIP_WAIT:-false}"
31+
2432
# Load provider interface
2533
# shellcheck source=providers/provider-interface.sh
2634
source "${SCRIPT_DIR}/providers/provider-interface.sh"
2735

2836
# Load environment configuration
2937
load_environment() {
30-
local config_script="${SCRIPT_DIR}/configure-env.sh"
38+
log_info "Loading environment configuration: ${ENVIRONMENT} for provider: ${PROVIDER}"
3139

32-
if [[ -f "${config_script}" ]]; then
33-
log_info "Loading environment configuration: ${ENVIRONMENT}"
34-
35-
# Source the environment variables
36-
if ! "${config_script}" "${ENVIRONMENT}"; then
37-
log_error "Failed to load environment configuration"
38-
exit 1
39-
fi
40-
41-
# Load the generated environment file
42-
local env_file="${CONFIG_DIR}/environments/${ENVIRONMENT}.env"
43-
if [[ -f "${env_file}" ]]; then
44-
# shellcheck source=/dev/null
45-
source "${env_file}"
46-
log_info "Environment variables loaded from: ${env_file}"
47-
else
48-
log_error "Environment file not found: ${env_file}"
49-
exit 1
50-
fi
40+
# Load the provider-specific environment file directly
41+
local env_file="${CONFIG_DIR}/environments/${ENVIRONMENT}-${PROVIDER}.env"
42+
if [[ -f "${env_file}" ]]; then
43+
# shellcheck source=/dev/null
44+
source "${env_file}"
45+
log_info "Environment variables loaded from: ${env_file}"
5146
else
52-
log_error "Configuration script not found: ${config_script}"
47+
log_error "Environment file not found: ${env_file}"
48+
log_error "Expected file: ${env_file}"
49+
log_info "Available files:"
50+
find "${CONFIG_DIR}/environments/" -name "*.env" -type f 2>/dev/null || echo "No .env files found"
5351
exit 1
5452
fi
5553
}

infrastructure/scripts/validate-config.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
1212
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1313
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
1414

15-
# Default values
16-
ENVIRONMENT="${1:-development}"
17-
VERBOSE="${VERBOSE:-false}"
18-
19-
# Source shared shell utilities
15+
# Source shared shell utilities first
2016
# shellcheck source=../../scripts/shell-utils.sh
2117
source "${PROJECT_ROOT}/scripts/shell-utils.sh"
2218

19+
# Parse arguments - NO DEFAULTS
20+
if [ $# -lt 1 ]; then
21+
echo "ERROR: ENVIRONMENT parameter is required"
22+
echo "Usage: $0 <ENVIRONMENT>"
23+
echo "Example: $0 development"
24+
exit 1
25+
fi
26+
27+
ENVIRONMENT="$1"
28+
VERBOSE="${VERBOSE:-false}"
29+
2330
# Check if required tools are available
2431
check_dependencies() {
2532
local missing_tools=()

infrastructure/terraform/providers/libvirt/provider.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ provider_validate_ssh_key() {
8686
log_error " Generate with: ssh-keygen -t rsa -b 4096 -f ~/.ssh/torrust_rsa -C \"[email protected]\""
8787
log_error ""
8888
log_error "Option 2: Configure SSH key in environment"
89-
log_error " Edit: infrastructure/config/environments/development.env"
89+
log_error " Edit: infrastructure/config/environments/development-libvirt.env"
9090
log_error " Set: SSH_PUBLIC_KEY=\"your-ssh-public-key-content\""
9191
log_error ""
9292
log_error "Option 3: Use existing SSH key"

project-words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Hillsboro
4646
HSTS
4747
INFOHASH
4848
initdb
49+
INNODB
4950
journalctl
5051
keygen
5152
keyrings

0 commit comments

Comments
 (0)