summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/package/gem.rb
blob: f6c84c23c0071987726b3524d53bbe8c0f2bb67e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Ruby gems support.
Puppet::Type.type(:package).provide :gem do
    desc "Ruby Gem support.  By default uses remote gems, but you can specify
        the path to a local gem via ``source``."

    commands :gem => "gem"

    def self.gemlist(hash)
        command = "#{command(:gem)} list "

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

        if name = hash[:justme]
            command += name
        end
        begin
            list = execute(command).split("\n\n").collect do |set|
                if gemhash = gemsplit(set)
                    gemhash[:provider] = :gem
                    gemhash
                else
                    nil
                end
            end.reject { |p| p.nil? }
        rescue Puppet::ExecutionFailure => detail
            raise Puppet::Error, "Could not list gems: %s" % detail
        end

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

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

    def self.list(justme = false)
        gemlist(:local => true).each do |hash|
            Puppet::Type.type(:package).installedpkg(hash)
        end
    end

    def install(useversion = true)
        command = "#{command(:gem)} install "
        if (! @model.should(:ensure).is_a? Symbol) and useversion
            command += "-v %s " % @model[:ensure]
        end
        if source = @model[:source]
            command += source
        else
            command += @model[:name]
        end
        begin
            execute(command)
        rescue Puppet::ExecutionFailure => detail
            raise Puppet::Error, "Could not install %s: %s" %
                [@model[:name], detail]
        end
    end

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

        return hash[:ensure]
    end

    def query
        self.class.gemlist(:justme => @model[:name], :local => true)
    end

    def uninstall
        begin
            # Remove everything, including the binaries.
            execute("#{command(:gem)} uninstall -x -a #{@model[:name]}")
        rescue Puppet::ExecutionFailure => detail
            raise Puppet::Error, "Could not uninstall %s: %s" %
                [@model[:name], detail]
        end
    end

    def update
        self.install(false)
    end
end

# $Id$