summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/package/yum.rb
blob: a9427061c875f1ad44bb8489ad9ff202e4e751d7 (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
Puppet::Type.type(:package).provide :yum, :parent => :rpm, :source => :rpm do
    desc "Support via ``yum``."

    has_feature :versionable

    commands :yum => "yum", :rpm => "rpm", :python => "python"
    
    YUMHELPER = File::join(File::dirname(__FILE__), "yumhelper.py")

    class << self
        attr_reader :updates
    end

    if command('rpm')
        confine :true => begin
                rpm('--version')
           rescue Puppet::ExecutionFailure
               false
           else
               true
           end
    end

    defaultfor :operatingsystem => [:fedora, :centos, :redhat]

    def self.prefetch(packages)
        @updates = {}
        if Process.euid != 0
            raise Puppet::Error, "The yum provider can only be used as root"
        end
        super
        python(YUMHELPER).each_line do |l|
            l.chomp!
            next if l.empty?
            if l[0,4] == "_pkg"
                hash = nevra_to_hash(l[5..-1])
                [hash[:name], "#{hash[:name]}.#{hash[:arch]}"].each do |n|
                    @updates[n] ||= []
                    @updates[n] << hash
                end
            end
        end
    end

    def install
        should = @resource.should(:ensure)
        self.debug "Ensuring => #{should}"
        wanted = @resource[:name]

        # XXX: We don't actually deal with epochs here.
        case should
        when true, false, Symbol
            # pass
            should = nil
        else
            # Add the package version
            wanted += "-%s" % should
        end

        output = yum "-d", "0", "-e", "0", "-y", :install, wanted

        is = self.query
        unless is
            raise Puppet::Error, "Could not find package %s" % self.name
        end

        # FIXME: Should we raise an exception even if should == :latest
        # and yum updated us to a version other than @param_hash[:ensure] ?
        if should && should != is[:ensure]
            raise Puppet::Error, "Failed to update to version #{should}, got version #{is[:ensure]} instead"
        end
    end

    # What's the latest package version available?
    def latest
        upd = self.class.updates[@resource[:name]]
        unless upd.nil?
            # FIXME: there could be more than one update for a package
            # because of multiarch
            upd = upd[0]
            return "#{upd[:version]}-#{upd[:release]}"
        else
            # Yum didn't find updates, pretend the current
            # version is the latest
            if properties[:ensure] == :absent
                raise Puppet::DevError, "Tried to get latest on a missing package"
            end
            return properties[:ensure]
        end
    end

    def update
        # Install in yum can be used for update, too
        self.install
    end
end

# $Id$