-
Notifications
You must be signed in to change notification settings - Fork 4
/
await-http.sh
277 lines (247 loc) · 7.25 KB
/
await-http.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/sh
# SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
# SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
# SPDX-License-Identifier: Apache-2.0
#
# https://github.com/vegardit/await.sh/
#
# shellcheck disable=SC2059 # (warning): Don't use variables in the printf format string
set -e
#################################################
# enable debug mode if requested
#################################################
if [ "${DEBUG:-}" = "1" ]; then
if command -v "ps" >/dev/null; then
echo "shell: $(ps -sp $$)" >&2
fi
PS4='+[$?] $0:$LINENO '
set -x
fi
##############################
# Exit codes
##############################
rc_cmd_not_found=2
rc_invalid_args=3
rc_timed_out=4
##############################
# Functions
##############################
show_help() {
echo "Usage: $0 [OPTION]... TIMEOUT URL... [-- COMMAND [ARG...]]"
echo
echo "Repeatedly performs HTTP GET requests until the URL returns a HTTP status code <= 399. Then executes COMMAND."
echo
echo "Parameters:"
echo " TIMEOUT - Number of seconds within the URL must be reachable."
echo " URL - URL(s) to be checked using HTTP GET."
echo " COMMAND - Command to be executed once the wait condition is satisfied."
echo
echo "Options:"
echo " -f - Force execution of COMMAND even if timeout occurred."
echo " -t SECS - Duration in seconds after which a connection attempt is aborted (optional, default: 10 seconds)."
echo " -w SECS - Duration in seconds to wait between retries (optional, default: 5 seconds)."
echo
echo "Examples:"
echo " $0 30 http://service1.local -- /opt/server/start.sh --port 8080"
echo " $0 30 http://service1.local https://service2.local -- /opt/server/start.sh --port 8080"
echo " $0 -w 10 30 https://service1.local -- /opt/server/start.sh --port 8080"
}
assert_command_exists() {
if ! command -v "$1" >/dev/null; then
echo "ERROR: Required command '$1' not found"'!' >&2
exit $rc_cmd_not_found
fi
}
is_integer() {
# kudos to jilles https://stackoverflow.com/a/3951175/5116073
case "$1" in
''|*[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
build_timeout_command() {
_timeout=$1
if command -v timeout >/dev/null; then
if timeout --help 2>&1 | grep -q -- "-t SECS"; then
# BusyBox < 1.30.0 http://lists.busybox.net/pipermail/busybox-cvs/2018-August/038305.html
# Usage: timeout [-t SECS] [-s SIG] PROG ARGS
echo "timeout -s 9 -t ${_timeout}"
else
echo "timeout -s 9 ${_timeout}"
fi
elif command -v perl >/dev/null; then
# kudos to pilcrow https://stackoverflow.com/a/3223441/5116073
echo "perl -e 'alarm shift; exec @ARGV' ${_timeout}"
else
assert_command_exists timeout
fi
}
build_http_get_command() {
# use wget by default (which starts faster), use curl if requested via PREFERED_HTTP_CLIENT or if wget is not available
if [ "$PREFERED_HTTP_CLIENT" = "curl" ] || (command -v curl >/dev/null && ! command -v wget >/dev/null); then
_http_get="curl -sSfL --range 0-0 -o /dev/null"
else
_wget_features=$(wget --help 2>&1)
_http_get="wget --header='Range: bytes=0-0' -O /dev/null" # ignore the output
# workaround for wget sometimes writing unwanted wget.log files https://savannah.gnu.org/bugs/?51181
_http_get="${_http_get} -o /dev/stderr"
if echo "${_wget_features}" | grep -q -- "--tries"; then
_http_get="${_http_get} --tries 1" # to prevent endless retries on GNU Wget
fi
if echo "${_wget_features}" | grep -q -e " -S," -e " -S "; then
_http_get="${_http_get} -S" # print server response
fi
fi
echo "${_http_get}"
}
##############################
# argument parsing
##############################
if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
show_help
exit
fi
assert_command_exists grep
if ! command -v wget >/dev/null; then
assert_command_exists curl
fi
force_execution=false
retry_interval=5
probe_timeout=10
# option parsing
while [ $# -gt 0 ]; do
case $1 in
-f) shift
force_execution=true
;;
-t) shift
if ! is_integer "$1"; then
show_help >&2
echo >&2
echo 'ERROR: Option -t must be followed by an integer!' >&2
exit $rc_invalid_args
fi
probe_timeout=$1 && shift
;;
-w) shift
if ! is_integer "$1"; then
show_help >&2
echo >&2
echo 'ERROR: Option -w must be followed by an integer!' >&2
exit $rc_invalid_args
fi
retry_interval=$1 && shift
;;
*) break ;;
esac
done
if [ $# -lt 2 ]; then
show_help >&2
echo >&2
echo 'ERROR: Required parameter missing!' >&2
exit $rc_invalid_args
fi
deadline=$1 && shift
if ! is_integer "$deadline"; then
show_help >&2
echo >&2
echo 'ERROR: TIMEOUT parameter must be an integer!' >&2
exit $rc_invalid_args
fi
# collect target addresses
targets=$1 && shift
while [ $# -gt 0 ]; do
if [ "$1" = "--" ]; then
break
fi
targets="$targets $1"
shift
done
if [ -z "$1" ]; then
command=""
else
if [ "$1" != "--" ] ; then
show_help >&2
echo >&2
echo "ERROR: Separator '--' is missing in parameter list!" >&2
exit $rc_invalid_args
fi
shift
if [ -z "$1" ]; then
command=""
else
command="$1" && shift
assert_command_exists "$command"
for arg in "$@"; do
case $arg in
*'"'*) # shellcheck disable=SC2089
command="$command '$arg'" ;;
*) command="$command \"$arg\"" ;;
esac
done
fi
fi
##############################
# waiting for condition
##############################
echo "Waiting up to $deadline seconds for [$targets] to get ready..."
wait_until=$(( $(date +%s) + deadline ))
timeout_cmd=$(build_timeout_command "$probe_timeout")
http_get=$(build_http_get_command)
while true; do
no_errors=true
last_error_msg=
last_error_target=
for target in $targets; do
printf "=> executing [$timeout_cmd $http_get $target]..."
set +e
# shellcheck disable=SC2086
result=$(eval $timeout_cmd $http_get "$target" 2>&1)
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "ERROR"
no_errors=false
last_error_msg=$result
last_error_target=$target
else
echo "OK"
fi
set -e
done
if [ "$no_errors" = "true" ]; then
break
fi
case $last_error_msg in
*"Unsupported scheme"*|*"wget: not an http or ftp url"*)
echo "ERROR: $last_error_msg" >&2
echo >&2
exit $rc_invalid_args
;;
esac
if [ "$(date +%s)" -ge $wait_until ]; then
echo "$last_error_msg" >&2
echo >&2
echo "ERROR: [$last_error_target] did not get ready within required time." >&2
if [ "$force_execution" = "true" ]; then
break
else
exit $rc_timed_out
fi
fi
sleep "$retry_interval"
done
echo "SUCCESS: Waiting condition is met."
##############################
# follow-up command execution
##############################
if [ -n "$command" ]; then
echo "Executing [$command]..."
if [ -n "$ZSH_VERSION" ]; then
# to prevent 'command not found: echo "UP"'
# shellcheck disable=SC2086,SC2090
exec ${command%% *} ${command#* }
else
# shellcheck disable=SC2086,SC2090
exec ${command} # using exec so shell process is terminated and signals send by docker deamon are receivable by command
fi
fi