Skip to content

Instantly share code, notes, and snippets.

@winhamwr
Created July 27, 2011 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save winhamwr/1109818 to your computer and use it in GitHub Desktop.
Save winhamwr/1109818 to your computer and use it in GitHub Desktop.
Fabric script to bundle an ami
def bundle_ami():
require('dev_aws_userid')
require('dev_aws_access_key_id')
require('dev_aws_secret_access_key')
require('ami_bucket')
require('config_folder')
require('hudson_slave_ami_name')
env.ami_name = env.hudson_slave_ami_name
# Enable multiverse repo
#sudo("echo 'deb http://us.archive.ubuntu.com/ubuntu/ lucid multiverse' | sudo tee /etc/apt/sources.list.d/multiverse.list", pty=True)
# Fix for bundling issues with the newest 10.04 AMI 01/14/2011
# https://bugs.launchpad.net/ubuntu/+source/euca2ools/+bug/667793
sudo("add-apt-repository ppa:smoser/bundle-fix-sru-test")
sudo('apt-get update', pty=True)
sudo('apt-get install -y ec2-ami-tools', pty=True)
# Upload the AWS cert and private key for bundling purposes
put("%(config_folder)s/ec2_cert.pem" % env, "/tmp/cert.pem")
put("%(config_folder)s/ec2_pk.pem" % env, "/tmp/pk.pem")
ec2conn = ec2.EC2Connection(
env.dev_aws_access_key_id, env.dev_aws_secret_access_key)
# Create an empty image folder (removing old if needed)
sudo("rm -R -f /mnt/ami")
sudo("mkdir /mnt/ami")
# Bundle the system
bundle_cmd = "ec2-bundle-vol -k /tmp/pk.pem --cert /tmp/cert.pem \
-u %(dev_aws_userid)s --arch i386 -s 10000 -d /mnt/ami -p %(ami_name)s --debug \
--batch -e /mnt,/home/ubuntu/.ssh" % env
# Upload the image to S3
upload_cmd = "ec2-upload-bundle -b %(ami_bucket)s -m /mnt/ami/%(ami_name)s.manifest.xml \
-a %(dev_aws_access_key_id)s -s %(dev_aws_secret_access_key)s --batch --retry" % env
sudo('%s; %s' % (bundle_cmd, upload_cmd))
# Register the AMI
logger.info("Registering the image")
manifest_location = "%(ami_bucket)s/%(ami_name)s.manifest.xml" % env
logger.debug("Image manifest: %s" % manifest_location)
ami_id = ec2conn.register_image(
name=env.ami_name,
image_location=manifest_location)
# Give the dev ami user permissions to launch this ami
ami = ec2.image.Image(ec2conn)
ami.id = ami_id
ami.update()
ami.set_launch_permissions(user_ids=[env.dev_ami_user])
logger.critical("New ami id: %s" % ami_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment