-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul_curl
49 lines (44 loc) · 1.53 KB
/
consul_curl
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
consul_curl() {
local HELP='consul_curl -c CONSUL_ENDPOINT -n SERVICE_NAME -p PATH'
HELP+=' [-d CONSUL_DC] [-- additional curl options]'
[ $# -lt 6 ] && echo $HELP 1>&2 && return 1
local OPTIND OPT consul_endpoint consul_servicename service_path \
consul_datacenter consul_dnsrr consul_nodename service_port consul_zone
while getopts ":hc:n:p:d:" OPT; do
case "${OPT}" in
c)
consul_endpoint="${OPTARG}"
;;
n)
consul_servicename="${OPTARG}"
;;
p)
service_path="${OPTARG}"
;;
d)
consul_datacenter="${OPTARG}."
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
echo $HELP 1>&2
return 1
;;
:)
echo "Option -$OPTARG requires an argument." 1>&2
echo $HELP 1>&2
return 1
;;
h|*)
echo $HELP 1>&2
return 1
;;
esac
done
shift $((OPTIND-1))
consul_zone=".service.${consul_datacenter}consul"
consul_dnsrr=$(dig @${consul_endpoint} +noall +short SRV ${consul_servicename}${consul_zone} | head -1)
consul_nodename=$(echo ${consul_dnsrr} | awk '{print $4}')
consul_nodeip=$(dig @${consul_endpoint} +noall +short ${consul_nodename})
service_port=$(echo ${consul_dnsrr} | awk '{print $3}')
curl -s -H "Host: ${consul_servicename}${consul_zone}" "http://${consul_nodeip}:${service_port}${service_path}" "$@"
}