This script is forked from https://github.com/eficode/wait-for, and a
check for the existence of nc
command is added by Taro Sato. It it
hosted here mainly for use within Dockerfile.
Last active
April 10, 2020 05:36
-
-
Save okomestudio/0812bdff4ded980b93ab1f473ef2aa2e to your computer and use it in GitHub Desktop.
wait_for script for Docker
This file contains hidden or 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 | |
TIMEOUT=15 | |
QUIET=0 | |
echoerr() { | |
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi | |
} | |
usage() { | |
exitcode="$1" | |
cat << USAGE >&2 | |
Usage: | |
$cmdname host:port [-t timeout] [-- command args] | |
-q | --quiet Do not output any status messages | |
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout | |
-- COMMAND ARGS Execute command with args after the test finishes | |
USAGE | |
exit "$exitcode" | |
} | |
wait_for() { | |
for i in `seq $TIMEOUT` ; do | |
nc -z "$HOST" "$PORT" > /dev/null 2>&1 | |
result=$? | |
if [ $result -eq 0 ] ; then | |
if [ $# -gt 0 ] ; then | |
exec "$@" | |
fi | |
exit 0 | |
fi | |
sleep 1 | |
done | |
echo "Operation timed out" >&2 | |
exit 1 | |
} | |
while [ $# -gt 0 ] | |
do | |
case "$1" in | |
*:* ) | |
HOST=$(printf "%s\n" "$1"| cut -d : -f 1) | |
PORT=$(printf "%s\n" "$1"| cut -d : -f 2) | |
shift 1 | |
;; | |
-q | --quiet) | |
QUIET=1 | |
shift 1 | |
;; | |
-t) | |
TIMEOUT="$2" | |
if [ "$TIMEOUT" = "" ]; then break; fi | |
shift 2 | |
;; | |
--timeout=*) | |
TIMEOUT="${1#*=}" | |
shift 1 | |
;; | |
--) | |
shift | |
break | |
;; | |
--help) | |
usage 0 | |
;; | |
*) | |
echoerr "Unknown argument: $1" | |
usage 1 | |
;; | |
esac | |
done | |
if [ "$HOST" = "" -o "$PORT" = "" ]; then | |
echoerr "Error: you need to provide a host and port to test." | |
usage 2 | |
fi | |
if ! command -v nc >/dev/null 2>&1 ; then | |
echoerr "Install netcat" | |
usage 2 | |
fi | |
wait_for "$@" |
This file contains hidden or 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
#!/usr/bin/env bats | |
@test "google should be immediately found" { | |
run ./wait_for google.com:80 -- echo 'success' | |
[ "$output" = "success" ] | |
} | |
@test "nonexistent server should not start command" { | |
run ./wait_for -t 1 noserver:9999 -- echo 'success' | |
[ "$status" -ne 0 ] | |
[ "$output" != "success" ] | |
} |
This file contains hidden or 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
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment