diff options
author | Adam Edwards <adamed@opscode.com> | 2014-05-02 09:22:40 -0700 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2014-05-30 13:05:47 -0700 |
commit | 771662f8edc7f8251379f3207399bbdffa2248cc (patch) | |
tree | 8467ad262dd7618b22daa4f8b3d88e3359bf550c /lib/chef/win32 | |
parent | a6b19c08da6365e883d38d0ed202068df16301ab (diff) | |
download | chef-771662f8edc7f8251379f3207399bbdffa2248cc.tar.gz |
Re-implement ruby-wmi functionality
Diffstat (limited to 'lib/chef/win32')
-rw-r--r-- | lib/chef/win32/registry.rb | 1 | ||||
-rw-r--r-- | lib/chef/win32/version.rb | 7 | ||||
-rw-r--r-- | lib/chef/win32/wmi.rb | 80 |
3 files changed, 84 insertions, 4 deletions
diff --git a/lib/chef/win32/registry.rb b/lib/chef/win32/registry.rb index 1e8f53b464..18f12d26b8 100644 --- a/lib/chef/win32/registry.rb +++ b/lib/chef/win32/registry.rb @@ -20,7 +20,6 @@ require 'chef/reserved_names' if RUBY_PLATFORM =~ /mswin|mingw32|windows/ require 'win32/registry' - require 'ruby-wmi' require 'win32/api' end diff --git a/lib/chef/win32/version.rb b/lib/chef/win32/version.rb index 7f5fcceead..e2776bdd1b 100644 --- a/lib/chef/win32/version.rb +++ b/lib/chef/win32/version.rb @@ -18,6 +18,7 @@ require 'chef/win32/api' require 'chef/win32/api/system' +require 'chef/win32/wmi' class Chef module ReservedNames::Win32 @@ -114,7 +115,6 @@ class Chef # version numbers on Windows Server 2012 R2 and Windows 8.1 -- # WMI always returns the truth. See article at # http://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx - require 'ruby-wmi' # CHEF-4888: Work around ruby #2618, expected to be fixed in Ruby 2.1.0 # https://github.com/ruby/ruby/commit/588504b20f5cc880ad51827b93e571e32446e5db @@ -122,8 +122,9 @@ class Chef WIN32OLE.ole_initialize - os_info = WMI::Win32_OperatingSystem.find(:first) - os_version = os_info.send('Version') + wmi = WMI.new + os_info = wmi.first_of('Win32_OperatingSystem') + os_version = os_info['version'] WIN32OLE.ole_uninitialize diff --git a/lib/chef/win32/wmi.rb b/lib/chef/win32/wmi.rb new file mode 100644 index 0000000000..c3d6cf7d3b --- /dev/null +++ b/lib/chef/win32/wmi.rb @@ -0,0 +1,80 @@ +# +# Author:: Adam Edwards (<adamed@getchef.com>) +# Copyright:: Copyright 2014 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 'chef/reserved_names' + +class Chef + module ReservedNames::Win32 + + class WMI + def initialize(namespace = nil, results_as_mash = false) + @connection = new_connection(namespace.nil? ? 'root/cimv2' : namespace) + @results_as_mash = results_as_mash + end + + def query(wql_query) + results = start_query(wql_query) + + result_set = [] + + results.each do | result | + result_set.push(wmi_result_to_snapshot(result)) + end + + result_set + end + + def instances_of(wmi_class) + query("select * from #{wmi_class}") + end + + def first_of(wmi_class) + query_result = start_query("select * from #{wmi_class}") + wmi_result_to_snapshot(query_result.first) + end + + private + + def start_query(wql_query) + @connection.ExecQuery(wql_query) + end + + def new_connection(namespace) + locator = WIN32OLE.new("WbemScripting.SWbemLocator") + locator.ConnectServer('.', namespace) + end + + def wmi_result_to_hash(wmi_object) + property_map = {} + wmi_object.properties_.each do |property| + property_map[property.name.downcase] = wmi_object.send(property.name) + end + + property_map[:wmi_object] = wmi_object + + property_map + end + + def wmi_result_to_snapshot(wmi_object) + snapshot = wmi_result_to_hash(wmi_object) + @results_as_mash ? Mash.new(snapshot) : snapshot + end + end + + end +end |