|
|
|
@ -5,32 +5,84 @@
|
|
|
|
|
# bind : bind interface address
|
|
|
|
|
# server-ip : server ip address
|
|
|
|
|
|
|
|
|
|
# exit on error
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
# create directory if doesn't exist and set permissions
|
|
|
|
|
mkdir -p /opt/consul/{config,data} && chmod 700 /opt/consul
|
|
|
|
|
mkdir -p /etc/consul.d/ && chown -R consul:consul /etc/consul.d/
|
|
|
|
|
|
|
|
|
|
# write systemd conf
|
|
|
|
|
cat >>/etc/systemd/system/consul.service <<EOF
|
|
|
|
|
[Unit]
|
|
|
|
|
Description="HashiCorp Consul - A service mesh solution"
|
|
|
|
|
Documentation=https://www.consul.io/
|
|
|
|
|
Requires=network-online.target
|
|
|
|
|
After=network-online.target
|
|
|
|
|
ConditionFileNotEmpty=/etc/consul.d/consul.hcl
|
|
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
|
EnvironmentFile=/etc/consul.d/consul.env
|
|
|
|
|
User=consul
|
|
|
|
|
Group=consul
|
|
|
|
|
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
|
|
|
|
|
ExecReload=/bin/kill --signal HUP $MAINPID
|
|
|
|
|
KillMode=process
|
|
|
|
|
KillSignal=SIGTERM
|
|
|
|
|
Restart=on-failure
|
|
|
|
|
LimitNOFILE=65536
|
|
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# reload systemd
|
|
|
|
|
systemctl daemon-reload
|
|
|
|
|
|
|
|
|
|
# parameter setup
|
|
|
|
|
consulopts="-config-dir=/opt/consul/config -data-dir=/opt/consul/data"
|
|
|
|
|
consultype="client"
|
|
|
|
|
bind_ip=""
|
|
|
|
|
server_ip=""
|
|
|
|
|
|
|
|
|
|
# read parameters
|
|
|
|
|
while [[ "$#" -gt 0 ]]
|
|
|
|
|
do
|
|
|
|
|
case $1 in
|
|
|
|
|
-server)
|
|
|
|
|
consulopts+="-server "
|
|
|
|
|
consultype="server"
|
|
|
|
|
;;
|
|
|
|
|
-bind)
|
|
|
|
|
consulopts+="-bind=$2 "
|
|
|
|
|
bind_ip="$2"
|
|
|
|
|
;;
|
|
|
|
|
-server-ip)
|
|
|
|
|
consulopts+="-retry-join=$2 "
|
|
|
|
|
server_ip="$2"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
shift
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
printf "initializing consul $consultype with following options: \n%s\n" "$consulopts"
|
|
|
|
|
# write config file
|
|
|
|
|
|
|
|
|
|
if [[ $consultype -eq "client" ]] then
|
|
|
|
|
|
|
|
|
|
cat >> /etc/consul.d/consul.hcl <<EOF
|
|
|
|
|
datacenter = "dc1"
|
|
|
|
|
data_dir = "/opt/consul"
|
|
|
|
|
client_addr = $bind_ip
|
|
|
|
|
retry_join = ["$server_ip"]
|
|
|
|
|
EOF
|
|
|
|
|
chmod 640 /etc/consul.d/consul.hcl
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
cat >> /etc/consul.d/server.hcl <<EOF
|
|
|
|
|
datacenter = "dc1"
|
|
|
|
|
data_dir = "/opt/consul"
|
|
|
|
|
bind_addr = $bind_ip
|
|
|
|
|
server = true
|
|
|
|
|
bootstrap_expect = 1
|
|
|
|
|
EOF
|
|
|
|
|
chmod 640 /etc/consul.d/server.hcl
|
|
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
consul=$(which consul)
|
|
|
|
|
$consul agent $consulopts
|
|
|
|
|
echo "Done! Please launch with systemctl start consul"
|
|
|
|
|