# Facter is to check Libvirt package version on Ubuntu and CentOS systems.
# It's required for checking group name which is created by libvirt package.
# If libvirt version on Ubuntu is 1.2.9 or more group name is 'libvirt';
# if version is less than 1.2.9, group name should be default (libvirtd for Ubuntu).

require 'facter/util/resolution'

Facter.add(:libvirt_package_version) do
  setcode do
    version=nil
    case Facter.value('osfamily')
      when /(?i)(debian)/
        pkg_grep_cmd = "apt-cache policy"
        out = Facter::Util::Resolution.exec("#{pkg_grep_cmd} libvirt-bin")
        version = out.split(/\n/).grep(/Candidate/i)[0].split(/\s+/)[2] if out
      when /(?i)(redhat)/
        pkg_grep_cmd = "yum info"
        out = Facter::Util::Resolution.exec("#{pkg_grep_cmd} libvirt")
        version = out.split(/\n/).grep(/Version/i)[0].split(/\s+/)[2] if out
    end
    version
  end
end

# Facter is to check Libvirt package version on Ubuntu and CentOS systems.
# It's required for checking group name which is created by libvirt package.
# If libvirt version on Ubuntu is 1.2.9 or more group name is 'libvirt';
# if version is less than 1.2.9, group name should be default (libvirtd for Ubuntu).

require 'facter/util/resolution'

Facter.add(:libvirt_package_version) do
  setcode do
    version=nil
    case Facter.value('osfamily')
      when /(?i)(debian)/
        pkg_grep_cmd = "apt-cache policy"
        out = Facter::Util::Resolution.exec("#{pkg_grep_cmd} libvirt-bin")
        out.each_line do |line|
          version = line.scan(%r{\s+Candidate:\s+(.*)}).join() if line =~ %r{Candidate:}
          break unless version.nil?
        end
      when /(?i)(redhat)/
        pkg_grep_cmd = "yum info"
        out = Facter::Util::Resolution.exec("#{pkg_grep_cmd} libvirt 2>&1")
        out.each_line do |line|
          version = line.scan(%r{Version\s+:\s+(.*)\s+}).join() if line =~ %r{Version\s+:}
          break unless version.nil?
        end
    end
    version
  end
end

Handle libvirt version correctly when missing
Handle libvirt version correctly when missing

Since 7.0 did not include a centos version of the libvirt package, the
libvirt_package_version fact caused puppet to spit errors. This change
updates the logic under RedHat based operating systems to ensure that
the output from the yum command is properly parsed.

Change-Id: I87adb8e0262cf9be83ac6766bf974266378e1b30
Closes-Bug: 1485990
Co-Authored-By: Stanislav Makar <ee7f23babcc48cdccfe636defb0ccf5f2327eff2@mirantis.com>
