\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/ohai-8.24.0/lib/ohai/plugins/

Viewing File: packages.rb

# Author:: "Christian Höltje" <choltje@us.ibm.com>
# Author:: "Christopher M. Luciano" <cmlucian@us.ibm.com>
# Author:: Shahul Khajamohideen (<skhajamohid1@bloomberg.net>)
# Copyright (C) 2015 IBM Corp.
# Copyright (C) 2015 Bloomberg Finance L.P.
# 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.
#
#
Ohai.plugin(:Packages) do
  provides "packages"
  depends "platform_family"

  WINDOWS_ATTRIBUTE_ALIASES = {
    "DisplayVersion" => "version",
    "Publisher" => "publisher",
    "InstallDate" => "installdate",
  } unless defined?(WINDOWS_ATTRIBUTE_ALIASES)

  collect_data(:linux) do
    packages Mash.new
    if %w{debian}.include? platform_family
      format = '${Package}\t${Version}\t${Architecture}\n'
      so = shell_out("dpkg-query -W -f='#{format}'")
      pkgs = so.stdout.lines

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

    elsif %w{rhel fedora suse pld}.include? platform_family
      format = '%{NAME}\t%|EPOCH?{%{EPOCH}}:{0}|\t%{VERSION}\t%{RELEASE}\t%{INSTALLTIME}\t%{ARCH}\n'
      so = shell_out("rpm -qa --qf '#{format}'")
      pkgs = so.stdout.lines

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

  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
        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
    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
  end

  collect_data(:freebsd) do
    packages Mash.new
    so = shell_out('pkg query -a "%n %v"')
    # Output format is
    # name version
    so.stdout.lines do |pkg|
      name, version = pkg.split(" ")
      packages[name] = { "version" => version }
    end
  end

  def collect_ips_packages
    so = shell_out("pkg list -H")
    # Output format is
    # NAME (PUBLISHER)    VERSION    IFO
    so.stdout.lines.each do |pkg|
      tokens = pkg.split
      if tokens.length == 3 # No publisher info
        name, version, = tokens
      else
        name, publisher, version, = tokens
        publisher = publisher[1..-2]
      end
      packages[name] = { "version" => version }
      packages[name]["publisher"] = publisher if publisher
    end
  end

  def collect_sysv_packages
    so = shell_out("pkginfo -l")
    # Each package info is separated by a blank line
    chunked_lines = so.stdout.lines.map(&:strip).chunk do |line|
      !line.empty? || nil
    end
    chunked_lines.each do |_, lines|
      package = {}
      lines.each do |line|
        key, value = line.split(":", 2)
        package[key.strip.downcase] = value.strip unless value.nil?
      end
      # pkginst is the installed package name
      packages[package["pkginst"]] = package.tap do |p|
        p.delete("pkginst")
      end
    end
  end

  collect_data(:solaris2) do
    packages Mash.new
    collect_ips_packages
    collect_sysv_packages
  end
end