lib/specinfra/ec2_metadata.rb in specinfra-2.23.0 vs lib/specinfra/ec2_metadata.rb in specinfra-2.24.0
- old
+ new
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
module Specinfra
class Ec2Metadata
- def initialize
+ def initialize(host_inventory)
+ @host_inventory = host_inventory
+
@base_uri = 'http://169.254.169.254/latest/meta-data/'
@metadata = {}
end
def get
@@ -14,11 +16,11 @@
def [](key)
if @metadata[key].nil?
begin
require "specinfra/ec2_metadata/#{key}"
inventory_class = Specinfra::Ec2Metadata.const_get(key.to_s.to_camel_case)
- @metadata[key] = inventory_class.get
+ @metadata[key] = inventory_class.new(@host_inventory).get
rescue LoadError
@metadata[key] = nil
end
end
@@ -57,11 +59,11 @@
private
def get_metadata(path='')
metadata = {}
- keys = Specinfra::Runner.run_command("curl -s #{@base_uri}#{path}").stdout.split("\n")
+ keys = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}").stdout.split("\n")
keys.each do |key|
if key =~ %r{/$}
metadata[key[0..-2]] = get_metadata(path + key)
else
@@ -77,10 +79,10 @@
metadata
end
def get_endpoint(path)
- ret = Specinfra::Runner.run_command("curl -s #{@base_uri}#{path}")
+ ret = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}")
if ret.success?
ret.stdout
else
nil
end