class Google::Cloud::Env::ComputeSMBIOS

Access to the SMBIOS information needed to determine if this Ruby process is running on a Google compute platform.

This information lives at a file system path on Linux, but in the Registry on Windows.

You can provide an override to “mock out” the behavior of this object.

Constants

LINUX_FILEPATH

@private The Linux file path

WINDOWS_KEYNAME

@private The Windows registry key name

WINDOWS_KEYPATH

@private The Windows registry key path

Attributes

override_product_name[RW]

The current override value for the product name, either a string value, or nil to disable mocking.

@return [nil,String]

Public Class Methods

new() click to toggle source

Create an SMBIOS access object

# File lib/google/cloud/env/compute_smbios.rb, line 35
def initialize
  @product_name_cache = LazyValue.new { load_product_name }
  @override_product_name = nil
end

Public Instance Methods

google_compute?() click to toggle source

Determine whether the SMBIOS state suggests that we are running on a Google compute platform.

This method may read the file system (on Linux) or registry (on Windows) the first time it is called, but it will cache the result for subsequent calls.

@return [true,false]

# File lib/google/cloud/env/compute_smbios.rb, line 82
def google_compute?
  product_name.include? "Google"
end
product_name() click to toggle source

Read the product name. On a Google compute platform, this should include the word “Google”.

This method may read the file system (on Linux) or registry (on Windows) the first time it is called, but it will cache the result for subsequent calls.

@return [String] Product name, or the empty string if not found.

# File lib/google/cloud/env/compute_smbios.rb, line 50
def product_name
  @override_product_name || @product_name_cache.get.first
end
product_name_source() click to toggle source

The source of the product name data. Will be one of the following:

  • ‘:linux` - The data comes from the Linux SMBIOS under /sys

  • ‘:windows` - The data comes from the Windows Registry

  • ‘:error` - The data could not be obtained

  • ‘:override` - The data comes from an override

This method may read the file system (on Linux) or registry (on Windows) the first time it is called, but it will cache the result for subsequent calls.

@return [Symbol] The source

# File lib/google/cloud/env/compute_smbios.rb, line 68
def product_name_source
  @override_product_name ? :override : @product_name_cache.get.last
end
with_override_product_name(override_name) { || ... } click to toggle source

Run the given block with the product name mock modified. This is generally used for debugging/testing/mocking.

@param override_name [nil,String]

# File lib/google/cloud/env/compute_smbios.rb, line 100
def with_override_product_name override_name
  old_override = @override_product_name
  begin
    @override_product_name = override_name
    yield
  ensure
    @override_product_name = old_override
  end
end

Private Instance Methods

load_product_name() click to toggle source
# File lib/google/cloud/env/compute_smbios.rb, line 121
def load_product_name
  require "win32/registry"
  Win32::Registry::HKEY_LOCAL_MACHINE.open WINDOWS_KEYPATH do |reg|
    return [reg[WINDOWS_KEYNAME].to_s, :windows]
  end
rescue LoadError
  begin
    File.open LINUX_FILEPATH do |file|
      return [file.readline(chomp: true), :linux]
    end
  rescue IOError, SystemCallError
    ["", :error]
  end
end