Class: AWS::AMI
- Inherits:
-
Object
- Object
- AWS::AMI
- Defined in:
- lib/aws_ami/ami.rb
Instance Method Summary collapse
-
#build(name, parameters) ⇒ Object
name: new AMI name, for example: mingle-saas-base parameters: BaseAMI: the base AMI id for the new AMI, for example: “ami-0d153248” for the “ubuntu/images/ebs/ubuntu-precise-12.04-amd64-server-20121001” in us-west-1 region KeyName: the ssh key name for accessing the ec2 instance while building the AMI, this is only used when you need to debug problems InstallScript: the script installs everything need for the AMI, from 2048 to 16k bytes depending on the base ami provided.
-
#initialize(options = {}) ⇒ AMI
constructor
region: aws region of the new AMI assume_yes: true for deleting stack when creating image failed.
Constructor Details
#initialize(options = {}) ⇒ AMI
region: aws region of the new AMI assume_yes: true for deleting stack when creating image failed
8 9 10 11 12 13 |
# File 'lib/aws_ami/ami.rb', line 8 def initialize(={}) @assume_yes = [:assume_yes] @region = [:region] @publish_to_account = [:publish_to_account] @test = [:test] end |
Instance Method Details
#build(name, parameters) ⇒ Object
name: new AMI name, for example: mingle-saas-base parameters:
BaseAMI: the base AMI id for the new AMI, for example:
"ami-0d153248" for the "ubuntu/images/ebs/ubuntu-precise-12.04-amd64-server-20121001" in us-west-1 region
KeyName: the ssh key name for accessing the ec2 instance while building the AMI, this is only used when you need to debug problems
InstallScript: the script installs everything need for the AMI, from 2048 to 16k bytes depending on the base ami provided
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/aws_ami/ami.rb', line 21 def build(name, parameters) stack = cloudformation.stacks.create("build-#{name}-ami", load_formation, :disable_rollback => true, :parameters => parameters) logger.info "creating stack" wait_until_created(stack) begin instance_id = stack.resources['EC2Instance'].physical_resource_id if @test unless gets.strip == 'y' logger.info "delete stack and stop" stack.delete return end logger.info "continue to create image" end logger.info "creating image" image = ec2.instances[instance_id].create_image(name, :description => "Created at #{Time.now}") sleep 2 until image.exists? logger.info "image #{image.id} state: #{image.state}" sleep 5 until image.state != :pending if image.state == :failed raise "Create image failed" end logger.info "image created" logger.info "delete #{stack.name} stack" stack.delete rescue => e logger.error "Creating AMI failed #{e.}" logger.error e.backtrace.join("\n") logger.info "delete #{stack.name}? [y/n]" if @assume_yes || gets.strip.downcase == 'y' logger.info 'delete stack' stack.delete else logger.info "left stack live" end raise e end if @publish_to_account logger.info "add permissions for #{@publish_to_account}" image..add(@publish_to_account.gsub(/-/, '')) end logger.info "Image #{name}[#{image.id}] created" end |