summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathen Harvey <nathenharvey@google.com>2018-11-19 13:46:51 -0500
committerNathen Harvey <nathenharvey@google.com>2018-11-20 08:56:00 -0500
commitad9bdc78a79ab16cd34a0e0da404d73c7a327b1e (patch)
tree1731bfdc40f5f84c5394f6cfdc49241d0eb2cced
parentcc31ee1324e8953cba8ad406a9a5faaab50018fc (diff)
downloadohai-ad9bdc78a79ab16cd34a0e0da404d73c7a327b1e.tar.gz
Adds more resilient GCE checking
Check the system information to determine if this system is running on Google Compute Engine. See also the Google Cloud Documentation for [detecting if you are running in Compute Engine](https://cloud.google.com/compute/docs/instances/managing-instances#dmi). Signed-off-by: Nathen Harvey <nathenharvey@google.com> Update release notes Signed-off-by: Nathen Harvey <nathenharvey@google.com>
-rw-r--r--RELEASE_NOTES.md4
-rw-r--r--lib/ohai/plugins/gce.rb50
2 files changed, 45 insertions, 9 deletions
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index bfd31e51..28b73bcc 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -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
diff --git a/lib/ohai/plugins/gce.rb b/lib/ohai/plugins/gce.rb
index 726456f2..b38cf7fc 100644
--- a/lib/ohai/plugins/gce.rb
+++ b/lib/ohai/plugins/gce.rb
@@ -23,13 +23,43 @@ Ohai.plugin(:GCE) do
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
@@ -38,7 +68,11 @@ Ohai.plugin(:GCE) do
# 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