github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/scripts/wait-for (about)

     1  #!/bin/sh
     2  
     3  TIMEOUT=15
     4  QUIET=0
     5  
     6  echoerr() {
     7    if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
     8  }
     9  
    10  usage() {
    11    exitcode="$1"
    12    cat << USAGE >&2
    13  Usage:
    14    $cmdname host:port [-t timeout] [-- command args]
    15    -q | --quiet                        Do not output any status messages
    16    -t TIMEOUT | --timeout=timeout      Timeout in seconds, zero for no timeout
    17    -- COMMAND ARGS                     Execute command with args after the test finishes
    18  USAGE
    19    exit "$exitcode"
    20  }
    21  
    22  wait_for() {
    23    for i in `seq $TIMEOUT` ; do
    24      nc -z "$HOST" "$PORT" > /dev/null 2>&1
    25      
    26      result=$?
    27      if [ $result -eq 0 ] ; then
    28        if [ $# -gt 0 ] ; then
    29          exec "$@"
    30        fi
    31        exit 0
    32      fi
    33      sleep 1
    34    done
    35    echo "Operation timed out" >&2
    36    exit 1
    37  }
    38  
    39  while [ $# -gt 0 ]
    40  do
    41    case "$1" in
    42      *:* )
    43      HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
    44      PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
    45      shift 1
    46      ;;
    47      -q | --quiet)
    48      QUIET=1
    49      shift 1
    50      ;;
    51      -t)
    52      TIMEOUT="$2"
    53      if [ "$TIMEOUT" = "" ]; then break; fi
    54      shift 2
    55      ;;
    56      --timeout=*)
    57      TIMEOUT="${1#*=}"
    58      shift 1
    59      ;;
    60      --)
    61      shift
    62      break
    63      ;;
    64      --help)
    65      usage 0
    66      ;;
    67      *)
    68      echoerr "Unknown argument: $1"
    69      usage 1
    70      ;;
    71    esac
    72  done
    73  
    74  if [ "$HOST" = "" -o "$PORT" = "" ]; then
    75    echoerr "Error: you need to provide a host and port to test."
    76    usage 2
    77  fi
    78  
    79  wait_for "$@"