Class: ActiveAws::Ec2Provisioner

Inherits:
Base
  • Object
show all
Defined in:
lib/active_aws/ec2_provisioner.rb

Constant Summary collapse

ATTRIBUTES =
[
  :instance_type, 
  :key_name, 
  :image_id, 
  :security_group_ids,
  :subnet_id,
  :user_data,
  :block_device_mappings,
  :tag_specifications,
  :network_interfaces,
  :launch_template,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

configure, inherited, load_config!

Constructor Details

#initialize(name, **params) {|_self| ... } ⇒ Ec2Provisioner

Returns a new instance of Ec2Provisioner.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
25
26
27
28
29
30
# File 'lib/active_aws/ec2_provisioner.rb', line 21

def initialize( name, **params )
  @name = name

  ATTRIBUTES.each do |k|
    method = "#{k}="
    self.send(method, params[k]) if params[k].present?
  end

  yield( self ) if block_given?
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/active_aws/ec2_provisioner.rb', line 17

def name
  @name
end

#tag_specsObject (readonly)

Returns the value of attribute tag_specs.



19
20
21
# File 'lib/active_aws/ec2_provisioner.rb', line 19

def tag_specs
  @tag_specs
end

Instance Method Details

#exec!(extra_tags = {}) ⇒ Object



43
44
45
46
47
# File 'lib/active_aws/ec2_provisioner.rb', line 43

def exec!( extra_tags={} )
  ec2 = ActiveAws::Ec2.find_by_name( name )
  return ec2 if ec2
  forced_exec!( extra_tags )
end

#forced_exec!(extra_tags = {}) ⇒ Object



49
50
51
52
53
# File 'lib/active_aws/ec2_provisioner.rb', line 49

def forced_exec!( extra_tags={} )
  client = ActiveAws::Ec2.client
  response = client.run_instances( self.to_h(extra_tags) )
  ActiveAws::Ec2.new( **response.instances[0] )
end

#to_h(extra_tags = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_aws/ec2_provisioner.rb', line 55

def to_h( extra_tags={} )
  extra_tags = extra_tags.merge({ "Name" => self.name })
  attrs = ATTRIBUTES.reduce({}) do |acc,k|
    acc[k] = self.send( k )
    acc
  end
  attrs = attrs.merge(
    max_count: 1,
    min_count: 1,
    tag_specifications: tag_specs.map {|ts|
      ts = if ts.resource_type == "instance"
        ts.merge( extra_tags )
      else
        ts
      end
      ts.to_param
    }
  )
  attrs
end