summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/package/darwinport.rb
blob: ccf0ae82d43804b9fb1e7fe6ad2daad47f5adcb7 (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
Puppet::Type.type(:package).provide :darwinport do
    desc "Package management using DarwinPorts on OS X."

    PORT = "/opt/local/bin/port"
    confine :exists => PORT, :operatingsystem => "Darwin"

    def self.eachpkgashash
        # list out all of the packages
        open("| #{PORT} list installed") { |process|
            regex = %r{(\S+)\s+@(\S+)\s+(\S+)}
            fields = [:name, :version, :location]
            hash = {}

            # now turn each returned line into a package object
            process.each { |line|
                hash.clear

                if match = regex.match(line)
                    fields.zip(match.captures) { |field,value|
                        hash[field] = value
                    }

                    hash.delete :location
                    hash[:ensure] = hash[:version]
                    yield hash.dup
                else
                    raise Puppet::DevError,
                        "Failed to match dpkg line %s" % line
                end
            }
        }
    end

    def self.list
        packages = []

        eachpkgashash do |hash|
            pkg = Puppet.type(:package).installedpkg(hash)
            packages << pkg
        end

        return packages
    end

    def install
        should = @model[:ensure]

        # Seems like you can always say 'upgrade'
        cmd = "#{PORT} upgrade #{@model[:name]}"

        begin
            output = execute(cmd)
        rescue Puppet::ExecutionFailure
            raise Puppet::PackageError.new(output)
        end
    end

    def query
        version = nil
        self.class.eachpkgashash do |hash|
            if hash[:name] == @model[:name]
                return hash
            end
        end

        return nil
    end

    def latest
        info = %x{#{PORT} search '^#{@model[:name]}$' 2>/dev/null}

        if $? != 0 or info =~ /^Error/
            return nil
        end

        ary = info.split(/\s+/)
        version = ary[2].sub(/^@/, '')

        return version
    end

    def uninstall
        cmd = "#{PORT} uninstall #{@model[:name]}"
        output = %x{#{cmd} 2>&1}
        if $? != 0
            raise Puppet::PackageError.new(output)
        end
    end

    def update
        return install()
    end
end

# $Id$