Class: Chef::Provider::Package::Rubygems

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

Defined Under Namespace

Classes: AlternateGemEnvironment, CurrentGemEnvironment, GemEnvironment

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#shell_out, #shell_out!

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 = nil) ⇒ Rubygems

Returns a new instance of Rubygems.



305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/chef/provider/package/rubygems.rb', line 305

def initialize(new_resource, run_context=nil)
  super
  if new_resource.gem_binary
    if new_resource.options && new_resource.options.kind_of?(Hash)
      msg =  "options cannot be given as a hash when using an explicit gem_binary\n"
      msg << "in #{new_resource} from #{new_resource.source_line}"
      raise ArgumentError, msg
    end
    @gem_env = AlternateGemEnvironment.new(new_resource.gem_binary)
  else
    @gem_env = CurrentGemEnvironment.new
  end
end

Dynamic Method Handling

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

Instance Attribute Details

#gem_envObject (readonly)

Returns the value of attribute gem_env.



297
298
299
# File 'lib/chef/provider/package/rubygems.rb', line 297

def gem_env
  @gem_env
end

Instance Method Details

#all_installed_versionsObject



352
353
354
355
356
# File 'lib/chef/provider/package/rubygems.rb', line 352

def all_installed_versions
  @all_installed_versions ||= begin
    @gem_env.installed_versions(Gem::Dependency.new(gem_dependency.name, '>= 0'))
  end
end

#candidate_versionObject



371
372
373
374
375
376
377
378
379
380
381
# File 'lib/chef/provider/package/rubygems.rb', line 371

def candidate_version
  @candidate_version ||= begin
    if target_version_already_installed?
      nil
    elsif source_is_remote?
      @gem_env.candidate_version_from_remote(gem_dependency, *gem_sources).to_s
    else
      @gem_env.candidate_version_from_file(gem_dependency, @new_resource.source).to_s
    end
  end
end

#current_versionObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/chef/provider/package/rubygems.rb', line 328

def current_version
  #raise 'todo'
  # If one or more matching versions are installed, the newest of them
  # is the current version
  if !matching_installed_versions.empty?
    gemspec = matching_installed_versions.last
    logger.debug { "#{@new_resource} found installed gem #{gemspec.name} version #{gemspec.version} matching #{gem_dependency}"}
    gemspec
  # If no version matching the requirements exists, the latest installed
  # version is the current version.
  elsif !all_installed_versions.empty?
    gemspec = all_installed_versions.last
    logger.debug { "#{@new_resource} newest installed version of gem #{gemspec.name} is #{gemspec.version}" }
    gemspec
  else
    logger.debug { "#{@new_resource} no installed version found for #{gem_dependency.to_s}"}
    nil
  end
end

#gem_binary_pathObject



417
418
419
# File 'lib/chef/provider/package/rubygems.rb', line 417

def gem_binary_path
  @new_resource.gem_binary || 'gem'
end

#gem_dependencyObject



319
320
321
# File 'lib/chef/provider/package/rubygems.rb', line 319

def gem_dependency
  Gem::Dependency.new(@new_resource.package_name, @new_resource.version)
end

#gem_sourcesObject



358
359
360
# File 'lib/chef/provider/package/rubygems.rb', line 358

def gem_sources
  @new_resource.source ? Array(@new_resource.source) : nil
end

#install_package(name, version) ⇒ Object

Installs the gem, using either the gems API or shelling out to ‘gem` according to the following criteria:

  1. Use gems API (Gem::DependencyInstaller) by default

  2. shell out to ‘gem install` when a String of options is given

  3. use gems API with options if a hash of options is given



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/chef/provider/package/rubygems.rb', line 398

def install_package(name, version)
  if source_is_remote? && @new_resource.gem_binary.nil?
    if @new_resource.options.nil?
      @gem_env.install(gem_dependency, :sources => gem_sources)
    elsif @new_resource.options.kind_of?(Hash)
      options = @new_resource.options
      options[:sources] = gem_sources
      @gem_env.install(gem_dependency, options)
    else
      install_via_gem_command(name, version)
    end
  elsif @new_resource.gem_binary.nil?
    @gem_env.install(@new_resource.source)
  else
    install_via_gem_command(name,version)
  end
  true
end

#install_via_gem_command(name, version) ⇒ Object



421
422
423
424
# File 'lib/chef/provider/package/rubygems.rb', line 421

def install_via_gem_command(name, version)
  src = @new_resource.source && "  --source=#{@new_resource.source} --source=http://rubygems.org"
  shell_out!("#{gem_binary_path} install #{name} -q --no-rdoc --no-ri -v \"#{version}\"#{src}#{opts}", :env=>nil)
end

#load_current_resourceObject



362
363
364
365
366
367
368
369
# File 'lib/chef/provider/package/rubygems.rb', line 362

def load_current_resource
  @current_resource = Chef::Resource::Package::GemPackage.new(@new_resource.name)
  @current_resource.package_name(@new_resource.package_name)
  if current_spec = current_version
    @current_resource.version(current_spec.version.to_s)
  end
  @current_resource
end

#loggerObject



299
300
301
# File 'lib/chef/provider/package/rubygems.rb', line 299

def logger
  Chef::Log.logger
end

#matching_installed_versionsObject



348
349
350
# File 'lib/chef/provider/package/rubygems.rb', line 348

def matching_installed_versions
  @matching_installed_versions ||= @gem_env.installed_versions(gem_dependency)
end

#purge_package(name, version) ⇒ Object



452
453
454
# File 'lib/chef/provider/package/rubygems.rb', line 452

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

#remove_package(name, version) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/chef/provider/package/rubygems.rb', line 430

def remove_package(name, version)
  if @new_resource.gem_binary.nil?
    if @new_resource.options.nil?
      @gem_env.uninstall(name, version)
    elsif @new_resource.options.kind_of?(Hash)
      @gem_env.uninstall(name, version, @new_resource.options)
    else
      uninstall_via_gem_command(name, version)
    end
  else
    uninstall_via_gem_command(name, version)
  end
end

#source_is_remote?Boolean

Returns:

  • (Boolean)


323
324
325
326
# File 'lib/chef/provider/package/rubygems.rb', line 323

def source_is_remote?
  return true if @new_resource.source.nil?
  URI.parse(@new_resource.source).absolute?
end

#target_version_already_installed?Boolean

Returns:

  • (Boolean)


383
384
385
386
387
388
389
390
# File 'lib/chef/provider/package/rubygems.rb', line 383

def target_version_already_installed?
  return false unless @current_resource && @current_resource.version
  return false if @current_resource.version.nil?
  # in the future we could support squiggly requirements like "~> 1.2.0"
  # for now, the behavior when using anything other than exact 
  # requirements is undefined.
  Gem::Requirement.new(@new_resource.version).satisfied_by?(Gem::Version.new(@current_resource.version))
end

#uninstall_via_gem_command(name, version) ⇒ Object



444
445
446
447
448
449
450
# File 'lib/chef/provider/package/rubygems.rb', line 444

def uninstall_via_gem_command(name, version)
  if version
    shell_out!("#{gem_binary_path} uninstall #{name} -q -x -I -v \"#{version}\"#{opts}", :env=>nil)
  else
    shell_out!("#{gem_binary_path} uninstall #{name} -q -x -I -a#{opts}", :env=>nil)
  end
end

#upgrade_package(name, version) ⇒ Object



426
427
428
# File 'lib/chef/provider/package/rubygems.rb', line 426

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