Upgrade vendored wait-for.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dan Helfman 2023-02-23 16:01:42 -08:00
parent 57051f25b6
commit 675c4e0c8f
1 changed files with 127 additions and 14 deletions

141
wait-for
View File

@ -1,7 +1,34 @@
#!/bin/sh #!/bin/sh
# The MIT License (MIT)
#
# Copyright (c) 2017 Eficode Oy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
VERSION="2.2.4"
set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result"
TIMEOUT=15 TIMEOUT=15
QUIET=0 QUIET=0
# The protocol to make the request with, either "tcp" or "http"
PROTOCOL="tcp"
echoerr() { echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
@ -11,48 +38,113 @@ usage() {
exitcode="$1" exitcode="$1"
cat << USAGE >&2 cat << USAGE >&2
Usage: Usage:
$cmdname host:port [-t timeout] [-- command args] $0 host:port|url [-t timeout] [-- command args]
-q | --quiet Do not output any status messages -q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
Defaults to 15 seconds
-v | --version Show the version of this tool
-- COMMAND ARGS Execute command with args after the test finishes -- COMMAND ARGS Execute command with args after the test finishes
USAGE USAGE
exit "$exitcode" exit "$exitcode"
} }
wait_for() { wait_for() {
for i in `seq $TIMEOUT` ; do case "$PROTOCOL" in
nc -z "$HOST" "$PORT" > /dev/null 2>&1 tcp)
if ! command -v nc >/dev/null; then
echoerr 'nc command is missing!'
exit 1
fi
;;
http)
if ! command -v wget >/dev/null; then
echoerr 'wget command is missing!'
exit 1
fi
;;
esac
TIMEOUT_END=$(($(date +%s) + TIMEOUT))
while :; do
case "$PROTOCOL" in
tcp)
nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1
;;
http)
wget --timeout=1 --tries=1 -q "$HOST" -O /dev/null > /dev/null 2>&1
;;
*)
echoerr "Unknown protocol '$PROTOCOL'"
exit 1
;;
esac
result=$? result=$?
if [ $result -eq 0 ] ; then if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then if [ $# -gt 7 ] ; then
for result in $(seq $(($# - 7))); do
result=$1
shift
set -- "$@" "$result"
done
TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7
shift 7
exec "$@" exec "$@"
fi fi
exit 0 exit 0
fi fi
if [ $TIMEOUT -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then
echo "Operation timed out" >&2
exit 1
fi
sleep 1 sleep 1
done done
echo "Operation timed out" >&2
exit 1
} }
while [ $# -gt 0 ] while :; do
do
case "$1" in case "$1" in
http://*|https://*)
HOST="$1"
PROTOCOL="http"
shift 1
;;
*:* ) *:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1) HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2) PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
shift 1 shift 1
;; ;;
-v | --version)
echo $VERSION
exit
;;
-q | --quiet) -q | --quiet)
QUIET=1 QUIET=1
shift 1 shift 1
;; ;;
-t) -q-*)
QUIET=0
echoerr "Unknown option: $1"
usage 1
;;
-q*)
QUIET=1
result=$1
shift 1
set -- -"${result#-q}" "$@"
;;
-t | --timeout)
TIMEOUT="$2" TIMEOUT="$2"
if [ "$TIMEOUT" = "" ]; then break; fi
shift 2 shift 2
;; ;;
-t*)
TIMEOUT="${1#-t}"
shift 1
;;
--timeout=*) --timeout=*)
TIMEOUT="${1#*=}" TIMEOUT="${1#*=}"
shift 1 shift 1
@ -64,16 +156,37 @@ do
--help) --help)
usage 0 usage 0
;; ;;
-*)
QUIET=0
echoerr "Unknown option: $1"
usage 1
;;
*) *)
QUIET=0
echoerr "Unknown argument: $1" echoerr "Unknown argument: $1"
usage 1 usage 1
;; ;;
esac esac
done done
if [ "$HOST" = "" -o "$PORT" = "" ]; then if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
echoerr "Error: you need to provide a host and port to test." echoerr "Error: invalid timeout '$TIMEOUT'"
usage 2 usage 3
fi fi
case "$PROTOCOL" in
tcp)
if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi
;;
http)
if [ "$HOST" = "" ]; then
echoerr "Error: you need to provide a host to test."
usage 2
fi
;;
esac
wait_for "$@" wait_for "$@"