Class: ImageOptimize::ImageBundleEc2EBS

Inherits:
ImageBundleEc2Base show all
Defined in:
lib/image_bundle/image_bundle_ec2_ebs.rb

Overview

Handles the snapshoting and registration of EBS based images

Constant Summary

Constants inherited from ImageBundleEc2Base

ImageOptimize::ImageBundleEc2Base::RETRY_TIMEOUT_SEC

Instance Method Summary collapse

Methods inherited from ImageBundleEc2Base

#initialize, #register_image

Methods inherited from ImageBundleBase

#add_image_to_next_instance, #initialize, #log, #register_image

Methods included from Command

#debug, #execute

Methods included from Common

#not_implemented

Constructor Details

This class inherits a constructor from ImageOptimize::ImageBundleEc2Base

Instance Method Details

#register_command(name = nil, description = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/image_bundle/image_bundle_ec2_ebs.rb', line 64

def register_command(name=nil, description=nil)
  unless @snapshot_id
    fail("@snapshot_id cannot be nil. Be sure to run snapshot_instance first.")
  end

  # use ec2 tools to register snapshot as an image
  # TODO: this command only maps in 4 ephemeral devices to the new image. Use metadata to get actual count.
  cmd = "ec2-register --region #{region} --snapshot #{@snapshot_id} --description '#{@image_description}' --block-device-mapping '/dev/sdb=ephemeral0' --block-device-mapping '/dev/sdc=ephemeral1' --block-device-mapping '/dev/sdd=ephemeral2' --block-device-mapping '/dev/sde=ephemeral3' --kernel #{kernel_aki} --root-device-name #{@attachment_device} --architecture x86_64 --name '#{name}' #{common_options}"
  cmd
end

#snapshot_instance(name = nil, description = nil) ⇒ Object



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
# File 'lib/image_bundle/image_bundle_ec2_ebs.rb', line 25

def snapshot_instance(name=nil, description=nil)

  # find root volume
  #
  root_volume = nil
  begin
    device_name = "/dev/sda"
    @log.info "Locating root volume attached to #{device_name}..."
    attachment = instance.volume_attachments.index.select { |va| va.device =~ /#{device_name}/ }.first
    root_volume = attachment.volume
    @attachment_device = attachment.device
    @log.info "Found volume #{root_volume.show.name} attached at #{@attachment_device}"
  rescue Exception => e
    fail("FATAL: cannot find root volume. Check device_name which can vary depending on hypervisor and/or kernel. Also instance-store images not currently supported.", e)
  end

  # create snapshot
  @log.info "Creating snapshot of server."
  options = { :parent_volume_href => root_volume.href }
  options.merge!(:name => name) if name
  options.merge!(:description => description) if description
  snapshot = @instance_client.volume_snapshots.create(:volume_snapshot => options)
  @log.info "Snapshot name '#{name}'" if name

  # wait for snapshot to complete
  @log.info "Waiting for snapshot to become available"
  current_state = snapshot.show.state
  delay_sec = 10
  Timeout::timeout(RETRY_TIMEOUT_SEC) do
    while current_state != "available" do
      @log.info "  snapshot state: #{current_state}. try again in #{delay_sec} seconds..."
      sleep delay_sec
      current_state = snapshot.show.state
    end
  end
  @log.info "Snapshot is now available"
  @snapshot_id = snapshot.show.resource_uid
end