Class: Rivet::Ec2

Inherits:
Object
  • Object
show all
Defined in:
lib/rivet/ec2/ec2.rb

Constant Summary collapse

OPTIONS =
[
  :associate_public_ip_address,
  :availability_zone,
  :block_device_mappings,
  :count,
  :dedicated_tenancy,
  :disable_api_termination,
  :ebs_optimized,
  :elastic_ips,
  :iam_instance_profile,
  :image_id,
  :instance_initiated_shutdown_behavior,
  :instance_type,
  :kernel_id,
  :key_name,
  :key_pair,
  :monitoring_enabled,
  :network_interfaces,
  :placement_group,
  :private_ip_address,
  :ramdisk_id,
  :security_group_ids,
  :security_groups,
  :subnet,
  :tags,
  :user_data
].each { |a| attr_reader a }
REQUIRED_OPTIONS =
[
  :image_id
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Ec2

Returns a new instance of Ec2.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rivet/ec2/ec2.rb', line 39

def initialize(config)
  @ec2 = AWS::EC2.new
  @name = config.name
  @user_data = Bootstrap.new(config).user_data

  OPTIONS.each do |o|
    if config.respond_to?(o)
      instance_variable_set("@#{o}", config.send(o))
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/rivet/ec2/ec2.rb', line 37

def name
  @name
end

Instance Method Details

#display(level = 'info') ⇒ Object



51
52
53
54
55
# File 'lib/rivet/ec2/ec2.rb', line 51

def display(level = 'info')
  options.each_pair do |attr, values|
    Rivet::Log.write(level, "  #{attr}: #{values}")
  end
end

#optionsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rivet/ec2/ec2.rb', line 57

def options
  options = {}

  OPTIONS.each do |field|
    local_value = self.send(field)
    options[field] = local_value unless local_value.nil?
  end

  REQUIRED_OPTIONS.each do |field|
    unless options.has_key? field
      options[field] = self.send(field)
    end
  end
  options
end

#syncObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rivet/ec2/ec2.rb', line 73

def sync
  # The AWS ruby SDK forces you to apply tags AFTER creation
  # This option must be removed so the create call doesn't blow up.
  server_options = options
  tags_to_add    = server_options.delete :tags
  eips_to_add    = server_options.delete :elastic_ips
  enis_to_add    = server_options.delete :network_interfaces
  instances      = @ec2.instances.create server_options

  # Since create returns either an instance object or an array let us
  # just go ahead and make that more sane
  instances = [instances] unless instances.respond_to? :each

  add_tags(instances,tags_to_add)
  ready_instances = wait_until_running instances
  add_eips(ready_instances,eips_to_add) if eips_to_add
  add_network_interfaces(ready_instances,enis_to_add) if enis_to_add
end