-
Notifications
You must be signed in to change notification settings - Fork 4
/
await-cmd.sh
236 lines (212 loc) · 6 KB
/
await-cmd.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
#!/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 TEST_COMMAND [ARG...] [-- COMMAND [ARG...]]"
echo
echo "Executes TEST_COMMAND repeatedly until it's exit code is 0. Then executes COMMAND."
echo
echo "Parameters:"
echo " TIMEOUT - Duration in seconds within TEST_COMMAND must return exit code 0."
echo " TEST_COMMAND - Command that will be executed to test if the waiting condition is met."
echo " COMMAND - Command to be executed once the TEST_COMMAND succeeded (optional)."
echo
echo "Options:"
echo " -f - Force execution of COMMAND even if timeout occurred."
echo " -t SECS - Duration in seconds after which a TEST_COMMAND process is terminated (optional, default: 10 seconds)."
echo " -w SECS - Waiting period in seconds between each execution of TEST_COMMAND (optional, default: 5 seconds)."
echo
echo "Examples:"
echo " $0 30 /opt/scripts/check_remote_services.sh -- /opt/server/start.sh --port 8080"
echo " $0 -w 10 30 /opt/scripts/check_remote_services.sh -- /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
}
##############################
# argument parsing
##############################
if [ $# -eq 1 ] && [ "$1" = "--help" ]; then
show_help
exit
fi
assert_command_exists grep
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
test_command=$1 && shift
assert_command_exists "$test_command"
# collect parameters of test command
while [ $# -gt 0 ]; do
if [ "$1" = "--" ]; then
break
fi
case $1 in
*'"'*) test_command="$test_command '$1'" ;;
*) test_command="$test_command \"$1\"" ;;
esac
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 [$test_command] to get ready..."
wait_until=$(( $(date +%s) + deadline ))
timeout_cmd=$(build_timeout_command "$probe_timeout")
while true; do
set +e
printf "=> executing [$timeout_cmd $test_command]..."
# shellcheck disable=SC2086
result=$(eval $timeout_cmd $test_command 2>&1)
# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
echo "ERROR"
set -e
break
else
echo "OK"
fi
set -e
if [ "$(date +%s)" -ge $wait_until ]; then
echo "$result" >&2
echo >&2
echo "ERROR: [$test_command] 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