blt04 / puppet-rvm

A puppet module for installing and using RVM (Ruby Version Manager)
Other
242 stars 280 forks source link

Some minor edits #2

Closed jamtur01 closed 13 years ago

jamtur01 commented 13 years ago

Some minor edits and fixes:

  1. Fixed path otherwise you'll get a default provider failure when Puppet can't find the rvm command
  2. Fixed the gem selection - you'll see this is also upstream in the gem provider
  3. Added no-ri and no-rdoc to speed up installation.

I would have done a pull request but it appears already having a repo called puppet-rvm messes that up.

require 'puppet/provider/package'
require 'uri'

# Ruby gems support.
Puppet::Type.type(:rvm_gem).provide(:gem) do
  desc "Ruby Gem support using RVM."

  commands :rvmcmd => "/usr/local/rvm/bin/rvm"

  def ruby_version
    resource[:ruby_version]
  end

  def gembinary
    [command(:rvmcmd), ruby_version, "gem"]
  end

  def gemlist(hash)
    command = gembinary + ['list']

    if hash[:local]
      command << "--local"
    else
      command << "--remote"
    end

    if name = hash[:justme]
      command << name + "$"
    end

    begin
      list = execute(command).split("\n").collect do |set|
        if gemhash = self.class.gemsplit(set)
          gemhash[:provider] = :gem
          gemhash
        else
          nil
        end
      end.compact
    rescue Puppet::ExecutionFailure => detail
      raise Puppet::Error, "Could not list gems: #{detail}"
    end

    if hash[:justme]
      return list.shift
    else
      return list
    end
  end

  def self.gemsplit(desc)
    case desc
    when /^\*\*\*/, /^\s*$/, /^\s+/; return nil
    when /^(\S+)\s+\((.+)\)/
      name = $1
      version = $2.split(/,\s*/)[0]
      return {
        :name => name,
        :ensure => version
      }
    else
      Puppet.warning "Could not match #{desc}"
      nil
    end
  end

  def install(useversion = true)
    command = gembinary + ['install']
    command << "-v" << resource[:ensure] if (! resource[:ensure].is_a? Symbol) and useversion
    # Always include dependencies
    command << "--include-dependencies" << "--no-rdoc" << "no-ri"

    if source = resource[:source]
      begin
        uri = URI.parse(source)
      rescue => detail
        fail "Invalid source '#{uri}': #{detail}"
      end

      case uri.scheme
      when nil
        # no URI scheme => interpret the source as a local file
        command << source
      when /file/i
        command << uri.path
      when 'puppet'
        # we don't support puppet:// URLs (yet)
        raise Puppet::Error.new("puppet:// URLs are not supported as gem sources")
      else
        # interpret it as a gem repository
        command << "--source" << "#{source}" << resource[:name]
      end
    else
      command << resource[:name]
    end

    output = execute(command)
    # Apparently some stupid gem versions don't exit non-0 on failure
    self.fail "Could not install: #{output.chomp}" if output.include?("ERROR")
  end

  def latest
    # This always gets the latest version available.
    hash = gemlist(:justme => resource[:name])

    hash[:ensure]
  end

  def query
    gemlist(:justme => resource[:name], :local => true)
  end

  def uninstall
    execute(gembinary + ["uninstall", "-x", "-a", resource[:name]])
  end

  def update
    self.install(false)
  end
end
blt04 commented 13 years ago

Thanks for the fixes. Regarding 2, where do you see the upstream changes?

jamtur01 commented 13 years ago

They are from https://github.com/puppetlabs/puppet/blob/master/lib/puppet/provider/package/gem.rb for ticket http://projects.puppetlabs.com/issues/3127.

Also in your manifest system.pp your "creates => '/usr/local/bin/rvm'," is invalid I think. Should be "creates => '/usr/local/rvm',"

Cheers

blt04 commented 13 years ago

James, I couldn't quite figure out the fix you included in the issue. I've applied the change from the bug you mentioned and fixed the rvm paths in these commits: c740d3b0c18f0e936b03598c84bbd3fd69f37e49 37a3fc2aa1f425cc932921637a46642edf58b04b

I'm not sure if command detail is a "diff gone bad" or something else. Please let me know if you see something else. Thanks!