From d5befd809755c93cb5eecdd8d50c1fac13010c70 Mon Sep 17 00:00:00 2001 From: Alex Polvi Date: Wed, 24 Jul 2013 21:32:29 -0700 Subject: [PATCH 1/2] add ebs script --- build_ebs_on_ec2.sh | 134 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 build_ebs_on_ec2.sh diff --git a/build_ebs_on_ec2.sh b/build_ebs_on_ec2.sh new file mode 100755 index 0000000000..182b887a16 --- /dev/null +++ b/build_ebs_on_ec2.sh @@ -0,0 +1,134 @@ +#!/bin/bash -ex +# +# This expects to run on an EC2 instance. +# + +# AKI ids from: +# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedkernels.html +# we need pv-grub-hd00 x86_64 + +declare -A AKI +AKI["us-east-1"]=aki-b4aa75dd +AKI["us-west-1"]=aki-eb7e26ae +AKI["us-west-2"]=aki-f837bac8 +AKI["eu-west-1"]=aki-8b655dff +AKI["ap-southeast-1"]=aki-fa1354a8 +AKI["ap-southeast-2"]=aki-3d990e07 +AKI["ap-northeast-1"]=aki-40992841 +AKI["sa-east-1"]=aki-c88f51d5 +# AKI["gov-west-1"]=aki-75a4c056 + +readonly COREOS_EPOCH=1372636800 +TODAYS_VERSION=$(( (`date +%s` - ${COREOS_EPOCH}) / 86400 )) +EXTRA_VERSION=$(date +%H-%M) + +if [ -z "$1" ]; then + echo "usage: $0 [http|path/to/bin.bz2]" + exit 1 +fi + +binurl=$1 + +# Size of AMI file system +size=8 # GB + +arch=x86_64 +arch2=amd64 +ephemeraldev=/dev/sdb +version="master-$TODAYS_VERSION-$EXTRA_VERSION" + +#TBD +name="CoreOS-$version" +description="CoreOS master" + +export EC2_CERT=$(echo /tmp/*cert*.pem) +export EC2_PRIVATE_KEY=$(echo /tmp/*pk*.pem) + +zoneurl=http://instance-data/latest/meta-data/placement/availability-zone +zone=$(curl -s $zoneurl) +region=$(echo $zone | sed 's/.$//') + +# this is defined in: build_library/ami_constants.sh +akiid=${AKI[$region]} + +if [ -z "$akiid" ]; then + echo "$0: Can't identify AKI, using region: $region"; + exit 1 +fi + +if [ -z "$(which ec2-attach-volume)" ]; then + # Update and install Ubuntu packages + export DEBIAN_FRONTEND=noninteractive + sudo perl -pi -e 's/^# *(deb .*multiverse)$/$1/' /etc/apt/sources.list + sudo apt-get update + sudo -E apt-get upgrade -y + sudo -E apt-get install -y \ + ec2-api-tools \ + ec2-ami-tools +fi + +export EC2_URL=http://ec2.$region.amazonaws.com + +# Create and mount temporary EBS volume with file system to hold new AMI image +volumeid=$(ec2-create-volume --size $size --availability-zone $zone | + cut -f2) +instanceid=$(wget -qO- http://instance-data/latest/meta-data/instance-id) +ec2-attach-volume --device /dev/sdi --instance "$instanceid" "$volumeid" +while [ ! -e /dev/sdi -a ! -e /dev/xvdi ] + do sleep 3; done +if [ -e /dev/xvdi ]; then + dev=/dev/xvdi +else + dev=/dev/sdi +fi + +# if it is on the local fs, just use it, otherwise try to download it +if [ -e "$binurl" ]; then + bzcat $binurl | dd of=$dev bs=128M +else + curl -s $binurl | bunzip2 | dd of=$dev bs=128M +fi + +ec2-detach-volume "$volumeid" +while ec2-describe-volumes "$volumeid" | grep -q ATTACHMENT + do sleep 3; done +snapshotid=$(ec2-create-snapshot --description "$name" "$volumeid" | cut -f2) +while ec2-describe-snapshots "$snapshotid" | grep -q pending + do sleep 30; done + +# Register the snapshot as a new AMI +amiid=$(ec2-register \ + --name "$name" \ + --description "$description" \ + --architecture "$arch" \ + --kernel "$akiid" \ + --block-device-mapping /dev/sda=$snapshotid::true \ + --block-device-mapping $ephemeraldev=ephemeral0 \ + --snapshot "$snapshotid" | + cut -f2) + +ec2-delete-volume "$volumeid" + +cat < Date: Thu, 25 Jul 2013 07:50:45 -0700 Subject: [PATCH 2/2] add scripts --- build_ebs_on_ec2.sh => oem/ami/build_ebs_on_ec2.sh | 7 ++++--- oem/ami/zip_and_ship_ami.sh | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) rename build_ebs_on_ec2.sh => oem/ami/build_ebs_on_ec2.sh (93%) create mode 100755 oem/ami/zip_and_ship_ami.sh diff --git a/build_ebs_on_ec2.sh b/oem/ami/build_ebs_on_ec2.sh similarity index 93% rename from build_ebs_on_ec2.sh rename to oem/ami/build_ebs_on_ec2.sh index 182b887a16..c060836355 100755 --- a/build_ebs_on_ec2.sh +++ b/oem/ami/build_ebs_on_ec2.sh @@ -2,6 +2,8 @@ # # This expects to run on an EC2 instance. # +# mad props to Eric Hammond for the initial script +# https://github.com/alestic/alestic-hardy-ebs/blob/master/bin/alestic-hardy-ebs-build-ami # AKI ids from: # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedkernels.html @@ -84,7 +86,7 @@ fi # if it is on the local fs, just use it, otherwise try to download it if [ -e "$binurl" ]; then - bzcat $binurl | dd of=$dev bs=128M + bunzip2 -c $binurl | dd of=$dev bs=128M else curl -s $binurl | bunzip2 | dd of=$dev bs=128M fi @@ -103,8 +105,7 @@ amiid=$(ec2-register \ --architecture "$arch" \ --kernel "$akiid" \ --block-device-mapping /dev/sda=$snapshotid::true \ - --block-device-mapping $ephemeraldev=ephemeral0 \ - --snapshot "$snapshotid" | + --block-device-mapping $ephemeraldev=ephemeral0 | cut -f2) ec2-delete-volume "$volumeid" diff --git a/oem/ami/zip_and_ship_ami.sh b/oem/ami/zip_and_ship_ami.sh new file mode 100755 index 0000000000..9ff236a0b9 --- /dev/null +++ b/oem/ami/zip_and_ship_ami.sh @@ -0,0 +1,10 @@ +#!/bin/bash -xe +# +# this needs more refactoring, but is used to build the latest image + +IMG="../build/images/amd64-generic/latest/coreos_ami_image.bin" +ssh -i ~/.ssh/coreos-images.pem ubuntu@23.22.1.1 "mkdir -p /mnt/tmp" + +bzip2 $IMG +scp -i ~/.ssh/coreos-images.pem build_ebs_on_ec2.sh $IMG.bz2 ubuntu@23.22.1.1:/mnt/tmp +ssh -i ~/.ssh/coreos-images.pem ubuntu@23.22.1.1 "sudo /mnt/tmp/build_ebs_on_ec2.sh /mnt/tmp/coreos_ami_image.bin.bz2"