Skip to content

Commit ace4173

Browse files
committed
Clean up check.sh and allow it to take multiple args
Checked with shellcheck==0.7.0
1 parent 146e8ce commit ace4173

File tree

1 file changed

+99
-56
lines changed

1 file changed

+99
-56
lines changed

check.sh

+99-56
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,116 @@
1-
set -x
1+
#!/usr/bin/env bash
2+
# Run checks for the provided service(s).
3+
# To specify multiple services, separate them with spaces or plus signs (+).
4+
# To specify all services, just pass in "all".
5+
#
6+
# Examples:
7+
# ./check.sh lms
8+
# ./check.sh lms+forum
9+
# ./check.sh lms+forum discovery
10+
# ./check.sh all
11+
#
12+
# Exists 0 if successful; non-zero otherwise.
13+
#
14+
# Fails if no services specified.
15+
#
16+
# Note that passing in a non-existent service will not fail if there are
17+
# other successful checks.
218

3-
service=$1
19+
set -e
20+
set -o pipefail
21+
set -u
422

5-
if [[ $service == "registrar" ]] || [[ $service == "all" ]]; then
6-
curl http://localhost:18734/heartbeat # registrar
7-
if [ $? -ne 0 ]; then
8-
docker-compose logs
9-
exit 2
23+
# Grab all arguments into one string, replacing plus signs with spaces.
24+
# Pad on either side with spaces so that the regex in `should_check` works correctly.
25+
services=" ${*//+/ } "
26+
27+
# Which checks succeeded and failed.
28+
succeeded=""
29+
failed=""
30+
31+
# Returns whether service in first arg should be checked.
32+
should_check() {
33+
local service="$1"
34+
if [[ "$services" == *" all "* ]] || [[ "$services" == *" $service "* ]]; then
35+
return 0 # Note that '0' means 'success' (i.e., true) in bash.
36+
else
37+
return 1
1038
fi
11-
echo
39+
}
40+
41+
# Runs a check named $1 on service $2 using the command $3.
42+
run_check() {
43+
local check_name="$1"
44+
local service="$2"
45+
local cmd="$3"
46+
echo "> $cmd"
47+
set +e # Disable exit-on-error
48+
if $cmd; then # Run the command itself and check if it succeeded.
49+
succeeded="$succeeded $check_name"
50+
else
51+
docker-compose logs "$service"
52+
failed="$failed $check_name"
53+
fi
54+
set -e # Re-enable exit-on-error
55+
echo # Newline
56+
}
57+
58+
if should_check registrar; then
59+
echo "Checking Registrar heartbeat:"
60+
run_check registrar_heartbeat registrar \
61+
"curl --fail -L http://localhost:18734/heartbeat"
1262
fi
13-
if [[ $service == "lms" ]] || [[ $service == "all" ]]; then
63+
64+
if should_check lms; then
1465
echo "Checking LMS heartbeat:"
15-
curl http://localhost:18000/heartbeat #LMs
16-
if [ $? -ne 0 ]; then
17-
docker-compose logs
18-
exit 2
19-
fi
20-
echo
66+
run_check lms_heartbeat lms \
67+
"curl --fail -L http://localhost:18000/heartbeat"
68+
2169
echo "Checking Studio heartbeat:"
22-
curl http://localhost:18010/heartbeat # Studio
23-
if [ $? -ne 0 ]; then
24-
docker-compose logs
25-
exit 2
26-
fi
27-
echo
70+
run_check studio_heartbeat lms \
71+
"curl --fail -L http://localhost:18010/heartbeat"
72+
73+
echo "Validating LMS volume:"
74+
run_check lms_volume lms \
75+
"make validate-lms-volume"
2876
fi
29-
if [[ $service == "ecommerce" ]] || [[ $service == "all" ]]; then
77+
78+
if should_check ecommerce; then
3079
echo "Checking ecommerce health:"
31-
curl http://localhost:18130/health/ # Ecommerce
32-
if [ $? -ne 0 ]; then
33-
docker-compose logs
34-
exit 2
35-
fi
36-
echo
80+
run_check ecommerce_heartbeat ecommerce \
81+
"curl --fail -L http://localhost:18130/health/"
3782
fi
38-
if [[ $service == "discovery" ]] || [[ $service == "all" ]]; then
83+
84+
if should_check discovery; then
3985
echo "Checking discovery health:"
40-
curl http://localhost:18381/health/ # Discovery
41-
if [ $? -ne 0 ]; then
42-
docker-compose logs
43-
exit 2
44-
fi
45-
echo
86+
run_check discovery_heartbeat discovery \
87+
"curl --fail -L http://localhost:18381/health/"
4688
fi
47-
if [[ $service == "forum" ]] || [[ $service == "all" ]]; then
48-
echo "Checking forum health:"
49-
curl http://localhost:44567/heartbeat # Forums
50-
if [ $? -ne 0 ]; then
51-
docker-compose logs
52-
exit 2
53-
fi
89+
90+
if should_check forum; then
91+
echo "Checking forum heartbeat:"
92+
run_check forum_heartbeat forum \
93+
"curl --fail -L http://localhost:44567/heartbeat"
5494
fi
5595

56-
if [[ $service == "edx_notes_api" ]]; then
57-
echo "Checking edx_notes_api health:"
58-
curl http://localhost:18120/heartbeat # edx_notes_api
59-
if [ $? -ne 0 ]; then
60-
docker-compose logs
61-
exit 2
62-
fi
96+
if should_check edx_notes_api; then
97+
echo "Checking edx_notes_api heartbeat:"
98+
run_check edx_notes_api_heartbeat edx_notes_api \
99+
"curl --fail -L http://localhost:18120/heartbeat"
63100
fi
64101

65-
if [[ $service == "credentials" ]]; then
66-
echo "Checking credentials health:"
67-
curl http://localhost:18150/heartbeat # credentials
68-
if [ $? -ne 0 ]; then
69-
docker-compose logs
70-
exit 2
71-
fi
102+
if should_check credentials; then
103+
echo "Checking credentials heartbeat:"
104+
run_check credentials_heartbeat credentials \
105+
"curl --fail -L http://localhost:18150/health"
72106
fi
73107

108+
echo "Successful checks:${succeeded:- NONE}"
109+
echo "Failed checks:${failed:- NONE}"
110+
if [[ "$succeeded" ]]; then
111+
echo "Check result: SUCCESS"
112+
exit 0
113+
else
114+
echo "Check result: FAILURE"
115+
exit 2
116+
fi

0 commit comments

Comments
 (0)