Last active
April 7, 2024 00:33
-
-
Save efrecon/86456960e2110b287632fd7f42c1cd31 to your computer and use it in GitHub Desktop.
Wait for a port at remote site to be opened. Same as https://github.com/Eficode/wait-for but with options to be silent and an exponential backoff when waiting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# This comes from : https://github.com/Eficode/wait-for | |
# 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. | |
WAITFOR_TIMEOUT=${WAITFOR_TIMEOUT:-120} | |
WAITFOR_QUIET=${WAITFOR_QUIET:-0} | |
WAITFOR_VERBOSE=${WAITFOR_VERBOSE:-0} | |
WAITFOR_WAIT=${WAITFOR_WAIT:-1} | |
WAITFOR_SPEED=${WAITFOR_SPEED:-2} | |
WAITFOR_BACKOFF=${WAITFOR_BACKOFF:-15} | |
WAITFOR_HOST=${WAITFOR_HOST:-} | |
WAITFOR_PORT=${WAITFOR_PORT:-} | |
echoerr() { | |
if [ "$WAITFOR_QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi | |
} | |
usage() { | |
exitcode="$1" | |
cat << USAGE >&2 | |
Usage: | |
$cmdname host:port [-t timeout] [-- command args] | |
-v | --verbose Be more verbose when waiting | |
-q | --quiet Do not output any status messages | |
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout | |
-w WAIT | --wait=seconds Number of seconds to wait between attempts | |
-b BACKOFF | --backoff=seconds Maximum number of seconds to wait for exponential backoff | |
-s SPEED | --speed=float Seconds multiplier when exponential backoff. | |
-- COMMAND ARGS Execute command with args after the test finishes | |
USAGE | |
exit "$exitcode" | |
} | |
wait_once() { | |
NC=`which nc` | |
BASH=`which bash` | |
if [ -n "$NC" ]; then | |
if [ "$WAITFOR_VERBOSE" = "1" -a "$WAITFOR_QUIET" = "0" ]; then | |
echo "Waiting for ${WAITFOR_HOST}:${WAITFOR_PORT} with ${NC}..." 1>&2 | |
fi | |
${NC} -z -w 5 "$WAITFOR_HOST" "$WAITFOR_PORT" > /dev/null 2>&1 | |
elif [ -n "$BASH" ]; then | |
if [ "$WAITFOR_VERBOSE" = "1" -a "$WAITFOR_QUIET" = "0" ]; then | |
echo "Waiting for ${WAITFOR_HOST}:${WAITFOR_PORT} with ${BASH}..." 1>&2 | |
fi | |
timeout 5 ${BASH} -c "echo >/dev/tcp/${WAITFOR_HOST}/${WAITFOR_PORT}" >/dev/null 2>&1 | |
else | |
echo "CANNOT test connection to ${WAITFOR_HOST}:${WAITFOR_PORT}!" | |
exit 0 | |
fi | |
result=$? | |
if [ $result -eq 0 ] ; then | |
if [ $# -gt 0 ] ; then | |
if [ "$WAITFOR_VERBOSE" = "1" -a "$WAITFOR_QUIET" = "0" ]; then | |
echo "Connection to ${WAITFOR_HOST}:${WAITFOR_PORT} established" 1>&2 | |
fi | |
exec "$@" | |
fi | |
exit 0 | |
fi | |
sleep ${WAITFOR_WAIT} | |
} | |
adjust() { | |
if [ "${WAITFOR_SPEED}" -ne "1" ]; then | |
WAITFOR_WAIT=$(awk "BEGIN{print int($WAITFOR_WAIT * $WAITFOR_SPEED)}") | |
if [ "${WAITFOR_WAIT}" -gt "${WAITFOR_BACKOFF}" ]; then | |
WAITFOR_WAIT="${WAITFOR_BACKOFF}" | |
fi | |
fi | |
} | |
wait_for() { | |
if [ "$WAITFOR_TIMEOUT" = "" -o "$WAITFOR_TIMEOUT" -le "0" ]; then | |
while true; do | |
wait_once "$@" | |
adjust | |
done | |
else | |
waited=0 | |
while [ "$waited" -lt "${WAITFOR_TIMEOUT}" ]; do | |
wait_once "$@" | |
waited=$(expr $waited + ${WAITFOR_WAIT}) | |
adjust | |
done | |
fi | |
echo "Operation timed out" >&2 | |
exit 1 | |
} | |
while [ $# -gt 0 ] | |
do | |
case "$1" in | |
*:* ) | |
WAITFOR_HOST=$(printf "%s\n" "$1"| cut -d : -f 1) | |
WAITFOR_PORT=$(printf "%s\n" "$1"| cut -d : -f 2) | |
shift 1 | |
;; | |
-q | --quiet) | |
WAITFOR_QUIET=1 | |
shift 1 | |
;; | |
-v | --verbose) | |
WAITFOR_VERBOSE=1 | |
shift 1 | |
;; | |
-t | --timeout) | |
WAITFOR_TIMEOUT="$2" | |
if [ "$WAITFOR_TIMEOUT" = "" ]; then break; fi | |
shift 2 | |
;; | |
--timeout=*) | |
WAITFOR_TIMEOUT="${1#*=}" | |
shift 1 | |
;; | |
-w | --wait) | |
WAITFOR_WAIT="$2" | |
shift 2 | |
;; | |
--wait=*) | |
WAITFOR_WAIT="${1#*=}" | |
shift 1 | |
;; | |
-b | --backoff) | |
WAITFOR_BACKOFF="$2" | |
shift 2 | |
;; | |
--backoff=*) | |
WAITFOR_BACKOFF="${1#*=}" | |
shift 1 | |
;; | |
-s | --speed) | |
WAITFOR_SPEED="$2" | |
shift 2 | |
;; | |
--speed=*) | |
WAITFOR_SPEED="${1#*=}" | |
shift 1 | |
;; | |
--) | |
shift | |
break | |
;; | |
--help) | |
usage 0 | |
;; | |
*) | |
echoerr "Unknown argument: $1" | |
usage 1 | |
;; | |
esac | |
done | |
if [ "$WAITFOR_HOST" = "" -o "$WAITFOR_PORT" = "" ]; then | |
echoerr "Error: you need to provide a host and port to test." | |
usage 2 | |
fi | |
wait_for "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment