Module: MovableInk::AWS::Metadata

Included in:
MovableInk::AWS
Defined in:
lib/movable_ink/aws/metadata.rb

Instance Method Summary collapse

Instance Method Details

#availability_zoneObject



44
45
46
# File 'lib/movable_ink/aws/metadata.rb', line 44

def availability_zone
  @availability_zone ||= ('placement/availability-zone')
end

#http(timeout_seconds: 1) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/movable_ink/aws/metadata.rb', line 7

def http(timeout_seconds: 1)
  @http = begin
    http = Net::HTTP.new("169.254.169.254", 80)
    http.open_timeout = timeout_seconds
    http.read_timeout = timeout_seconds
    http
  end
end

#instance_idObject



48
49
50
# File 'lib/movable_ink/aws/metadata.rb', line 48

def instance_id
  @instance_id ||= ('instance-id')
end

#instance_identity_documentObject



38
39
40
41
42
# File 'lib/movable_ink/aws/metadata.rb', line 38

def instance_identity_document
  @instance_identity_document ||= JSON.parse(retrieve_dynamicdata('instance-identity/document'))
rescue JSON::ParserError
  return
end

#instance_typeObject



52
53
54
# File 'lib/movable_ink/aws/metadata.rb', line 52

def instance_type
  @instance_type ||= ('instance-type')
end

#private_ipv4Object



56
57
58
# File 'lib/movable_ink/aws/metadata.rb', line 56

def private_ipv4
  @ipv4 ||= ('local-ipv4')
end

#retrieve_data(url, tries: 3) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/movable_ink/aws/metadata.rb', line 16

def retrieve_data(url, tries: 3)
  tries.times do |num|
    num += 1
    request = Net::HTTP::Get.new(url)
    request['X-aws-ec2-metadata-token'] = imds_token
    response = http(timeout_seconds: num * 3).request(request)
    return response.body
  rescue Net::OpenTimeout, Net::ReadTimeout, Errno::EHOSTDOWN, Errno::ECONNREFUSED
    sleep(num * 2)
  end

  raise MovableInk::AWS::Errors::MetadataTimeout
end

#retrieve_dynamicdata(key, tries: 3) ⇒ Object



34
35
36
# File 'lib/movable_ink/aws/metadata.rb', line 34

def retrieve_dynamicdata(key, tries: 3)
  retrieve_data("/latest/dynamic/#{key}", tries: tries)
end

#retrieve_metadata(key, tries: 3) ⇒ Object



30
31
32
# File 'lib/movable_ink/aws/metadata.rb', line 30

def (key, tries: 3)
  retrieve_data("/latest/meta-data/#{key}", tries: tries)
end