summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/package/openbsd.rb
blob: 2a0cb87d8b77aa23dce067fcd0a055a8dce23b28 (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
107
108
# Packaging on OpenBSD.  Doesn't work anywhere else that I know of.
Puppet::Type.type(:package).provide :openbsd do
    desc "OpenBSD's form of ``pkg_add`` support."

    commands :info => "pkg_info", :add => "pkg_add", :delete => "pkg_delete"

    defaultfor :operatingsystem => :openbsd

    def self.list
        packages = []

        begin
            execpipe(listcmd()) do |process|
                # our regex for matching pkg_info output
                regex = %r{^(\S+)-([^-\s]+)\s+(.+)}
                fields = [:name, :ensure, :description]
                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
                        }
                        yup = nil
                        name = hash[:name]

                        hash[:provider] = self.name

                        pkg = Puppet.type(:package).installedpkg(hash)
                        packages << pkg
                    else
                        # Print a warning on lines we can't match, but move
                        # on, since it should be non-fatal
                        warning("Failed to match line %s" % line)
                    end
                }
            end
            # Mark as absent any packages we didn't find
            Puppet.type(:package).each do |pkg|
                unless packages.include? pkg
                    pkg.is = [:ensure, :absent] 
                end
            end

            return packages
        rescue Puppet::ExecutionFailure
            return nil
        end
    end

    def self.listcmd
        "#{command(:info)} -a"
    end

    def install
        should = @model[:ensure]

        unless @model[:source]
            raise Puppet::Error,
                "You must specify a package source for BSD packages"
        end

        cmd = command(:add) + " " + @model[:source]

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

    def query
        hash = {}
        begin
            # list out our specific package
            info = execute("#{command(:info)} #{@model[:name]}")
        rescue Puppet::ExecutionFailure
            raise Puppet::PackageError.new(info)
        end

        # Search for the version info
        if info =~ /Information for #{@model[:name]}-(\S+)/
            hash[:ensure] = $1
        else
            return nil
        end

        # And the description
        if info =~ /Comment:\s*\n(.+)/
            hash[:description] = $1
        end

        return hash
    end

    def uninstall
        cmd = "#{command(:delete)} %s" % @model[:name]
        begin
            output = execute(cmd)
        rescue Puppet::ExecutionFailure
            raise Puppet::PackageError.new(output)
        end
    end
end

# $Id$