[Pkg-puppet-devel] [facter] 42/61: (#16668) Backport WMI changes to per-interface facts

Stig Sandbeck Mathisen ssm at debian.org
Mon Nov 4 15:02:01 UTC 2013


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 3ade301829ee8121b86d5608fea91149f5a38f60
Author: Josh Cooper <josh at puppetlabs.com>
Date:   Mon Jul 29 15:52:46 2013 -0700

    (#16668) Backport WMI changes to per-interface facts
    
    Previously, facter was using WMI for the global interface facts,
    e.g. ipaddress, but still using netsh for the per-interface facts.
    
    This commit updates the per-interface facts to use the WMI
    methods. This commit is broken out in order to make merging up to
    master easier, since the code has been drastically modified there.
---
 lib/facter/util/ip.rb         |   32 ++++++++++++++------------------
 lib/facter/util/ip/windows.rb |    2 +-
 spec/unit/util/ip_spec.rb     |   30 +++++++++---------------------
 3 files changed, 24 insertions(+), 40 deletions(-)

diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb
index cb035e3..c15a6a1 100644
--- a/lib/facter/util/ip.rb
+++ b/lib/facter/util/ip.rb
@@ -31,11 +31,7 @@ module Facter::Util::IP
       :macaddress => /(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/,
       :netmask  => /.*\s+netmask (\S+)\s.*/
     },
-    :windows => {
-      :ipaddress  => /\s+IP Address:\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
-      :ipaddress6 => /Address ((?![fe80|::1])(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/,
-      :netmask  => /\s+Subnet Prefix:\s+\S+\s+\(mask ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\)/
-    }
+    :windows => {}
   }
 
   # Convert an interface name into purely alphanumeric characters.
@@ -61,11 +57,12 @@ module Facter::Util::IP
   end
 
   def self.get_interfaces
-    return [] unless output = Facter::Util::IP.get_all_interface_output()
+    if Facter.value(:kernel) == 'windows'
+      require 'facter/util/ip/windows'
+      return Facter::Util::IP::Windows.interfaces
+    end
 
-    # windows interface names contain spaces and are quoted and can appear multiple
-    # times as ipv4 and ipv6
-    return output.scan(/\s* connected\s*(\S.*)/).flatten.uniq if Facter.value(:kernel) == 'windows'
+    return [] unless output = Facter::Util::IP.get_all_interface_output()
 
     # Our regex appears to be stupid, in that it leaves colons sitting
     # at the end of interfaces.  So, we have to trim those trailing
@@ -89,9 +86,6 @@ module Facter::Util::IP
         output.sub!(/^[^\n]*\n/, "")            # delete the header line.
         output
       end
-    when 'windows'
-      output = %x|#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ip show interface|
-      output += %x|#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ipv6 show interface|
     end
     output
   end
@@ -172,12 +166,9 @@ module Facter::Util::IP
   def self.get_output_for_interface_and_label(interface, label)
     return get_single_interface_output(interface) unless Facter.value(:kernel) == 'windows'
 
-    if label == 'ipaddress6'
-      output = %x|#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ipv6 show address \"#{interface}\"|
-    else
-      output = %x|#{ENV['SYSTEMROOT']}/system32/netsh.exe interface ip show address \"#{interface}\"|
-    end
-    output
+    require 'facter/util/ip/windows'
+    output = Facter::Util::IP::Windows.value_for_interface_and_label(interface, label)
+    output ? output : ""
   end
 
   def self.get_bonding_master(interface)
@@ -220,6 +211,11 @@ module Facter::Util::IP
   # @return [String] representing the requested value.  An empty array is
   # returned if the kernel is not supported by the REGEX_MAP constant.
   def self.get_interface_value(interface, label)
+    if Facter.value(:kernel) == 'windows'
+      require 'facter/util/ip/windows'
+      return Facter::Util::IP::Windows.value_for_interface_and_label(interface, label)
+    end
+
     tmp1 = []
 
     kernel = Facter.value(:kernel).downcase.to_sym
diff --git a/lib/facter/util/ip/windows.rb b/lib/facter/util/ip/windows.rb
index 805a2bb..30c4c05 100644
--- a/lib/facter/util/ip/windows.rb
+++ b/lib/facter/util/ip/windows.rb
@@ -1,6 +1,7 @@
 # encoding: UTF-8
 
 require 'facter/util/wmi'
+require 'facter/util/ip'
 
 class Facter::Util::IP::Windows
   # The WMI query used to return ip information
@@ -157,7 +158,6 @@ class Facter::Util::IP::Windows
       require 'facter/util/registry'
       bindings = {}
 
-
       Facter::Util::Registry.hklm_read(@key, 'Bind').each_with_index do |entry, index|
         match_data = entry.match(/\\Device\\(\{.*\})/)
         unless match_data.nil?
diff --git a/spec/unit/util/ip_spec.rb b/spec/unit/util/ip_spec.rb
index 3d45c82..67772b4 100755
--- a/spec/unit/util/ip_spec.rb
+++ b/spec/unit/util/ip_spec.rb
@@ -52,8 +52,8 @@ describe Facter::Util::IP do
 
   it "should return a list of only connected interfaces on Windows" do
     Facter.fact(:kernel).stubs(:value).returns("windows")
-    windows_netsh = my_fixture_read("windows_netsh_all_interfaces")
-    Facter::Util::IP.stubs(:get_all_interface_output).returns(windows_netsh)
+
+    Facter::Util::IP::Windows.expects(:interfaces).returns(["Loopback Pseudo-Interface 1", "Local Area Connection", "Teredo Tunneling Pseudo-Interface"])
     Facter::Util::IP.get_interfaces().should == ["Loopback Pseudo-Interface 1", "Local Area Connection", "Teredo Tunneling Pseudo-Interface"]
   end
 
@@ -373,41 +373,29 @@ describe Facter::Util::IP do
   end
 
   describe "on Windows" do
+    require 'facter/util/ip/windows'
+
     before :each do
       Facter.stubs(:value).with(:kernel).returns("windows")
     end
 
     it "should return ipaddress information" do
-      windows_netsh = my_fixture_read("windows_netsh_single_interface")
-
-      Facter::Util::IP.expects(:get_output_for_interface_and_label).with("Local Area Connection", "ipaddress").returns(windows_netsh)
+      Facter::Util::IP::Windows.expects(:value_for_interface_and_label).with("Local Area Connection", "ipaddress").returns('172.16.138.216')
 
       Facter::Util::IP.get_interface_value("Local Area Connection", "ipaddress").should == "172.16.138.216"
     end
 
-    it "should return a human readable netmask" do
-      windows_netsh = my_fixture_read("windows_netsh_single_interface")
-
-      Facter::Util::IP.expects(:get_output_for_interface_and_label).with("Local Area Connection", "netmask").returns(windows_netsh)
-
-      Facter::Util::IP.get_interface_value("Local Area Connection", "netmask").should == "255.255.255.0"
-    end
-
     it "should return network information" do
-      windows_netsh = my_fixture_read("windows_netsh_single_interface")
-
-      Facter::Util::IP.stubs(:get_output_for_interface_and_label).with("Local Area Connection", "ipaddress").returns(windows_netsh)
-      Facter::Util::IP.stubs(:get_output_for_interface_and_label).with("Local Area Connection", "netmask").returns(windows_netsh)
+      Facter::Util::IP::Windows.expects(:value_for_interface_and_label).with("Local Area Connection", "ipaddress").returns('172.16.138.216')
+      Facter::Util::IP::Windows.expects(:value_for_interface_and_label).with("Local Area Connection", "netmask").returns('255.255.255.0')
 
       Facter::Util::IP.get_network_value("Local Area Connection").should == "172.16.138.0"
     end
 
     it "should return ipaddress6 information" do
-      windows_netsh = my_fixture_read("windows_netsh_single_interface6")
-
-      Facter::Util::IP.expects(:get_output_for_interface_and_label).with("Teredo Tunneling Pseudo-Interface", "ipaddress6").returns(windows_netsh)
+      Facter::Util::IP::Windows.expects(:value_for_interface_and_label).with("Local Area Connection", "ipaddress6").returns("2001:0:4137:9e76:2087:77a:53ef:7527")
 
-      Facter::Util::IP.get_interface_value("Teredo Tunneling Pseudo-Interface", "ipaddress6").should == "2001:0:4137:9e76:2087:77a:53ef:7527"
+      Facter::Util::IP.get_interface_value("Local Area Connection", "ipaddress6").should == "2001:0:4137:9e76:2087:77a:53ef:7527"
     end
   end
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list