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.

105 lines
2.0 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
3 years ago
if [ "$#" -eq 0 ]; then
3 years ago
echo "No options specified, exiting."
exit 1
3 years ago
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
3 years ago
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
3 years ago
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" ]
3 years ago
then
3 years ago
cat >/etc/consul.d/consul.hcl <<EOF
3 years ago
datacenter = "dc1"
data_dir = "/opt/consul"
3 years ago
bind_addr = "$bind_ip"
3 years ago
retry_join = ["$server_ip"]
EOF
chmod 640 /etc/consul.d/consul.hcl
else
3 years ago
cat >/etc/consul.d/server.hcl <<EOF
3 years ago
datacenter = "dc1"
data_dir = "/opt/consul"
3 years ago
bind_addr = "$bind_ip"
3 years ago
server = true
bootstrap_expect = 1
3 years ago
ui_config {
enabled = true
}
EOF
chmod 640 /etc/consul.d/server.hcl
fi
echo "Done! Please launch with systemctl start consul"