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

Inherits:
Chef::Provider::Package show all
Includes:
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.



309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/chef/provider/package/rubygems.rb', line 309

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.



303
304
305
# File 'lib/chef/provider/package/rubygems.rb', line 303

def gem_env
  @gem_env
end

Instance Method Details

#all_installed_versionsObject



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

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

#candidate_versionObject



375
376
377
378
379
380
381
382
383
384
385
# File 'lib/chef/provider/package/rubygems.rb', line 375

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



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/chef/provider/package/rubygems.rb', line 332

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 { "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 { "Newest installed version of gem #{gemspec.name} is #{gemspec.version}" }
    gemspec
  else
    logger.debug { "No installed version found for #{gem_dependency.to_s}"}
    nil
  end
end

#gem_binary_pathObject



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

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

#gem_dependencyObject



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

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

#gem_sourcesObject



362
363
364
# File 'lib/chef/provider/package/rubygems.rb', line 362

def gem_sources
  @new_resource.source ? [@new_resource.source, 'http://rubygems.org'] : 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



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/chef/provider/package/rubygems.rb', line 402

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



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

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



366
367
368
369
370
371
372
373
# File 'lib/chef/provider/package/rubygems.rb', line 366

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



305
306
307
# File 'lib/chef/provider/package/rubygems.rb', line 305

def logger
  Chef::Log.logger
end

#matching_installed_versionsObject



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

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

#purge_package(name, version) ⇒ Object



456
457
458
# File 'lib/chef/provider/package/rubygems.rb', line 456

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

#remove_package(name, version) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/chef/provider/package/rubygems.rb', line 434

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)


327
328
329
330
# File 'lib/chef/provider/package/rubygems.rb', line 327

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)


387
388
389
390
391
392
393
394
# File 'lib/chef/provider/package/rubygems.rb', line 387

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



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

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



430
431
432
# File 'lib/chef/provider/package/rubygems.rb', line 430

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