blob: 5a8be2509c1ee1171f3e475e00b6c0c850b2ce14 [file] [log] [blame]
Taka Cho6d188af2021-01-11 16:48:33 -05001#!/bin/sh
2# ============LICENSE_START====================================================
3# Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
liamfallonfaac45b2022-09-01 12:05:47 +01004# Modifications Copyright (C) 2022 Nordix Foundation.
Taka Cho6d188af2021-01-11 16:48:33 -05005# =============================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18# SPDX-License-Identifier: Apache-2.0
19# ============LICENSE_END======================================================
20
liamfallonfaac45b2022-09-01 12:05:47 +010021usage() {
22 echo args: [-t timeout] [-c command] hostname1 port1 hostname2 port2 ... >&2
23 exit 1
24}
25
liamfallona660eda2022-07-14 13:29:58 +010026tmout=300
Taka Cho6d188af2021-01-11 16:48:33 -050027cmd=
liamfallonfaac45b2022-09-01 12:05:47 +010028while getopts c:t: opt
29do
Taka Cho6d188af2021-01-11 16:48:33 -050030 case "$opt" in
liamfallonfaac45b2022-09-01 12:05:47 +010031 c)
32 cmd="$OPTARG"
33 ;;
34
35 t)
36 tmout="$OPTARG"
37 ;;
38
39 *)
40 usage
41 ;;
Taka Cho6d188af2021-01-11 16:48:33 -050042 esac
43done
Taka Cho6d188af2021-01-11 16:48:33 -050044
liamfallonfaac45b2022-09-01 12:05:47 +010045nargs=$((OPTIND-1))
46shift "$nargs"
47
48even_args=$(($#%2))
49if [ $# -lt 2 ] || [ "$even_args" -ne 0 ]
50then
51 usage
Taka Cho6d188af2021-01-11 16:48:33 -050052fi
53
liamfallonfaac45b2022-09-01 12:05:47 +010054while [ $# -ge 2 ]
55do
56 export host="$1"
57 export port="$2"
Taka Cho6d188af2021-01-11 16:48:33 -050058 shift
59 shift
60
61 echo "Waiting for $host port $port..."
Taka Cho6d188af2021-01-11 16:48:33 -050062
liamfallonfaac45b2022-09-01 12:05:47 +010063 while [ "$tmout" -gt 0 ]
64 do
liamfallonc4bd9e42022-09-08 09:56:47 +010065 if command -v docker > /dev/null 2>&1
66 then
67 docker ps
68 fi
69
liamfallonfaac45b2022-09-01 12:05:47 +010070 nc -vz "$host" "$port"
71 rc=$?
72
73 if [ $rc -eq 0 ]
74 then
75 break
76 else
77 tmout=$((tmout-1))
78 sleep 1
79 fi
80 done
81
82 if [ $rc -ne 0 ]
83 then
Taka Cho6d188af2021-01-11 16:48:33 -050084 echo "$host port $port cannot be reached"
85 exit $rc
86 fi
87done
88
89$cmd
90
91exit 0