1
+ #! /bin/sh
2
+
3
+ # The MIT License (MIT)
4
+ #
5
+ # Copyright (c) 2017 Eficode Oy
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ set -- " $@ " -- " $TIMEOUT " " $QUIET " " $PROTOCOL " " $HOST " " $PORT " " $result "
26
+ TIMEOUT=15
27
+ QUIET=0
28
+ # The protocol to make the request with, either "tcp" or "http"
29
+ PROTOCOL=" tcp"
30
+
31
+ echoerr () {
32
+ if [ " $QUIET " -ne 1 ]; then printf " %s\n" " $* " 1>&2 ; fi
33
+ }
34
+
35
+ usage () {
36
+ exitcode=" $1 "
37
+ cat << USAGE >&2
38
+ Usage:
39
+ $0 host:port|url [-t timeout] [-- command args]
40
+ -q | --quiet Do not output any status messages
41
+ -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
42
+ -- COMMAND ARGS Execute command with args after the test finishes
43
+ USAGE
44
+ exit " $exitcode "
45
+ }
46
+
47
+ wait_for () {
48
+ case " $PROTOCOL " in
49
+ tcp)
50
+ if ! command -v nc > /dev/null; then
51
+ echoerr ' nc command is missing!'
52
+ exit 1
53
+ fi
54
+ ;;
55
+ wget)
56
+ if ! command -v wget > /dev/null; then
57
+ echoerr ' nc command is missing!'
58
+ exit 1
59
+ fi
60
+ ;;
61
+ esac
62
+
63
+ while : ; do
64
+ case " $PROTOCOL " in
65
+ tcp)
66
+ nc -z " $HOST " " $PORT " > /dev/null 2>&1
67
+ ;;
68
+ http)
69
+ wget --timeout=1 -q " $HOST " -O /dev/null > /dev/null 2>&1
70
+ ;;
71
+ * )
72
+ echoerr " Unknown protocol '$PROTOCOL '"
73
+ exit 1
74
+ ;;
75
+ esac
76
+
77
+ result=$?
78
+
79
+ if [ $result -eq 0 ] ; then
80
+ if [ $# -gt 7 ] ; then
81
+ for result in $( seq $(( $# - 7 )) ) ; do
82
+ result=$1
83
+ shift
84
+ set -- " $@ " " $result "
85
+ done
86
+
87
+ TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
88
+ shift 7
89
+ exec " $@ "
90
+ fi
91
+ exit 0
92
+ fi
93
+
94
+ if [ " $TIMEOUT " -le 0 ]; then
95
+ break
96
+ fi
97
+ TIMEOUT=$(( TIMEOUT - 1 ))
98
+
99
+ sleep 1
100
+ done
101
+ echo " Operation timed out" >&2
102
+ exit 1
103
+ }
104
+
105
+ while : ; do
106
+ case " $1 " in
107
+ http://* |https://* )
108
+ HOST=" $1 "
109
+ PROTOCOL=" http"
110
+ shift 1
111
+ ;;
112
+ * :* )
113
+ HOST=$( printf " %s\n" " $1 " | cut -d : -f 1)
114
+ PORT=$( printf " %s\n" " $1 " | cut -d : -f 2)
115
+ shift 1
116
+ ;;
117
+ -q | --quiet)
118
+ QUIET=1
119
+ shift 1
120
+ ;;
121
+ -q-* )
122
+ QUIET=0
123
+ echoerr " Unknown option: $1 "
124
+ usage 1
125
+ ;;
126
+ -q* )
127
+ QUIET=1
128
+ result=$1
129
+ shift 1
130
+ set -- -" ${result# -q} " " $@ "
131
+ ;;
132
+ -t | --timeout)
133
+ TIMEOUT=" $2 "
134
+ shift 2
135
+ ;;
136
+ -t* )
137
+ TIMEOUT=" ${1# -t} "
138
+ shift 1
139
+ ;;
140
+ --timeout=* )
141
+ TIMEOUT=" ${1#* =} "
142
+ shift 1
143
+ ;;
144
+ --)
145
+ shift
146
+ break
147
+ ;;
148
+ --help)
149
+ usage 0
150
+ ;;
151
+ -* )
152
+ QUIET=0
153
+ echoerr " Unknown option: $1 "
154
+ usage 1
155
+ ;;
156
+ * )
157
+ QUIET=0
158
+ echoerr " Unknown argument: $1 "
159
+ usage 1
160
+ ;;
161
+ esac
162
+ done
163
+
164
+ if ! [ " $TIMEOUT " -ge 0 ] 2> /dev/null; then
165
+ echoerr " Error: invalid timeout '$TIMEOUT '"
166
+ usage 3
167
+ fi
168
+
169
+ case " $PROTOCOL " in
170
+ tcp)
171
+ if [ " $HOST " = " " ] || [ " $PORT " = " " ]; then
172
+ echoerr " Error: you need to provide a host and port to test."
173
+ usage 2
174
+ fi
175
+ ;;
176
+ http)
177
+ if [ " $HOST " = " " ]; then
178
+ echoerr " Error: you need to provide a host to test."
179
+ usage 2
180
+ fi
181
+ ;;
182
+ esac
183
+
184
+ wait_for " $@ "
0 commit comments