forked from waisbrot/docker-wait
-
Notifications
You must be signed in to change notification settings - Fork 4
/
wait
executable file
·142 lines (130 loc) · 3.17 KB
/
wait
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
usage() {
echo ""
echo "usage: $0 [-p] [-c] [-t]"
echo ""
echo "optional arguments:"
echo " -p only test exposed ports from the linked container that are in this list"
echo " -c custom connection(s) to test, host:port, multiple seperated by comma"
echo " -t timeout after which connection attempt will fail, default 30"
echo ""
echo " Example: $0 -c google.com:443,github.com:443 -t 15"
echo " $0 -p 8080"
}
set -e
timeout=${TIMEOUT:-30}
# Parse arguments
while getopts ":t:c:p:" opt; do
case $opt in
t)
timeout=$OPTARG
case $timeout in
''|*[!0-9]*)
echo "Invalid timeout number: $timeout" >&2
exit 1
;;
esac
;;
c)
TARGETS="$OPTARG"
;;
p)
PORTS="$OPTARG"
;;
\?)
set +x
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
set +x
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
esac
done
if [ -n "$PORTS" ]; then
ports=$(echo "$PORTS"|tr , ' '|tr -s '[:space:]' '\n')
fi
# our target format is a newline-delimited list where each item is "host:ip"
if [ -z "$TARGETS" ]; then
# empty TARGETS variable will default to checking all host/ports exposed
uris=""
oldifs="$IFS"
IFS=$'\n'
for var in $(env); do
case "$var" in
*_TCP=*)
uris="$uris $(echo "$var" | cut -d= -f2 | cut -c7-)"
;;
esac
done
IFS="$oldifs"
if [ -n "$PORTS" ]; then
urisinc=""
for uri in $uris
do
port=$(echo $uri | cut -d: -f2)
[ -n "${port}" ]
#check if port should be checked
for portex in $ports
do
if [ $portex = $port ]; then
if [ -z "$uriinc" ]; then
urisinc="$urisinc $uri"
else
urisinc="$uri"
fi
continue 2
fi
done
echo "Not checking $uri because port is not included."
done
uris=$urisinc
fi
if [ $(echo $uris | wc -w) -lt 1 ]; then
echo "The linked container(s) export no ports and you didn't specify any manual targets." >&2
usage
exit 1
fi
else
uris=$(echo "$TARGETS"|tr , ' '|tr -s '[:space:]' '\n'|uniq)
fi
# wait for each target
if [ -z "$uris" ]; then
echo "No wait targets found." >&2
usage
exit 1
fi
for uri in $uris
do
host=$(echo $uri | cut -d: -f1)
port=$(echo $uri | cut -d: -f2)
[ -n "${host}" ]
[ -n "${port}" ]
echo -n "Waiting for ${uri} ."
timestamp="$(date +%s)"
totalelapseds=0
totalalloweds=$timeout
while [ "$totalelapseds" -lt "$totalalloweds" ] && ! nc -z -w 1 $host $port
do
elapseds="$((($(date +%s)-$timestamp)))"
if [ $elapseds -lt 1 ]; then
sleep 1
elapseds=$(($elapseds+1))
fi
totalelapseds=$(($totalelapseds+$elapseds))
echo -n '.'
timestamp="$(date +%s%N)"
done
if [ "$totalelapseds" -lt "$totalalloweds" ]; then
echo " up!"
else
echo " ERROR: unable to connect" >&2
exit 1
fi
done
echo "Everything is up"
exit 0