github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/bddtests/scripts/wait-for-it.sh (about) 1 #!/bin/sh 2 # Modified from https://github.com/vishnubob/wait-for-it to be POSIX compatible 3 # Use this script to test if a given TCP host/port are available 4 # The MIT License (MIT) 5 # Copyright (c) 2016 Giles Hall 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy of 8 # this software and associated documentation files (the "Software"), to deal in 9 # the Software without restriction, including without limitation the rights to 10 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 11 # of the Software, and to permit persons to whom the Software is furnished to do 12 # 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 # 26 # SPDX-License-Identifier: Apache-2.0 27 # 28 29 30 cmdname=$(basename $0) 31 32 TIMEOUT=${TIMEOUT:-15} 33 STRICT=${STRICT:-0} 34 CHILD=${CHILD:-0} 35 QUIET=${QUIET:-0} 36 37 echoerr() { if [ $QUIET -ne 1 ]; then echo "$@" 1>&2; fi } 38 39 usage() 40 { 41 cat << USAGE >&2 42 Usage: 43 $cmdname host:port [-s] [-t timeout] [-- command args] 44 -h HOST | --host=HOST Host or IP under test 45 -p PORT | --port=PORT TCP port under test 46 Alternatively, you specify the host and port as host:port 47 -s | --strict Only execute subcommand if the test succeeds 48 -q | --quiet Don't output any status messages 49 -t TIMEOUT | --timeout=TIMEOUT 50 Timeout in seconds, zero for no timeout 51 -- COMMAND ARGS Execute command with args after the test finishes 52 USAGE 53 exit 1 54 } 55 56 wait_for() 57 { 58 if [ $TIMEOUT -gt 0 ]; then 59 echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" 60 else 61 echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" 62 fi 63 start_ts=$(date +%s) 64 while : 65 do 66 nc -z $HOST $PORT >/dev/null 2>&1 67 result=$? 68 if [ $result -eq 0 ]; then 69 end_ts=$(date +%s) 70 echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" 71 break 72 fi 73 sleep 1 74 done 75 return $result 76 } 77 78 wait_for_wrapper() 79 { 80 # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 81 if [ $QUIET -eq 1 ]; then 82 timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & 83 else 84 timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & 85 fi 86 PID=$! 87 trap "kill -INT -$PID" INT 88 wait $PID 89 RESULT=$? 90 if [ $RESULT -ne 0 ]; then 91 echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" 92 fi 93 return $RESULT 94 } 95 96 # process arguments 97 while [ $# -gt 0 ] 98 do 99 case "$1" in 100 *:* ) 101 HOST=$(echo $1 | cut --fields=1 --delimiter=:) 102 PORT=$(echo $1 | cut --fields=2- --delimiter=:) 103 shift 1 104 ;; 105 --child) 106 CHILD=1 107 shift 1 108 ;; 109 -q | --quiet) 110 QUIET=1 111 shift 1 112 ;; 113 -s | --strict) 114 STRICT=1 115 shift 1 116 ;; 117 -h) 118 HOST="$2" 119 if [ "$HOST" = "" ]; then break; fi 120 shift 2 121 ;; 122 --host=*) 123 HOST="${1#*=}" 124 shift 1 125 ;; 126 -p) 127 PORT="$2" 128 if [ "$PORT" = "" ]; then break; fi 129 shift 2 130 ;; 131 --port=*) 132 PORT="${1#*=}" 133 shift 1 134 ;; 135 -t) 136 TIMEOUT="$2" 137 if [ "$TIMEOUT" = "" ]; then break; fi 138 shift 2 139 ;; 140 --timeout=*) 141 TIMEOUT="${1#*=}" 142 shift 1 143 ;; 144 --) 145 shift 146 CLI="$@" 147 break 148 ;; 149 --help) 150 usage 151 ;; 152 *) 153 echoerr "Unknown argument: $1" 154 usage 155 ;; 156 esac 157 done 158 159 if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then 160 echoerr "Error: you need to provide a host and port to test." 161 usage 162 fi 163 164 if [ $CHILD -gt 0 ]; then 165 wait_for 166 RESULT=$? 167 exit $RESULT 168 else 169 if [ $TIMEOUT -gt 0 ]; then 170 wait_for_wrapper 171 RESULT=$? 172 else 173 wait_for 174 RESULT=$? 175 fi 176 fi 177 178 if [ "$CLI" != "" ]; then 179 if [ $RESULT -ne 0 ] && [ $STRICT -eq 1 ]; then 180 echoerr "$cmdname: strict mode, refusing to execute subprocess" 181 exit $RESULT 182 fi 183 exec $CLI 184 else 185 exit $RESULT 186 fi