forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify-common.sh
238 lines (208 loc) · 5.3 KB
/
verify-common.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash -e
DELAY="${DELAY:-0}"
DOCKER_NO_PULL="${DOCKER_NO_PULL:-}"
MANUAL="${MANUAL:-}"
NAME="${NAME:-}"
PATHS="${PATHS:-.}"
UPARGS="${UPARGS:-}"
ENVOY_EXAMPLES_DEBUG="${ENVOY_EXAMPLES_DEBUG:-}"
if [[ -n "$DOCKER_COMPOSE" ]]; then
read -ra DOCKER_COMPOSE <<< "$DOCKER_COMPOSE"
else
DOCKER_COMPOSE=(docker compose)
fi
run_log () {
echo -e "\n> [${NAME}] ${*}"
}
bring_up_example_stack () {
local args path up_args
args=("${UPARGS[@]}")
path="$1"
read -ra up_args <<< "up --quiet-pull --pull missing --build --wait -d ${args[*]}"
if [[ -z "$DOCKER_NO_PULL" ]]; then
run_log "Pull the images ($path)"
"${DOCKER_COMPOSE[@]}" pull -q
echo
fi
run_log "Bring up services ($path)"
"${DOCKER_COMPOSE[@]}" "${up_args[@]}" || return 1
if [[ -n "$ENVOY_EXAMPLES_DEBUG" ]]; then
echo "----------------------------------------------"
docker system df -v
echo
sudo du -ch / | grep "[0-9]G"
echo
df -h
echo
echo "----------------------------------------------"
fi
echo
}
bring_up_example () {
local path paths
read -ra paths <<< "$(echo "$PATHS" | tr ',' ' ')"
for path in "${paths[@]}"; do
pushd "$path" > /dev/null || return 1
bring_up_example_stack "$path" || {
echo "ERROR: starting ${NAME} ${path}" >&2
return 1
}
popd > /dev/null || return 1
done
if [[ "$DELAY" -ne "0" ]]; then
run_log "Snooze for ${DELAY} while ${NAME} gets started"
sleep "$DELAY"
fi
for path in "${paths[@]}"; do
pushd "$path" > /dev/null || return 1
"${DOCKER_COMPOSE[@]}" ps
"${DOCKER_COMPOSE[@]}" logs
popd > /dev/null || return 1
done
}
bring_down_example () {
local path paths
read -ra paths <<< "$(echo "$PATHS" | tr ',' ' ')"
for path in "${paths[@]}"; do
pushd "$path" > /dev/null || return 1
cleanup_stack "$path" || {
echo "ERROR: cleanup ${NAME} ${path}" >&2
}
popd > /dev/null
done
}
cleanup_stack () {
local path down_args
path="$1"
down_args=(--remove-orphans)
if [[ -n "$DOCKER_RMI_CLEANUP" ]]; then
down_args+=(--rmi all)
fi
# Remove sandbox volumes by default
if [[ -z "$DOCKER_SAVE_VOLUMES" ]]; then
down_args+=(--volumes)
fi
run_log "Cleanup ($path)"
"${DOCKER_COMPOSE[@]}" down "${down_args[@]}"
}
debug_failure () {
>&2 echo "FAILURE DEBUG"
>&2 echo "DISK SPACE"
df -h
>&2 echo "DOCKER COMPOSE LOGS"
"${DOCKER_COMPOSE[@]}" logs
>&2 echo "DOCKER COMPOSE PS"
"${DOCKER_COMPOSE[@]}" ps
}
cleanup () {
local code="$?"
if [[ "$code" -ne 0 ]]; then
debug_failure
fi
bring_down_example
if [[ "$code" -ne 0 ]]; then
run_log Failed
else
run_log Success
fi
echo
}
_curl () {
local arg curl_command
curl_command=(curl -s)
if [[ ! "$*" =~ "-X" ]]; then
curl_command+=(-X GET)
fi
for arg in "${@}"; do
curl_command+=("$arg")
done
"${curl_command[@]}" || {
echo "ERROR: curl (${curl_command[*]})" >&2
return 1
}
}
responds_with () {
local expected response
expected="$1"
shift
response=$(_curl "${@}")
grep -s "$expected" <<< "$response" || {
echo "ERROR: curl (${*})" >&2
echo "EXPECTED: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
responds_without () {
local expected response
expected="$1"
shift
response=$(_curl "${@}")
# shellcheck disable=2266
grep -s "$expected" <<< "$response" | [[ "$(wc -l)" -eq 0 ]] || {
echo "ERROR: curl (${*})" >&2
echo "DID NOT EXPECT: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
responds_with_header () {
local expected response
expected="$1"
shift
response=$(_curl --head "${@}")
grep -s "$expected" <<< "$response" || {
echo "ERROR: curl (${*})" >&2
echo "EXPECTED HEADER: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
responds_without_header () {
local expected response
expected="$1"
shift
response=$(_curl --head "${@}")
# shellcheck disable=2266
grep -s "$expected" <<< "$response" | [[ "$(wc -l)" -eq 0 ]] || {
echo "ERROR: curl (${*})" >&2
echo "DID NOT EXPECT HEADER: $expected" >&2
echo "RECEIVED:" >&2
echo "$response" >&2
return 1
}
}
wait_for () {
local i=1 returns=1 seconds="$1"
shift
while ((i<=seconds)); do
if "${@}" &> /dev/null; then
returns=0
break
else
sleep 1
((i++))
fi
done
if [[ "$returns" != 0 ]]; then
echo "Wait (${seconds}) failed: ${*}" >&2
fi
return "$returns"
}
trap 'cleanup' EXIT
if [[ -z "$NAME" ]]; then
echo "ERROR: You must set the '$NAME' variable before sourcing this script" >&2
exit 1
fi
if [[ -z "$MANUAL" ]]; then
bring_up_example
fi
# These allow the functions to be used in subshells, e.g. in `wait_for`
export -f responds_with
export -f responds_without
export -f responds_with_header
export -f responds_without_header
export -f _curl