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.
- #sanitize_key(key) ⇒ Object
Instance Method Details
#fetch_metadata(id = "") ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 41 def (id = "") url = "#{GCE_METADATA_URL}/#{id}" url = "#{url}?recursive=true" if url.end_with?("/") response = http_get(url) if response.code == "200" json_data = parse_json(response.body) if json_data.nil? logger.warn("Mixin GCEMetadata: Metadata response is NOT valid JSON for id='#{id}'") if 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 else json_data end else logger.warn("Mixin GCEMetadata: Received response code #{response.code} requesting metadata for id='#{id}'") nil end end |
#has_trailing_slash?(data) ⇒ Boolean
Returns is there a trailing /?.
70 71 72 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 70 def has_trailing_slash?(data) !!( data =~ %r{/$} ) end |
#http_get(uri) ⇒ Object
fetch the meta content with a timeout and the required header
32 33 34 35 36 37 38 39 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 32 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 |
#sanitize_key(key) ⇒ Object
74 75 76 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 74 def sanitize_key(key) key.gsub(%r{\-|/}, "_") end |