On May 23, 2011, at 11:49 PM, William Van Hevelingen wrote:

> Signed-off-by: William Van Hevelingen <[email protected]>
> ---
> Local-branch: feature/master/7621-windows_osrelease
> lib/facter/operatingsystemrelease.rb |   17 +++++++++++++++++
> lib/facter/util/mswindows.rb         |   16 ++++++++++++++++
> lib/facter/windowsrelease.rb         |   18 ++++++++++++++++++
> 3 files changed, 51 insertions(+), 0 deletions(-)
> create mode 100644 lib/facter/util/mswindows.rb
> create mode 100644 lib/facter/windowsrelease.rb
> 
> diff --git a/lib/facter/operatingsystemrelease.rb 
> b/lib/facter/operatingsystemrelease.rb
> index 347fe7f..2ec7b05 100644
> --- a/lib/facter/operatingsystemrelease.rb
> +++ b/lib/facter/operatingsystemrelease.rb
> @@ -123,5 +123,22 @@ Facter.add(:operatingsystemrelease) do
> end
> 
> Facter.add(:operatingsystemrelease) do
> +  confine :operatingsystem => :windows
> +  require 'facter/util/mswindows'

This 'require' should be in the 'setcode' - otherwise this will probably 
actually fail to load on non-Windows boxes (assuming that util lib fails to 
load on non-Windows boxes).

> +  osrelease = ""
> +
> +  # Included spaces to ensure its uniqueness
> +  @releases = [" XP ", " Vista ", " 7 ", " 2003 ", " 2008 ", " 2008 R2 ", " 
> 8 "]
> +
> +  @releases.each do |r|
> +    if Facter::Util::MSWindows.getWindowsRelease.include?(r)
> +      setcode do
> +        osrelease = r.gsub(/\s/, "") 
> +      end
> +    end
> +  end
> +end

This seems pretty awkward, and would likely work much better something like 
this:

setcode do
  result = nil
  [" XP ", " Vista ", " 7 ", " 2003 ", " 2008 ", " 2008 R2 ", " 8 "].each do |r|
    if ....include?(r)
      result = r.sub(...)
    end
  end
  result
end

This way all of your code is actually only called when the release info is 
called for.

In fact, in looking at this, I don't think this does much of what you think it 
does - e.g., the '@releases' variable isn't really useful as an instance 
variable, because it's never accessed once the facts are actually created.

> +Facter.add(:operatingsystemrelease) do
>     setcode do Facter[:kernelrelease].value end
> end
> diff --git a/lib/facter/util/mswindows.rb b/lib/facter/util/mswindows.rb
> new file mode 100644
> index 0000000..78b8172
> --- /dev/null
> +++ b/lib/facter/util/mswindows.rb
> @@ -0,0 +1,16 @@
> +module Facter::Util::MSWindows
> +
> +  def self.getWindowsRelease
> +    caption = ""
> +    self.Win32_OperatingSystem.each { |x| caption = x["Caption"] }
> +    caption
> +  end
> +
> +  def self.Win32_OperatingSystem
> +    require 'win32ole'
> +    wmi = WIN32OLE.connect("winmgmts://")
> +    query = wmi.ExecQuery("select * from Win32_OperatingSystem")
> +    query

This can be simplified quite a bit:

def ...
  require 'win32ole'
  WIN32OLE.connect("winmgmts://").ExecQuery("select * from 
Win32_OperatingSystem")
end

> +  end
> +
> +end
> diff --git a/lib/facter/windowsrelease.rb b/lib/facter/windowsrelease.rb
> new file mode 100644
> index 0000000..3469622
> --- /dev/null
> +++ b/lib/facter/windowsrelease.rb
> @@ -0,0 +1,18 @@
> +# Fact: windowsrelease
> +#
> +# Purpose:
> +#   Returns the windowsrelease of the system.
> +#
> +# Resolution:
> +#   Uses WMI to query for the OS caption string
> +#
> +# Caveats:
> +#
> +require 'facter/util/mswindows'
> +
> +Facter.add(:windowsrelease) do
> +    confine :operatingsystem => :windows
> +    setcode do
> +      Facter::Util::MSWindows.getWindowsRelease
> +    end
> +end
> -- 
> 1.7.0.4
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/puppet-dev?hl=en.
> 


-- 
A government that robs Peter to pay Paul can always depend on the
support of Paul.        -- George Bernard Shaw
---------------------------------------------------------------------
Luke Kanies  -|-   http://puppetlabs.com   -|-   http://about.me/lak




-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to