You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.7 KiB
Bash

#!/bin/bash
# Hashicorp Consul bootstrap script
# Params:
# server : if present, bootstraps a server
# 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 /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
consultype="client"
bind_ip=""
server_ip=""
# read parameters
while [[ "$#" -gt 0 ]]
do
case $1 in
-server)
consultype="server"
;;
-bind)
bind_ip="$2"
;;
-server-ip)
server_ip="$2"
;;
esac
shift
done
# 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
echo "Done! Please launch with systemctl start consul"