Add a WAITFORIT_ prefix in front of all variables

pull/44/head
Frédéric Woelffel 7 years ago
parent db049716e4
commit 410d77eda4

@ -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=$(basename $0) WAITFORIT_cmdname=$(basename $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 & tim eout $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,21 +72,21 @@ 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)
@ -118,7 +118,7 @@ do
;; ;;
--) --)
shift shift
CLI=("$@") WAITFORIT_CLI=("$@")
break break
;; ;;
--help) --help)
@ -136,42 +136,42 @@ if [[ "$HOST" == "" || "$PORT" == "" ]]; then
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?
# check to see if timeout is from busybox? # check to see if timeout is from busybox?
TIMEOUT_PATH=$(realpath $(which timeout)) WAITFORIT_TIMEOUT_PATH=$(realpath $(which timeout))
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 [[ $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

Loading…
Cancel
Save