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.

107 lines
2.1 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
if [ "$#" -eq 0 ]; then
echo "No options specified, exiting."
exit 1
elif [ "$#" -eq 1 ] && [ "$1" == "-h" ]; then
echo "HashiCorp Consul Bootstrap Script. Provide:"
echo "-server for server install,"
echo "-bind for the bind interface address,"
echo "-server-ip for the server ip address."
exit 1
fi
# create directory if doesn't exist and set permissions
mkdir -p /etc/consul.d/ && chown -R consul:consul /etc/consul.d/
# create consul data dir and set permissions
mkdir -p /opt/consul && chown -R consul:consul /opt/consul
# 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
[Service]
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=null
server_ip=null
# 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 == "client" ]
then
cat >/etc/consul.d/consul.hcl <<EOF
datacenter = "dc1"
data_dir = "/opt/consul"
bind_addr = "$bind_ip"
retry_join = ["$server_ip"]
EOF
chmod 640 /etc/consul.d/consul.hcl
chown consul:consul /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
ui_config {
enabled = true
}
EOF
chmod 640 /etc/consul.d/server.hcl
chown consul:consul /etc/consul.d/server.hcl
fi
echo "Done! Please launch with systemctl start consul"