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
|
Puppet::Type.type(:package).provide :dpkg do
desc "Package management via ``dpkg``. Because this only uses ``dpkg``
and not ``apt``, you must specify the source of any packages you want
to manage."
commands :dpkg => "/usr/bin/dpkg"
commands :dpkgquery => "/usr/bin/dpkg-query"
def self.list
packages = []
# list out all of the packages
cmd = "#{command(:dpkgquery)} -W --showformat '${Status} ${Package} ${Version}\\n'"
Puppet.debug "Executing '%s'" % cmd
execpipe(cmd) do |process|
# our regex for matching dpkg output
regex = %r{^(\S+ +\S+ +\S+) (\S+) (\S*)$}
fields = [:status, :name, :ensure]
hash = {}
# now turn each returned line into a package object
process.each { |line|
if match = regex.match(line)
hash.clear
fields.zip(match.captures) { |field,value|
hash[field] = value
}
hash[:provider] = self.name
packages.push Puppet.type(:package).installedpkg(hash)
else
Puppet.warning "Failed to match dpkg-query line %s" %
line.inspect
end
}
end
return packages
end
def install
unless file = @model[:source]
raise ArgumentError, "You cannot install dpkg packages without a source"
end
dpkg "-i", file
end
def query
packages = []
fields = [:desired, :error, :status, :name, :ensure]
hash = {}
# list out our specific package
begin
output = dpkgquery("-W", "--showformat",
'${Status} ${Package} ${Version}\\n', @model[:name]
)
rescue Puppet::ExecutionFailure
# dpkg-query exits 1 if the package is not found.
return {:ensure => :absent, :status => 'missing',
:name => @model[:name], :error => 'ok'}
end
# Our regex for matching dpkg-query output. We could probably just
# use split here, but I'm not positive that dpkg-query will never
# return whitespace.
regex = %r{^(\S+) (\S+) (\S+) (\S+) (\S*)$}
line = output.split("\n").shift.chomp
if match = regex.match(line)
fields.zip(match.captures) { |field,value|
hash[field] = value
}
else
notice "Failed to handle dpkg-query line %s" % line.inspect
return {:ensure => :absent, :status => 'missing',
:name => @model[:name], :error => 'ok'}
end
if hash[:error] != "ok"
raise Puppet::PackageError.new(
"Package %s, version %s is in error state: %s" %
[hash[:name], hash[:ensure], hash[:error]]
)
end
# DPKG can discuss packages that are no longer installed, so allow that.
if hash[:status] != "installed"
hash[:ensure] = :absent
end
return hash
end
def uninstall
dpkg "-r", @model[:name]
end
end
# $Id$
|