Skip to content

Instantly share code, notes, and snippets.

@kgorskowski
Last active November 22, 2016 17:57
Show Gist options
  • Save kgorskowski/b8f70af69882edb4e3439611db6b8fc4 to your computer and use it in GitHub Desktop.
Save kgorskowski/b8f70af69882edb4e3439611db6b8fc4 to your computer and use it in GitHub Desktop.
#!/bin/bash
hostname="consul-$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
CONF=/etc/consul/config/000-consul.json
TEMPLATE="/mnt/dashboard/consul-config/config.json"
x=0
rm -f $CONF
rm -f /tmp/instances $$ rm -f /tmp/*.json
restart consul
while [ -z "$(curl -s http://localhost:8500/v1/status/leader)" ] || [ "$(curl -s http://localhost:8500/v1/status/leader)" == \"\" ]; do
echo "no cluster leader"
while [ ! -s /tmp/instances ]; do
aws ec2 describe-instances --region eu-west-1 --filters 'Name=tag:Name,Values=dashing-ecs-instance' 'Name=instance-state-name,Values=running' | jq '.Reservations[].Instances[].PrivateIpAddress' > /tmp/instances
sleep 10
done
while [ "$x" -lt 30 ] && [ ! -e $TEMPLATE ]; do
x=$((x+1))
sleep 1
done
if [ ! -e $TEMPLATE ]
then
echo "$TEMPLATE not found in time limit" >> /var/log/consul.log
exit 1
fi
internalIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
cat /mnt/dashboard/consul-config/config.json | jq ".advertise_addr += \"$internalIP\"" > /tmp/step1.json
jq --slurpfile instances /tmp/instances '.retry_join=$instances' < /tmp/step1.json > $CONF
stop consul
rm -rf /var/consul/*
start consul
sleep 10
done
echo "Consul Cluster is healtyh, leader is $(curl -s http://localhost:8500/v1/status/leader), cleaning up"
rm -f /tmp/instances $$ rm -f /tmp/*.json
start on runlevel [2345]
script
/usr/local/bin/bootstrap-consul
end script
description "Consul agent"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
script
if [ -f "/etc/service/consul" ]; then
. /etc/service/consul
fi
# Make sure to use all our CPUs, because Consul can block a scheduler thread
export GOMAXPROCS=`nproc`
# Get the public IP
exec /usr/local/bin/consul agent \
-config-dir="/etc/consul/config" \
${CONSUL_FLAGS} \
>>/var/log/consul.log 2>&1
end script
#!/bin/bash
echo ECS_CLUSTER=dashing-ecs > /etc/ecs/ecs.config
sudo yum install -y nfs-utils
mkdir -p /mnt/dashboard
sudo mount -t nfs4 -o nfsvers=4.1 $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone).*filesystem*.efs.eu-west-1.amazonaws.com:/ /mnt/dashboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment