Skip to content
This repository was archived by the owner on Oct 7, 2021. It is now read-only.

Commit 5777d8c

Browse files
committed
adds waiting for multiple hosts based on eficode/pull/11
1 parent 7c02f49 commit 5777d8c

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your
99
## Usage
1010

1111
```
12-
wait-for host:port [-t timeout] [-- command args]
12+
wait-for [host:port...] [-t timeout] [-- command args]
1313
-q | --quiet Do not output any status messages
1414
-l | --loose Execute subcommand even if the test times out
1515
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
@@ -52,13 +52,32 @@ services:
5252
- db
5353
```
5454

55+
To wait for several containers to become available:
56+
57+
```
58+
version: '2'
59+
services:
60+
db:
61+
image: postgres:9.4
62+
63+
elk:
64+
image: sebp/elk
65+
66+
backend:
67+
build: backend
68+
command: sh -c './wait-for db:5432 elk:9563 -- npm start'
69+
depends_on:
70+
- db
71+
- elk
72+
```
73+
5574
## Testing
5675

5776
Ironically testing is done using [bats](https://github.com/sstephenson/bats), which on the other hand is depending on [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)).
5877

5978
docker build -t wait-for .
6079
docker run -t wait-for
61-
80+
6281
## Note
6382

6483
Make sure netcat is installed in your Dockerfile before running the command.

wait-for

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ usage() {
3131
exitcode="$1"
3232
cat << USAGE >&2
3333
Usage:
34-
$(basename $0) host:port [-t timeout] [-- command args]
34+
$(basename $0) [host:port...] [-t timeout] [-- command args]
3535
-q | --quiet Do not output any status messages
3636
-l | --loose Execute subcommand even if the test times out
3737
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
@@ -56,24 +56,25 @@ wait_for() {
5656
local result
5757
for i in `seq $TIMEOUT` ; do
5858
# use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
59-
test_connection "$HOST" "$PORT"
59+
test_connection "$1" "$2"
6060
result=$?
6161
if [ $result -eq 0 ] ; then break ; fi
6262
sleep 1
6363
done
64-
[ $result -ne 0 ] && echoerr "Operation timed out"
64+
[ $result -ne 0 ] && echoerr "Operation timed out waiting for $1:$2"
6565
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
6666
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
6767
fi
6868
exit $result
6969
}
7070

71+
SERVICES=""
72+
7173
while [ $# -gt 0 ]
7274
do
7375
case "$1" in
7476
*:* )
75-
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
76-
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
77+
SERVICES="${SERVICES} $1"
7778
shift 1
7879
;;
7980
-q | --quiet)
@@ -107,9 +108,23 @@ do
107108
esac
108109
done
109110

110-
if [ "$HOST" = "" -o "$PORT" = "" ]; then
111-
echoerr "Error: you need to provide a host and port to test."
112-
usage 2
111+
if [ "$SERVICES" = "" ] ; then
112+
echoerr "Error: you need to provide at least one service to test."
113+
usage 2
113114
fi
114115

115-
wait_for "$@"
116+
for SERVICE in ${SERVICES} ; do
117+
HOST=$(printf "%s\n" "$SERVICE"| cut -d : -f 1)
118+
PORT=$(printf "%s\n" "$SERVICE"| cut -d : -f 2)
119+
120+
if [ "$HOST" = "" -o "$PORT" = "" ]; then
121+
echoerr "Error: you need to provide a host and port to test."
122+
usage 2
123+
fi
124+
125+
wait_for "$HOST" "$PORT"
126+
done
127+
128+
if [ $# -gt 0 ] ; then
129+
exec "$@"
130+
fi

wait-for.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
[ "$output" != "success" ]
1414
}
1515

16+
@test "google and bing should be immediately found" {
17+
run ./wait-for google.com:80 bing.com:80 -- echo 'success'
18+
19+
[ "$output" = "success" ]
20+
}
21+
1622
@test "nonexistent server should start command if loose option is specified" {
1723
run ./wait-for -q -t 1 -l noserver:9999 -- echo 'passable' 2>&1
1824

0 commit comments

Comments
 (0)