Created
November 30, 2017 22:32
-
-
Save dblandin/7bb71ab6502748a067e74f03f5d8d388 to your computer and use it in GitHub Desktop.
buildkite docker build EC2 instance setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
exec > >(tee /var/log/prepare-instance.log|logger -t prepare-instance -s 2>/dev/console) 2>&1 | |
set -e | |
AZ=$(curl --show-error --silent http://169.254.169.254/latest/meta-data/placement/availability-zone) | |
ID=$(curl --show-error --silent http://169.254.169.254/latest/meta-data/instance-id) | |
REGION=$(printf "%s" "$AZ" | sed 's/.$//') | |
set_instance_health() { | |
echo "Prepare script failed. Marking instance unhealthy." | |
aws autoscaling set-instance-health \ | |
--instance-id "$ID" \ | |
--health-status "$1" \ | |
--no-should-respect-grace-period \ | |
--region "$REGION" | |
} | |
trap '[ $? -ne 0 ] && set_instance_health Unhealthy' EXIT | |
EBS_VOLUME_NAME=buildkite-docker-ebs | |
aws ec2 wait \ | |
--region "$REGION" \ | |
volume-available \ | |
--filters "Name=tag:Name,Values=$EBS_VOLUME_NAME" \ | |
Name=status,Values=available \ | |
"Name=availability-zone,Values=$AZ" | |
fetch_ebs_volume_id() { | |
aws ec2 describe-volumes \ | |
--filters "Name=tag:Name,Values=$EBS_VOLUME_NAME" \ | |
Name=status,Values=available \ | |
"Name=availability-zone,Values=$AZ" \ | |
--region "$REGION" | jq -r '.Volumes[0].VolumeId' | |
} | |
EBS_VOLUME_ID=$(fetch_ebs_volume_id) | |
aws ec2 attach-volume \ | |
--device /dev/sdf \ | |
--instance-id "$ID" \ | |
--region "$REGION" \ | |
--volume-id "$EBS_VOLUME_ID" | |
echo "Update packages" | |
sudo yum update --assumeyes | |
aws ec2 wait \ | |
--region "$REGION" \ | |
volume-in-use \ | |
--filters "Name=attachment.instance-id,Values=$ID" \ | |
Name=attachment.status,Values=attached \ | |
Name=status,Values=in-use \ | |
--volume-ids "$EBS_VOLUME_ID" | |
sudo service docker stop | |
sudo mount /dev/xvdf /var/lib/docker | |
cat <<EOM > /etc/sysconfig/docker | |
# We use the overlay2 storage driver for performance | |
OPTIONS="-s overlay2 --debug --registry-mirror=https://ci-registry.codeclimate.net" | |
DAEMON_MAXFILES=4096 | |
DAEMON_PIDFILE_TIMEOUT=30 | |
# Force native resolver to work around https://github.com/docker/docker/issues/22673 | |
export GODEBUG=netdns=cgo | |
EOM | |
sudo initctl reload-configuration | |
sudo service docker start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment