diff options
author | Thom May <thom@may.lt> | 2018-02-28 09:13:02 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-28 09:13:02 +0000 |
commit | 32feaa2790cf7496cd36fd59ed770d403703f04a (patch) | |
tree | 5abe86aed703a4e76c4c755694f4f12d765251c5 | |
parent | 7dda775427133867ceb455ccd06fe6fb0373212a (diff) | |
parent | 53adda5b4f32aeec2593da7a926aa783b0259afe (diff) | |
download | ohai-32feaa2790cf7496cd36fd59ed770d403703f04a.tar.gz |
Merge pull request #1142 from chef/windows_versions
Add additional information to the kernel plugin on Windows
-rw-r--r-- | lib/ohai/plugins/kernel.rb | 77 | ||||
-rw-r--r-- | spec/unit/plugins/kernel_spec.rb | 52 | ||||
-rw-r--r-- | spec/unit/plugins/windows/kernel_spec.rb | 76 |
3 files changed, 147 insertions, 58 deletions
diff --git a/lib/ohai/plugins/kernel.rb b/lib/ohai/plugins/kernel.rb index ad9242e6..9da36820 100644 --- a/lib/ohai/plugins/kernel.rb +++ b/lib/ohai/plugins/kernel.rb @@ -26,6 +26,7 @@ Ohai.plugin(:Kernel) do provides "kernel", "kernel/modules" # common initial kernel attribute values + # @return [Mash] basic kernel properties from uname def init_kernel kernel Mash.new [["uname -s", :name], ["uname -r", :release], @@ -38,6 +39,7 @@ Ohai.plugin(:Kernel) do end # common *bsd code for collecting modules data + # @return [Mash] def bsd_modules(path) modules = Mash.new so = shell_out("#{Ohai.abs_path(path)}") @@ -50,15 +52,56 @@ Ohai.plugin(:Kernel) do modules end - # windows - def machine_lookup(sys_type) + # given the OperatingSystemSKU value from WMI's Win32_OperatingSystem class + # https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx + # return if we're on a Server Core installation + # @param [String] sku OperatingSystemSKU value from Win32_OperatingSystem + # @return [boolean] + def server_core?(sku) + return true if [ + 12, # Server Datacenter Core + 39, # Server Datacenter without Hyper-V Core + 14, # Server Enterprise Core + 41, # Server Enterprise without Hyper-V Core + 13, # Server Standard Core + 40, # Server Standard without Hyper-V Core + 63, # Small Business Server Premium Core + 53, # Server Solutions Premium Core + 46, # Storage Server Enterprise Core + 43, # Storage Server Express Core + 44, # Storage Server Standard Core + 45, # Storage Server Workgroup Core + 29 # Web Server Core + ].include?(sku) + false + end + + # given the SystemType value from WMI's Win32_ComputerSystem class + # https://msdn.microsoft.com/en-us/library/aa394102(v=vs.85).aspx + # return the architecture type + # @param [String] sys_type SystemType value from Win32_ComputerSystem + # @return [String] x86_64 or i386 + def arch_lookup(sys_type) return "x86_64" if sys_type == "x64-based PC" return "i386" if sys_type == "X86-based PC" sys_type end - # windows - def os_lookup(sys_type) + # given the ProductType value from WMI's Win32_OperatingSystem class + # https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx + # return either workstation or server + # @param [Integer] type ProductType value from Win32_OperatingSystem + # @return [String] Workstation or Server + def product_type_decode(type) + return "Workstation" if type == 1 + "Server" + end + + # decode the OSType field from WMI Win32_OperatingSystem class + # https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx + # @param [Integer] sys_type OSType value from Win32_OperatingSystem + # @return [String] the human consumable OS type value + def os_type_decode(sys_type) case sys_type when 18 then "WINNT" # most likely so first when 0 then "Unknown" @@ -72,6 +115,25 @@ Ohai.plugin(:Kernel) do end end + # decode the PCSystemType field from WMI Win32_OperatingSystem class + # https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx + # @param [Integer] type the integer value from PCSystemType + # @return [String] the human consumable OS type value + def pc_system_type_decode(type) + case type + when 4 then "Enterprise Server" # most likely so first + when 0 then "Unspecified" + when 1 then "Desktop" + when 2 then "Mobile" + when 3 then "Workstation" + when 5 then "SOHO Server" + when 6 then "Appliance PC" + when 7 then "Performance Server" + when 8 then "Maximum" + else nil + end + end + collect_data(:default) do kernel init_kernel end @@ -185,7 +247,9 @@ Ohai.plugin(:Kernel) do kernel[:name] = "#{kernel[:os_info][:caption]}" kernel[:release] = "#{kernel[:os_info][:version]}" kernel[:version] = "#{kernel[:os_info][:version]} #{kernel[:os_info][:csd_version]} Build #{kernel[:os_info][:build_number]}" - kernel[:os] = os_lookup(kernel[:os_info][:os_type]) || languages[:ruby][:host_os] + kernel[:os] = os_type_decode(kernel[:os_info][:os_type]) || languages[:ruby][:host_os] + kernel[:product_type] = product_type_decode(kernel[:os_info][:product_type]) + kernel[:server_core] = server_core?(kernel[:os_info][:operating_system_sku]) kernel[:cs_info] = Mash.new host = wmi.first_of("Win32_ComputerSystem") @@ -194,6 +258,7 @@ Ohai.plugin(:Kernel) do kernel[:cs_info][p.name.wmi_underscore.to_sym] = host[p.name.downcase] end - kernel[:machine] = machine_lookup("#{kernel[:cs_info][:system_type]}") + kernel[:machine] = arch_lookup("#{kernel[:cs_info][:system_type]}") + kernel[:system_type] = pc_system_type_decode(kernel[:cs_info][:pc_system_type]) end end diff --git a/spec/unit/plugins/kernel_spec.rb b/spec/unit/plugins/kernel_spec.rb index ed78d866..8e25975e 100644 --- a/spec/unit/plugins/kernel_spec.rb +++ b/spec/unit/plugins/kernel_spec.rb @@ -35,56 +35,4 @@ describe Ohai::System, "plugin kernel" do it_should_check_from_mash("kernel", "version", "uname -v", [0, "Darwin Kernel Version 9.5.0: Wed Sep 3 11:29:43 PDT 2008; root:xnu-1228.7.58~1\/RELEASE_I386\n", ""]) it_should_check_from_mash("kernel", "machine", "uname -m", [0, "i386\n", ""]) it_should_check_from_mash("kernel", "processor", "uname -p", [0, "i386\n", ""]) - - describe "when running on windows", :windows_only do - before do - require "wmi-lite/wmi" - - @ohai_system = Ohai::System.new - @plugin = get_plugin("kernel", @ohai_system) - - # Mock a Win32_OperatingSystem OLE32 WMI object - caption = double("WIN32OLE", :name => "Caption") - version = double("WIN32OLE", :name => "Version") - build_number = double("WIN32OLE", :name => "BuildNumber") - csd_version = double("WIN32OLE", :name => "CsdVersion") - os_type = double("WIN32OLE", :name => "OsType") - os_properties = [ caption, version, build_number, csd_version, os_type ] - - os = double( "WIN32OLE", - :properties_ => os_properties) - - allow(os).to receive(:invoke).with(build_number.name).and_return("7601") - allow(os).to receive(:invoke).with(csd_version.name).and_return("Service Pack 1") - allow(os).to receive(:invoke).with(os_type.name).and_return(18) - allow(os).to receive(:invoke).with(caption.name).and_return("Microsoft Windows 7 Ultimate") - allow(os).to receive(:invoke).with(version.name).and_return("6.1.7601") - - os_wmi = WmiLite::Wmi::Instance.new(os) - - expect_any_instance_of(WmiLite::Wmi).to receive(:first_of).with("Win32_OperatingSystem").and_return(os_wmi) - - # Mock a Win32_ComputerSystem OLE32 WMI object - x64_system_type = "x64-based PC" - - cs = double("WIN32OLE", - :properties_ => [ double("WIN32OLE", :name => "SystemType") ]) - - allow(cs).to receive(:invoke).with("SystemType").and_return(x64_system_type) - - cs_wmi = WmiLite::Wmi::Instance.new(cs) - - expect_any_instance_of(WmiLite::Wmi).to receive(:first_of).with("Win32_ComputerSystem").and_return(cs_wmi) - - @plugin.run - end - it "should set the correct system information" do - expect(@ohai_system.data[:kernel][:name]).to eq("Microsoft Windows 7 Ultimate") - expect(@ohai_system.data[:kernel][:release]).to eq("6.1.7601") - expect(@ohai_system.data[:kernel][:version]).to eq("6.1.7601 Service Pack 1 Build 7601") - expect(@ohai_system.data[:kernel][:os]).to eq("WINNT") - expect(@ohai_system.data[:kernel][:machine]).to eq("x86_64") - end - end - end diff --git a/spec/unit/plugins/windows/kernel_spec.rb b/spec/unit/plugins/windows/kernel_spec.rb new file mode 100644 index 00000000..377034cb --- /dev/null +++ b/spec/unit/plugins/windows/kernel_spec.rb @@ -0,0 +1,76 @@ +# +# Copyright:: Copyright (c) 2018 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../../../spec_helper.rb" + +describe Ohai::System, "Windows kernel plugin", :windows_only do + let(:plugin) { get_plugin("kernel") } + + before do + require "wmi-lite/wmi" + + # Mock a Win32_OperatingSystem OLE32 WMI object + caption = double("WIN32OLE", :name => "Caption") + version = double("WIN32OLE", :name => "Version") + build_number = double("WIN32OLE", :name => "BuildNumber") + csd_version = double("WIN32OLE", :name => "CsdVersion") + os_type = double("WIN32OLE", :name => "OsType") + product_type = double("WIN32OLE", :name => "ProductType") + operating_system_sku = double("WIN32OLE", :name => "OperatingSystemSKU") + os_properties = [ caption, version, build_number, csd_version, os_type, product_type, operating_system_sku ] + + os = double( "WIN32OLE", + :properties_ => os_properties) + + allow(os).to receive(:invoke).with(build_number.name).and_return("7601") + allow(os).to receive(:invoke).with(csd_version.name).and_return("Service Pack 1") + allow(os).to receive(:invoke).with(os_type.name).and_return(18) + allow(os).to receive(:invoke).with(caption.name).and_return("Microsoft Windows 7 Ultimate") + allow(os).to receive(:invoke).with(version.name).and_return("6.1.7601") + allow(os).to receive(:invoke).with(product_type.name).and_return(1) + allow(os).to receive(:invoke).with(operating_system_sku.name).and_return(48) + + os_wmi = WmiLite::Wmi::Instance.new(os) + expect_any_instance_of(WmiLite::Wmi).to receive(:first_of).with("Win32_OperatingSystem").and_return(os_wmi) + + system_type = double("WIN32OLE", :name => "SystemType") + pc_system_type = double("WIN32OLE", :name => "PCSystemType") + cs_properties = [ system_type, pc_system_type ] + + cs = double("WIN32OLE", + :properties_ => cs_properties) + + allow(cs).to receive(:invoke).with(system_type.name).and_return("x64-based PC") + allow(cs).to receive(:invoke).with(pc_system_type.name).and_return(2) + + cs_wmi = WmiLite::Wmi::Instance.new(cs) + expect_any_instance_of(WmiLite::Wmi).to receive(:first_of).with("Win32_ComputerSystem").and_return(cs_wmi) + + plugin.run + end + + it "should set the correct system information" do + expect(plugin[:kernel][:name]).to eq("Microsoft Windows 7 Ultimate") + expect(plugin[:kernel][:release]).to eq("6.1.7601") + expect(plugin[:kernel][:version]).to eq("6.1.7601 Service Pack 1 Build 7601") + expect(plugin[:kernel][:os]).to eq("WINNT") + expect(plugin[:kernel][:machine]).to eq("x86_64") + expect(plugin[:kernel][:system_type]).to eq("Mobile") + expect(plugin[:kernel][:product_type]).to eq("Workstation") + expect(plugin[:kernel][:server_core]).to eq(false) + end +end |