summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-05-13 23:34:24 -0700
committersersut <serdar@opscode.com>2014-05-30 13:05:48 -0700
commit2f11b7f0806e2ac81e68cdd7fccff280e70bc6f3 (patch)
treeded26108a8ac5417fc48cb2fb916a3ed967300cd
parentb8238c9c6bb59b906554e8d9bad4169589e5071d (diff)
downloadchef-2f11b7f0806e2ac81e68cdd7fccff280e70bc6f3.tar.gz
Use wmi-lite gem dependency for wmi access
-rw-r--r--Gemfile3
-rw-r--r--chef-x86-mingw32.gemspec1
-rw-r--r--lib/chef/platform/query_helpers.rb4
-rw-r--r--lib/chef/provider/env/windows.rb2
-rw-r--r--lib/chef/win32/version.rb4
-rw-r--r--lib/chef/win32/wmi.rb86
-rw-r--r--spec/functional/win32/versions_spec.rb2
-rw-r--r--spec/support/platform_helpers.rb6
8 files changed, 12 insertions, 96 deletions
diff --git a/Gemfile b/Gemfile
index 3a8889bdc8..e3afa25b14 100644
--- a/Gemfile
+++ b/Gemfile
@@ -3,7 +3,8 @@ gemspec :name => "chef"
gem "activesupport", "< 4.0.0", :group => :compat_testing, :platform => "ruby"
-gem 'ohai', :git => 'https://github.com/opscode/ohai.git', :branch => 'adamed/wmi'
+gem 'ohai', :git => 'https://github.com/opscode/ohai.git', :branch => 'adamed/wmi-lite'
+gem 'wmi-lite', :git => 'https://github.com/opscode/wmi-lite.git', :branch => 'master'
group(:docgen) do
gem "yard"
diff --git a/chef-x86-mingw32.gemspec b/chef-x86-mingw32.gemspec
index 819c7eddbb..e82548d749 100644
--- a/chef-x86-mingw32.gemspec
+++ b/chef-x86-mingw32.gemspec
@@ -13,5 +13,6 @@ gemspec.add_dependency "win32-mutex", "0.4.1"
gemspec.add_dependency "win32-process", "0.7.3"
gemspec.add_dependency "win32-service", "0.8.2"
gemspec.add_dependency "win32-mmap", "0.4.0"
+gemspec.add_dependency "wmi-lite"
gemspec
diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb
index fd9873ecbe..7be5697bf7 100644
--- a/lib/chef/platform/query_helpers.rb
+++ b/lib/chef/platform/query_helpers.rb
@@ -31,14 +31,14 @@ class Chef
def windows_server_2003?
return false unless windows?
- require 'chef/win32/wmi'
+ require 'wmi-lite/wmi'
# CHEF-4888: Work around ruby #2618, expected to be fixed in Ruby 2.1.0
# https://github.com/ruby/ruby/commit/588504b20f5cc880ad51827b93e571e32446e5db
# https://github.com/ruby/ruby/commit/27ed294c7134c0de582007af3c915a635a6506cd
WIN32OLE.ole_initialize
- wmi = Chef::ReservedNames::Win32::WMI.new
+ wmi = WmiLite::Wmi.new
host = wmi.first_of('Win32_OperatingSystem')
is_server_2003 = (host['version'] && host['version'].start_with?("5.2"))
diff --git a/lib/chef/provider/env/windows.rb b/lib/chef/provider/env/windows.rb
index 5a2bc0ec4b..dba6ed562d 100644
--- a/lib/chef/provider/env/windows.rb
+++ b/lib/chef/provider/env/windows.rb
@@ -59,7 +59,7 @@ class Chef
end
def env_obj(key_name)
- wmi = Chef::ReservedNames::Win32::WMI.new
+ wmi = WmiLite::Wmi.new
environment_variables = wmi.instances_of('Win32_Environment')
new_result = find_env(environment_variables, key_name)
new_result.nil? ? nil : new_result[:wmi_object]
diff --git a/lib/chef/win32/version.rb b/lib/chef/win32/version.rb
index e2776bdd1b..53b5f9a0d6 100644
--- a/lib/chef/win32/version.rb
+++ b/lib/chef/win32/version.rb
@@ -18,7 +18,7 @@
require 'chef/win32/api'
require 'chef/win32/api/system'
-require 'chef/win32/wmi'
+require 'wmi-lite/wmi'
class Chef
module ReservedNames::Win32
@@ -122,7 +122,7 @@ class Chef
WIN32OLE.ole_initialize
- wmi = WMI.new
+ wmi = WmiInstance::Wmi.new
os_info = wmi.first_of('Win32_OperatingSystem')
os_version = os_info['version']
diff --git a/lib/chef/win32/wmi.rb b/lib/chef/win32/wmi.rb
deleted file mode 100644
index 9ed02c6aea..0000000000
--- a/lib/chef/win32/wmi.rb
+++ /dev/null
@@ -1,86 +0,0 @@
-#
-# 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 'win32ole'
-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}")
- first_result = nil
- query_result.each do | record |
- first_result = record
- break
- end
- first_result.nil? ? nil : wmi_result_to_snapshot(first_result)
- 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.invoke(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
diff --git a/spec/functional/win32/versions_spec.rb b/spec/functional/win32/versions_spec.rb
index e1a12a4dc6..7b314b394d 100644
--- a/spec/functional/win32/versions_spec.rb
+++ b/spec/functional/win32/versions_spec.rb
@@ -24,7 +24,7 @@ end
describe "Chef::ReservedNames::Win32::Version", :windows_only, :not_supported_on_win2k3 do
before do
- wmi = Chef::ReservedNames::Win32::WMI.new
+ wmi = WmiInstance::Wmi.new
host = wmi.first_of('Win32_OperatingSystem')
# Use WMI to determine current OS version.
diff --git a/spec/support/platform_helpers.rb b/spec/support/platform_helpers.rb
index 37ce5a6ca7..dc626a926f 100644
--- a/spec/support/platform_helpers.rb
+++ b/spec/support/platform_helpers.rb
@@ -27,11 +27,11 @@ def windows?
!!(RUBY_PLATFORM =~ /mswin|mingw|windows/)
end
-require 'chef/win32/wmi' if windows?
+require 'wmi-lite/wmi' if windows?
def windows_domain_joined?
return false unless windows?
- wmi = Chef::ReservedNames::Win32::WMI.new
+ wmi = WmiLite::Wmi.new
computer_system = wmi.first_of('Win32_ComputerSystem')
computer_system['partofdomain']
end
@@ -39,7 +39,7 @@ end
def windows_win2k3?
return false unless windows?
- wmi = Chef::ReservedNames::Win32::WMI.new
+ wmi = WmiLite::Wmi.new
host = wmi.first_of('Win32_OperatingSystem')
(host['version'] && host['version'].start_with?("5.2"))
end