Skip to content

Instantly share code, notes, and snippets.

@thpham
Forked from yaronr/bootstrap-consul.sh
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thpham/d5e729fec3a099465770 to your computer and use it in GitHub Desktop.
Save thpham/d5e729fec3a099465770 to your computer and use it in GitHub Desktop.
#!/bin/bash
source /etc/environment
hostname=$(cat /etc/machine-id)
machines=$(etcdctl ls /consul.io/bootstrap/machines)
#If there are 'machines' then the cluster has been initialised.
if [ -z "$machines" ]
then
flags="${flags} -bootstrap"
else
echo "This cluster has already been bootstrapped"
# set -join flags
flags=$(etcdctl ls /consul.io/bootstrap/machines | while read line; do
ip=$(etcdctl get ${line})
echo ${flags} -join ${ip}
done)
fi
echo "Flags are:" $flags
#set 'this' machine in etcd (so that others could peer)
etcdctl set /consul.io/bootstrap/machines/${hostname} ${COREOS_PRIVATE_IPV4}
/usr/bin/docker run --name consul \
-h ${hostname} \
-p 8300:8300 \
-p 8301:8301 \
-p 8301:8301/udp \
-p 8302:8302 \
-p 8302:8302/udp \
-p 8400:8400 \
-p 8500:8500 \
-p 53:53/udp \
-v /opt/consul/data:/data \
progrium/consul:latest -server -advertise ${COREOS_PRIVATE_IPV4} ${flags}
[Unit]
Description=Consul agent bootstrap
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/mkdir -p /opt/consul/data
ExecStartPre=-/usr/bin/wget --retry-connrefused -t 5 -N -P /opt/consul/ https://gist.githubusercontent.com/yaronr/aa5e9d1871f047568c84/raw/bootstrap-consul.sh
ExecStartPre=-/usr/bin/chmod +x /opt/consul/bootstrap-consul.sh
ExecStartPre=-/usr/bin/docker kill consul
ExecStartPre=-/usr/bin/docker rm consul
#Make sure this ip is not there already
ExecStartPre=-etcdctl rm /consul.io/bootstrap/machines/$(cat /etc/machine-id)
ExecStart=/opt/consul/bootstrap-consul.sh
ExecStop=-/usr/bin/docker stop consul
ExecStopPost=-etcdctl rm /consul.io/bootstrap/machines/$(cat /etc/machine-id)
[Install]
WantedBy=multi-user.target
[X-Fleet]
Global=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment