Class: AWS::AMI

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_ami/ami.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AMI

name: AMI name region: aws region of the new AMI key_name: AWS EC2 ssh key name for accessing ec2 instance for debuging problems install_script: a script file can be run on the ec2 instance that is launched from base ami to install packages base_ami: a yml file describes base AMI used for building new AMI for each region timeout: Timeout for running install script, default 600 publish_to_account: publish the AMI to this account if need assume_yes: true for deleting stack when creating image failed



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aws_ami/ami.rb', line 14

def initialize(options={})
  @name = options[:name] || raise("Must have a name for the AMI")
  @region = options[:region] || raise("Must specify aws region")
  @key_name = options[:key_name] || raise("Please specify aws ssh key name, so that you can debug problem when something goes wrong")
  @install_script = File.read(options[:install_script]) || raise("Must provide install packages script file")

  @base_ami = YAML.load(File.read(options[:base_ami]))[@region] || raise("Must provide base ami yml file for each region")

  @timeout = options[:timeout] || '600'
  @assume_yes = options[:assume_yes] || false
  @publish_to_account = options[:publish_to_account]
  @test = options[:test]
end

Instance Method Details

#buildObject



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aws_ami/ami.rb', line 28

def build
  stack = cloudformation.stacks.create("build-#{@name}-ami",
                                       load_formation,
                                       :disable_rollback => true,
                                       :parameters => {
                                         'Timeout' => @timeout,
                                         'KeyName' => @key_name,
                                         'InstallScript' => @install_script,
                                         "BaseAMI" => @base_ami
                                       })
  logger.info "creating stack for region #{@region}"
  begin
    wait_until_created(stack)
    instance_id = stack.resources['EC2Instance'].physical_resource_id
    instance = ec2.instances[instance_id]
    if @test
      puts "Build AMI Stack created"
      puts "EC2 instance dns name: #{instance.dns_name}, ip address: #{instance.ip_address}"
      puts "continue to create AMI Image? [y/n]"
      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 = instance.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.message}"
    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.permissions.add(@publish_to_account.gsub(/-/, ''))
  end
  image.id.tap do |image_id|
    logger.info "Image #{@name}[#{image_id}] created"
  end
end