Class: Specinfra::Ec2Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/specinfra/ec2_metadata.rb

Instance Method Summary collapse

Constructor Details

#initializeEc2Metadata

Returns a new instance of Ec2Metadata.



4
5
6
# File 'lib/specinfra/ec2_metadata.rb', line 4

def initialize
  @base_uri = 'http://169.254.169.254/latest/meta-data/'
end

Instance Method Details

#get(path = '') ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/specinfra/ec2_metadata.rb', line 8

def get(path='')
   = {}

  keys = Specinfra::Runner.run_command("curl #{@base_uri}#{path}").stdout.split("\n")

  keys.each do |key|
    if key =~ %r{/$}
      [key[0..-2]] = get(path + key)
    else
      if key =~ %r{=}
        key = key.split('=')[0] + '/'
        [key[0..-2]] = get(path + key)
      else
        ret = get_endpoint(path)
        [key] = get_endpoint(path + key) if ret
      end
    end
  end

  
end

#get_endpoint(path) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/specinfra/ec2_metadata.rb', line 30

def get_endpoint(path)
  ret = Specinfra::Runner.run_command("curl #{@base_uri}#{path}")
  if ret.success?
    ret.stdout
  else
    nil
  end
end