Last active
February 7, 2018 09:12
-
-
Save nick4fake/65a43764530648b33b30a7dfdf36b9af to your computer and use it in GitHub Desktop.
aws provision
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 | |
# AMI init script | |
set -e | |
export DEBIAN_FRONTEND=noninteractive | |
apt-get update | |
apt-get -y install python-pip | |
pip install --upgrade --user pip awscli | |
ln -sv /root/.local/bin/aws /usr/sbin/aws | |
echo 'Europe/Kiev' > /etc/timezone | |
dpkg-reconfigure --frontend noninteractive tzdata | |
# Adding docker repo | |
# https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/ | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - | |
add-apt-repository \ | |
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \ | |
$(lsb_release -cs) \ | |
stable" | |
apt-get update | |
apt-get -y upgrade | |
# Installing docker | |
apt-get -y install docker-ce | |
adduser ubuntu docker | |
curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose | |
sudo chmod +x /usr/local/bin/docker-compose | |
# ----------------------------------- | |
cat << 'EOF1' > /root/run.sh | |
#!/usr/bin/env bash | |
set -e | |
EC_VOLUME=$1 | |
EC_IP_ALLOC=$2 | |
EC_KEY=$3 | |
EC_SECRET=$4 | |
# https://stackoverflow.com/questions/625644/find-out-the-instance-id-from-within-an-ec2-machine | |
die() { status=$1; shift; echo "FATAL: $*"; exit $status; } | |
EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`" | |
test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id' | |
EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`" | |
test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone' | |
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`" | |
mkdir ~/.aws | |
cat << EOF2 >> ~/.aws/config | |
[default] | |
region = ${EC2_REGION} | |
EOF2 | |
cat << EOF2 >> ~/.aws/credentials | |
[default] | |
aws_access_key_id = ${EC_KEY} | |
aws_secret_access_key = ${EC_SECRET} | |
EOF2 | |
# assoc ip | |
aws ec2 associate-address --allocation-id ${EC_IP_ALLOC} --instance-id ${EC2_INSTANCE_ID} --allow-reassociation | |
# attaching volume | |
# https://stackoverflow.com/questions/8423891/attaching-ebs-during-startup-of-amazon-ec2-spot-instance | |
aws ec2 attach-volume --volume-id ${EC_VOLUME} --instance ${EC2_INSTANCE_ID} --device /dev/xvdm | |
sleep 5 | |
# mounting volume | |
mkdir -p /mnt/data | |
echo '/dev/xvdm1 /mnt/data ext4 defaults,nofail 0 2' >> /etc/fstab | |
mount -a | |
aws ecr get-login --no-include-email --region ${EC2_REGION} | bash | |
cp -Rv /root/.docker /home/ubuntu/.docker | |
chown -Rv ubuntu:ubuntu /home/ubuntu/.docker | |
service docker stop | |
if [ ! -d /mnt/data/docker ]; then | |
mv /var/lib/docker /mnt/data/docker | |
else | |
rm -Rf /var/lib/docker | |
fi | |
ln -sv /mnt/data/docker /var/lib/docker | |
if [ -f /mnt/data/provision.sh ]; then | |
. /mnt/data/provision.sh | |
fi | |
service docker start || true | |
touch /etc/cluster_created2 | |
EOF1 | |
chmod +x /root/run.sh | |
# ----------------------------------- | |
touch /etc/cluster_created1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment