Class: Google::Cloud::Env::ComputeSMBIOS

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/env/compute_smbios.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComputeSMBIOS

Create an SMBIOS access object



35
36
37
38
# 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

Instance Attribute Details

#override_product_namenil, String

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

Returns:

  • (nil, String)


92
93
94
# File 'lib/google/cloud/env/compute_smbios.rb', line 92

def override_product_name
  @override_product_name
end

Instance Method Details

#google_compute?true, false

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.

Returns:

  • (true, false)


82
83
84
# File 'lib/google/cloud/env/compute_smbios.rb', line 82

def google_compute?
  product_name.include? "Google"
end

#product_nameString

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.

Returns:

  • (String)

    Product name, or the empty string if not found.



50
51
52
# File 'lib/google/cloud/env/compute_smbios.rb', line 50

def product_name
  @override_product_name || @product_name_cache.get.first
end

#product_name_sourceSymbol

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.

Returns:

  • (Symbol)

    The source



68
69
70
# 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) ⇒ Object

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

Parameters:

  • override_name (nil, String)


100
101
102
103
104
105
106
107
108
# 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