-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhealth-check.sh
executable file
·106 lines (84 loc) · 3.21 KB
/
health-check.sh
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
#!/bin/bash
# Function to check if a service has a healthcheck
service_has_healthcheck() {
local service=$1
docker-compose -f docker-compose.yml config | grep -A10 "$service:" | grep "healthcheck" > /dev/null
}
# Function to check health status of a service
check_service_health() {
local service=$1
local max_attempts=$2
local attempt=1
# Skip if no healthcheck exists
if ! service_has_healthcheck "$service"; then
echo "⚠️ Skipping health check for $service (no healthcheck configured)"
return 0
fi
echo "Checking health status of $service..."
while [[ $attempt -le $max_attempts ]]; do
# Get container ID first
container_id=$(docker-compose -f docker-compose.yml ps -q "$service")
if [[ -z "$container_id" ]]; then
echo "⚠️ No container found for $service"
return 1
fi
# Get health status using container ID
health_status=$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id" 2>/dev/null)
echo "Debug: Service=$service, Attempt=$attempt/$max_attempts, Status=$health_status"
if [[ "$health_status" == "healthy" ]]; then
echo "✅ $service is healthy"
return 0
fi
# If container is running but no health check is configured
if [[ -z "$health_status" ]] && [[ $(docker inspect --format='{{.State.Status}}' "$container_id") == "running" ]]; then
echo "⚠️ $service is running (no health check configured)"
return 0
fi
echo "⏳ Attempt $attempt/$max_attempts: $service is $health_status"
attempt=$((attempt + 1))
sleep 10
done
echo "❌ $service failed to become healthy after $max_attempts attempts"
return 1
}
# Main health check function
check_all_services_health() {
local timeout_minutes=5
local max_attempts=$((timeout_minutes * 6)) # 6 attempts per minute (10-second intervals)
local services=($(docker-compose config --services))
local failed_services=()
echo "Starting health checks for all services..."
echo "Timeout set to $timeout_minutes minutes ($max_attempts attempts)"
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo "❌ Docker is not running or not accessible"
return 1
fi
# Check if docker-compose file exists
if [[ ! -f "docker-compose.yml" ]]; then
echo "❌ docker-compose.yml not found in current directory"
return 1
fi
# First check if services are running
echo "Checking if services are up..."
docker-compose -f docker-compose.yml ps
for service in "${services[@]}"; do
if ! check_service_health "$service" "$max_attempts"; then
failed_services+=("$service")
fi
done
if [[ ${#failed_services[@]} -eq 0 ]]; then
echo "🎉 All services are healthy!"
return 0
else
echo "❌ The following services failed health checks:"
printf '%s\n' "${failed_services[@]}"
return 1
fi
}
# Enable debug mode
#set -x
# Run the health checks
check_all_services_health
# Exit with the status of the health check
exit $?