Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # ============LICENSE_START==================================================== |
| 3 | # Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. |
adheli.tavares | 9fc9fa4 | 2023-02-09 11:56:27 +0000 | [diff] [blame] | 4 | # Modifications Copyright (C) 2022-2023 Nordix Foundation. |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 5 | # ============================================================================= |
| 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 | |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 21 | usage() { |
| 22 | echo args: [-t timeout] [-c command] hostname1 port1 hostname2 port2 ... >&2 |
| 23 | exit 1 |
| 24 | } |
| 25 | |
liamfallon | a660eda | 2022-07-14 13:29:58 +0100 | [diff] [blame] | 26 | tmout=300 |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 27 | cmd= |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 28 | while getopts c:t: opt |
| 29 | do |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 30 | case "$opt" in |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 31 | c) |
| 32 | cmd="$OPTARG" |
| 33 | ;; |
| 34 | |
| 35 | t) |
| 36 | tmout="$OPTARG" |
| 37 | ;; |
| 38 | |
| 39 | *) |
| 40 | usage |
| 41 | ;; |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 42 | esac |
| 43 | done |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 44 | |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 45 | nargs=$((OPTIND-1)) |
| 46 | shift "$nargs" |
| 47 | |
| 48 | even_args=$(($#%2)) |
| 49 | if [ $# -lt 2 ] || [ "$even_args" -ne 0 ] |
| 50 | then |
| 51 | usage |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 52 | fi |
| 53 | |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 54 | while [ $# -ge 2 ] |
| 55 | do |
| 56 | export host="$1" |
| 57 | export port="$2" |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 58 | shift |
| 59 | shift |
| 60 | |
| 61 | echo "Waiting for $host port $port..." |
Taka Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 62 | |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 63 | while [ "$tmout" -gt 0 ] |
| 64 | do |
liamfallon | c4bd9e4 | 2022-09-08 09:56:47 +0100 | [diff] [blame] | 65 | if command -v docker > /dev/null 2>&1 |
| 66 | then |
adheli.tavares | 9fc9fa4 | 2023-02-09 11:56:27 +0000 | [diff] [blame] | 67 | docker ps --format "table {{ .Names }}\t{{ .Status }}" |
liamfallon | c4bd9e4 | 2022-09-08 09:56:47 +0100 | [diff] [blame] | 68 | fi |
| 69 | |
liamfallon | faac45b | 2022-09-01 12:05:47 +0100 | [diff] [blame] | 70 | 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 Cho | 6d188af | 2021-01-11 16:48:33 -0500 | [diff] [blame] | 84 | echo "$host port $port cannot be reached" |
| 85 | exit $rc |
| 86 | fi |
| 87 | done |
| 88 | |
| 89 | $cmd |
| 90 | |
| 91 | exit 0 |