forked from rboyer/devconsul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul.sh
executable file
·59 lines (46 loc) · 1.31 KB
/
consul.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
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"
if ! command -v consul >/dev/null 2>&1 ; then
echo "ERROR: no 'consul' binary on PATH. Please run 'make dev' from your consul checkout" >&2
exit 1
fi
readonly master_token_file=./cache/master-token.val
master_token() {
if [[ ! -f "${master_token_file}" ]]; then
echo "no master token defined in ${master_token_file}" >&2
exit 1
fi
local token
read -r token < "${master_token_file}"
# trim any whitespace; this overdoes it in the middle, but tokens don't have
# whitespace in the middle so :shrug:
echo "${token//[[:space:]]}"
}
if [[ $# -lt 1 ]]; then
echo "Missing required cluster arg" >&2
exit 1
fi
cluster=$1
shift
node="${cluster}-server1"
error_message="unknown cluster: ${cluster}"
if [[ "$cluster" == *-* ]]; then
# actually this is a node query
node="${cluster}"
error_message="unknown node: ${node}"
fi
ip="$(devconsul config | jq -r ".localAddrs[\"${node}\"]")"
if [[ "$ip" = "null" ]]; then
echo "${error_message}" >&2
exit 1
fi
export CONSUL_HTTP_ADDR="http://${ip}:8500"
readonly acls="$(devconsul config | jq -r ".acls")"
if [[ -n "$acls" ]]; then
export CONSUL_HTTP_TOKEN="$(master_token)"
if [[ -z "$CONSUL_HTTP_TOKEN" ]]; then
exit 1
fi
fi
exec consul "$@"