Skip to content

Commit

Permalink
Merge pull request #1292 from nathenharvey/gce/dmi
Browse files Browse the repository at this point in the history
Adds more resilient GCE checking
  • Loading branch information
tas50 authored Nov 20, 2018
2 parents cc31ee1 + ad9bdc7 commit b40f669
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
4 changes: 3 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ This will be updated as the development of Ohai 15 progresses.

## Cloud Plugin Improvements

The Google Compute Engine (GCE) plugin now identifies `chef-ohai` as the User-Agent making requests to the Google Cloud metadata server (metadata.google.internal).
Use system information (e.g., DMI/SMBIOS or System Information (msinfo)) to determine whether a system is running on Google Compute Engine (GCE). In the absence of an [Ohai hint](https://docs.chef.io/ohai.html#hints), system information will be consulted to make the determination.

The GCE plugin now identifies `chef-ohai` as the User-Agent making requests to the Google Cloud metadata server (metadata.google.internal).

# Ohai Release Notes 14.6

Expand Down
50 changes: 42 additions & 8 deletions lib/ohai/plugins/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,43 @@

provides "gce"

# Checks for gce metadata server
#
# === Return
# true:: If gce metadata server found
# false:: Otherwise
def has_gce_metadata?
can_socket_connect?(Ohai::Mixin::GCEMetadata::GCE_METADATA_ADDR, 80)
# look for GCE string in dmi vendor bios data within the sys tree.
# this works even if the system lacks dmidecode use by the Dmi plugin
# @return [Boolean] do we have Google Compute Engine DMI data?
def has_gce_dmi?
if file_val_if_exists("/sys/class/dmi/id/product_name") =~ /Google Compute Engine/
logger.trace("Plugin GCE: has_gce_dmi? == true")
true
else
logger.trace("Plugin GCE: has_gce_dmi? == false")
false
end
end

# return the contents of a file if the file exists
# @param path[String] abs path to the file
# @return [String] contents of the file if it exists
def file_val_if_exists(path)
if ::File.exist?(path)
::File.read(path)
end
end

# looks at the Manufacturer and Model WMI values to see if they starts with Google.
# @return [Boolean] Are the manufacturer and model Google?
def has_gce_system_info?
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
require "wmi-lite/wmi"
wmi = WmiLite::Wmi.new
computer_system = wmi.first_of("Win32_ComputerSystem")
if computer_system["Manufacturer"] =~ /^Google/ && computer_system["Model"] =~ /^Google/
logger.trace("Plugin GCE: has_gce_system_info? == true")
return true
end
else
logger.trace("Plugin GCE: has_gce_system_info? == false")
false
end
end

# Identifies gce
Expand All @@ -38,7 +68,11 @@ def has_gce_metadata?
# true:: If gce can be identified
# false:: Otherwise
def looks_like_gce?
hint?("gce") || has_gce_metadata?
return true if hint?("gce")

if has_gce_dmi? || has_gce_system_info?
return true if can_socket_connect?(Ohai::Mixin::GCEMetadata::GCE_METADATA_ADDR, 80)
end
end

collect_data do
Expand Down

0 comments on commit b40f669

Please sign in to comment.