-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsp-consul-client
106 lines (91 loc) · 2.44 KB
/
sp-consul-client
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
# variables
my_ip6=$(ifconfig ens4 | grep "inet addr" | awk '{ print substr($2,6) }')
consul_domain="consul.$HOSTNAME"
#echo " ===> Updating package cache"
#apt-get update
echo " ===> Installing unzip"
apt-get install -y unzip
echo " ===> Creating Consul user"
useradd -m -d /var/lib/consul -r consul
echo " ===> Creating Consul-related directories and files"
mkdir -p /var/lib/consul
chown consul: /var/lib/consul
chmod 750 /var/lib/consul
mkdir -p /etc/consul.d
chown consul: /etc/consul.d
chmod 750 /etc/consul.d
mkdir -p /opt/consul-web
chown consul: /opt/consul-web
chmod 750 /opt/consul-web
touch /var/log/consul.log
chown consul: /var/log/consul.log
chmod 640 /var/log/consul.log
echo " ===> Installing Consul"
if [[ ! -f /usr/local/bin/consul ]]; then
pushd /tmp
wget https://releases.hashicorp.com/consul/1.2.0/consul_1.2.0_linux_amd64.zip
unzip consul_1.2.0_linux_amd64.zip
mv consul /usr/local/bin
popd
fi
for attempt in {1..3}; do
# _nodes=($(dig -t txt ${consul_domain} | grep TXT | awk '{print $5}' | grep -v ^$ | sort | tr -d \"))
_nodes=$HOSTNAME
if [[ -z $_nodes ]]; then
echo "No consul nodes found. Sleeping"
sleep 10
else
break
fi
done
_consul_nodes=""
for _node in "${_nodes[@]}"; do
if [[ -z $_consul_nodes ]]; then
_consul_nodes="\"${_node}\""
else
_consul_nodes="${_consul_nodes}, \"${_node}\""
fi
done
echo " ===> Configuring Consul"
cat >/etc/consul.d/config.json <<EOF
{
"advertise_addr": "${my_ip6}",
"encrypt": "vZOYuDxz4pA9EHb/MdmPyQ==",
"client_addr": "0.0.0.0",
"bind_addr": "${my_ip6}",
"data_dir": "/var/lib/consul",
"datacenter": "DC1",
"rejoin_after_leave": true,
"retry_interval": "30s",
"retry_join": ["10.128.0.12"],
"server": false,
"enable_syslog": false,
"log_level": "INFO"
}
EOF
echo " ===> Installing init script"
cat >/etc/systemd/system/consul.service <<EOF
[Unit]
Description=consul agent
Requires=network-online.target
After=network-online.target
[Service]
EnvironmentFile=-/etc/sysconfig/consul
Environment=GOMAXPROCS=2
Restart=on-failure
ExecStart=/bin/bash -ce "exec /usr/local/bin/consul agent -config-dir=/etc/consul.d >>/var/log/consul.log 2>&1"
ExecReload=/bin/kill -HUP
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
EOF
echo " ===> Starting Consul"
systemctl enable consul.service
systemctl daemon-reload
echo " ===> Starting Consul"
service consul start