|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright (c) 2017 Eficode Oy |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +VERSION="2.2.3" |
| 26 | + |
| 27 | +set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result" |
| 28 | +TIMEOUT=15 |
| 29 | +QUIET=0 |
| 30 | +# The protocol to make the request with, either "tcp" or "http" |
| 31 | +PROTOCOL="tcp" |
| 32 | + |
| 33 | +echoerr() { |
| 34 | + if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi |
| 35 | +} |
| 36 | + |
| 37 | +usage() { |
| 38 | + exitcode="$1" |
| 39 | + cat << USAGE >&2 |
| 40 | +Usage: |
| 41 | + $0 host:port|url [-t timeout] [-- command args] |
| 42 | + -q | --quiet Do not output any status messages |
| 43 | + -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout |
| 44 | + -v | --version Show the version of this tool |
| 45 | + -- COMMAND ARGS Execute command with args after the test finishes |
| 46 | +USAGE |
| 47 | + exit "$exitcode" |
| 48 | +} |
| 49 | + |
| 50 | +wait_for() { |
| 51 | + case "$PROTOCOL" in |
| 52 | + tcp) |
| 53 | + if ! command -v nc >/dev/null; then |
| 54 | + echoerr 'nc command is missing!' |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + ;; |
| 58 | + http) |
| 59 | + if ! command -v wget >/dev/null; then |
| 60 | + echoerr 'wget command is missing!' |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + ;; |
| 64 | + esac |
| 65 | + |
| 66 | + TIMEOUT_END=$(($(date +%s) + TIMEOUT)) |
| 67 | + |
| 68 | + while :; do |
| 69 | + case "$PROTOCOL" in |
| 70 | + tcp) |
| 71 | + nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1 |
| 72 | + ;; |
| 73 | + http) |
| 74 | + wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1 |
| 75 | + ;; |
| 76 | + *) |
| 77 | + echoerr "Unknown protocol '$PROTOCOL'" |
| 78 | + exit 1 |
| 79 | + ;; |
| 80 | + esac |
| 81 | + |
| 82 | + result=$? |
| 83 | + |
| 84 | + if [ $result -eq 0 ] ; then |
| 85 | + if [ $# -gt 7 ] ; then |
| 86 | + for result in $(seq $(($# - 7))); do |
| 87 | + result=$1 |
| 88 | + shift |
| 89 | + set -- "$@" "$result" |
| 90 | + done |
| 91 | + |
| 92 | + TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7 |
| 93 | + shift 7 |
| 94 | + exec "$@" |
| 95 | + fi |
| 96 | + exit 0 |
| 97 | + fi |
| 98 | + |
| 99 | + if [ $TIMEOUT -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then |
| 100 | + echo "Operation timed out" >&2 |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + sleep 1 |
| 105 | + done |
| 106 | +} |
| 107 | + |
| 108 | +while :; do |
| 109 | + case "$1" in |
| 110 | + http://*|https://*) |
| 111 | + HOST="$1" |
| 112 | + PROTOCOL="http" |
| 113 | + shift 1 |
| 114 | + ;; |
| 115 | + *:* ) |
| 116 | + HOST=$(printf "%s\n" "$1"| cut -d : -f 1) |
| 117 | + PORT=$(printf "%s\n" "$1"| cut -d : -f 2) |
| 118 | + shift 1 |
| 119 | + ;; |
| 120 | + -v | --version) |
| 121 | + echo $VERSION |
| 122 | + exit |
| 123 | + ;; |
| 124 | + -q | --quiet) |
| 125 | + QUIET=1 |
| 126 | + shift 1 |
| 127 | + ;; |
| 128 | + -q-*) |
| 129 | + QUIET=0 |
| 130 | + echoerr "Unknown option: $1" |
| 131 | + usage 1 |
| 132 | + ;; |
| 133 | + -q*) |
| 134 | + QUIET=1 |
| 135 | + result=$1 |
| 136 | + shift 1 |
| 137 | + set -- -"${result#-q}" "$@" |
| 138 | + ;; |
| 139 | + -t | --timeout) |
| 140 | + TIMEOUT="$2" |
| 141 | + shift 2 |
| 142 | + ;; |
| 143 | + -t*) |
| 144 | + TIMEOUT="${1#-t}" |
| 145 | + shift 1 |
| 146 | + ;; |
| 147 | + --timeout=*) |
| 148 | + TIMEOUT="${1#*=}" |
| 149 | + shift 1 |
| 150 | + ;; |
| 151 | + --) |
| 152 | + shift |
| 153 | + break |
| 154 | + ;; |
| 155 | + --help) |
| 156 | + usage 0 |
| 157 | + ;; |
| 158 | + -*) |
| 159 | + QUIET=0 |
| 160 | + echoerr "Unknown option: $1" |
| 161 | + usage 1 |
| 162 | + ;; |
| 163 | + *) |
| 164 | + QUIET=0 |
| 165 | + echoerr "Unknown argument: $1" |
| 166 | + usage 1 |
| 167 | + ;; |
| 168 | + esac |
| 169 | +done |
| 170 | + |
| 171 | +if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then |
| 172 | + echoerr "Error: invalid timeout '$TIMEOUT'" |
| 173 | + usage 3 |
| 174 | +fi |
| 175 | + |
| 176 | +case "$PROTOCOL" in |
| 177 | + tcp) |
| 178 | + if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then |
| 179 | + echoerr "Error: you need to provide a host and port to test." |
| 180 | + usage 2 |
| 181 | + fi |
| 182 | + ;; |
| 183 | + http) |
| 184 | + if [ "$HOST" = "" ]; then |
| 185 | + echoerr "Error: you need to provide a host to test." |
| 186 | + usage 2 |
| 187 | + fi |
| 188 | + ;; |
| 189 | +esac |
| 190 | + |
| 191 | +wait_for "$@" |
0 commit comments