github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/contrib/wait-for-it.sh (about)

     1  #!/usr/bin/env bash
     2  #   Use this script to test if a given TCP host/port are available
     3  #
     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  cmdname=$(basename $0)
    26  
    27  echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
    28  
    29  usage()
    30  {
    31      cat << USAGE >&2
    32  Usage:
    33      $cmdname host:port [-s] [-t timeout] [-- command args]
    34      -h HOST | --host=HOST       Host or IP under test
    35      -p PORT | --port=PORT       TCP port under test
    36                                  Alternatively, you specify the host and port as host:port
    37      -s | --strict               Only execute subcommand if the test succeeds
    38      -q | --quiet                Don't output any status messages
    39      -t TIMEOUT | --timeout=TIMEOUT
    40                                  Timeout in seconds, zero for no timeout
    41      -- COMMAND ARGS             Execute command with args after the test finishes
    42  USAGE
    43      exit 1
    44  }
    45  
    46  wait_for()
    47  {
    48      if [[ $TIMEOUT -gt 0 ]]; then
    49          echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
    50      else
    51          echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
    52      fi
    53      start_ts=$(date +%s)
    54      while :
    55      do
    56          if [[ $ISBUSY -eq 1 ]]; then
    57              nc -z $HOST $PORT
    58              result=$?
    59          else
    60              (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
    61              result=$?
    62          fi
    63          if [[ $result -eq 0 ]]; then
    64              end_ts=$(date +%s)
    65              echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
    66              break
    67          fi
    68          sleep 1
    69      done
    70      return $result
    71  }
    72  
    73  wait_for_wrapper()
    74  {
    75      # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
    76      if [[ $QUIET -eq 1 ]]; then
    77          timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    78      else
    79          timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
    80      fi
    81      PID=$!
    82      trap "kill -INT -$PID" INT
    83      wait $PID
    84      RESULT=$?
    85      if [[ $RESULT -ne 0 ]]; then
    86          echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
    87      fi
    88      return $RESULT
    89  }
    90  
    91  # process arguments
    92  while [[ $# -gt 0 ]]
    93  do
    94      case "$1" in
    95          *:* )
    96          hostport=(${1//:/ })
    97          HOST=${hostport[0]}
    98          PORT=${hostport[1]}
    99          shift 1
   100          ;;
   101          --child)
   102          CHILD=1
   103          shift 1
   104          ;;
   105          -q | --quiet)
   106          QUIET=1
   107          shift 1
   108          ;;
   109          -s | --strict)
   110          STRICT=1
   111          shift 1
   112          ;;
   113          -h)
   114          HOST="$2"
   115          if [[ $HOST == "" ]]; then break; fi
   116          shift 2
   117          ;;
   118          --host=*)
   119          HOST="${1#*=}"
   120          shift 1
   121          ;;
   122          -p)
   123          PORT="$2"
   124          if [[ $PORT == "" ]]; then break; fi
   125          shift 2
   126          ;;
   127          --port=*)
   128          PORT="${1#*=}"
   129          shift 1
   130          ;;
   131          -t)
   132          TIMEOUT="$2"
   133          if [[ $TIMEOUT == "" ]]; then break; fi
   134          shift 2
   135          ;;
   136          --timeout=*)
   137          TIMEOUT="${1#*=}"
   138          shift 1
   139          ;;
   140          --)
   141          shift
   142          CLI=("$@")
   143          break
   144          ;;
   145          --help)
   146          usage
   147          ;;
   148          *)
   149          echoerr "Unknown argument: $1"
   150          usage
   151          ;;
   152      esac
   153  done
   154  
   155  if [[ "$HOST" == "" || "$PORT" == "" ]]; then
   156      echoerr "Error: you need to provide a host and port to test."
   157      usage
   158  fi
   159  
   160  TIMEOUT=${TIMEOUT:-15}
   161  STRICT=${STRICT:-0}
   162  CHILD=${CHILD:-0}
   163  QUIET=${QUIET:-0}
   164  
   165  # check to see if timeout is from busybox?
   166  # check to see if timeout is from busybox?
   167  TIMEOUT_PATH=$(realpath $(which timeout))
   168  if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
   169          ISBUSY=1
   170          BUSYTIMEFLAG="-t"
   171  else
   172          ISBUSY=0
   173          BUSYTIMEFLAG=""
   174  fi
   175  
   176  if [[ $CHILD -gt 0 ]]; then
   177      wait_for
   178      RESULT=$?
   179      exit $RESULT
   180  else
   181      if [[ $TIMEOUT -gt 0 ]]; then
   182          wait_for_wrapper
   183          RESULT=$?
   184      else
   185          wait_for
   186          RESULT=$?
   187      fi
   188  fi
   189  
   190  if [[ $CLI != "" ]]; then
   191      if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
   192          echoerr "$cmdname: strict mode, refusing to execute subprocess"
   193          exit $RESULT
   194      fi
   195      exec "${CLI[@]}"
   196  else
   197      exit $RESULT
   198  fi