2020-02-03 21:53:02 +00:00
|
|
|
#!/usr/bin/env sh
|
2017-05-02 17:13:27 +00:00
|
|
|
|
2020-02-03 21:53:02 +00:00
|
|
|
WAITFOR_TIMEOUT=${WAITFOR_TIMEOUT:-15}
|
|
|
|
WAITFOR_QUIET=${WAITFOR_QUIET:-0}
|
2017-05-02 17:13:27 +00:00
|
|
|
|
|
|
|
echoerr() {
|
2020-02-03 15:20:00 +00:00
|
|
|
if [ "$WAITFOR_QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
|
2017-05-02 17:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
exitcode="$1"
|
|
|
|
cat << USAGE >&2
|
2020-02-03 21:53:25 +00:00
|
|
|
usage: wait-for host:port [-t timeout] [-- command args]
|
|
|
|
|
|
|
|
Synchronize services like docker containers and wait for readiness.
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-q | --quiet Do not output any status messages
|
|
|
|
-t WAITFOR_TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
|
|
|
|
-- COMMAND ARGS Execute command with args after the test finishes
|
2017-05-02 17:13:27 +00:00
|
|
|
USAGE
|
|
|
|
exit "$exitcode"
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for() {
|
2020-02-03 21:53:25 +00:00
|
|
|
for _ in $(seq "$WAITFOR_TIMEOUT") ; do
|
|
|
|
nc -w 1 -z "$WAITFOR_HOST" "$WAITFOR_PORT" > /dev/null 2>&1
|
2020-02-03 15:08:25 +00:00
|
|
|
|
2017-05-02 17:13:27 +00:00
|
|
|
result=$?
|
|
|
|
if [ $result -eq 0 ] ; then
|
2017-08-25 00:50:42 +00:00
|
|
|
if [ $# -gt 0 ] ; then
|
|
|
|
exec "$@"
|
2017-05-02 17:13:27 +00:00
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
2020-02-03 22:05:30 +00:00
|
|
|
echo "error: operation timed out" >&2
|
2017-05-02 17:13:27 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
while [ $# -gt 0 ]
|
|
|
|
do
|
|
|
|
case "$1" in
|
|
|
|
*:* )
|
2020-02-03 15:20:00 +00:00
|
|
|
WAITFOR_HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
|
|
|
|
WAITFOR_PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
|
2017-05-02 17:13:27 +00:00
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
-q | --quiet)
|
2020-02-03 15:20:00 +00:00
|
|
|
WAITFOR_QUIET=1
|
2017-05-02 17:13:27 +00:00
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
-t)
|
2020-02-03 15:20:00 +00:00
|
|
|
WAITFOR_TIMEOUT="$2"
|
|
|
|
if [ "$WAITFOR_TIMEOUT" = "" ]; then break; fi
|
2017-05-02 17:13:27 +00:00
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--timeout=*)
|
2020-02-03 15:20:00 +00:00
|
|
|
WAITFOR_TIMEOUT="${1#*=}"
|
2017-05-02 17:13:27 +00:00
|
|
|
shift 1
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
--help)
|
|
|
|
usage 0
|
|
|
|
;;
|
|
|
|
*)
|
2020-02-03 22:05:30 +00:00
|
|
|
echoerr "unknown argument: $1"
|
2017-05-02 17:13:27 +00:00
|
|
|
usage 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-02-03 15:08:25 +00:00
|
|
|
if ! [ -x "$(command -v nc)" ]; then
|
2020-02-03 22:05:30 +00:00
|
|
|
echoerr "error: netcat is required for wait-for to run"
|
2020-02-03 15:08:25 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-02-03 15:20:00 +00:00
|
|
|
if [ "$WAITFOR_HOST" = "" ] || [ "$WAITFOR_PORT" = "" ]; then
|
2020-02-03 22:05:30 +00:00
|
|
|
echoerr "error: you need to provide a host and port to test"
|
2017-05-02 17:13:27 +00:00
|
|
|
usage 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
wait_for "$@"
|