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

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods inherited from Chef::Provider::Package

#action_install, #action_purge, #action_remove, #action_upgrade, #expand_options, #get_preseed_file, #initialize, #preseed_package, #should_remove_package

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #initialize

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Constructor Details

This class inherits a constructor from Chef::Provider::Package

Dynamic Method Handling

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

Instance Method Details

#candidate_versionObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/provider/package/rubygems.rb', line 74

def candidate_version
  return @candidate_version if @candidate_version

  status = popen4("#{gem_binary_path} list --remote #{@new_resource.package_name}#{' --source=' + @new_resource.source if @new_resource.source}") do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      installed_versions = gem_list_parse(line)
      next unless installed_versions
      Chef::Log.debug("candidate_version: remote rubygem(s) available: #{installed_versions.inspect}")
      
      unless installed_versions.empty?
        Chef::Log.debug("candidate_version: setting install candidate version to #{installed_versions.first}")
        @candidate_version = installed_versions.first
      end
    end

  end

  unless status.exitstatus == 0
    raise Chef::Exceptions::Package, "#{gem_binary_path} list --remote failed - #{status.inspect}!"
  end
  @candidate_version
end

#gem_binary_pathObject



38
39
40
41
# File 'lib/chef/provider/package/rubygems.rb', line 38

def gem_binary_path
  path = @new_resource.gem_binary
  path ? path : 'gem'
end

#gem_list_parse(line) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/chef/provider/package/rubygems.rb', line 28

def gem_list_parse(line)
  installed_versions = Array.new
  if line.match("^#{@new_resource.package_name} \\((.+?)\\)$")
    installed_versions = $1.split(/, /)
    installed_versions
  else
    nil
  end
end

#install_package(name, version) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/chef/provider/package/rubygems.rb', line 97

def install_package(name, version)
  src = nil
  if @new_resource.source
    src = "  --source=#{@new_resource.source} --source=http://gems.rubyforge.org"
  end  
  run_command_with_systems_locale(
    :command => "#{gem_binary_path} install #{name} -q --no-rdoc --no-ri -v \"#{version}\"#{src}#{opts}"
  )
end

#load_current_resourceObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/provider/package/rubygems.rb', line 43

def load_current_resource
  @current_resource = Chef::Resource::Package.new(@new_resource.name)
  @current_resource.package_name(@new_resource.package_name)
  @current_resource.version(nil)

  # First, we need to look up whether we have the local gem installed or not
  status = popen4("#{gem_binary_path} list --local #{@new_resource.package_name}") do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      installed_versions = gem_list_parse(line)
      next unless installed_versions
      # If the version we are asking for is installed, make that our current
      # version.  Otherwise, go ahead and use the highest one, which
      # happens to come first in the array.
      if installed_versions.detect { |v| v == @new_resource.version }
        Chef::Log.debug("#{@new_resource.package_name} at version #{@new_resource.version}")
        @current_resource.version(@new_resource.version)
      else
        iv = installed_versions.first
        Chef::Log.debug("#{@new_resource.package_name} at version #{iv}")
        @current_resource.version(iv)
      end
    end
  end
  
  unless status.exitstatus == 0
    raise Chef::Exceptions::Package, "#{gem_binary_path} list --local failed - #{status.inspect}!"
  end
  
  @current_resource
end

#purge_package(name, version) ⇒ Object



123
124
125
# File 'lib/chef/provider/package/rubygems.rb', line 123

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

#remove_package(name, version) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/provider/package/rubygems.rb', line 111

def remove_package(name, version)
  if version
    run_command_with_systems_locale(
      :command => "#{gem_binary_path} uninstall #{name} -q -v \"#{version}\""
    )
  else
    run_command_with_systems_locale(
      :command => "#{gem_binary_path} uninstall #{name} -q -a"
    )
  end
end

#upgrade_package(name, version) ⇒ Object



107
108
109
# File 'lib/chef/provider/package/rubygems.rb', line 107

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