diff --git a/README.md b/README.md index f1956f7..8ed4bd4 100644 --- a/README.md +++ b/README.md @@ -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 [host:port...] [-t timeout] [-- command args] -q | --quiet Do not output any status messages -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes @@ -43,6 +43,26 @@ 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)). diff --git a/wait-for b/wait-for index ddfc39e..d0bd79c 100755 --- a/wait-for +++ b/wait-for @@ -11,7 +11,7 @@ usage() { exitcode="$1" cat << USAGE >&2 Usage: - $cmdname host:port [-t timeout] [-- command args] + $cmdname host:port [host:port...] [-t timeout] [-- command args] -q | --quiet Do not output any status messages -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes @@ -25,23 +25,22 @@ wait_for() { result=$? if [ $result -eq 0 ] ; then - if [ $# -gt 0 ] ; then - exec "$@" - fi - exit 0 + return + else + sleep 1 fi - sleep 1 done echo "Operation timed out" >&2 exit 1 } +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) @@ -71,9 +70,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 +done + +if [ $# -gt 0 ] ; then + exec "$@" +fi diff --git a/wait-for.bats b/wait-for.bats index cbea6a4..597f976 100644 --- a/wait-for.bats +++ b/wait-for.bats @@ -12,3 +12,9 @@ [ "$status" -ne 0 ] [ "$output" != "success" ] } + +@test "google and bing should be immediately found" { + run ./wait-for google.com:80 bing.com:80 -- echo 'success' + + [ "$output" = "success" ] +}