blob: 6dcb3cab6a0a79b56609b33baf6fff3410933270 [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.
adheli.tavares9fc9fa42023-02-09 11:56:27 +00004# Modifications Copyright (C) 2022-2023 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
adheli.tavares9fc9fa42023-02-09 11:56:27 +000067 docker ps --format "table {{ .Names }}\t{{ .Status }}"
liamfallonc4bd9e42022-09-08 09:56:47 +010068 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