github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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  
     5  cmdname=$(basename $0)
     6  
     7  TIMEOUT=${TIMEOUT:-15}
     8  STRICT=${STRICT:-0}
     9  CHILD=${CHILD:-0}
    10  QUIET=${QUIET:-0}
    11  
    12  echoerr() { if [ $QUIET -ne 1 ]; then echo "$@" 1>&2; fi }
    13  
    14  usage()
    15  {
    16      cat << USAGE >&2
    17  Usage:
    18      $cmdname host:port [-s] [-t timeout] [-- command args]
    19      -h HOST | --host=HOST       Host or IP under test
    20      -p PORT | --port=PORT       TCP port under test
    21                                  Alternatively, you specify the host and port as host:port
    22      -s | --strict               Only execute subcommand if the test succeeds
    23      -q | --quiet                Don't output any status messages
    24      -t TIMEOUT | --timeout=TIMEOUT
    25                                  Timeout in seconds, zero for no timeout
    26      -- COMMAND ARGS             Execute command with args after the test finishes
    27  USAGE
    28      exit 1
    29  }
    30  
    31  wait_for()
    32  {
    33      if [ $TIMEOUT -gt 0 ]; then
    34          echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
    35      else
    36          echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
    37      fi
    38      start_ts=$(date +%s)
    39      while :
    40      do
    41          nc -z $HOST $PORT >/dev/null 2>&1
    42          result=$?
    43          if [ $result -eq 0 ]; then
    44              end_ts=$(date +%s)
    45              echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
    46              break
    47          fi
    48          sleep 1
    49      done
    50      return $result
    51  }
    52  
    53  wait_for_wrapper()
    54  {
    55      # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
    56      if [ $QUIET -eq 1 ]; then
    57          timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    58      else
    59          timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    60      fi
    61      PID=$!
    62      trap "kill -INT -$PID" INT
    63      wait $PID
    64      RESULT=$?
    65      if [ $RESULT -ne 0 ]; then
    66          echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
    67      fi
    68      return $RESULT
    69  }
    70  
    71  # process arguments
    72  while [ $# -gt 0 ]
    73  do
    74      case "$1" in
    75          *:* )
    76          HOST=$(echo $1 | cut --fields=1 --delimiter=:)
    77          PORT=$(echo $1 | cut --fields=2- --delimiter=:)
    78          shift 1
    79          ;;
    80          --child)
    81          CHILD=1
    82          shift 1
    83          ;;
    84          -q | --quiet)
    85          QUIET=1
    86          shift 1
    87          ;;
    88          -s | --strict)
    89          STRICT=1
    90          shift 1
    91          ;;
    92          -h)
    93          HOST="$2"
    94          if [ "$HOST" = "" ]; then break; fi
    95          shift 2
    96          ;;
    97          --host=*)
    98          HOST="${1#*=}"
    99          shift 1
   100          ;;
   101          -p)
   102          PORT="$2"
   103          if [ "$PORT" = "" ]; then break; fi
   104          shift 2
   105          ;;
   106          --port=*)
   107          PORT="${1#*=}"
   108          shift 1
   109          ;;
   110          -t)
   111          TIMEOUT="$2"
   112          if [ "$TIMEOUT" = "" ]; then break; fi
   113          shift 2
   114          ;;
   115          --timeout=*)
   116          TIMEOUT="${1#*=}"
   117          shift 1
   118          ;;
   119          --)
   120          shift
   121          CLI="$@"
   122          break
   123          ;;
   124          --help)
   125          usage
   126          ;;
   127          *)
   128          echoerr "Unknown argument: $1"
   129          usage
   130          ;;
   131      esac
   132  done
   133  
   134  if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then
   135      echoerr "Error: you need to provide a host and port to test."
   136      usage
   137  fi
   138  
   139  if [ $CHILD -gt 0 ]; then
   140      wait_for
   141      RESULT=$?
   142      exit $RESULT
   143  else
   144      if [ $TIMEOUT -gt 0 ]; then
   145          wait_for_wrapper
   146          RESULT=$?
   147      else
   148          wait_for
   149          RESULT=$?
   150      fi
   151  fi
   152  
   153  if [ "$CLI" != "" ]; then
   154      if [ $RESULT -ne 0 ] && [ $STRICT -eq 1 ]; then
   155          echoerr "$cmdname: strict mode, refusing to execute subprocess"
   156          exit $RESULT
   157      fi
   158      exec $CLI
   159  else
   160      exit $RESULT
   161  fi