Class: Chef::Provider::Package::Rubygems
Defined Under Namespace
Classes: AlternateGemEnvironment, CurrentGemEnvironment, GemEnvironment
Constant Summary
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary collapse
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Instance Method Summary
collapse
#action_install, #action_purge, #action_reconfig, #action_remove, #action_upgrade, #as_array, #define_resource_requirements, #expand_options, #get_preseed_file, #have_any_matching_version?, #preseed_package, #preseed_resource, #reconfig_package, #removing_package?, #whyrun_supported?
#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale
#popen4
#popen4
#action_nothing, #converge_by, #define_resource_requirements, #events, #node, #process_resource_requirements, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, #whyrun_mode?, #whyrun_supported?
#node_map, #provides, #provides?
#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited
Constructor Details
#initialize(new_resource, run_context = nil) ⇒ Rubygems
Returns a new instance of Rubygems.
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/chef/provider/package/rubygems.rb', line 368
def initialize(new_resource, run_context=nil)
super
@cleanup_gem_env = true
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)
Chef::Log.debug("#{@new_resource} using gem '#{new_resource.gem_binary}'")
elsif is_omnibus? && (!@new_resource.instance_of? Chef::Resource::ChefGem)
if new_resource.options && new_resource.options.kind_of?(Hash)
msg = [
"Gem options must be passed to gem_package as a string instead of a hash when",
"using this installation of Chef because it runs with its own packaged Ruby. A hash",
"may only be used when installing a gem to the same Ruby installation that Chef is",
"running under. See https://docs.chef.io/resource_gem_package.html for more information.",
"Error raised at #{new_resource} from #{new_resource.source_line}",
].join("\n")
raise ArgumentError, msg
end
gem_location = find_gem_by_path
@new_resource.gem_binary gem_location
@gem_env = AlternateGemEnvironment.new(gem_location)
Chef::Log.debug("#{@new_resource} using gem '#{gem_location}'")
else
@gem_env = CurrentGemEnvironment.new
@cleanup_gem_env = false
Chef::Log.debug("#{@new_resource} using gem from running ruby environment")
end
end
|
Instance Attribute Details
#cleanup_gem_env ⇒ Object
Returns the value of attribute cleanup_gem_env.
357
358
359
|
# File 'lib/chef/provider/package/rubygems.rb', line 357
def cleanup_gem_env
@cleanup_gem_env
end
|
#gem_env ⇒ Object
Returns the value of attribute gem_env.
356
357
358
|
# File 'lib/chef/provider/package/rubygems.rb', line 356
def gem_env
@gem_env
end
|
Instance Method Details
#all_installed_versions ⇒ Object
464
465
466
467
468
|
# File 'lib/chef/provider/package/rubygems.rb', line 464
def all_installed_versions
@all_installed_versions ||= begin
@gem_env.installed_versions(Gem::Dependency.new(gem_dependency.name, '>= 0'))
end
end
|
#candidate_version ⇒ Object
490
491
492
493
494
495
496
497
498
499
500
|
# File 'lib/chef/provider/package/rubygems.rb', line 490
def candidate_version
@candidate_version ||= begin
if target_version_already_installed?(@current_resource.version, @new_resource.version)
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
|
#cleanup_after_converge ⇒ Object
483
484
485
486
487
488
|
# File 'lib/chef/provider/package/rubygems.rb', line 483
def cleanup_after_converge
if @cleanup_gem_env
logger.debug { "#{@new_resource} resetting gem environment to default" }
Gem.clear_paths
end
end
|
#current_version ⇒ Object
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
# File 'lib/chef/provider/package/rubygems.rb', line 440
def 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
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
|
#find_gem_by_path ⇒ Object
417
418
419
420
421
422
423
|
# File 'lib/chef/provider/package/rubygems.rb', line 417
def find_gem_by_path
Chef::Log.debug("#{@new_resource} searching for 'gem' binary in path: #{ENV['PATH']}")
separator = ::File::ALT_SEPARATOR ? ::File::ALT_SEPARATOR : ::File::SEPARATOR
path_to_first_gem = ENV['PATH'].split(::File::PATH_SEPARATOR).select { |path| ::File.exists?(path + separator + "gem") }.first
raise Chef::Exceptions::FileNotFound, "Unable to find 'gem' binary in path: #{ENV['PATH']}" if path_to_first_gem.nil?
path_to_first_gem + separator + "gem"
end
|
#gem_binary_path ⇒ Object
534
535
536
|
# File 'lib/chef/provider/package/rubygems.rb', line 534
def gem_binary_path
@new_resource.gem_binary || 'gem'
end
|
#gem_dependency ⇒ Object
425
426
427
|
# File 'lib/chef/provider/package/rubygems.rb', line 425
def gem_dependency
Gem::Dependency.new(@new_resource.package_name, @new_resource.version)
end
|
#gem_sources ⇒ Object
470
471
472
|
# File 'lib/chef/provider/package/rubygems.rb', line 470
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:
-
Use gems API (Gem::DependencyInstaller) by default
-
shell out to ‘gem install` when a String of options is given
-
use gems API with options if a hash of options is given
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
# File 'lib/chef/provider/package/rubygems.rb', line 515
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
# File 'lib/chef/provider/package/rubygems.rb', line 538
def install_via_gem_command(name, version)
if @new_resource.source =~ /\.gem$/i
name = @new_resource.source
elsif @new_resource.clear_sources
src = ' --clear-sources'
src << (@new_resource.source && " --source=#{@new_resource.source}" || '')
else
src = @new_resource.source && " --source=#{@new_resource.source} --source=https://rubygems.org"
end
if !version.nil? && version.length > 0
shell_out!("#{gem_binary_path} install #{name} -q --no-rdoc --no-ri -v \"#{version}\"#{src}#{opts}", :env=>nil)
else
shell_out!("#{gem_binary_path} install \"#{name}\" -q --no-rdoc --no-ri #{src}#{opts}", :env=>nil)
end
end
|
#is_omnibus? ⇒ Boolean
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/chef/provider/package/rubygems.rb', line 403
def is_omnibus?
if RbConfig::CONFIG['bindir'] =~ %r!/opt/(opscode|chef)/embedded/bin!
Chef::Log.debug("#{@new_resource} detected omnibus installation in #{RbConfig::CONFIG['bindir']}")
true
elsif RbConfig::CONFIG['bindir'].sub(/^[\w]:/, '') == "/opscode/chef/embedded/bin"
Chef::Log.debug("#{@new_resource} detected omnibus installation in #{RbConfig::CONFIG['bindir']}")
true
else
false
end
end
|
#load_current_resource ⇒ Object
474
475
476
477
478
479
480
481
|
# File 'lib/chef/provider/package/rubygems.rb', line 474
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
|
#logger ⇒ Object
359
360
361
|
# File 'lib/chef/provider/package/rubygems.rb', line 359
def logger
Chef::Log.logger
end
|
#matching_installed_versions ⇒ Object
460
461
462
|
# File 'lib/chef/provider/package/rubygems.rb', line 460
def matching_installed_versions
@matching_installed_versions ||= @gem_env.installed_versions(gem_dependency)
end
|
#purge_package(name, version) ⇒ Object
580
581
582
|
# File 'lib/chef/provider/package/rubygems.rb', line 580
def purge_package(name, version)
remove_package(name, version)
end
|
#remove_package(name, version) ⇒ Object
558
559
560
561
562
563
564
565
566
567
568
569
570
|
# File 'lib/chef/provider/package/rubygems.rb', line 558
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
429
430
431
432
433
434
435
436
437
438
|
# File 'lib/chef/provider/package/rubygems.rb', line 429
def source_is_remote?
return true if @new_resource.source.nil?
scheme = URI.parse(@new_resource.source).scheme
scheme = nil if scheme =~ /^[a-z]$/
%w{http https}.include?(scheme)
rescue URI::InvalidURIError
Chef::Log.debug("#{@new_resource} failed to parse source '#{@new_resource.source}' as a URI, assuming a local path")
false
end
|
#target_version_already_installed?(current_version, new_version) ⇒ Boolean
502
503
504
505
506
507
|
# File 'lib/chef/provider/package/rubygems.rb', line 502
def target_version_already_installed?(current_version, new_version)
return false unless current_version
return false if new_version.nil?
Gem::Requirement.new(new_version).satisfied_by?(Gem::Version.new(current_version))
end
|
#uninstall_via_gem_command(name, version) ⇒ Object
572
573
574
575
576
577
578
|
# File 'lib/chef/provider/package/rubygems.rb', line 572
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
554
555
556
|
# File 'lib/chef/provider/package/rubygems.rb', line 554
def upgrade_package(name, version)
install_package(name, version)
end
|