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

Commit

Permalink
adds waiting for multiple hosts based on eficode/pull/11
Browse files Browse the repository at this point in the history
  • Loading branch information
Okeanos committed Apr 2, 2020
1 parent 7c02f49 commit 5777d8c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your
## Usage

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

To wait for several containers to become available:

```
version: '2'
services:
db:
image: postgres:9.4
elk:
image: sebp/elk
backend:
build: backend
command: sh -c './wait-for db:5432 elk:9563 -- npm start'
depends_on:
- db
- elk
```

## Testing

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)).

docker build -t wait-for .
docker run -t wait-for

## Note

Make sure netcat is installed in your Dockerfile before running the command.
Expand Down
33 changes: 24 additions & 9 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$(basename $0) host:port [-t timeout] [-- command args]
$(basename $0) [host:port...] [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-l | --loose Execute subcommand even if the test times out
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
Expand All @@ -56,24 +56,25 @@ wait_for() {
local result
for i in `seq $TIMEOUT` ; do
# use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
test_connection "$HOST" "$PORT"
test_connection "$1" "$2"
result=$?
if [ $result -eq 0 ] ; then break ; fi
sleep 1
done
[ $result -ne 0 ] && echoerr "Operation timed out"
[ $result -ne 0 ] && echoerr "Operation timed out waiting for $1:$2"
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
fi
exit $result
}

SERVICES=""

while [ $# -gt 0 ]
do
case "$1" in
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
SERVICES="${SERVICES} $1"
shift 1
;;
-q | --quiet)
Expand Down Expand Up @@ -107,9 +108,23 @@ do
esac
done

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

wait_for "$@"
for SERVICE in ${SERVICES} ; do
HOST=$(printf "%s\n" "$SERVICE"| cut -d : -f 1)
PORT=$(printf "%s\n" "$SERVICE"| cut -d : -f 2)

if [ "$HOST" = "" -o "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi

wait_for "$HOST" "$PORT"
done

if [ $# -gt 0 ] ; then
exec "$@"
fi
6 changes: 6 additions & 0 deletions wait-for.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
[ "$output" != "success" ]
}

@test "google and bing should be immediately found" {
run ./wait-for google.com:80 bing.com:80 -- echo 'success'

[ "$output" = "success" ]
}

@test "nonexistent server should start command if loose option is specified" {
run ./wait-for -q -t 1 -l noserver:9999 -- echo 'passable' 2>&1

Expand Down

0 comments on commit 5777d8c

Please sign in to comment.