instead, use systemd and a conf file

main
Ayush Mukherjee 3 years ago
parent ce4eabb396
commit 20a930b985

@ -5,32 +5,84 @@
# bind : bind interface address # bind : bind interface address
# server-ip : server ip address # server-ip : server ip address
# exit on error
set -e
# create directory if doesn't exist and set permissions # 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 # parameter setup
consulopts="-config-dir=/opt/consul/config -data-dir=/opt/consul/data"
consultype="client" consultype="client"
bind_ip=""
server_ip=""
# read parameters # read parameters
while [[ "$#" -gt 0 ]] while [[ "$#" -gt 0 ]]
do do
case $1 in case $1 in
-server) -server)
consulopts+="-server "
consultype="server" consultype="server"
;; ;;
-bind) -bind)
consulopts+="-bind=$2 " bind_ip="$2"
;; ;;
-server-ip) -server-ip)
consulopts+="-retry-join=$2 " server_ip="$2"
;; ;;
esac esac
shift shift
done 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) echo "Done! Please launch with systemctl start consul"
$consul agent $consulopts

Loading…
Cancel
Save