-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontainers_deployment.sh
More file actions
executable file
·157 lines (137 loc) · 5.4 KB
/
containers_deployment.sh
File metadata and controls
executable file
·157 lines (137 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
set -e
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DATE=$(date +%Y%m%d)
LOG_FILE="$PROJECT_DIR/logs/containers_deployment_$DATE.log"
APT_REQUIREMENTS_FILE="$PROJECT_DIR/config/requirements-apt.txt"
# Create logs directory if it doesn't exist
mkdir -p "$PROJECT_DIR/logs"
# Defining logging function
log() {
local level="$1"
local message="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
"INFO")
echo "[$timestamp] [INFO] $message" | tee -a "$LOG_FILE"
;;
"DEBUG")
echo "[$timestamp] [DEBUG] $message" | tee -a "$LOG_FILE"
;;
"ERROR")
echo "[$timestamp] [ERROR] $message" | tee -a "$LOG_FILE"
;;
"WARNING")
echo "[$timestamp] [WARNING] $message" | tee -a "$LOG_FILE"
;;
*)
echo "[$timestamp] [INFO] $level $message" | tee -a "$LOG_FILE"
;;
esac
}
log "INFO" "========================================="
log "INFO" "Starting Mediatech container deployment"
log "INFO" "Date: $(date '+%Y-%m-%d %H:%M:%S')"
log "INFO" "========================================="
# Check if docker-compose.yml exists
if [ ! -f "$PROJECT_DIR/docker-compose.yml" ]; then
log "ERROR" "docker-compose.yml not found in $PROJECT_DIR"
exit 1
fi
cd "$PROJECT_DIR"
# Step 1: Prepare persistence
log "INFO" "========================================="
log "INFO" "Step 1: Preparing data history persistence"
log "INFO" "========================================="
if [ ! -f config/data_history.json ]; then
log "INFO" "Creating config/data_history.json for persistence"
echo '{}' > config/data_history.json
else
log "DEBUG" "config/data_history.json already exists"
fi
# Step 2: Build and deploy
log "INFO" "========================================="
log "INFO" "Step 2: Building and deploying containers"
log "INFO" "========================================="
if [ -n "$(sudo docker ps -a --filter "name=airflow-init" --format '{{.Names}}')" ]; then
log "INFO" "Container airflow-init already exists. Skipping airflow-init step."
log "INFO" "Forcing rebuild without cache to ensure code is up-to-date."
log "INFO" "Running: docker compose build --no-cache"
if ! sudo docker compose build --no-cache 2>&1 | tee -a "$LOG_FILE"; then
log "ERROR" "Build failed"
exit 1
fi
log "INFO" "Build successful"
log "INFO" "Running: docker compose up -d --remove-orphans"
if ! sudo docker compose up -d --remove-orphans 2>&1 | tee -a "$LOG_FILE"; then
log "ERROR" "Container deployment failed"
exit 1
fi
log "INFO" "Container deployment successful"
else
log "INFO" "Container 'airflow-init' not found. Running initialization steps."
log "INFO" "Running: docker compose build --no-cache"
if sudo docker compose build --no-cache 2>&1 | tee -a "$LOG_FILE"; then
log "INFO" "Build successful"
else
log "ERROR" "Build failed"
exit 1
fi
log "INFO" "Running: docker compose up airflow-init"
if sudo docker compose up airflow-init 2>&1 | tee -a "$LOG_FILE"; then
log "INFO" "Airflow init successful"
else
log "ERROR" "Airflow init failed"
exit 1
fi
log "INFO" "Running: docker compose up -d"
if sudo docker compose up -d 2>&1 | tee -a "$LOG_FILE"; then
log "INFO" "Containers started successfully"
else
log "ERROR" "Failed to start containers"
exit 1
fi
fi
# Step 3: Health check
log "INFO" "========================================="
log "INFO" "Step 3: Performing health checks"
log "INFO" "========================================="
log "INFO" "Waiting for services to be ready..."
# Wait for containers to be ready (more robust than sleep)
timeout 120 bash -c 'until sudo docker compose ps | grep -q "healthy\|running"; do sleep 2; done' || {
log "ERROR" "Services failed to start within 120 seconds"
sudo docker compose logs --tail=50 | tee -a "$LOG_FILE"
exit 1
}
# Test CLI availability
log "INFO" "Testing mediatech CLI availability..."
if sudo docker compose exec -T airflow-scheduler mediatech --help >/dev/null 2>&1; then
log "INFO" "Mediatech CLI is available and working"
else
log "ERROR" "Mediatech CLI unavailable in container"
log "ERROR" "Container logs:"
sudo docker compose logs --tail=20 airflow-scheduler | tee -a "$LOG_FILE"
exit 1
fi
# Step 4: Cleanup
log "INFO" "========================================="
log "INFO" "Step 4: Cleaning up unused images"
log "INFO" "========================================="
log "INFO" "Running: docker image prune -f"
if sudo docker image prune -f 2>&1 | tee -a "$LOG_FILE"; then
log "INFO" "Image cleanup completed"
else
log "WARNING" "Image cleanup had issues (non-critical)"
fi
# Step 5: Final report
log "INFO" "========================================="
log "INFO" "Step 5: Final report"
log "INFO" "========================================="
log "INFO" "Active containers:"
sudo docker compose ps | tee -a "$LOG_FILE"
log "INFO" "========================================="
log "INFO" "Container deployment completed successfully!"
log "INFO" "Services status: $(sudo docker compose ps --format 'table {{.Service}}\t{{.Status}}' | tail -n +2 | wc -l) services running"
log "INFO" "Full logs: $LOG_FILE"
log "INFO" "Completed at: $(date '+%Y-%m-%d %H:%M:%S')"
log "INFO" "========================================="