Class: Bolt::Plugin::AwsInventory
- Inherits:
-
Object
- Object
- Bolt::Plugin::AwsInventory
- Defined in:
- lib/bolt/plugin/aws_inventory.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #config_client(opts) ⇒ Object
- #hooks ⇒ Object
-
#initialize(config:, **_kwargs) ⇒ AwsInventory
constructor
A new instance of AwsInventory.
-
#lookup(instance, attribute) ⇒ Object
Look for an instance attribute specified in the inventory file.
- #name ⇒ Object
-
#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.
- #resolve_reference(opts) ⇒ Object
- #warn_missing_attribute(instance, attribute) ⇒ Object
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
#client ⇒ Object
Returns the value of attribute client.
8 9 10 |
# File 'lib/bolt/plugin/aws_inventory.rb', line 8 def client @client end |
#config ⇒ Object (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 = {} if opts.key?('region') [:region] = opts['region'] end if opts.key?('profile') [:profile] = opts['profile'] end if config['credentials'] creds = File.(config['credentials']) if File.exist?(creds) [:credentials] = ::Aws::SharedCredentials.new(path: creds) else raise Bolt::ValidationError, "Cannot load credentials file #{config['credentials']}" end end ::Aws::EC2::Client.new() end |
#hooks ⇒ Object
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 |
#name ⇒ Object
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 |