forked from n3llyb0y/docker-wait
-
Notifications
You must be signed in to change notification settings - Fork 11
/
wait
executable file
·49 lines (42 loc) · 1.06 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
#!/bin/bash
set -e
timeout=${TIMEOUT:-30}
# 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=$(env | grep _TCP= | cut -d= -f2 | cut -c7- )
if [ $(echo $uris | wc -w) -lt 1 ]; then
echo "The linked container(s) export no ports and you didn't specify any manual targets. Exiting" >&2
exit 1
fi
else
uris=$(echo $TARGETS | sed -e 's/,/ /g' -e 's/\s+/\n/g' | uniq)
fi
# wait for each target
if [ -z "$uris" ]; then
echo "No wait targets found. Exiting" >&2
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} ."
seconds=0
while [ "$seconds" -lt "$timeout" ] && ! nc -z -w1 $host $port
do
echo -n .
seconds=$((seconds+1))
sleep 1
done
if [ "$seconds" -lt "$timeout" ]; then
echo " up!"
else
echo " ERROR: unable to connect" >&2
exit 1
fi
done
echo "Everything is up"
exit 0