Sha256: f44e793cd19379bd2af59ce55ba186ba5f1c8e994085a21aae9137bb038f5363

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Discotheque
  class Metadata

    attr_reader :instance_id, :image_id, :group_name, :availability_zone

    def initialize(mock=false)
      if mock == true
        @image_id = "ami-221fec4b" # amazon linux ami
        @group_name = "default"
        @availability_zone = "us-east-1a"
        return
      else
        base_url = "http://169.254.169.254/latest/meta-data/"
        begin
          %w[instance-id ami-id security-groups availability-zone].each do |datum|
            datum == "availability-zone" ? url="#{base_url}/placement/#{datum}" : url="#{base_url}#{datum}"
            d = HTTParty.get("#{url}").parsed_response
            if datum == "security-groups" 
              instance_variable_set "@group_name", d
            elsif datum == "ami-id"
              instance_variable_set "@image_id", d
            else
              instance_variable_set "@#{datum.gsub(/-/,"_")}", d
            end
          end
        rescue Errno::EHOSTUNREACH
          puts "This only makes sense on EC2 hosts"
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
discotheque-0.0.1 lib/discotheque/metadata.rb