Ohai::Mixin::GCEMetadata

Public Instance Methods

can_metadata_connect?(addr, port, timeout=2) click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 27
def can_metadata_connect?(addr, port, timeout=2)
  t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
  saddr = Socket.pack_sockaddr_in(port, addr)
  connected = false

  begin
    t.connect_nonblock(saddr)
  rescue Errno::EINPROGRESS
    r,w,e = IO::select(nil,[t],nil,timeout)
    if !w.nil?
      connected = true
    else
      begin
        t.connect_nonblock(saddr)
      rescue Errno::EISCONN
        t.close
        connected = true
      rescue SystemCallError
      end
    end
  rescue SystemCallError
  end
  Ohai::Log.debug("can_metadata_connect? == #{connected}")
  connected
end
fetch_metadata(id='') click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 57
def fetch_metadata(id='')
  uri = "#{GCE_METADATA_URL}/#{id}"
  response = http_client.get(uri)
  return nil unless response.code == "200"
  
  if json?(response.body)
    data = StringIO.new(response.body)
    parser = Yajl::Parser.new
    parser.parse(data)
  elsif  has_trailing_slash?(id) or (id == '')
    temp={}
    response.body.split("\n").each do |sub_attr|
      temp[sanitize_key(sub_attr)] = fetch_metadata("#{id}#{sub_attr}")
    end
    temp
  else
    response.body
  end
end
has_trailing_slash?(data) click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 92
def has_trailing_slash?(data)
  !! ( data =~ %{/$} )
end
http_client() click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 53
def http_client
  Net::HTTP.start(GCE_METADATA_ADDR).tap {|h| h.read_timeout = 600}
end
json?(data) click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 77
def json?(data)
  data = StringIO.new(data)
  parser = Yajl::Parser.new
  begin
    parser.parse(data)
    true
  rescue Yajl::ParseError
    false
  end
end
multiline?(data) click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 88
def multiline?(data)
  data.lines.to_a.size > 1
end
sanitize_key(key) click to toggle source
# File lib/ohai/mixin/gce_metadata.rb, line 96
def sanitize_key(key)
  key.gsub(/\-|\//, '_')
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.