Created
September 4, 2008 13:22
-
-
Save code/8770 to your computer and use it in GitHub Desktop.
This file contains 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
# File: ec2_bundle.rb | |
# Author: Luke Hubbard | |
# Gist: http://gist.github.com/gists/8770 | |
# License: MIT | |
# Desc: Simple script to bundle and upload an EC2 instance | |
require 'open-uri' | |
# Helpers | |
def read(file) | |
open(file).read.chomp | |
end | |
def run(cmd) | |
puts cmd | |
exit unless system(cmd) | |
end | |
# Config | |
aws_auth_home = '~/.aws' | |
aws_auth_home.gsub!(/^\~\//,`echo ~`.chomp+'/') | |
id_pem = 'REPLACE.pem' | |
pk_pem = 'pk-REPLACE.pem' | |
cert_pem = 'cert-REPLACE_.pem' | |
server = 'your.server.com' | |
bundle = "#{server}-#{Time.new.strftime("%Y-%m-%d")}" | |
account_number = read("#{aws_auth_home}/account_number") | |
access_key_id = read("#{aws_auth_home}/access_key_id") | |
secret_access_key = read("#{aws_auth_home}/secret_access_key") | |
arch = 'i386' | |
ami_bucket = 'REPLACE' | |
# 1. Upload pems | |
run "scp -i #{aws_auth_home}/#{id_pem} #{aws_auth_home}/#{pk_pem} #{aws_auth_home}/#{cert_pem} root@#{server}:/mnt" | |
# 2. Bundle image | |
run "ssh -i #{aws_auth_home}/#{id_pem} root@#{server} \"ec2-bundle-vol -d /mnt -k /mnt/#{pk_pem} -c /mnt/#{cert_pem} -u '#{account_number}' -r #{arch} -p #{bundle}\"" | |
# 3. Upload bundle | |
run "ssh -i #{aws_auth_home}/#{id_pem} root@#{server} \"ec2-upload-bundle -b #{ami_bucket} -m /mnt/#{bundle}.manifest.xml -a '#{access_key_id}' -s '#{secret_access_key}'\"" | |
# 4. Cleanup after | |
run "ssh -i #{aws_auth_home}/#{id_pem} root@#{server} \"rm /mnt/#{pk_pem} && rm /mnt/#{pk_cert} && rm /mnt/#{bundle}*\"" | |
# 5. Register bundle | |
run "ec2-register #{ami_bucket}/#{bundle}.manifest.xml" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment