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

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider::Package

#candidate_version

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider::Package

#action_install, #action_purge, #action_reconfig, #action_remove, #as_array, #define_resource_requirements, #expand_options, #get_preseed_file, #have_any_matching_version?, #preseed_package, #preseed_resource, #reconfig_package, #removing_package?, #target_version_already_installed?, #whyrun_supported?

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #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, #cleanup_after_converge, #converge_by, #define_resource_requirements, #events, #node, node_map, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Constructor Details

#initialize(new_resource, run_context) ⇒ Yum

Returns a new instance of Yum.



952
953
954
955
956
# File 'lib/chef/provider/package/yum.rb', line 952

def initialize(new_resource, run_context)
  super

  @yum = YumCache.instance
end

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?



1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
# File 'lib/chef/provider/package/yum.rb', line 1203

def action_upgrade
  # Could be uninstalled or have no candidate
  if @current_resource.version.nil? || !candidate_version_array.any?
    super
  elsif candidate_version_array.zip(current_version_array).any? do |c, i|
          RPMVersion.parse(c) > RPMVersion.parse(i)
        end
    super
  else
    Chef::Log.debug("#{@new_resource} is at the latest version - nothing to do")
  end
end

#archObject

Extra attributes



961
962
963
964
965
966
967
# File 'lib/chef/provider/package/yum.rb', line 961

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

#flush_cacheObject



969
970
971
972
973
974
975
# File 'lib/chef/provider/package/yum.rb', line 969

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



1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'lib/chef/provider/package/yum.rb', line 1182

def install_package(name, version)
  if @new_resource.source
    yum_command("yum -d0 -e0 -y#{expand_options(@new_resource.options)} localinstall #{@new_resource.source}")
  else
    install_remote_package(name, version)
  end

  if flush_cache[:after]
    @yum.reload
  else
    @yum.reload_installed
  end
end

#install_remote_package(name, version) ⇒ Object



1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
# File 'lib/chef/provider/package/yum.rb', line 1116

def install_remote_package(name, version)
  # Work around yum not exiting with an error if a package doesn't exist
  # for CHEF-2062
  all_avail = as_array(name).zip(as_array(version)).any? do |n, v|
    @yum.version_available?(n, v, arch)
  end
  method = log_method = nil
  methods = []
  if all_avail
    # 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
    as_array(name).zip(as_array(version)).each do |n, v|
      method = "install"
      log_method = "installing"
      idx = package_name_array.index(n)
      unless @yum.allow_multi_install.include?(n)
        if RPMVersion.parse(current_version_array[idx]) > RPMVersion.parse(v)
          # We allow downgrading only in the evenit of single-package
          # rules where the user explicitly allowed it
          if allow_downgrade
            method = "downgrade"
            log_method = "downgrading"
          else
            # we bail like yum when the package is older
            raise Chef::Exceptions::Package, "Installed package #{n}-#{current_version_array[idx]} is newer " +
                                             "than candidate package #{n}-#{v}"
          end
        end
      end
      # methods don't count for packages we won't be touching
      next if RPMVersion.parse(current_version_array[idx]) == RPMVersion.parse(v)
      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 #{name} has a mix of upgrade and downgrade packages. Cannot proceed."
    end

    repos = []
    pkg_string_bits = []
    index = 0
    as_array(name).zip(as_array(version)).each do |n, v|
      s = ''
      unless v == current_version_array[index]
        s = "#{n}-#{v}#{yum_arch}"
        repo = @yum.package_repository(n, v, arch)
        repos << "#{s} from #{repo} repository"
        pkg_string_bits << s
      end
      index += 1
    end
    pkg_string = pkg_string_bits.join(' ')
    Chef::Log.info("#{@new_resource} #{log_method} #{repos.join(' ')}")
    yum_command("yum -d0 -e0 -y#{expand_options(@new_resource.options)} #{method} #{pkg_string}")
  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

#load_current_resourceObject

Standard Provider methods for Parent



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
# File 'lib/chef/provider/package/yum.rb', line 1017

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

  if @new_resource.options
    repo_control = []
    @new_resource.options.split.each do |opt|
      if opt =~ %r{--(enable|disable)repo=.+}
        repo_control << opt
      end
    end

    if repo_control.size > 0
      @yum.enable_extra_repo_control(repo_control.join(" "))
    else
      @yum.disable_extra_repo_control
    end
  else
    @yum.disable_extra_repo_control
  end

  # At this point package_name could be:
  #
  # 1) a package name, eg: "foo"
  # 2) a package name.arch, eg: "foo.i386"
  # 3) or a dependency, eg: "foo >= 1.1"

  # Check if we have name or name+arch which has a priority over a dependency
  package_name_array.each_with_index do |n, index|
    unless @yum.package_available?(n)
      # If they aren't in the installed packages they could be a dependency
      dep = parse_dependency(n, new_version_array[index])
      if dep
        if @new_resource.package_name.is_a?(Array)
          @new_resource.package_name(package_name_array - [n] + [dep.first])
          @new_resource.version(new_version_array - [new_version_array[index]] + [dep.last]) if dep.last
        else
          @new_resource.package_name(dep.first)
          @new_resource.version(dep.last) if dep.last
        end
      end
    end
  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)

  installed_version = []
  @candidate_version = []
  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")
    shell_out!("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@new_resource.source}", :timeout => Chef::Config[:yum_timeout]).stdout.each_line do |line|
      case line
      when /([\w\d_.-]+)\s([\w\d_.-]+)/
        @current_resource.package_name($1)
        @new_resource.version($2)
      end
    end
    @candidate_version << @new_resource.version
    installed_version << @yum.installed_version(@current_resource.package_name, arch)
  else
    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}")

    package_name_array.each do |pkg|
      installed_version << @yum.installed_version(pkg, arch)
      @candidate_version << @yum.candidate_version(pkg, arch)
    end

  end

  if installed_version.size == 1
    @current_resource.version(installed_version[0])
    @candidate_version = @candidate_version[0]
  else
    @current_resource.version(installed_version)
  end

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

  @current_resource
end

#purge_package(name, version) ⇒ Object



1237
1238
1239
# File 'lib/chef/provider/package/yum.rb', line 1237

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

#remove_package(name, version) ⇒ Object



1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
# File 'lib/chef/provider/package/yum.rb', line 1220

def remove_package(name, version)
  if version
    remove_str = as_array(name).zip(as_array(version)).map do |x|
      "#{x.join('-')}#{yum_arch}"
    end.join(' ')
  else
    remove_str = as_array(name).map { |n| "#{n}#{yum_arch}" }.join(' ')
  end
  yum_command("yum -d0 -e0 -y#{expand_options(@new_resource.options)} remove #{remove_str}")

  if flush_cache[:after]
    @yum.reload
  else
    @yum.reload_installed
  end
end

#upgrade_package(name, version) ⇒ Object



1216
1217
1218
# File 'lib/chef/provider/package/yum.rb', line 1216

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

#yum_archObject

Helpers



980
981
982
# File 'lib/chef/provider/package/yum.rb', line 980

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

#yum_command(command) ⇒ Object



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
1012
# File 'lib/chef/provider/package/yum.rb', line 984

def yum_command(command)
  status = shell_out(command, {:timeout => Chef::Config[:yum_timeout]})

  # This is fun: rpm can encounter errors in the %post/%postun scripts which aren't
  # considered fatal - meaning the rpm is still successfully installed. These issue
  # cause yum to emit a non fatal warning but still exit(1). As there's currently no
  # way to suppress this behavior and an exit(1) will break a Chef run we make an
  # effort to trap these and re-run the same install command - it will either fail a
  # second time or succeed.
  #
  # A cleaner solution would have to be done in python and better hook into
  # yum/rpm to handle exceptions as we see fit.
  if status.exitstatus == 1
    status.stdout.each_line do |l|
      # rpm-4.4.2.3 lib/psm.c line 2182
      if l =~ %r{^error: %(post|postun)\(.*\) scriptlet failed, exit status \d+$}
        Chef::Log.warn("#{@new_resource} caught non-fatal scriptlet issue: \"#{l}\". Can't trust yum exit status " +
                       "so running install again to verify.")
        status = shell_out(command, {:timeout => Chef::Config[:yum_timeout]})
        break
      end
    end
  end

  if status.exitstatus > 0
    command_output = "STDOUT: #{status.stdout}\nSTDERR: #{status.stderr}"
    raise Chef::Exceptions::Exec, "#{command} returned #{status.exitstatus}:\n#{command_output}"
  end
end