Skip to content

Instantly share code, notes, and snippets.

@slawekkolodziej
Created May 18, 2016 11:04
Show Gist options
  • Save slawekkolodziej/3dbcd3a27b5949ab48e022ce68e3523d to your computer and use it in GitHub Desktop.
Save slawekkolodziej/3dbcd3a27b5949ab48e022ce68e3523d to your computer and use it in GitHub Desktop.
AWS user data script for setting up a consul cluster using all instances in a given ECS
#!/bin/bash -xe
S3_BUCKET=foo
EXPECTED_SERVERS=2
yum install -y aws-cli
aws s3 cp s3://$S3_BUCKET/ecs.config /etc/ecs/ecs.config
aws s3 cp s3://$S3_BUCKET/docker.config /etc/sysconfig/docker
run_consul_agent() {
peers=""
if [ "$1" ]
then
peers="-retry-join $1"
fi
docker run -d \
--name=consul-agent \
-e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true}' \
-p $EC2_IP:8300:8300 \
-p $EC2_IP:8301:8301 \
-p $EC2_IP:8301:8301/udp \
-p $EC2_IP:8302:8302 \
-p $EC2_IP:8302:8302/udp \
-p $EC2_IP:8400:8400 \
-p $EC2_IP:8500:8500 \
-p $DOCKER_BRIDGE:8500:8500 \
-p $DOCKER_BRIDGE:53:8600 \
-p $DOCKER_BRIDGE:53:8600/udp \
consul:v0.6.4 consul agent \
-data-dir=/consul/data \
-node=$HOSTNAME \
-client 0.0.0.0 \
-advertise $EC2_IP $peers
}
run_consul_server() {
peers=""
if [ "$1" ]
then
peers="-retry-join $1"
fi
docker run -d \
--name=consul-server \
-e 'CONSUL_LOCAL_CONFIG={"leave_on_terminate": true}' \
-p $EC2_IP:8300:8300 \
-p $EC2_IP:8301:8301 \
-p $EC2_IP:8301:8301/udp \
-p $EC2_IP:8302:8302 \
-p $EC2_IP:8302:8302/udp \
-p $EC2_IP:8400:8400 \
-p $EC2_IP:8500:8500 \
-p $DOCKER_BRIDGE:8500:8500 \
-p $DOCKER_BRIDGE:53:8600 \
-p $DOCKER_BRIDGE:53:8600/udp \
consul:v0.6.4 consul agent \
-server \
-bootstrap-expect=$EXPECTED_SERVERS \
-data-dir=/consul/data \
-node=$HOSTNAME \
-client 0.0.0.0 \
-advertise $EC2_IP $peers
}
# Reconfigure Docker
DOCKER_BRIDGE=$(ifconfig docker0 | sed -n -e 's/^ \+inet addr:\([0-9.]\+\).\+$/\1/p')
sed -i "s/PLACEHOLDER_DOCKER_BRIDGE/${DOCKER_BRIDGE}/g" "/etc/sysconfig/docker"
# Reload Docker with new config
service docker reload
# Import cluster name as ECS_CLUSTER
source /etc/ecs/ecs.config
# Get other instance data from AWS meta-data
EC2_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
EC2_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
EC2_REGION=$(echo $EC2_ZONE | sed -n -e 's/\([a-z]\+-[a-z]\+-[0-9]\+\).\+/\1/p')
# Fetch cluster instance ARNs
INSTANCE_ARNS=$(aws ecs list-container-instances \
--region $EC2_REGION \
--cluster $ECS_CLUSTER \
--query 'containerInstanceArns' \
--output text | sed 's/\t/ /g')
echo "INSTANCE_ARNS: $INSTANCE_ARNS"
# Fetch cluster instance IDs
if [ "$INSTANCE_ARNS" ]
then
INSTANCE_IDS=$(aws ecs describe-container-instances \
--region $EC2_REGION \
--cluster $ECS_CLUSTER \
--container-instances $INSTANCE_ARNS \
--query "containerInstances[*].ec2InstanceId" \
--output text | sed 's/\t/ /g')
fi
echo "INSTANCE_IDS: $INSTANCE_IDS"
# Fetch cluster instance IPs
if [ "$INSTANCE_IDS" ]
then
CLUSTER_IPS=$(aws ec2 describe-instances \
--region $EC2_REGION \
--instance-ids $INSTANCE_IDS \
--query "Reservations[*].Instances[*].PrivateIpAddress" \
--output text | sed 's/\t/ /g')
fi
echo "CLUSTER_IPS: $CLUSTER_IPS"
# Run consul
if [ "$CLUSTER_IPS" ]
then
CONSUL_CONTAINER_ID=$(run_consul_agent "$CLUSTER_IPS")
echo "CONSUL_CONTAINER_ID: $CONSUL_CONTAINER_ID"
sleep 2
CONSUL_INFO=$(docker exec $CONSUL_CONTAINER_ID consul info)
echo "CONSUL_INFO: $CONSUL_INFO"
IS_SERVER=$(echo $CONSUL_INFO | sed -n -e 's/.\+server = \(true\|false\).\+/\1/p')
echo "IS_SERVER: $IS_SERVER"
KNOWN_SERVERS=$(echo $CONSUL_INFO | sed -n -e 's/.\+known_servers = \([0-9]\+\).\+/\1/p')
echo "KNOWN_SERVERS: $KNOWN_SERVERS"
if [ "$KNOWN_SERVERS" -lt "$EXPECTED_SERVERS" ]
then
docker stop $CONSUL_CONTAINER_ID
docker rm $CONSUL_CONTAINER_ID
run_consul_server "$CLUSTER_IPS"
fi
else
echo "NO KNOWN SERVERS"
run_consul_server
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment