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