Class: Bolt::Plugin::Aws::EC2

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/plugin/aws.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ EC2

Returns a new instance of EC2.



12
13
14
15
16
# File 'lib/bolt/plugin/aws.rb', line 12

def initialize(config)
  require 'aws-sdk-ec2'
  @config = config
  @logger = Logging.logger[self]
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/bolt/plugin/aws.rb', line 9

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/bolt/plugin/aws.rb', line 10

def config
  @config
end

Instance Method Details

#config_client(opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bolt/plugin/aws.rb', line 26

def config_client(opts)
  return client if client

  options = {}

  if opts.key?('region')
    options[:region] = opts['region']
  end
  if opts.key?('profile')
    options[:profile] = opts['profile']
  end
  if config['credentials']
    creds = File.expand_path(config['credentials'])
    if File.exist?(creds)
      options[:credentials] = ::Aws::SharedCredentials.new(path: creds)
    else
      raise Bolt::ValidationError, "Cannot load credentials file #{config['credentials']}"
    end
  end

  ::Aws::EC2::Client.new(options)
end

#hooksObject



22
23
24
# File 'lib/bolt/plugin/aws.rb', line 22

def hooks
  %w[inventory_targets]
end

#inventory_targets(opts) ⇒ Object



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
# File 'lib/bolt/plugin/aws.rb', line 49

def inventory_targets(opts)
  client = config_client(opts)
  resource = ::Aws::EC2::Resource.new(client: client)

  # Retrieve a list of EC2 instances and create a list of targets
  # Note: It doesn't seem possible to filter stubbed responses...
  resource.instances(filters: opts['filters']).map do |instance|
    next unless instance.state.name == 'running'
    target = {}

    if opts.key?('uri')
      uri = lookup(instance, opts['uri'])
      target['uri'] = uri if uri
    end
    if opts.key?('name')
      real_name = lookup(instance, opts['name'])
      target['name'] = real_name if real_name
    end
    if opts.key?('config')
      target['config'] = resolve_config(instance, opts['config'])
    end

    target if target['uri'] || target['name']
  end.compact
end

#lookup(instance, attribute) ⇒ Object

Look for an instance attribute specified in the inventory file



76
77
78
79
80
81
82
# File 'lib/bolt/plugin/aws.rb', line 76

def lookup(instance, attribute)
  value = instance.data.respond_to?(attribute) ? instance.data[attribute] : nil
  unless value
    warn_missing_attribute(instance, attribute)
  end
  value
end

#nameObject



18
19
20
# File 'lib/bolt/plugin/aws.rb', line 18

def name
  'aws::ec2'
end

#resolve_config(name, config_template) ⇒ Object

Walk the “template” config mapping provided in the plugin config and replace all values with the corresponding value from the resource parameters.



91
92
93
94
95
96
97
98
99
# File 'lib/bolt/plugin/aws.rb', line 91

def resolve_config(name, config_template)
  Bolt::Util.walk_vals(config_template) do |value|
    if value.is_a?(String)
      lookup(name, value)
    else
      value
    end
  end
end

#warn_missing_attribute(instance, attribute) ⇒ Object



84
85
86
# File 'lib/bolt/plugin/aws.rb', line 84

def warn_missing_attribute(instance, attribute)
  @logger.warn("Could not find attribute #{attribute} of instance #{instance.instance_id}")
end