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 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 41 def (id = "") response = http_get("#{GCE_METADATA_URL}/#{id}") 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 /?.
68 69 70 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 68 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
72 73 74 |
# File 'lib/ohai/mixin/gce_metadata.rb', line 72 def sanitize_key(key) key.gsub(%r{\-|/}, "_") end |