Skip to content

Commit 7fafb54

Browse files
authored
Merge pull request #889 from metacpan/ssoriche/add_wait-for-it
Add wait-for-it.sh script to container
2 parents 12c6f2b + 3e6c948 commit 7fafb54

File tree

2 files changed

+204
-1
lines changed

2 files changed

+204
-1
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM perl:5.22
22

33
ENV PERL_MM_USE_DEFAULT=1 PERL_CARTON_PATH=/carton
44

5+
COPY wait-for-it.sh /
56
COPY cpanfile cpanfile.snapshot /metacpan-api/
67
WORKDIR /metacpan-api
78

@@ -24,4 +25,4 @@ USER metacpan-api:users
2425

2526
EXPOSE 5000
2627

27-
CMD [ "./wait-for-it.sh", "db:5432", "carton", "exec", "morbo", "-l", "http://*:5000", "-w", "root", "./bin/api.pl"]
28+
CMD [ "/wait-for-it.sh", "db:5432", "--", "carton", "exec", "morbo", "-l", "http://*:5000", "-w", "root", "./bin/api.pl"]

wait-for-it.sh

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#!/bin/bash
2+
#
3+
# See https://github.com/vishnubob/wait-for-it
4+
#
5+
# The MIT License (MIT)
6+
# Copyright (c) 2016 Giles Hall
7+
8+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
9+
# this software and associated documentation files (the "Software"), to deal in
10+
# the Software without restriction, including without limitation the rights to
11+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12+
# of the Software, and to permit persons to whom the Software is furnished to do
13+
# so, subject to the following conditions:
14+
15+
# The above copyright notice and this permission notice shall be included in all
16+
# copies or substantial portions of the Software.
17+
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
# SOFTWARE.
25+
26+
#!/usr/bin/env bash
27+
# Use this script to test if a given TCP host/port are available
28+
29+
cmdname=$(basename $0)
30+
31+
echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
32+
33+
usage()
34+
{
35+
cat << USAGE >&2
36+
Usage:
37+
$cmdname host:port [-s] [-t timeout] [-- command args]
38+
-h HOST | --host=HOST Host or IP under test
39+
-p PORT | --port=PORT TCP port under test
40+
Alternatively, you specify the host and port as host:port
41+
-s | --strict Only execute subcommand if the test succeeds
42+
-q | --quiet Don't output any status messages
43+
-t TIMEOUT | --timeout=TIMEOUT
44+
Timeout in seconds, zero for no timeout
45+
-- COMMAND ARGS Execute command with args after the test finishes
46+
USAGE
47+
exit 1
48+
}
49+
50+
wait_for()
51+
{
52+
if [[ $TIMEOUT -gt 0 ]]; then
53+
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
54+
else
55+
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
56+
fi
57+
start_ts=$(date +%s)
58+
while :
59+
do
60+
if [[ $ISBUSY -eq 1 ]]; then
61+
nc -z $HOST $PORT
62+
result=$?
63+
else
64+
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
65+
result=$?
66+
fi
67+
if [[ $result -eq 0 ]]; then
68+
end_ts=$(date +%s)
69+
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
70+
break
71+
fi
72+
sleep 1
73+
done
74+
return $result
75+
}
76+
77+
wait_for_wrapper()
78+
{
79+
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
80+
if [[ $QUIET -eq 1 ]]; then
81+
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
82+
else
83+
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
84+
fi
85+
PID=$!
86+
trap "kill -INT -$PID" INT
87+
wait $PID
88+
RESULT=$?
89+
if [[ $RESULT -ne 0 ]]; then
90+
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
91+
fi
92+
return $RESULT
93+
}
94+
95+
# process arguments
96+
while [[ $# -gt 0 ]]
97+
do
98+
case "$1" in
99+
*:* )
100+
hostport=(${1//:/ })
101+
HOST=${hostport[0]}
102+
PORT=${hostport[1]}
103+
shift 1
104+
;;
105+
--child)
106+
CHILD=1
107+
shift 1
108+
;;
109+
-q | --quiet)
110+
QUIET=1
111+
shift 1
112+
;;
113+
-s | --strict)
114+
STRICT=1
115+
shift 1
116+
;;
117+
-h)
118+
HOST="$2"
119+
if [[ $HOST == "" ]]; then break; fi
120+
shift 2
121+
;;
122+
--host=*)
123+
HOST="${1#*=}"
124+
shift 1
125+
;;
126+
-p)
127+
PORT="$2"
128+
if [[ $PORT == "" ]]; then break; fi
129+
shift 2
130+
;;
131+
--port=*)
132+
PORT="${1#*=}"
133+
shift 1
134+
;;
135+
-t)
136+
TIMEOUT="$2"
137+
if [[ $TIMEOUT == "" ]]; then break; fi
138+
shift 2
139+
;;
140+
--timeout=*)
141+
TIMEOUT="${1#*=}"
142+
shift 1
143+
;;
144+
--)
145+
shift
146+
CLI=("$@")
147+
break
148+
;;
149+
--help)
150+
usage
151+
;;
152+
*)
153+
echoerr "Unknown argument: $1"
154+
usage
155+
;;
156+
esac
157+
done
158+
159+
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
160+
echoerr "Error: you need to provide a host and port to test."
161+
usage
162+
fi
163+
164+
TIMEOUT=${TIMEOUT:-15}
165+
STRICT=${STRICT:-0}
166+
CHILD=${CHILD:-0}
167+
QUIET=${QUIET:-0}
168+
169+
# check to see if timeout is from busybox?
170+
# check to see if timeout is from busybox?
171+
TIMEOUT_PATH=$(realpath $(which timeout))
172+
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
173+
ISBUSY=1
174+
BUSYTIMEFLAG="-t"
175+
else
176+
ISBUSY=0
177+
BUSYTIMEFLAG=""
178+
fi
179+
180+
if [[ $CHILD -gt 0 ]]; then
181+
wait_for
182+
RESULT=$?
183+
exit $RESULT
184+
else
185+
if [[ $TIMEOUT -gt 0 ]]; then
186+
wait_for_wrapper
187+
RESULT=$?
188+
else
189+
wait_for
190+
RESULT=$?
191+
fi
192+
fi
193+
194+
if [[ $CLI != "" ]]; then
195+
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
196+
echoerr "$cmdname: strict mode, refusing to execute subprocess"
197+
exit $RESULT
198+
fi
199+
exec "${CLI[@]}"
200+
else
201+
exit $RESULT
202+
fi

0 commit comments

Comments
 (0)