Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows packages plugin - Get packages from registry #778

Merged
merged 3 commits into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 59 additions & 46 deletions lib/ohai/plugins/packages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,78 @@
provides "packages"
depends "platform_family"

WINDOWS_ATTRIBUTE_ALIASES = {
"DisplayVersion" => "version",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this plugin was disabled by default here. Are you intentionally reverting that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I think I have the proper background on why this plugin was disabled. It seems safe to enable it again. :)

"Publisher" => "publisher",
"InstallDate" => "installdate"
}

collect_data(:linux) do
if configuration(:enabled)
packages Mash.new
if %w{debian}.include? platform_family
so = shell_out("dpkg-query -W")
pkgs = so.stdout.lines
packages Mash.new
if %w{debian}.include? platform_family
so = shell_out("dpkg-query -W")
pkgs = so.stdout.lines

pkgs.each do |pkg|
name, version = pkg.split
packages[name] = { "version" => version }
end
pkgs.each do |pkg|
name, version = pkg.split
packages[name] = { "version" => version }
end

elsif %w{rhel fedora suse}.include? platform_family
require "shellwords"
format = Shellwords.escape '%{NAME}\t%{VERSION}\t%{RELEASE}\n'
so = shell_out("rpm -qa --queryformat #{format}")
pkgs = so.stdout.lines
elsif %w{rhel fedora suse}.include? platform_family
require "shellwords"
format = Shellwords.escape '%{NAME}\t%{VERSION}\t%{RELEASE}\n'
so = shell_out("rpm -qa --queryformat #{format}")
pkgs = so.stdout.lines

pkgs.each do |pkg|
name, version, release = pkg.split
packages[name] = { "version" => version, "release" => release }
end
pkgs.each do |pkg|
name, version, release = pkg.split
packages[name] = { "version" => version, "release" => release }
end
end
end

collect_data(:windows) do
if configuration(:enabled)
packages Mash.new
require "wmi-lite"

wmi = WmiLite::Wmi.new
w32_product = wmi.instances_of("Win32_Product")

w32_product.find_all.each do |product|
name = product["name"]
def collect_programs_from_registry_key(key_path)
# from http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx
if ::RbConfig::CONFIG["target_cpu"] == "i386"
reg_type = Win32::Registry::KEY_READ | 0x100
elsif ::RbConfig::CONFIG["target_cpu"] == "x86_64"
reg_type = Win32::Registry::KEY_READ | 0x200
else
reg_type = Win32::Registry::KEY_READ
end
Win32::Registry::HKEY_LOCAL_MACHINE.open(key_path, reg_type) do |reg|
reg.each_key do |key, _wtime|
pkg = reg.open(key)
name = pkg["DisplayName"] rescue nil
next if name.nil?
package = packages[name] = Mash.new
%w{version vendor installdate}.each do |attr|
package[attr] = product[attr]
WINDOWS_ATTRIBUTE_ALIASES.each do |registry_attr, package_attr|
value = pkg[registry_attr] rescue nil
package[package_attr] = value unless value.nil?
end
end
end
end

collect_data(:windows) do
require "win32/registry"
packages Mash.new
collect_programs_from_registry_key('Software\Microsoft\Windows\CurrentVersion\Uninstall')
# on 64 bit systems, 32 bit programs are stored here
collect_programs_from_registry_key('Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
end

collect_data(:aix) do
if configuration(:enabled)
packages Mash.new
so = shell_out("lslpp -L -q -c")
pkgs = so.stdout.lines
packages Mash.new
so = shell_out("lslpp -L -q -c")
pkgs = so.stdout.lines

# Output format is
# Package Name:Fileset:Level
# On aix, filesets are packages and levels are versions
pkgs.each do |pkg|
_, name, version = pkg.split(":")
packages[name] = { "version" => version }
end
# Output format is
# Package Name:Fileset:Level
# On aix, filesets are packages and levels are versions
pkgs.each do |pkg|
_, name, version = pkg.split(":")
packages[name] = { "version" => version }
end
end

Expand Down Expand Up @@ -119,10 +134,8 @@ def collect_sysv_packages
end

collect_data(:solaris2) do
if configuration(:enabled)
packages Mash.new
collect_ips_packages
collect_sysv_packages
end
packages Mash.new
collect_ips_packages
collect_sysv_packages
end
end
Loading