Skip to content

Instantly share code, notes, and snippets.

@kgorskowski
Last active November 22, 2016 17:57
Show Gist options
  • Select an option

  • Save kgorskowski/b8f70af69882edb4e3439611db6b8fc4 to your computer and use it in GitHub Desktop.

Select an option

Save kgorskowski/b8f70af69882edb4e3439611db6b8fc4 to your computer and use it in GitHub Desktop.

Revisions

  1. Karsten Gorskowski revised this gist Aug 17, 2016. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions bootstrap-consul
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    #!/bin/bash
    internalIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    instanceID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    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"
    @@ -23,9 +21,10 @@ while [ -z "$(curl -s http://localhost:8500/v1/status/leader)" ] || [ "$(curl -s
    done
    if [ ! -e $TEMPLATE ]
    then
    echo "$TEMPLATE not found in time limit"
    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
  2. Karsten Gorskowski revised this gist Aug 17, 2016. 3 changed files with 21 additions and 3 deletions.
    14 changes: 14 additions & 0 deletions consul-bootstrap.sh → bootstrap-consul
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,12 @@ internalIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    instanceID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    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
    @@ -12,6 +17,15 @@ while [ -z "$(curl -s http://localhost:8500/v1/status/leader)" ] || [ "$(curl -s
    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"
    exit 1
    fi
    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
    5 changes: 5 additions & 0 deletions bootstrap-consul.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    start on runlevel [2345]

    script
    /usr/local/bin/bootstrap-consul
    end script
    5 changes: 2 additions & 3 deletions user-data
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #!/bin/bash
    echo ECS_CLUSTER=dashing-ecs >> /etc/ecs/ecs.config
    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
    bash /usr/local/bin/consul-bootstrap
    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
  3. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions consul-bootstrap.sh
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,12 @@ 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"
    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
    # cat /etc/consul/config/000-consul.json | jq 'del(.retry_join[])' | jq 'del(.advertise_addr)' > /tmp/cleaned.json
    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
    cat /mnt/dashboard/consul-config/config.json | jq ".advertise_addr += \"$internalIP\"" > /tmp/step1.json
    if [ -s /tmp/instances ]; then
    jq --slurpfile instances /tmp/instances '.retry_join=$instances' < /tmp/step1.json > $CONF
    fi
    jq --slurpfile instances /tmp/instances '.retry_join=$instances' < /tmp/step1.json > $CONF
    stop consul
    rm -rf /var/consul/*
    start consul
  4. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 17 additions and 11 deletions.
    28 changes: 17 additions & 11 deletions consul-bootstrap.sh
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,23 @@
    #!/bin/bash
    stop consul

    internalIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    instanceID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    hostname="consul-$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
    CONF=/etc/consul/config/000-consul.json
    rm -f $CONF
    restart consul

    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

    cat /etc/consul/config/000-consul.json | jq 'del(.retry_join[])' | jq 'del(.advertise_addr)' > /tmp/cleaned.json
    cat /tmp/cleaned.json | jq ".advertise_addr += \"$internalIP\"" > /tmp/step1.json
    jq --slurpfile instances /tmp/instances '.retry_join=$instances' < /tmp/step1.json > $CONF
    rm -f /tmp/instances $$ rm -f /tmp/*.json
    rm -rf /var/consul/

    start consul
    while [ -z "$(curl -s http://localhost:8500/v1/status/leader)" ] || [ "$(curl -s http://localhost:8500/v1/status/leader)" == \"\" ]; do
    echo "no cluster leader"
    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
    # cat /etc/consul/config/000-consul.json | jq 'del(.retry_join[])' | jq 'del(.advertise_addr)' > /tmp/cleaned.json
    cat /mnt/dashboard/consul-config/config.json | jq ".advertise_addr += \"$internalIP\"" > /tmp/step1.json
    if [ -s /tmp/instances ]; then
    jq --slurpfile instances /tmp/instances '.retry_join=$instances' < /tmp/step1.json > $CONF
    fi
    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
  5. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion user-data
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,5 @@
    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).fs-beb05b77.efs.eu-west-1.amazonaws.com:/ /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
    bash /usr/local/bin/consul-bootstrap
  6. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 9 additions and 16 deletions.
    25 changes: 9 additions & 16 deletions consul-bootstrap.sh
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,17 @@
    #!/bin/bash
    stop consul

    internalIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    instanceID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    hostname="consul-$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
    CONF=/etc/consul/config/000-consul.json

    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

    aws ec2 describe-instances --region eu-west-1 --filters 'Name=instance-state-name,Values=running' | jq -r '.Reservations[].Instances[].PrivateIpAddress' > /tmp/instances

    while read line;
    do
    if [ "$line" != "$internalIP" ]; then

    echo "Adding address $line"
    cat /etc/consul/config/000-consul.json | jq 'del(.retry_join[])' | jq 'del(.advertise_addr)' > /tmp/cleaned.json
    cat /tmp/cleaned.json | jq ".retry_join += [\"$line\"]" | jq ".advertise_addr += \"$internalIP\"" > /tmp/${line}-consul.json

    if [ -s /tmp/${line}-consul.json ]; then
    cp /tmp/${line}-consul.json /etc/consul/config/000-consul.json
    fi
    fi
    done < /tmp/instances
    cat /etc/consul/config/000-consul.json | jq 'del(.retry_join[])' | jq 'del(.advertise_addr)' > /tmp/cleaned.json
    cat /tmp/cleaned.json | jq ".advertise_addr += \"$internalIP\"" > /tmp/step1.json
    jq --slurpfile instances /tmp/instances '.retry_join=$instances' < /tmp/step1.json > $CONF
    rm -f /tmp/instances $$ rm -f /tmp/*.json
    rm -rf /var/consul/*
    rm -rf /var/consul/

    start consul
  7. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions user-data
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    #!/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).fs-beb05b77.efs.eu-west-1.amazonaws.com:/ /mnt/dashboard
    bash /usr/local/bin/consul-bootstrap
  8. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 17 additions and 6 deletions.
    23 changes: 17 additions & 6 deletions consul.conf
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,22 @@
    description "Consul server process"
    description "Consul agent"

    start on (local-filesystems and net-device-up IFACE=eth0)
    stop on runlevel [!12345]
    start on runlevel [2345]
    stop on runlevel [!2345]

    respawn

    setuid consul
    setgid consul
    script
    if [ -f "/etc/service/consul" ]; then
    . /etc/service/consul
    fi

    exec consul agent -config-dir /etc/consul/config/
    # 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
  9. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions consul.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    description "Consul server process"

    start on (local-filesystems and net-device-up IFACE=eth0)
    stop on runlevel [!12345]

    respawn

    setuid consul
    setgid consul

    exec consul agent -config-dir /etc/consul/config/
  10. Karsten Gorskowski revised this gist Aug 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion consul-bootstrap.sh
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,4 @@ do
    fi
    done < /tmp/instances
    rm -f /tmp/instances $$ rm -f /tmp/*.json
    rm -rf /var/consul/
    rm -rf /var/consul/*
  11. Karsten Gorskowski created this gist Aug 16, 2016.
    24 changes: 24 additions & 0 deletions consul-bootstrap.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/bin/bash

    internalIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    instanceID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
    hostname="consul-$(curl -s http://169.254.169.254/latest/meta-data/instance-id)"


    aws ec2 describe-instances --region eu-west-1 --filters 'Name=instance-state-name,Values=running' | jq -r '.Reservations[].Instances[].PrivateIpAddress' > /tmp/instances

    while read line;
    do
    if [ "$line" != "$internalIP" ]; then

    echo "Adding address $line"
    cat /etc/consul/config/000-consul.json | jq 'del(.retry_join[])' | jq 'del(.advertise_addr)' > /tmp/cleaned.json
    cat /tmp/cleaned.json | jq ".retry_join += [\"$line\"]" | jq ".advertise_addr += \"$internalIP\"" > /tmp/${line}-consul.json

    if [ -s /tmp/${line}-consul.json ]; then
    cp /tmp/${line}-consul.json /etc/consul/config/000-consul.json
    fi
    fi
    done < /tmp/instances
    rm -f /tmp/instances $$ rm -f /tmp/*.json
    rm -rf /var/consul/