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

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

Defined Under Namespace

Classes: 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, #action_upgrade, #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.



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

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

#archObject



158
159
160
161
162
163
164
# File 'lib/chef/provider/package/yum.rb', line 158

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

#install_package(name, version) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/chef/provider/package/yum.rb', line 209

def install_package(name, version)
  if @new_resource.source 
    run_command_with_systems_locale(
      :command => "yum -d0 -e0 -y #{@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, yum_arch)
      run_command_with_systems_locale(
        :command => "yum -d0 -e0 -y #{@new_resource.options} install #{name}-#{version}#{yum_arch}"
      )
    else
      raise ArgumentError, "#{@new_resource.name}: Version #{version} of #{name} not found. Did you specify both version and release? (version-release, e.g. 1.84-10.fc6)"
    end
  end
  @yum.flush
end

#load_current_resourceObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/chef/provider/package/yum.rb', line 170

def load_current_resource
  @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("Checking rpm status for  #{@new_resource.package_name}")
    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

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

  @yum.refresh

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

  @current_resource.version(installed_version)
  if candidate_version
    @candidate_version = candidate_version
  else
    @candidate_version = installed_version
  end
  Chef::Log.debug("#{@current_resource.name}: Installed version: #{installed_version} Candidate version: #{candidate_version}")

  @current_resource
end

#purge_package(name, version) ⇒ Object



254
255
256
# File 'lib/chef/provider/package/yum.rb', line 254

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

#remove_package(name, version) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/chef/provider/package/yum.rb', line 240

def remove_package(name, version)
  if version
    run_command_with_systems_locale(
     :command => "yum -d0 -e0 -y #{@new_resource.options} remove #{name}-#{version}#{yum_arch}"
    )
  else
    run_command_with_systems_locale(
     :command => "yum -d0 -e0 -y #{@new_resource.options} remove #{name}#{yum_arch}"
    )
  end
    
  @yum.flush
end

#upgrade_package(name, version) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/chef/provider/package/yum.rb', line 227

def upgrade_package(name, version)
  # If we're not given a version, running update is the correct
  # option. If we are, then running install_package is right.
  unless version
    run_command_with_systems_locale(
      :command => "yum -d0 -e0 -y #{@new_resource.options} update #{name}#{yum_arch}"
    )   
    @yum.flush
  else
    install_package(name, version)
  end
end

#yum_archObject



166
167
168
# File 'lib/chef/provider/package/yum.rb', line 166

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