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

Commit 8ba6858

Browse files
committed
refactor: [#14] centralize log_section function and improve Docker Compose deployment
- Move log_section function from duplicated implementations to scripts/shell-utils.sh - Remove duplicates from tests/test-e2e.sh, infrastructure/tests/test-ci.sh, infrastructure/tests/test-local.sh - Add vm_exec_with_timeout function to deploy-app.sh for robust long-running SSH commands - Split Docker Compose pull and up commands for better progress feedback and error isolation - Remove deprecated --include-deps flag from docker compose pull (now default behavior) - Add 10-minute timeout for Docker image pulls to prevent deployment hangs - Validate E2E and deployment workflows with refactored code - All CI tests and deployment health checks pass
1 parent 42c52c4 commit 8ba6858

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

infrastructure/scripts/deploy-app.sh

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ TERRAFORM_DIR="${PROJECT_ROOT}/infrastructure/terraform"
1212

1313
# Default values
1414
ENVIRONMENT="${1:-local}"
15-
# Get script configuration
16-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17-
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
1815
VM_IP="${2:-}"
1916
SKIP_HEALTH_CHECK="${SKIP_HEALTH_CHECK:-false}"
2017

@@ -156,6 +153,24 @@ vm_exec() {
156153
fi
157154
}
158155

156+
# Execute command on VM via SSH with timeout
157+
vm_exec_with_timeout() {
158+
local vm_ip="$1"
159+
local command="$2"
160+
local timeout="${3:-300}" # Default 5 minutes
161+
local description="${4:-}"
162+
163+
if [[ -n "${description}" ]]; then
164+
log_info "${description}"
165+
fi
166+
167+
# Use timeout command to limit execution time
168+
if ! timeout "${timeout}" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=30 torrust@"${vm_ip}" "${command}"; then
169+
log_error "Failed to execute command on VM (timeout: ${timeout}s): ${command}"
170+
exit 1
171+
fi
172+
}
173+
159174
# RELEASE STAGE: Deploy application code and configuration
160175
release_stage() {
161176
local vm_ip="$1"
@@ -382,12 +397,20 @@ run_stage() {
382397
fi
383398
" "Stopping existing services"
384399

385-
# Pull latest images and start services
386-
vm_exec "${vm_ip}" "
400+
# Pull latest images with timeout (10 minutes for large images)
401+
log_info "Pulling Docker images (this may take several minutes for large images)..."
402+
vm_exec_with_timeout "${vm_ip}" "
387403
cd /home/torrust/github/torrust/torrust-tracker-demo/application
388404
389-
# Pull latest images
405+
# Pull images with progress output
406+
echo 'Starting Docker image pull...'
390407
docker compose pull
408+
echo 'Docker image pull completed'
409+
" 600 "Pulling Docker images with 10-minute timeout"
410+
411+
# Start services
412+
vm_exec "${vm_ip}" "
413+
cd /home/torrust/github/torrust/torrust-tracker-demo/application
391414
392415
# Start services
393416
docker compose up -d

infrastructure/tests/test-ci.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ source "${PROJECT_ROOT}/scripts/shell-utils.sh"
1717
# Set log file for tee output
1818
export SHELL_UTILS_LOG_FILE="${TEST_LOG_FILE}"
1919

20-
log_section() {
21-
log ""
22-
log "${BLUE}===============================================${NC}"
23-
log "${BLUE}$1${NC}"
24-
log "${BLUE}===============================================${NC}"
25-
}
26-
2720
# Initialize test log
2821
init_test_log() {
2922
init_log_file "${TEST_LOG_FILE}" "Torrust Tracker Demo - CI-Compatible Tests"

infrastructure/tests/test-local.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ source "${PROJECT_ROOT}/scripts/shell-utils.sh"
1717
# Set log file for tee output
1818
export SHELL_UTILS_LOG_FILE="${TEST_LOG_FILE}"
1919

20-
log_section() {
21-
log ""
22-
log "${BLUE}===============================================${NC}"
23-
log "${BLUE}$1${NC}"
24-
log "${BLUE}===============================================${NC}"
25-
}
26-
2720
# Initialize test log
2821
init_test_log() {
2922
init_log_file "${TEST_LOG_FILE}" "Torrust Tracker Demo - Local-Only Tests"

scripts/shell-utils.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# log_success "Operation completed successfully"
1515
# log_warning "This is a warning"
1616
# log_error "This is an error"
17+
# log_section "Major Section Title"
1718
#
1819
# # Use HTTP testing:
1920
# result=$(test_http_endpoint "http://example.com" "expected content")
@@ -78,6 +79,14 @@ log_trace() {
7879
fi
7980
}
8081

82+
# Section header logging - displays a prominent section separator
83+
log_section() {
84+
log ""
85+
log "${BLUE}===============================================${NC}"
86+
log "${BLUE}$1${NC}"
87+
log "${BLUE}===============================================${NC}"
88+
}
89+
8190
# Additional utility functions
8291

8392
# Check if command exists

tests/test-e2e.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ source "${PROJECT_ROOT}/scripts/shell-utils.sh"
2525
# Set log file for tee output
2626
export SHELL_UTILS_LOG_FILE="${TEST_LOG_FILE}"
2727

28-
log_section() {
29-
log ""
30-
log "${BLUE}===============================================${NC}"
31-
log "${BLUE}$1${NC}"
32-
log "${BLUE}===============================================${NC}"
33-
}
34-
3528
# Helper function to get VM IP address from libvirt
3629
get_vm_ip() {
3730
virsh domifaddr torrust-tracker-demo 2>/dev/null | grep ipv4 | awk '{print $4}' | cut -d'/' -f1 || echo ""

0 commit comments

Comments
 (0)