Class: Chef::Provider::Package::Yum

Inherits:
Chef::Provider::Package show all
Includes:
Mixin::GetSourceFromPackage
Defined in:
lib/chef/provider/package/yum.rb

Defined Under Namespace

Classes: RPMDb, RPMDbPackage, RPMDependency, RPMPackage, RPMProvide, RPMRequire, RPMUtils, RPMVersion, YumCache

Instance Attribute Summary

Attributes inherited from Chef::Provider::Package

#candidate_version

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider::Package

#action_install, #action_purge, #action_remove, #expand_options, #get_preseed_file, #preseed_package, #preseed_resource, #removing_package?

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(new_resource, run_context) ⇒ Yum

Returns a new instance of Yum.



873
874
875
876
877
# File 'lib/chef/provider/package/yum.rb', line 873

def initialize(new_resource, run_context)
  super

  @yum = YumCache.instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Method Details

#action_upgradeObject

Keep upgrades from trying to install an older candidate version. Can happen when a new version is installed then removed from a repository, now the older available version shows up as a viable install candidate.

Can be done in upgrade_package but an upgraded from->to log message slips out

Hacky - better overall solution? Custom compare in Package provider?



1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/chef/provider/package/yum.rb', line 1020

def action_upgrade
  # Ensure the candidate is newer
  if RPMVersion.parse(candidate_version) > RPMVersion.parse(@current_resource.version)
    super
  # Candidate is older
  else
    Chef::Log.debug("#{@new_resource} is at the latest version - nothing to do")
  end
end

#allow_downgradeObject



898
899
900
901
902
903
904
# File 'lib/chef/provider/package/yum.rb', line 898

def allow_downgrade
  if @new_resource.respond_to?("allow_downgrade")
    @new_resource.allow_downgrade
  else
    false
  end
end

#archObject

Extra attributes



882
883
884
885
886
887
888
# File 'lib/chef/provider/package/yum.rb', line 882

def arch
  if @new_resource.respond_to?("arch")
    @new_resource.arch
  else
    nil
  end
end

#flush_cacheObject



890
891
892
893
894
895
896
# File 'lib/chef/provider/package/yum.rb', line 890

def flush_cache
  if @new_resource.respond_to?("flush_cache")
    @new_resource.flush_cache
  else
    { :before => false, :after => false }
  end
end

#install_package(name, version) ⇒ Object



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/chef/provider/package/yum.rb', line 969

def install_package(name, version)
  if @new_resource.source
    run_command_with_systems_locale(
      :command => "yum -d0 -e0 -y#{expand_options(@new_resource.options)} localinstall #{@new_resource.source}"
    )
  else
    # Work around yum not exiting with an error if a package doesn't exist for CHEF-2062
    if @yum.version_available?(name, version, arch)
      method = "install"

      # More Yum fun:
      #
      # yum install of an old name+version will exit(1)
      # yum install of an old name+version+arch will exit(0) for some reason
      #
      # Some packages can be installed multiple times like the kernel
      unless @yum.allow_multi_install.include?(name)
        if RPMVersion.parse(@current_resource.version) > RPMVersion.parse(version)
          # Unless they want this...
          if allow_downgrade
            method = "downgrade"
          else
            # we bail like yum when the package is older
            raise Chef::Exceptions::Package, "Installed package #{name}-#{@current_resource.version} is newer " +
                                             "than candidate package #{name}-#{version}"
          end
        end
      end

      run_command_with_systems_locale(
        :command => "yum -d0 -e0 -y#{expand_options(@new_resource.options)} #{method} #{name}-#{version}#{yum_arch}"
      )
    else
      raise Chef::Exceptions::Package, "Version #{version} of #{name} not found. Did you specify both version " +
                                       "and release? (version-release, e.g. 1.84-10.fc6)"
    end
  end
  if flush_cache[:after]
    @yum.reload
  else
    @yum.reload_installed
  end
end

#load_current_resourceObject

Standard Provider methods for Parent



916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/chef/provider/package/yum.rb', line 916

def load_current_resource
  if flush_cache[:before]
    @yum.reload
  end

  unless @yum.package_available?(@new_resource.package_name)
    parse_dependency
  end

  # Don't overwrite an existing arch
  unless arch
    parse_arch
  end

  @current_resource = Chef::Resource::Package.new(@new_resource.name)
  @current_resource.package_name(@new_resource.package_name)

  if @new_resource.source
    unless ::File.exists?(@new_resource.source)
      raise Chef::Exceptions::Package, "Package #{@new_resource.name} not found: #{@new_resource.source}"
    end

    Chef::Log.debug("#{@new_resource} checking rpm status")
    status = popen4("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@new_resource.source}") do |pid, stdin, stdout, stderr|
      stdout.each do |line|
        case line
        when /([\w\d_.-]+)\s([\w\d_.-]+)/
          @current_resource.package_name($1)
          @new_resource.version($2)
        end
      end
    end
  end

  if @new_resource.version
    new_resource = "#{@new_resource.package_name}-#{@new_resource.version}#{yum_arch}"
  else
    new_resource = "#{@new_resource.package_name}#{yum_arch}"
  end

  Chef::Log.debug("#{@new_resource} checking yum info for #{new_resource}")

  installed_version = @yum.installed_version(@new_resource.package_name, arch)
  @current_resource.version(installed_version)

  @candidate_version = @yum.candidate_version(@new_resource.package_name, arch)

  Chef::Log.debug("#{@new_resource} installed version: #{installed_version || "(none)"} candidate version: " +
                  "#{@candidate_version || "(none)"}")

  @current_resource
end

#purge_package(name, version) ⇒ Object



1051
1052
1053
# File 'lib/chef/provider/package/yum.rb', line 1051

def purge_package(name, version)
  remove_package(name, version)
end

#remove_package(name, version) ⇒ Object



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/chef/provider/package/yum.rb', line 1034

def remove_package(name, version)
  if version
    run_command_with_systems_locale(
     :command => "yum -d0 -e0 -y#{expand_options(@new_resource.options)} remove #{name}-#{version}#{yum_arch}"
    )
  else
    run_command_with_systems_locale(
     :command => "yum -d0 -e0 -y#{expand_options(@new_resource.options)} remove #{name}#{yum_arch}"
    )
  end
  if flush_cache[:after]
    @yum.reload
  else
    @yum.reload_installed
  end
end

#upgrade_package(name, version) ⇒ Object



1030
1031
1032
# File 'lib/chef/provider/package/yum.rb', line 1030

def upgrade_package(name, version)
  install_package(name, version)
end

#yum_archObject

Helpers



909
910
911
# File 'lib/chef/provider/package/yum.rb', line 909

def yum_arch
  arch ? ".#{arch}" : nil
end