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

Inherits:
Chef::Provider::Package show all
Includes:
Mixin::GetSourceFromPackage
Defined in:
lib/chef/provider/package/yum.rb,
lib/chef/provider/package/yum/rpm_utils.rb,
lib/chef/provider/package/yum/yum_cache.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

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

Instance Method Summary collapse

Methods inherited from Chef::Provider::Package

#action_lock, #action_unlock, #as_array, #expand_options, #get_preseed_file, #have_any_matching_version?, #multipackage_api_adapter, #options, #preseed_package, #preseed_resource, #reconfig_package, #removing_package?, #target_version_already_installed?, #version_requirement_satisfied?

Methods included from Mixin::SubclassDirective

#subclass_directive

Methods included from Mixin::ShellOut

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods inherited from Chef::Provider

action, #action_nothing, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #events, include_resource_dsl?, include_resource_dsl_module, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

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

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

#initialize(new_resource, run_context) ⇒ Yum

Overload the Package provider to keep track of the YumCache


41
42
43
44
45
46
# File 'lib/chef/provider/package/yum.rb', line 41

def initialize(new_resource, run_context)
  super

  @yum = YumCache.instance
  @yum.yum_binary = yum_binary
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?


166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/chef/provider/package/yum.rb', line 166

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

#check_resource_semantics!Object


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/provider/package/yum.rb', line 49

def check_resource_semantics!
  super

  if !new_resource.version.nil? && package_name_array.length != new_version_array.length
    raise Chef::Exceptions::InvalidResourceSpecification, "Please provide a version for each package. Use `nil` for default version."
  end

  if !new_resource.arch.nil? && package_name_array.length != safe_arch_array.length
    raise Chef::Exceptions::InvalidResourceSpecification, "Please provide an architecture for each package. Use `nil` for default architecture."
  end
end

#define_resource_requirementsObject


62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/provider/package/yum.rb', line 62

def define_resource_requirements
  super

  # Ensure that the source file (if specified) is present on the file system
  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
end

#install_package(name, version) ⇒ Object


119
120
121
122
123
124
125
126
127
# File 'lib/chef/provider/package/yum.rb', line 119

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

  flush_cache[:after] ? @yum.reload : @yum.reload_installed
end

#load_current_resourceObject


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/provider/package/yum.rb', line 74

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

  if new_resource.source
    query_source_file
  else
    # 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"
    #
    # In the third case, we want to convert those dependency strings into
    # packages that we can actually install
    convert_dependency_strings_into_packages

    # Fill out the rest of the details by querying the Yum Cache
    query_yum_cache
  end

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

#lock_package(name, version) ⇒ Object


148
149
150
151
# File 'lib/chef/provider/package/yum.rb', line 148

def lock_package(name, version)
  lock_str = full_package_name(name, as_array(name).map { nil }).join(" ")
  yum_command("-d0 -e0 -y#{expand_options(new_resource.options)} versionlock add #{lock_str}")
end

#package_locked(name, version) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/provider/package/yum.rb', line 102

def package_locked(name, version)
  islocked = false
  locked = shell_out_with_timeout!("yum versionlock")
  locked.stdout.each_line do |line|
    line_package = line.sub(/-[^-]*-[^-]*$/, "").split(":").last.strip
    if line_package == name
      islocked = true
    end
  end
  islocked
end

#purge_package(name, version) ⇒ Object


143
144
145
# File 'lib/chef/provider/package/yum.rb', line 143

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

#remove_package(name, version) ⇒ Object


135
136
137
138
139
140
# File 'lib/chef/provider/package/yum.rb', line 135

def remove_package(name, version)
  remove_str = full_package_name(name, version).join(" ")
  yum_command("-d0 -e0 -y#{expand_options(new_resource.options)} remove #{remove_str}")

  flush_cache[:after] ? @yum.reload : @yum.reload_installed
end

#unlock_package(name, version) ⇒ Object


154
155
156
157
# File 'lib/chef/provider/package/yum.rb', line 154

def unlock_package(name, version)
  unlock_str = full_package_name(name, as_array(name).map { nil }).join(" ")
  yum_command("-d0 -e0 -y#{expand_options(new_resource.options)} versionlock delete #{unlock_str}")
end

#upgrade_package(name, version) ⇒ Object


130
131
132
# File 'lib/chef/provider/package/yum.rb', line 130

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