Class: EC2Launcher::BlockDeviceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2launcher/block_device_builder.rb

Overview

Helper class to build EC2 block device definitions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ec2, volume_size = nil) ⇒ BlockDeviceBuilder

Returns a new instance of BlockDeviceBuilder.

Parameters:

  • ec2 (AWS::EC2)

    interface to ec2

  • volume_size (Integer, nil) (defaults to: nil)

    size of new EBS volumes. If set to nil, uses EC2Launcher::Defaults::DEFAULT_VOLUME_SIZE.



19
20
21
22
23
24
25
26
27
28
# File 'lib/ec2launcher/block_device_builder.rb', line 19

def initialize(ec2, volume_size = nil)
  @ec2 = ec2
  @block_size = volume_size
  @volume_size ||= EC2Launcher::DEFAULT_VOLUME_SIZE

  @block_device_mappings = {}
  @block_device_tags = {}

  @log = Logger['ec2launcher']
end

Instance Attribute Details

#block_device_mappingsObject (readonly)

Returns the value of attribute block_device_mappings.



13
14
15
# File 'lib/ec2launcher/block_device_builder.rb', line 13

def block_device_mappings
  @block_device_mappings
end

#block_device_tagsObject (readonly)

Returns the value of attribute block_device_tags.



14
15
16
# File 'lib/ec2launcher/block_device_builder.rb', line 14

def block_device_tags
  @block_device_tags
end

Instance Method Details

#generate_block_devices(instance_type, environment, application, clone_host = nil) ⇒ Hash<String, Hash] returns a mapping of device names to device details

Generates the mappings for ephemeral and ebs volumes.

Parameters:

  • instance_type (String)

    type of instance. See EC2Launcher::Defaults::INSTANCE_TYPES.

  • environment (EC2Launcher::Environment)

    current environment

  • application (EC2Launcher::Application)

    current application

  • clone_host (String, nil) (defaults to: nil)

    FQDN of host to clone or nil to skip cloning.

Returns:

  • (Hash<String, Hash] returns a mapping of device names to device details)

    Hash<String, Hash] returns a mapping of device names to device details



39
40
41
42
43
44
45
46
47
# File 'lib/ec2launcher/block_device_builder.rb', line 39

def generate_block_devices(instance_type, environment, application, clone_host = nil)
  block_device_mappings = {}

  build_ephemeral_drives(block_device_mappings, instance_type)
  build_ebs_volumes(block_device_mappings, application.block_devices)
  clone_volumes(block_device_mappings, application, clone_host)

  block_device_mappings
end

#generate_device_tags(hostname, short_hostname, environment_name, block_devices) ⇒ Hash<String, Hash<String, String>] returns a mapping of device names to maps tag names and values.

Generates a mapping of block device names to tags.

Parameters:

  • hostname (String)

    FQDN for new host

  • short_hostname (String)

    short name for host host, without domain name.

  • environment_name (String)

    Name of current environment

  • block (EC2Launcher::DSL::BlockDevice)

    devices for this application

Returns:

  • (Hash<String, Hash<String, String>] returns a mapping of device names to maps tag names and values.)

    Hash<String, Hash<String, String>] returns a mapping of device names to maps tag names and values.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ec2launcher/block_device_builder.rb', line 57

def generate_device_tags(hostname, short_hostname, environment_name, block_devices)
  block_device_tags = {}
  unless block_devices.nil?
    base_device_name = "sdf"
    block_devices.each do |block_device|
      build_block_devices(block_device.count, base_device_name) do |device_name, index|
        block_device_tags["/dev/#{device_name}"] = {
          "purpose" => block_device.name,
          "host" => hostname,
          "environment" => environment_name
        }
        
        if block_device.raid_level.nil?
          block_device_tags["/dev/#{device_name}"]["Name"] = "#{short_hostname} #{block_device.name}"
        else
          block_device_tags["/dev/#{device_name}"]["Name"] = "#{short_hostname} #{block_device.name} raid (#{(index + 1).to_s})"
          block_device_tags["/dev/#{device_name}"]["raid_number"] = (index + 1).to_s
        end
      end
    end
  end
  block_device_tags
end