Make stand-alone Policy environment
[integration/csit.git] / scripts / policy / wait_for_port.sh
1 #!/bin/bash
2
3 tmout=120
4 cmd=
5
6 while getopts c:t: opt; do
7     case "$opt" in
8     c) cmd="$OPTARG" ;;
9     t) tmout="$OPTARG" ;;
10     esac
11 done
12 let nargs=$OPTIND-1
13 shift $nargs
14
15 let even_args=$#%2
16 if [[ $# -lt 2 || $even_args -ne 0 ]]; then
17         echo "args: [-t timeout] [-c command] hostname1 port1 hostname2 port2 ..." >&2
18         exit 1
19 fi
20
21 while [[ $# -ge 2 ]]; do
22     export host=$1
23     export port=$2
24     shift
25     shift
26
27     echo "Waiting for $host port $port..."
28     timeout $tmout bash -c 'until nc -vz "$host" "$port"; do echo -n ".";
29         sleep 1; done'
30     rc=$?
31
32     if [[ $rc != 0 ]]; then
33         echo "$host port $port cannot be reached"
34         exit $rc
35     fi
36 done
37
38 $cmd
39
40 exit 0