diff --git a/Dockerfile b/Dockerfile index 8973a4e..908e1fc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,8 @@ FROM alpine:3.6 +# This is the release of Consul to pull in. +ENV CONSUL_VERSION=1.0.6 +# This is the location of the releases. +ENV HASHICORP_RELEASES=https://releases.hashicorp.com RUN apk -v --update add \ bash \ python \ @@ -8,11 +12,30 @@ RUN apk -v --update add \ mailcap \ jq \ curl \ + ca-certificates \ + gnupg libcap \ + openssl \ + su-exec \ dumb-init \ && \ pip install --upgrade awscli==1.14.5 s3cmd==2.0.1 python-magic && \ apk -v --purge del py-pip && \ rm /var/cache/apk/* +# Set up certificates, and Consul. +RUN gpg --keyserver pgp.mit.edu --recv-keys 91A6E7F85D05C65630BEF18951852D87348FFC4C && \ + mkdir -p /tmp/build && \ + cd /tmp/build && \ + wget ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip && \ + wget ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_SHA256SUMS && \ + wget ${HASHICORP_RELEASES}/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_SHA256SUMS.sig && \ + gpg --batch --verify consul_${CONSUL_VERSION}_SHA256SUMS.sig consul_${CONSUL_VERSION}_SHA256SUMS && \ + grep consul_${CONSUL_VERSION}_linux_amd64.zip consul_${CONSUL_VERSION}_SHA256SUMS | sha256sum -c && \ + unzip -d /bin consul_${CONSUL_VERSION}_linux_amd64.zip && \ + cd /tmp && \ + rm -rf /tmp/build && \ + apk del gnupg openssl && \ + rm -rf /root/.gnupg + COPY scripts/*.sh /usr/local/bin/ COPY check_definitions/*.sh /usr/local/bin/check_definitions/ COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh diff --git a/check_definitions/ecs-cluster.sh b/check_definitions/ecs-cluster.sh index cd4c796..335570e 100755 --- a/check_definitions/ecs-cluster.sh +++ b/check_definitions/ecs-cluster.sh @@ -19,7 +19,7 @@ _SERVICE=$(cat <>> Currently NOT USED <<<< +# We probably want to replace this with something that eats json and do this in a non-horrible way +# It would safe clock time to get status from the JSON input of the consul watch. + +REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/ | sed 's/.$//') +ARN=$(curl -s http://localhost:51678/v1/metadata | jq -r .ContainerInstanceArn) +ECS_CLUSTER=$(curl -s http://localhost:51678/v1/metadata | jq -r .Cluster) +ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) +STATUS=$(aws --region $REGION autoscaling describe-auto-scaling-instances --instance-ids $ID | jq -r '.AutoScalingInstances[0].LifecycleState') + +if [ $STATUS = "Terminating:Wait" ]; then + aws --region $REGION ecs update-container-instances-state --cluster $ECS_CLUSTER --container-instances $ARN --status DRAINING + exit 0 +else + exit 0 +fi diff --git a/scripts/instance-status.sh b/scripts/instance-status.sh new file mode 100755 index 0000000..0bcf80c --- /dev/null +++ b/scripts/instance-status.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -e + +REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/ | sed 's/.$//') +ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) +ARN=$(curl -s http://localhost:51678/v1/metadata | jq -r .ContainerInstanceArn) +ECS_CLUSTER=$(curl -s http://localhost:51678/v1/metadata | jq -r .Cluster) +#ASG=$(aws ec2 describe-instances --region $REGION --instance-ids $ID | jq -r '.Reservations[0].Instances[0].Tags[1].Value') +CONTAINER_INSTANCE_STATUS=$(aws --region $REGION ecs describe-container-instances --cluster $ECS_CLUSTER --container-instances "$ARN" | jq -r .containerInstances[0].status) +STATUS=$(aws --region $REGION autoscaling describe-auto-scaling-instances --instance-ids $ID | jq -r '.AutoScalingInstances[0].LifecycleState') + +if [ $STATUS = "InService" ]; then + echo Status is Lifecycle State : $STATUS + echo ECS Instance Status : $CONTAINER_INSTANCE_STATUS +elif [ $STATUS = "Terminating:Wait" ]; then + aws --region $REGION ecs update-container-instances-state --cluster $ECS_CLUSTER --container-instances $ARN --status DRAINING + echo Status is Lifecycle State : $STATUS + echo ECS Instance Status : $CONTAINER_INSTANCE_STATUS + exit 255 +else + echo Status is Lifecycle State : $STATUS + echo ECS Instance Status : $CONTAINER_INSTANCE_STATUS + exit 1 +fi