-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
entrypoint
executable file
·131 lines (120 loc) · 3.09 KB
/
entrypoint
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
#!/bin/bash
set -eo pipefail
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
if ! echo "$timeout" | grep -qE '^[0-9]+$'; then
echo "Invalid timeout number: $timeout" >&2
exit 1
fi
;;
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" | sed -e 's/,/ /g' -e 's/\s+/\n/g')
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=$(env | grep _TCP= | cut -d= -f2 | cut -c7-)
if [[ -n "$PORTS" ]]; then
urisinc=""
for uri in $uris; do
port=$(echo "$uri" | cut -d: -f2)
if [[ -n "${port}" ]]; then
#check if port should be checked
for portex in $ports; do
if [[ "$portex" == "$port" ]]; then
if [[ -z "$urisinc" ]]; then
urisinc="$urisinc $uri"
else
urisinc="$uri"
fi
continue 2
fi
done
else
echo "Not checking $uri because port is not included."
fi
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" | sed -e 's/,/ /g' -e 's/\s+/\n/g' | 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)
if [[ -n "${host}" ]] && [[ -n "${port}" ]]; then
echo -n "Waiting for ${uri} ."
timestamp="$(date +%s)"
totalelapseds=0
totalalloweds=$timeout
while [[ "$totalelapseds" -lt "$totalalloweds" ]] && ! nc -z -w 1 "$host" "$port"; do
current_date="$(date +%s)"
elapseds="$((current_date - 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
else
echo " SKIP: Missing host or port in $uri"
fi
done
echo "Everything is up"
exit 0