Class: Ec2Meta::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_meta/fetcher.rb

Constant Summary collapse

API_HOST =
'169.254.169.254'.freeze
API_VERSION =
'2014-02-25'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Fetcher

Returns a new instance of Fetcher.



13
14
15
16
# File 'lib/ec2_meta/fetcher.rb', line 13

def initialize(options = {})
  @options = options
  @cache = ::Ec2Meta::Cache.new
end

Instance Method Details

#fetch(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ec2_meta/fetcher.rb', line 18

def fetch(path)
  @cache.fetch(path) do
    res = http_client.get(request_path(path))
    break res.body if res.code != '404'

    raise MetaNotFound, "'#{path}' not found." if fail_on_not_found?
    nil
  end
rescue Timeout::Error => e
  logger.error 'Can\'t fetch EC2 metadata from EC2 METADATA API.'
  logger.error 'ec2_meta gem is only available on AWS EC2 instance.'
  raise e
rescue MetaNotFound => e
  raise e
rescue => e
  logger.error "Can't fetch EC2 metadata from EC2 METADATA API.(#{e.message})"
  logger.error e.backtrace.join("\n")
  raise e
end