-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcheck.sh
More file actions
executable file
·123 lines (114 loc) · 3.62 KB
/
Copy pathcheck.sh
File metadata and controls
executable file
·123 lines (114 loc) · 3.62 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
#!/usr/bin/env bash
# Run health checks for the provided service(s).
# To specify multiple services, separate them with spaces or plus signs (+).
# To specify all running services, just pass in "all".
#
# Examples:
# ./check.sh lms
# ./check.sh lms+forum
# ./check.sh lms+forum discovery
# ./check.sh all
#
# Exits 0 if successful; non-zero otherwise.
#
# Fails if no services specified.
#
# Note that passing in a non-existent service will not fail if there are
# other successful checks.
set -eu -o pipefail
# Which checks succeeded and failed.
succeeded=""
failed=""
# Runs a check named $1 on service $2 using the host-side command $3.
run_check() {
local check_name="$1"
local service="$2"
local cmd="$3"
echo "> $cmd"
set +e # Disable exit-on-error
if bash -c "$cmd"; then # Run the command itself and check if it succeeded.
succeeded="$succeeded $check_name"
else
docker compose logs --tail 500 "$service" # Just show recent logs, not all history
failed="$failed $check_name"
fi
set -e # Re-enable exit-on-error
echo # Newline
}
# Print a service container's Docker healthcheck status, one of:
# healthy | unhealthy | starting | none | missing
# "none" means the container exists but declares no healthcheck; "missing"
# means no container is running for the service.
container_health() {
local service="$1" cid
cid="$(docker compose ps -q "$service" 2>/dev/null || true)"
if [[ -z "$cid" ]]; then
echo "missing"
return
fi
docker inspect \
--format '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' \
"$cid" 2>/dev/null || echo "missing"
}
# Default check: pass iff the service's container reports itself healthy.
# Services with no healthcheck (and no extra check) simply don't contribute a
# check, matching the old behavior for unrecognized services.
run_health_check() {
local service="$1"
local status
status="$(container_health "$service")"
case "$status" in
healthy)
echo "Checking $service: healthy"
succeeded="$succeeded ${service}_health"
echo
;;
starting|unhealthy)
echo "Checking $service: $status"
docker compose logs --tail 500 "$service"
failed="$failed ${service}_health"
echo
;;
none|missing)
# No container healthcheck to consult; rely on any extra checks.
:
;;
esac
}
# Extra/override checks for services that need more than their container
# healthcheck can express. Keep this SMALL -- it is the only place service
# names should be enumerated.
run_extra_checks() {
local service="$1"
case "$service" in
lms)
echo "Validating LMS volume:"
run_check lms_volume lms "make validate-lms-volume"
;;
esac
}
# Expand the requested services into a plain, space-separated list. "all"
# means every service with a running container (word-splitting is safe here
# because compose service names contain no whitespace).
requested=" ${*//+/ } "
if [[ "$requested" == *" all "* ]]; then
service_list="$(docker compose ps --services)"
else
service_list="${*//+/ }"
fi
for service in $service_list; do
run_health_check "$service"
run_extra_checks "$service"
done
echo "Successful checks:${succeeded:- NONE}"
echo "Failed checks:${failed:- NONE}"
if [[ -z "$succeeded" ]] && [[ -z "$failed" ]]; then
echo "No checks ran. Exiting as failure."
exit 1
elif [[ -z "$failed" ]]; then
echo "Check result: SUCCESS"
exit 0
else
echo "Check result: FAILURE"
exit 2
fi