Module: Ohai::Mixin::GCEMetadata
- Defined in:
- lib/ohai/mixin/gce_metadata.rb
Instance Method Summary collapse
- #fetch_metadata(id = "") ⇒ Object
-
#has_trailing_slash?(data) ⇒ Boolean
Is there a trailing /?.
-
#http_get(uri) ⇒ Object
fetch the meta content with a timeout and the required header.
-
#json?(data) ⇒ Boolean
Is the data JSON or not?.
- #sanitize_key(key) ⇒ Object
Instance Method Details
#fetch_metadata(id = "") ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 37 def (id = "") response = http_get("#{GCE_METADATA_URL}/#{id}") return nil unless response.code == "200" if json?(response.body) data = String(response.body) parser = FFI_Yajl::Parser.new parser.parse(data) elsif has_trailing_slash?(id) || (id == "") temp = {} response.body.split("\n").each do |sub_attr| temp[sanitize_key(sub_attr)] = ("#{id}#{sub_attr}") end temp else response.body end end |
#has_trailing_slash?(data) ⇒ Boolean
Returns is there a trailing /?.
73 74 75 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 73 def has_trailing_slash?(data) !! ( data =~ %r{/$} ) end |
#http_get(uri) ⇒ Object
fetch the meta content with a timeout and the required header
28 29 30 31 32 33 34 35 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 28 def http_get(uri) conn = Net::HTTP.start(GCE_METADATA_ADDR) conn.read_timeout = 6 conn.get(uri, { "Metadata-Flavor" => "Google", "User-Agent" => "chef-ohai/#{Ohai::VERSION}", }) end |
#json?(data) ⇒ Boolean
Returns is the data JSON or not?.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 59 def json?(data) data = String(data) parser = FFI_Yajl::Parser.new begin parser.parse(data) true rescue FFI_Yajl::ParseError false end end |
#sanitize_key(key) ⇒ Object
77 78 79 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 77 def sanitize_key(key) key.gsub(%r{\-|/}, "_") end |