Class: Bolt::Plugin::AwsInventory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, **_kwargs) ⇒ AwsInventory

Returns a new instance of AwsInventory.



11
12
13
14
15
# File 'lib/bolt/plugin/aws_inventory.rb', line 11

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/bolt/plugin/aws_inventory.rb', line 8

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#config_client(opts) ⇒ Object



25
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_inventory.rb', line 25

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



21
22
23
# File 'lib/bolt/plugin/aws_inventory.rb', line 21

def hooks
  [:resolve_reference]
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_inventory.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



17
18
19
# File 'lib/bolt/plugin/aws_inventory.rb', line 17

def name
  'aws_inventory'
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_inventory.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

#resolve_reference(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_inventory.rb', line 49

def resolve_reference(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

#warn_missing_attribute(instance, attribute) ⇒ Object



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

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