Sha256: f10a8406df5c291fbd9ba1ffb14a681a8f06b848f1871d649bbb384416b05c20

Contents?: true

Size: 1.78 KB

Versions: 223

Compression:

Stored size: 1.78 KB

Contents

# -*- coding: utf-8 -*-
module Specinfra
  class Ec2Metadata
    def initialize(host_inventory)
      @host_inventory = host_inventory

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

    def get
      @metadata = get_metadata
      self
    end

    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.new(@host_inventory).get
        rescue LoadError
          @metadata[key] = nil
        end
      end

      @metadata[key]
    end

    def empty?
      @metadata.empty?
    end

    def each
      keys.each do |k|
        yield k, @metadata[k]
      end
    end

    def each_key
      keys.each do |k|
        yield k
      end
    end

    def each_value
      keys.each do |k|
        yield @metadata[k]
      end
    end

    def keys
      @metadata.keys
    end

    def inspect
      @metadata
    end

    private
    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

    def get_endpoint(path)
      ret = @host_inventory.backend.run_command("curl -s #{@base_uri}#{path}")
      if ret.success?
        ret.stdout
      else
        nil
      end
    end

  end
end

Version data entries

223 entries across 223 versions & 3 rubygems

Version Path
specinfra-2.82.7 lib/specinfra/ec2_metadata.rb
specinfra-2.82.6 lib/specinfra/ec2_metadata.rb
specinfra-2.82.5 lib/specinfra/ec2_metadata.rb
specinfra-2.82.4 lib/specinfra/ec2_metadata.rb
specinfra-2.82.3 lib/specinfra/ec2_metadata.rb
specinfra-2.82.2 lib/specinfra/ec2_metadata.rb
specinfra-2.82.1 lib/specinfra/ec2_metadata.rb
specinfra-2.82.0 lib/specinfra/ec2_metadata.rb
specinfra-2.81.0 lib/specinfra/ec2_metadata.rb
specinfra-2.80.0 lib/specinfra/ec2_metadata.rb
specinfra-2.79.0 lib/specinfra/ec2_metadata.rb
specinfra-2.78.2 lib/specinfra/ec2_metadata.rb
specinfra-2.78.1 lib/specinfra/ec2_metadata.rb
specinfra-2.78.0 lib/specinfra/ec2_metadata.rb
specinfra-2.77.3 lib/specinfra/ec2_metadata.rb
specinfra-2.77.2 lib/specinfra/ec2_metadata.rb
specinfra-2.77.1 lib/specinfra/ec2_metadata.rb
specinfra-2.77.0 lib/specinfra/ec2_metadata.rb
specinfra-2.76.9 lib/specinfra/ec2_metadata.rb
specinfra-2.76.8 lib/specinfra/ec2_metadata.rb