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+ VERSION=" 2.2.4"
26+
27+ set -- " $@ " -- " $TIMEOUT " " $QUIET " " $PROTOCOL " " $HOST " " $PORT " " $result "
28+ TIMEOUT=15
29+ QUIET=0
30+ # The protocol to make the request with, either "tcp" or "http"
31+ PROTOCOL=" tcp"
32+
33+ echoerr () {
34+ if [ " $QUIET " -ne 1 ]; then printf " %s\n" " $* " 1>&2 ; fi
35+ }
36+
37+ usage () {
38+ exitcode=" $1 "
39+ cat << USAGE >&2
40+ Usage:
41+ $0 host:port|url [-t timeout] [-- command args]
42+ -q | --quiet Do not output any status messages
43+ -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
44+ Defaults to 15 seconds
45+ -v | --version Show the version of this tool
46+ -- COMMAND ARGS Execute command with args after the test finishes
47+ USAGE
48+ exit " $exitcode "
49+ }
50+
51+ wait_for () {
52+ case " $PROTOCOL " in
53+ tcp)
54+ if ! command -v nc > /dev/null; then
55+ echoerr ' nc command is missing!'
56+ exit 1
57+ fi
58+ ;;
59+ http)
60+ if ! command -v wget > /dev/null; then
61+ echoerr ' wget command is missing!'
62+ exit 1
63+ fi
64+ ;;
65+ esac
66+
67+ TIMEOUT_END=$(( $(date +% s) + TIMEOUT))
68+
69+ while : ; do
70+ case " $PROTOCOL " in
71+ tcp)
72+ nc -w 1 -z " $HOST " " $PORT " > /dev/null 2>&1
73+ ;;
74+ http)
75+ wget --timeout=1 --tries=1 -q " $HOST " -O /dev/null > /dev/null 2>&1
76+ ;;
77+ * )
78+ echoerr " Unknown protocol '$PROTOCOL '"
79+ exit 1
80+ ;;
81+ esac
82+
83+ result=$?
84+
85+ if [ $result -eq 0 ] ; then
86+ if [ $# -gt 7 ] ; then
87+ for result in $( seq $(( $# - 7 )) ) ; do
88+ result=$1
89+ shift
90+ set -- " $@ " " $result "
91+ done
92+
93+ TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
94+ shift 7
95+ exec " $@ "
96+ fi
97+ exit 0
98+ fi
99+
100+ if [ $TIMEOUT -ne 0 -a $( date +%s) -ge $TIMEOUT_END ]; then
101+ echo " Operation timed out" >&2
102+ exit 1
103+ fi
104+
105+ sleep 1
106+ done
107+ }
108+
109+ while : ; do
110+ case " $1 " in
111+ http://* |https://* )
112+ HOST=" $1 "
113+ PROTOCOL=" http"
114+ shift 1
115+ ;;
116+ * :* )
117+ HOST=$( printf " %s\n" " $1 " | cut -d : -f 1)
118+ PORT=$( printf " %s\n" " $1 " | cut -d : -f 2)
119+ shift 1
120+ ;;
121+ -v | --version)
122+ echo $VERSION
123+ exit
124+ ;;
125+ -q | --quiet)
126+ QUIET=1
127+ shift 1
128+ ;;
129+ -q-* )
130+ QUIET=0
131+ echoerr " Unknown option: $1 "
132+ usage 1
133+ ;;
134+ -q* )
135+ QUIET=1
136+ result=$1
137+ shift 1
138+ set -- -" ${result# -q} " " $@ "
139+ ;;
140+ -t | --timeout)
141+ TIMEOUT=" $2 "
142+ shift 2
143+ ;;
144+ -t* )
145+ TIMEOUT=" ${1# -t} "
146+ shift 1
147+ ;;
148+ --timeout=* )
149+ TIMEOUT=" ${1#* =} "
150+ shift 1
151+ ;;
152+ --)
153+ shift
154+ break
155+ ;;
156+ --help)
157+ usage 0
158+ ;;
159+ -* )
160+ QUIET=0
161+ echoerr " Unknown option: $1 "
162+ usage 1
163+ ;;
164+ * )
165+ QUIET=0
166+ echoerr " Unknown argument: $1 "
167+ usage 1
168+ ;;
169+ esac
170+ done
171+
172+ if ! [ " $TIMEOUT " -ge 0 ] 2> /dev/null; then
173+ echoerr " Error: invalid timeout '$TIMEOUT '"
174+ usage 3
175+ fi
176+
177+ case " $PROTOCOL " in
178+ tcp)
179+ if [ " $HOST " = " " ] || [ " $PORT " = " " ]; then
180+ echoerr " Error: you need to provide a host and port to test."
181+ usage 2
182+ fi
183+ ;;
184+ http)
185+ if [ " $HOST " = " " ]; then
186+ echoerr " Error: you need to provide a host to test."
187+ usage 2
188+ fi
189+ ;;
190+ esac
191+
192+ wait_for " $@ "
0 commit comments