summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-05-02 09:22:40 -0700
committersersut <serdar@opscode.com>2014-05-30 13:05:47 -0700
commit771662f8edc7f8251379f3207399bbdffa2248cc (patch)
tree8467ad262dd7618b22daa4f8b3d88e3359bf550c
parenta6b19c08da6365e883d38d0ed202068df16301ab (diff)
downloadchef-771662f8edc7f8251379f3207399bbdffa2248cc.tar.gz
Re-implement ruby-wmi functionality
-rw-r--r--lib/chef/platform/query_helpers.rb8
-rw-r--r--lib/chef/provider/env/windows.rb15
-rw-r--r--lib/chef/win32/registry.rb1
-rw-r--r--lib/chef/win32/version.rb7
-rw-r--r--lib/chef/win32/wmi.rb80
-rw-r--r--spec/functional/win32/versions_spec.rb8
-rw-r--r--spec/support/platform_helpers.rb11
7 files changed, 112 insertions, 18 deletions
diff --git a/lib/chef/platform/query_helpers.rb b/lib/chef/platform/query_helpers.rb
index f9f7af0343..a990535069 100644
--- a/lib/chef/platform/query_helpers.rb
+++ b/lib/chef/platform/query_helpers.rb
@@ -16,6 +16,8 @@
# limitations under the License.
#
+require 'chef/win32/wmi'
+
class Chef
class Platform
@@ -30,15 +32,15 @@ class Chef
def windows_server_2003?
return false unless windows?
- 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
# https://github.com/ruby/ruby/commit/27ed294c7134c0de582007af3c915a635a6506cd
WIN32OLE.ole_initialize
- host = WMI::Win32_OperatingSystem.find(:first)
- is_server_2003 = (host.version && host.version.start_with?("5.2"))
+ wmi = Chef::ReservedNames::Win32::WMI.new
+ host = wmi.first_of('Win32_OperatingSystem')
+ is_server_2003 = (host['version'] && host['version'].start_with?("5.2"))
WIN32OLE.ole_uninitialize
diff --git a/lib/chef/provider/env/windows.rb b/lib/chef/provider/env/windows.rb
index bf728b1fae..5a2bc0ec4b 100644
--- a/lib/chef/provider/env/windows.rb
+++ b/lib/chef/provider/env/windows.rb
@@ -17,7 +17,6 @@
#
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
- require 'ruby-wmi'
require 'Win32API'
end
@@ -51,9 +50,19 @@ class Chef
return obj ? obj.variablevalue : nil
end
+ def find_env(environment_variables, key_name)
+ environment_variables.find do | environment_variable |
+ puts "Class: #{environment_variable.class}"
+ puts "#{environment_variable.inspect.to_s}"
+ environment_variable['name'].downcase == key_name
+ end
+ end
+
def env_obj(key_name)
- WMI::Win32_Environment.find(:first,
- :conditions => { :name => key_name })
+ wmi = Chef::ReservedNames::Win32::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]
end
#see: http://msdn.microsoft.com/en-us/library/ms682653%28VS.85%29.aspx
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
diff --git a/spec/functional/win32/versions_spec.rb b/spec/functional/win32/versions_spec.rb
index b983b711da..e1a12a4dc6 100644
--- a/spec/functional/win32/versions_spec.rb
+++ b/spec/functional/win32/versions_spec.rb
@@ -19,13 +19,13 @@
require 'spec_helper'
if Chef::Platform.windows?
require 'chef/win32/version'
- require 'ruby-wmi'
end
describe "Chef::ReservedNames::Win32::Version", :windows_only, :not_supported_on_win2k3 do
before do
- host = WMI::Win32_OperatingSystem.find(:first)
+ wmi = Chef::ReservedNames::Win32::WMI.new
+ host = wmi.first_of('Win32_OperatingSystem')
# Use WMI to determine current OS version.
# On Win2k8R2 and later, we can dynamically obtain marketing
@@ -44,7 +44,7 @@ describe "Chef::ReservedNames::Win32::Version", :windows_only, :not_supported_on
# The name from WMI is actually what we want in Win2k8R2+.
# So this expectation sould continue to hold without modification
# as new versions of Windows are released.
- @current_os_version = host.caption
+ @current_os_version = host['caption']
end
@version = Chef::ReservedNames::Win32::Version.new
@@ -98,7 +98,7 @@ describe "Chef::ReservedNames::Win32::Version", :windows_only, :not_supported_on
def is_windows_server_2008?(wmi_host)
is_win2k8 = false
- os_version = wmi_host.send('Version')
+ os_version = wmi_host['version']
# The operating system version is a string in the following form
# that can be split into components based on the '.' delimiter:
diff --git a/spec/support/platform_helpers.rb b/spec/support/platform_helpers.rb
index 884893865f..37ce5a6ca7 100644
--- a/spec/support/platform_helpers.rb
+++ b/spec/support/platform_helpers.rb
@@ -27,18 +27,21 @@ def windows?
!!(RUBY_PLATFORM =~ /mswin|mingw|windows/)
end
-require 'ruby-wmi' if windows?
+require 'chef/win32/wmi' if windows?
def windows_domain_joined?
return false unless windows?
- WMI::Win32_ComputerSystem.find(:first).PartOfDomain
+ wmi = Chef::ReservedNames::Win32::WMI.new
+ computer_system = wmi.first_of('Win32_ComputerSystem')
+ computer_system['partofdomain']
end
def windows_win2k3?
return false unless windows?
- host = WMI::Win32_OperatingSystem.find(:first)
- (host.version && host.version.start_with?("5.2"))
+ wmi = Chef::ReservedNames::Win32::WMI.new
+ host = wmi.first_of('Win32_OperatingSystem')
+ (host['version'] && host['version'].start_with?("5.2"))
end
def mac_osx_106?