summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/package/openbsd.rb
blob: f76c371761c87abd4f702706e7441e07f3479d0b (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
require 'puppet/provider/package'

# Packaging on OpenBSD.  Doesn't work anywhere else that I know of.
Puppet::Type.type(:package).provide :openbsd, :parent => Puppet::Provider::Package do
    desc "OpenBSD's form of ``pkg_add`` support."

    commands :pkginfo => "pkg_info", :pkgadd => "pkg_add", :pkgdelete => "pkg_delete"

    defaultfor :operatingsystem => :openbsd
    confine :operatingsystem => :openbsd

    def self.instances
        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|
                    if match = regex.match(line)
                        fields.zip(match.captures) { |field,value|
                            hash[field] = value
                        }
                        yup = nil
                        name = hash[:name]

                        hash[:provider] = self.name

                        packages << new(hash)
                        hash = {}
                    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

            return packages
        rescue Puppet::ExecutionFailure
            return nil
        end
    end

    def self.listcmd
        [command(:pkginfo), " -a"]
    end

    def install
        should = @resource.should(:ensure)

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

        pkgadd @resource[:source]
    end

    def query
        hash = {}
        info = pkginfo @resource[:name]

        # Search for the version info
        if info =~ /Information for (inst:)?#{@resource[: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
        pkgdelete @resource[:name]
    end
end