macports.rb

asa hammond, 01/31/2010 08:50 pm

Download (2.3 kB)

 
1
require 'puppet/provider/package'
2

    
3
Puppet::Type.type(:package).provide :macports, :parent => Puppet::Provider::Package do
4
    desc "Package management using Macports on OS X."
5

    
6
    confine :operatingsystem => :darwin
7
    commands :port => "/opt/local/bin/port"
8

    
9
    def self.eachpkgashash
10
        # list out all of the packages
11
        open("| #{command(:port)} list installed") { |process|
12
            regex = %r{(\S+)\s+@(\S+)\s+(\S+)}
13
            fields = [:name, :ensure, :location]
14
            hash = {}
15

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

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

    
25
                    hash.delete :location
26
                    hash[:provider] = self.name
27
                    yield hash.dup
28
                else
29
                    raise Puppet::DevError,
30
                        "Failed to match dpkg line %s" % line
31
                end
32
            }
33
        }
34
    end
35

    
36
    def self.instances
37
        packages = []
38

    
39
        eachpkgashash do |hash|
40
            packages << new(hash)
41
        end
42

    
43
        return packages
44
    end
45

    
46
    def install
47
        should = @resource.should(:ensure)
48

    
49
        output = port "install", @resource[:name]
50
        if output =~ /^Error: No port/
51
            raise Puppet::ExecutionFailure, "Could not find package %s" % @resource[:name]
52
        end
53
    end
54

    
55
    def query
56
        version = nil
57
        self.class.eachpkgashash do |hash|
58
            if hash[:name] == @resource[:name]
59
                return hash
60
            end
61
        end
62

    
63
        return nil
64
    end
65

    
66
    def latest
67
        info = port :list, "#{@resource[:name]}"
68
        if $? != 0 or info =~ /^Error/
69
            return nil
70
        end
71

    
72
        ary = info.split(/\s+/)
73
        version = ary[1].sub(/^@/, '')  # versions are now the second in, not the third ins
74
        
75
    return version
76
    end
77

    
78
    def uninstall
79
        port :uninstall, @resource[:name]
80
    end
81

    
82
    def update
83
        should = @resource.should(:ensure)
84
        output = port "upgrade", @resource[:name]
85
        if output =~ /^Error: No port/
86
            raise Puppet::ExecutionFailure, "Could not find package %s" % @resource[:name]
87
        end
88
    end
89
end
90