class Specinfra::Ec2Metadata

Public Class Methods

new(host_inventory) click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 3
def initialize(host_inventory)
  @host_inventory = host_inventory

  @base_uri = 'http://169.254.169.254/latest/meta-data/'
  @metadata = {}
end

Public Instance Methods

[](key) click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 15
def [](key)
  if key.is_a?(Symbol)
    key = key.to_s
  end
  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.new(@host_inventory).get
    rescue LoadError
      @metadata[key] = nil
    end
  end

  @metadata[key]
end
each() { |k, metadata| ... } click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 36
def each
  keys.each do |k|
    yield k, @metadata[k]
  end
end
each_key() { |k| ... } click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 42
def each_key
  keys.each do |k|
    yield k
  end
end
each_value() { |metadata| ... } click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 48
def each_value
  keys.each do |k|
    yield @metadata[k]
  end
end
empty?() click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 32
def empty?
  @metadata.empty?
end
get() click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 10
def get
  @metadata = get_metadata
  self
end
inspect() click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 58
def inspect
  @metadata
end
keys() click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 54
def keys
  @metadata.keys
end

Private Instance Methods

get_endpoint(path) click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 85
def get_endpoint(path)
  ret = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}")
  if ret.success?
    ret.stdout
  else
    nil
  end
end
get_metadata(path='') click to toggle source
# File lib/specinfra/ec2_metadata.rb, line 63
def get_metadata(path='')
  metadata = {}

  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
      if key =~ %r{=}
        key = key.split('=')[0] + '/'
        metadata[key[0..-2]] = get_metadata(path + key)
      else
        ret = get_endpoint(path)
        metadata[key] = get_endpoint(path + key) if ret
      end
    end
  end

  metadata
end