Sha256: 682c6d04f4d54bcd2bb114087622f38a8f817bced8a492446afac19092468731

Contents?: true

Size: 948 Bytes

Versions: 5

Compression:

Stored size: 948 Bytes

Contents

require 'logger'
require 'open-uri'
require 'json'
require "timeout"

module Tagenv
  module Ec2
    class Metadata
      TIME_OUT = 3

      def self.ec2?
        begin
          ::Timeout.timeout(TIME_OUT) {
            open('http://169.254.169.254/latest/meta-data/instance-id').read
            return true
          }
        rescue => e
          return false
        end
      end

      def self.get_metadata(path)
        begin
          result = {}
          ::Timeout.timeout(TIME_OUT) {
            body = open('http://169.254.169.254' + path).read
            return body
          }
          return result
        rescue ::Timeout::Error => e
          raise "not EC2 instance"
        end
      end

      def self.get_document
        JSON.parse(get_metadata('/latest/dynamic/instance-identity/document/'))
      end

      def self.get_instance_id
        get_metadata('/latest/meta-data/instance-id')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tagenv-0.1.7 lib/tagenv/ec2/metadata.rb
tagenv-0.1.6 lib/tagenv/ec2/metadata.rb
tagenv-0.1.5 lib/tagenv/ec2/metadata.rb
tagenv-0.1.4 lib/tagenv/ec2/metadata.rb
tagenv-0.1.3 lib/tagenv/ec2/metadata.rb