This repository was archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmesosdns-resolver.sh
231 lines (197 loc) · 5.82 KB
/
mesosdns-resolver.sh
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
# Defaults
_portIndex=0
_separator=","
_server=""
# Alias for echo to stderr
errcho(){ >&2 echo $@; }
# Help
#read _help << HELPEND
# Parse command line arguments
while [[ $# > 0 ]]
do
key="$1"
case $key in
-h|--help)
cat << HELPEND
mesosdns-resolver.sh
--------------------
A bash script to resolve Mesos DNS SRV records to actual host:port endpoints
Options:
--------
-sn <service name> or --serviceName <service name> : (MANDATORY) The Mesos DNS service name (such as web.marathon.mesos).
-s <server ip> or --server <server ip> : Can be use to query a specify DNS server.
-d or --drill : Use drill instead of dig (for Alpine Linux)
-m or --mesosSuffix : The mesos DNS suffix (support for service groups).
-a or --all : If specified, all endpoints of a service will be returned,
(with standard separator "comma").
-pi <port index> or --portIndex <port index> : By default, the first port (index 0) will be returned.
If another port index shall be used, specify the index.
-se <separator> or --separator <separator> : The separator to be used then concatenating multiple endpoint results
(only usable together with the --all parameter).
HELPEND
exit 0
;;
-sn|--serviceName)
_serviceName="$2"
shift # past argument
;;
-s|--server)
_server="@$2"
shift # past argument
;;
-pi|--portIndex)
_portIndex="$2"
shift # past argument
;;
-se|--separator)
_separator="$2"
shift # past argument
;;
-a|--all)
_all=YES
;;
-d|--drill)
_drill=YES
;;
-m|--mesosSuffix)
_suffix="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
# Evaluation for the service name
if [[ ! -z "${_serviceName}" && ! -z "${_suffix}" ]]; then
IFS="."
nameArray=( $_serviceName )
suffixArray=( $_suffix )
queryServiceName="_"
nameSections=${#nameArray[@]}
suffixSections=${#suffixArray[@]}
significantSections=$(( nameSections - suffixSections - 1 ))
for index in "${!nameArray[@]}"
do
if [ "${index}" -eq "${significantSections}" ]; then
queryServiceName="${queryServiceName}${nameArray[index]}._tcp"
elif [ "${index}" -lt "${significantSections}" ]; then
queryServiceName="${queryServiceName}${nameArray[index]}."
else
queryServiceName="${queryServiceName}.${nameArray[index]}"
fi
done
elif [[ ! -z "${_serviceName}" ]]; then
IFS="."
nameArray=( $_serviceName )
queryServiceName="_"
for index in "${!nameArray[@]}"
do
if [ "${index}" -eq "0" ]; then
queryServiceName="${queryServiceName}${nameArray[0]}._tcp"
else
queryServiceName="${queryServiceName}.${nameArray[index]}"
fi
done
else
echo "Please supply a service name"
exit 1
fi
# Set IFS to newline
IFS=$'\n'
if [[ -z "${_drill}" ]]; then
# Use dig to get the answer section of the service name
_digResult=`dig +nocmd +tcp ${queryServiceName} SRV +noall +answer ${_server}`
else
_digResult=`drill -t ${queryServiceName} SRV ${_server} | grep "^_" | grep SRV`
fi
# If result from above is empty, the service name cannot be resolved. Exit script.
if [[ -z "${_digResult}" ]]; then
errcho "Service not found"
exit 1
fi
# If dig gave us a result, service name exists. Continue.
dnsHosts=($_digResult)
# Set IFS to space
IFS=" "
# Iterate dnsHosts and extract node info as new array
for index in "${!dnsHosts[@]}"
do
# Filter tabs characters (Mesos DNS bug?)
lineArray=($(echo "${dnsHosts[index]}" | tr "\t" " " | tr -s " "))
nodes+=("${lineArray[7]}|${lineArray[6]}")
done
# Sort nodes
IFS=$'\n' read -d '' -r -a sortedNodes < <(printf '%s\n' "${nodes[@]}" | sort)
# Set to newline
IFS=$'\n'
# Read response as array
if [[ -z "${_drill}" ]]; then
dnsIps=($(dig +nocmd +tcp ${queryServiceName} SRV +noall +additional ${_server}))
else
dnsIps=($(drill -t ${queryServiceName} SRV ${_server} | grep "^[a-zA-Z0-9_]" | grep -v SRV))
fi
# Set to space
IFS=' '
#associative array
declare -A serviceNodes
# Iterate dnsIps and extract node info as new array
for index in "${!dnsIps[@]}"
do
# Filter tabs characters (Mesos DNS bug?)
iplineArray=($(echo "${dnsIps[index]}" | tr "\t" " " | tr -s " "))
# Assign endpoint to serviceNode
serviceNodes["${iplineArray[0]}"]="${iplineArray[4]}"
done
# Keep the state
lastPortIndex=0
lastNodeName=""
# Store the endpoints in an associative array (hostname|port as key)
# and the IP address as value
declare -A endpoints
# Loop through nodes array and assign port indices and IP addresses
for index in "${!sortedNodes[@]}"
do
_hostname=$(echo "${sortedNodes[index]}" | cut -d "|" -f 1)
_port=$(echo "${sortedNodes[index]}" | cut -d "|" -f 2)
_ip=${serviceNodes[$_hostname]}
if [ "${_hostname}" = "${lastNodeName}" ]; then
lastPortIndex=$[lastPortIndex + 1]
else
lastPortIndex=0
lastNodeName=${_hostname}
fi
endpoints["${_hostname}"|"${lastPortIndex}"]="${_ip}:${_port}"
done
# Final output
_output=""
_counter=0
# Iterate over endpoints
for key in "${!endpoints[@]}"
do
# Determine values
_hostname=$(echo "${key}" | cut -d "|" -f 1)
_localPortIndex=$(echo "${key}" | cut -d "|" -f 2)
# Check for portIndex match
if [ "${_localPortIndex}" -eq "${_portIndex}" ]; then
# Increase counter
_counter=$[_counter + 1]
# Check if --all parameter was set
if [[ "${_all}" = "YES" ]]; then
if [[ "${_counter}" -eq "1" ]]; then
_output="${endpoints[$key]}"
else
_output="${_output}${_separator}${endpoints[$key]}"
fi
else
# Return first entry and exit
echo "${endpoints[$key]}"
exit 0
fi
fi
done
# Output for --all
echo "${_output}"
exit 0