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.



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/chef/provider/package/rubygems.rb', line 302

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.



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

def gem_env
  @gem_env
end

Instance Method Details

#all_installed_versionsObject



349
350
351
352
353
# File 'lib/chef/provider/package/rubygems.rb', line 349

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

#candidate_versionObject



368
369
370
371
372
373
374
375
376
377
378
# File 'lib/chef/provider/package/rubygems.rb', line 368

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



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

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



414
415
416
# File 'lib/chef/provider/package/rubygems.rb', line 414

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

#gem_dependencyObject



316
317
318
# File 'lib/chef/provider/package/rubygems.rb', line 316

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

#gem_sourcesObject



355
356
357
# File 'lib/chef/provider/package/rubygems.rb', line 355

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



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

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



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

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



359
360
361
362
363
364
365
366
# File 'lib/chef/provider/package/rubygems.rb', line 359

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



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

def logger
  Chef::Log.logger
end

#matching_installed_versionsObject



345
346
347
# File 'lib/chef/provider/package/rubygems.rb', line 345

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

#purge_package(name, version) ⇒ Object



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

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

#remove_package(name, version) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/chef/provider/package/rubygems.rb', line 427

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)


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

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)


380
381
382
383
384
385
386
387
# File 'lib/chef/provider/package/rubygems.rb', line 380

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



441
442
443
444
445
446
447
# File 'lib/chef/provider/package/rubygems.rb', line 441

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



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

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