class Chef::Provider::Package::Yum

Public Instance Methods

candidate_version() click to toggle source
# File lib/chef/provider/package/yum.rb, line 77
def candidate_version
  package_name_array.each_with_index.map do |pkg, i|
    available_version(i).version_with_arch
  end
end
define_resource_requirements() click to toggle source
# File lib/chef/provider/package/yum.rb, line 67
def define_resource_requirements
  requirements.assert(:install, :upgrade, :remove, :purge) do |a|
    a.assertion { !new_resource.source || ::File.exist?(new_resource.source) }
    a.failure_message Chef::Exceptions::Package, "Package #{new_resource.package_name} not found: #{new_resource.source}"
    a.whyrun "assuming #{new_resource.source} would have previously been created"
  end

  super
end
get_current_versions() click to toggle source
# File lib/chef/provider/package/yum.rb, line 83
def get_current_versions
  package_name_array.each_with_index.map do |pkg, i|
    installed_version(i).version_with_arch
  end
end
install_package(names, versions) click to toggle source
# File lib/chef/provider/package/yum.rb, line 89
def install_package(names, versions)
  method = nil
  methods = []
  names.each_with_index do |n, i|
    next if n.nil?

    av = available_version(i)

    name = av.name # resolve the name via the available/candidate version

    iv = python_helper.package_query(:whatinstalled, name)

    method = "install"

    # If this is a package like the kernel that can be installed multiple times, we'll skip over this logic
    if new_resource.allow_downgrade && version_gt?(iv.version_with_arch, av.version_with_arch) && !python_helper.install_only_packages(name)
      # We allow downgrading only in the evenit of single-package
      # rules where the user explicitly allowed it
      method = "downgrade"
    end

    methods << method
  end

  # We could split this up into two commands if we wanted to, but
  # for now, just don't support this.
  if methods.uniq.length > 1
    raise Chef::Exceptions::Package, "Multipackage rule has a mix of upgrade and downgrade packages. Cannot proceed."
  end

  if new_resource.source
    yum(options, "-y #{method}", new_resource.source)
  else
    resolved_names = names.each_with_index.map { |name, i| available_version(i).to_s unless name.nil? }
    yum(options, "-y #{method}", resolved_names)
  end
  flushcache
end
Also aliased as: upgrade_package
load_current_resource() click to toggle source
# File lib/chef/provider/package/yum.rb, line 57
def load_current_resource
  flushcache if new_resource.flush_cache[:before]

  @current_resource = Chef::Resource::YumPackage.new(new_resource.name)
  current_resource.package_name(new_resource.package_name)
  current_resource.version(get_current_versions)

  current_resource
end
lock_package(names, versions) click to toggle source

NB: the yum_package provider manages individual single packages, please do not submit issues or PRs to try to add wildcard support to lock / unlock. The best solution is to write an execute resource which does a not_if `yum versionlock | grep '^pattern“ kind of approach

# File lib/chef/provider/package/yum.rb, line 145
def lock_package(names, versions)
  yum("-d0 -e0 -y", options, "versionlock add", resolved_package_lock_names(names))
end
purge_package(names, versions)
Alias for: remove_package
python_helper() click to toggle source

Most of the magic in this class happens in the python helper script. The ruby side of this provider knows only enough to translate Chef-style new_resource name+package+version into a request to the python side. The python side is then responsible for knowing everything about RPMs and what is installed and what is available. The ruby side of this class should remain a lightweight translation layer to translate Chef requests into RPC requests to python. This class knows nothing about how to compare RPM versions, and does not maintain any cached state of installed/available versions and should be kept that way.

# File lib/chef/provider/package/yum.rb, line 53
def python_helper
  @python_helper ||= PythonHelper.instance
end
remove_package(names, versions) click to toggle source
# File lib/chef/provider/package/yum.rb, line 131
def remove_package(names, versions)
  resolved_names = names.each_with_index.map { |name, i| installed_version(i).to_s unless name.nil? }
  yum(options, "-y remove", resolved_names)
  flushcache
end
Also aliased as: purge_package
unlock_package(names, versions) click to toggle source

NB: the yum_package provider manages individual single packages, please do not submit issues or PRs to try to add wildcard support to lock / unlock. The best solution is to write an execute resource which does a only_if `yum versionlock | grep '^pattern“ kind of approach

# File lib/chef/provider/package/yum.rb, line 151
def unlock_package(names, versions)
  # yum versionlock delete on rhel6 needs the glob nonsense in the following command
  yum("-d0 -e0 -y", options, "versionlock delete", resolved_package_lock_names(names).map { |n| "'*:#{n}-*'" })
end
upgrade_package(names, versions)

yum upgrade does not work on uninstalled packaged, while install will upgrade

Alias for: install_package

Private Instance Methods

available_version(index) click to toggle source

@returns Array<Version>

# File lib/chef/provider/package/yum.rb, line 224
def available_version(index)
  @available_version ||= []

  @available_version[index] ||= if new_resource.source
                                  resolve_source_to_version_obj
                                else
                                  python_helper.package_query(:whatavailable, package_name_array[index], version: safe_version_array[index], arch: safe_arch_array[index], options: options)
                                end

  @available_version[index]
end
flushcache() click to toggle source

cache flushing is accomplished by simply restarting the python helper. this produces a roughly 15% hit to the runtime of installing/removing/upgrading packages. correctly using multipackage array installs (and the multipackage cookbook) can produce 600% improvements in runtime.

# File lib/chef/provider/package/yum.rb, line 250
def flushcache
  python_helper.restart
end
installed_version(index) click to toggle source

@returns Array<Version>

# File lib/chef/provider/package/yum.rb, line 237
def installed_version(index)
  @installed_version ||= []
  @installed_version[index] ||= if new_resource.source
                                  python_helper.package_query(:whatinstalled, available_version(index).name, version: safe_version_array[index], arch: safe_arch_array[index])
                                else
                                  python_helper.package_query(:whatinstalled, package_name_array[index], version: safe_version_array[index], arch: safe_arch_array[index])
                                end
  @installed_version[index]
end
locked_packages() click to toggle source
# File lib/chef/provider/package/yum.rb, line 171
def locked_packages
  @locked_packages ||=
    begin
      locked = shell_out_with_timeout!("yum versionlock list")
      locked.stdout.each_line.map do |line|
        line.sub(/-[^-]*-[^-]*$/, "").split(":").last.strip
      end
    end
end
packages_all_locked?(names, versions) click to toggle source
# File lib/chef/provider/package/yum.rb, line 181
def packages_all_locked?(names, versions)
  resolved_package_lock_names(names).all? { |n| locked_packages.include? n }
end
packages_all_unlocked?(names, versions) click to toggle source
# File lib/chef/provider/package/yum.rb, line 185
def packages_all_unlocked?(names, versions)
  !resolved_package_lock_names(names).any? { |n| locked_packages.include? n }
end
resolve_source_to_version_obj() click to toggle source
# File lib/chef/provider/package/yum.rb, line 212
def resolve_source_to_version_obj
  shell_out_with_timeout!("rpm -qp --queryformat '%{NAME} %{EPOCH} %{VERSION} %{RELEASE} %{ARCH}\n' #{new_resource.source}").stdout.each_line do |line|
    # this is another case of committing the sin of doing some lightweight mangling of RPM versions in ruby -- but the output of the rpm command
    # does not match what the yum library accepts.
    case line
      when /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/
        return Version.new($1, "#{$2 == '(none)' ? '0' : $2}:#{$3}-#{$4}", $5)
    end
  end
end
resolved_package_lock_names(names) click to toggle source

this will resolve things like `/usr/bin/perl` or virtual packages like `mysql` – it will not work (well? at all?) with globs that match multiple packages

# File lib/chef/provider/package/yum.rb, line 159
def resolved_package_lock_names(names)
  names.each_with_index.map do |name, i|
    if !name.nil?
      if installed_version(i).version.nil?
        available_version(i).name
      else
        installed_version(i).name
      end
    end
  end
end
safe_arch_array() click to toggle source
# File lib/chef/provider/package/yum.rb, line 276
def safe_arch_array
  if new_resource.arch.is_a?(Array)
    new_resource.arch
  elsif new_resource.arch.nil?
    package_name_array.map { nil }
  else
    [ new_resource.arch ]
  end
end
safe_version_array() click to toggle source
# File lib/chef/provider/package/yum.rb, line 266
def safe_version_array
  if new_resource.version.is_a?(Array)
    new_resource.version
  elsif new_resource.version.nil?
    package_name_array.map { nil }
  else
    [ new_resource.version ]
  end
end
version_compare(v1, v2) click to toggle source
# File lib/chef/provider/package/yum.rb, line 199
def version_compare(v1, v2)
  return false if v1.nil? || v2.nil?
  python_helper.compare_versions(v1, v2)
end
version_equals?(v1, v2) click to toggle source
# File lib/chef/provider/package/yum.rb, line 194
def version_equals?(v1, v2)
  return false if v1.nil? || v2.nil?
  python_helper.compare_versions(v1, v2) == 0
end
version_gt?(v1, v2) click to toggle source
# File lib/chef/provider/package/yum.rb, line 189
def version_gt?(v1, v2)
  return false if v1.nil? || v2.nil?
  python_helper.compare_versions(v1, v2) == 1
end
yum(*args) click to toggle source
# File lib/chef/provider/package/yum.rb, line 262
def yum(*args)
  shell_out_with_timeout!(a_to_s(yum_binary, *args))
end
yum_binary() click to toggle source
# File lib/chef/provider/package/yum.rb, line 254
def yum_binary
  @yum_binary ||=
    begin
      yum_binary = new_resource.yum_binary if new_resource.is_a?(Chef::Resource::YumPackage)
      yum_binary ||= ::File.exist?("/usr/bin/yum-deprecated") ? "yum-deprecated" : "yum"
    end
end
yum_syntax(name, version, arch) click to toggle source

Generate the yum syntax for the package

# File lib/chef/provider/package/yum.rb, line 205
def yum_syntax(name, version, arch)
  s = name
  s += "-#{version}" if version
  s += ".#{arch}" if arch
  s
end