Merge branch 'fwoelffel-master'
This commit is contained in:
133
wait-for-it.sh
133
wait-for-it.sh
@ -1,15 +1,15 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Use this script to test if a given TCP host/port are available
|
# Use this script to test if a given TCP host/port are available
|
||||||
|
|
||||||
cmdname=${0##*/}
|
WAITFORIT_cmdname=${0##*/}
|
||||||
|
|
||||||
echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
|
echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
|
||||||
|
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
cat << USAGE >&2
|
cat << USAGE >&2
|
||||||
Usage:
|
Usage:
|
||||||
$cmdname host:port [-s] [-t timeout] [-- command args]
|
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
|
||||||
-h HOST | --host=HOST Host or IP under test
|
-h HOST | --host=HOST Host or IP under test
|
||||||
-p PORT | --port=PORT TCP port under test
|
-p PORT | --port=PORT TCP port under test
|
||||||
Alternatively, you specify the host and port as host:port
|
Alternatively, you specify the host and port as host:port
|
||||||
@ -24,47 +24,47 @@ USAGE
|
|||||||
|
|
||||||
wait_for()
|
wait_for()
|
||||||
{
|
{
|
||||||
if [[ $TIMEOUT -gt 0 ]]; then
|
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
|
||||||
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
|
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
|
||||||
else
|
else
|
||||||
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
|
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
|
||||||
fi
|
fi
|
||||||
start_ts=$(date +%s)
|
WAITFORIT_start_ts=$(date +%s)
|
||||||
while :
|
while :
|
||||||
do
|
do
|
||||||
if [[ $ISBUSY -eq 1 ]]; then
|
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
|
||||||
nc -z $HOST $PORT
|
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
|
||||||
result=$?
|
WAITFORIT_result=$?
|
||||||
else
|
else
|
||||||
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
|
(echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
|
||||||
result=$?
|
WAITFORIT_result=$?
|
||||||
fi
|
fi
|
||||||
if [[ $result -eq 0 ]]; then
|
if [[ $WAITFORIT_result -eq 0 ]]; then
|
||||||
end_ts=$(date +%s)
|
WAITFORIT_end_ts=$(date +%s)
|
||||||
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
|
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
sleep 1
|
sleep 1
|
||||||
done
|
done
|
||||||
return $result
|
return $WAITFORIT_result
|
||||||
}
|
}
|
||||||
|
|
||||||
wait_for_wrapper()
|
wait_for_wrapper()
|
||||||
{
|
{
|
||||||
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
|
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
|
||||||
if [[ $QUIET -eq 1 ]]; then
|
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
|
||||||
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
|
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
|
||||||
else
|
else
|
||||||
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
|
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
|
||||||
fi
|
fi
|
||||||
PID=$!
|
WAITFORIT_PID=$!
|
||||||
trap "kill -INT -$PID" INT
|
trap "kill -INT -$WAITFORIT_PID" INT
|
||||||
wait $PID
|
wait $WAITFORIT_PID
|
||||||
RESULT=$?
|
WAITFORIT_RESULT=$?
|
||||||
if [[ $RESULT -ne 0 ]]; then
|
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
|
||||||
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
|
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
|
||||||
fi
|
fi
|
||||||
return $RESULT
|
return $WAITFORIT_RESULT
|
||||||
}
|
}
|
||||||
|
|
||||||
# process arguments
|
# process arguments
|
||||||
@ -72,53 +72,53 @@ while [[ $# -gt 0 ]]
|
|||||||
do
|
do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
*:* )
|
*:* )
|
||||||
hostport=(${1//:/ })
|
WAITFORIT_hostport=(${1//:/ })
|
||||||
HOST=${hostport[0]}
|
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
|
||||||
PORT=${hostport[1]}
|
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
--child)
|
--child)
|
||||||
CHILD=1
|
WAITFORIT_CHILD=1
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
-q | --quiet)
|
-q | --quiet)
|
||||||
QUIET=1
|
WAITFORIT_QUIET=1
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
-s | --strict)
|
-s | --strict)
|
||||||
STRICT=1
|
WAITFORIT_STRICT=1
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
-h)
|
-h)
|
||||||
HOST="$2"
|
WAITFORIT_HOST="$2"
|
||||||
if [[ $HOST == "" ]]; then break; fi
|
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
--host=*)
|
--host=*)
|
||||||
HOST="${1#*=}"
|
WAITFORIT_HOST="${1#*=}"
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
-p)
|
-p)
|
||||||
PORT="$2"
|
WAITFORIT_PORT="$2"
|
||||||
if [[ $PORT == "" ]]; then break; fi
|
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
--port=*)
|
--port=*)
|
||||||
PORT="${1#*=}"
|
WAITFORIT_PORT="${1#*=}"
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
-t)
|
-t)
|
||||||
TIMEOUT="$2"
|
WAITFORIT_TIMEOUT="$2"
|
||||||
if [[ $TIMEOUT == "" ]]; then break; fi
|
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
--timeout=*)
|
--timeout=*)
|
||||||
TIMEOUT="${1#*=}"
|
WAITFORIT_TIMEOUT="${1#*=}"
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
shift
|
shift
|
||||||
CLI=("$@")
|
WAITFORIT_CLI=("$@")
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
--help)
|
--help)
|
||||||
@ -131,47 +131,48 @@ do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
|
if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
|
||||||
echoerr "Error: you need to provide a host and port to test."
|
echoerr "Error: you need to provide a host and port to test."
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
TIMEOUT=${TIMEOUT:-15}
|
WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
|
||||||
STRICT=${STRICT:-0}
|
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
|
||||||
CHILD=${CHILD:-0}
|
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
|
||||||
QUIET=${QUIET:-0}
|
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}
|
||||||
|
|
||||||
# check to see if timeout is from busybox?
|
# check to see if timeout is from busybox?
|
||||||
TIMEOUT_PATH=$(type -p timeout)
|
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
|
||||||
TIMEOUT_PATH=$(realpath $TIMEOUT_PATH 2>/dev/null || readlink -f $TIMEOUT_PATH)
|
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
|
||||||
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
|
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
|
||||||
ISBUSY=1
|
WAITFORIT_ISBUSY=1
|
||||||
BUSYTIMEFLAG="-t"
|
WAITFORIT_BUSYTIMEFLAG="-t"
|
||||||
|
|
||||||
else
|
else
|
||||||
ISBUSY=0
|
WAITFORIT_ISBUSY=0
|
||||||
BUSYTIMEFLAG=""
|
WAITFORIT_BUSYTIMEFLAG=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $CHILD -gt 0 ]]; then
|
if [[ $WAITFORIT_CHILD -gt 0 ]]; then
|
||||||
wait_for
|
wait_for
|
||||||
RESULT=$?
|
WAITFORIT_RESULT=$?
|
||||||
exit $RESULT
|
exit $WAITFORIT_RESULT
|
||||||
else
|
else
|
||||||
if [[ $TIMEOUT -gt 0 ]]; then
|
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
|
||||||
wait_for_wrapper
|
wait_for_wrapper
|
||||||
RESULT=$?
|
WAITFORIT_RESULT=$?
|
||||||
else
|
else
|
||||||
wait_for
|
wait_for
|
||||||
RESULT=$?
|
WAITFORIT_RESULT=$?
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $CLI != "" ]]; then
|
if [[ $WAITFORIT_CLI != "" ]]; then
|
||||||
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
|
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
|
||||||
echoerr "$cmdname: strict mode, refusing to execute subprocess"
|
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
|
||||||
exit $RESULT
|
exit $WAITFORIT_RESULT
|
||||||
fi
|
fi
|
||||||
exec "${CLI[@]}"
|
exec "${WAITFORIT_CLI[@]}"
|
||||||
else
|
else
|
||||||
exit $RESULT
|
exit $WAITFORIT_RESULT
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user