Skip to content

Commit e8ebb05

Browse files
committed
bring OpenBSD package handling up to date to what is in the
ports tree and gets packaged.
1 parent 9b3514e commit e8ebb05

File tree

1 file changed

+61
-89
lines changed

1 file changed

+61
-89
lines changed

lib/puppet/provider/package/openbsd.rb

Lines changed: 61 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
has_feature :upgradeable
2525
has_feature :supports_flavors
2626

27+
mk_resource_methods
28+
2729
def self.instances
2830
packages = []
2931

@@ -46,12 +48,6 @@ def self.instances
4648

4749
packages << new(hash)
4850
hash = {}
49-
else
50-
unless line =~ /Updating the pkgdb/
51-
# Print a warning on lines we can't match, but move
52-
# on, since it should be non-fatal
53-
warning(_("Failed to match line %{line}") % { line: line })
54-
end
5551
end
5652
}
5753
end
@@ -67,26 +63,17 @@ def self.listcmd
6763
end
6864

6965
def latest
70-
parse_pkgconf
71-
72-
if @resource[:source][-1, 1] == ::File::SEPARATOR
73-
e_vars = { 'PKG_PATH' => @resource[:source] }
74-
else
75-
e_vars = {}
76-
end
77-
7866
if @resource[:flavor]
7967
query = "#{@resource[:name]}--#{@resource[:flavor]}"
8068
else
81-
query = @resource[:name]
69+
query = @resource[:name] + "--"
8270
end
8371

84-
output = Puppet::Util.withenv(e_vars) { pkginfo "-Q", query }
85-
version = properties[:ensure]
72+
output = Puppet::Util.withenv({}) {pkginfo "-Q", query}
8673

8774
if output.nil? or output.size == 0 or output =~ /Error from /
8875
debug "Failed to query for #{resource[:name]}"
89-
return version
76+
return properties[:ensure]
9077
else
9178
# Remove all fuzzy matches first.
9279
output = output.split.select { |p| p =~ /^#{resource[:name]}-(\d[^-]*)-?(\w*)/ }.join
@@ -95,21 +82,21 @@ def latest
9582

9683
if output =~ /^#{resource[:name]}-(\d[^-]*)-?(\w*) \(installed\)$/
9784
debug "Package is already the latest available"
98-
version
85+
return properties[:ensure]
9986
else
10087
match = /^(.*)-(\d[^-]*)-?(\w*)$/.match(output)
10188
debug "Latest available for #{resource[:name]}: #{match[2]}"
10289

103-
if version.to_sym == :absent || version.to_sym == :purged
90+
if properties[:ensure].to_sym == :absent
10491
return match[2]
10592
end
10693

107-
vcmp = version.split('.').map(&:to_i) <=> match[2].split('.').map(&:to_i)
94+
vcmp = properties[:ensure].split('.').map{|s|s.to_i} <=> match[2].split('.').map{|s|s.to_i}
10895
if vcmp > 0
10996
# The locally installed package may actually be newer than what a mirror
11097
# has. Log it at debug, but ignore it otherwise.
111-
debug "Package #{resource[:name]} #{version} newer then available #{match[2]}"
112-
version
98+
debug "Package #{resource[:name]} #{properties[:ensure]} newer then available #{match[2]}"
99+
return properties[:ensure]
113100
else
114101
match[2]
115102
end
@@ -120,57 +107,25 @@ def update
120107
install(true)
121108
end
122109

123-
def parse_pkgconf
124-
unless @resource[:source]
125-
if Puppet::FileSystem.exist?("/etc/pkg.conf")
126-
File.open("/etc/pkg.conf", "rb").readlines.each do |line|
127-
matchdata = line.match(/^installpath\s*=\s*(.+)\s*$/i)
128-
if matchdata
129-
@resource[:source] = matchdata[1]
130-
else
131-
matchdata = line.match(/^installpath\s*\+=\s*(.+)\s*$/i)
132-
if matchdata
133-
if @resource[:source].nil?
134-
@resource[:source] = matchdata[1]
135-
else
136-
@resource[:source] += ":" + matchdata[1]
137-
end
138-
end
139-
end
140-
end
141-
142-
unless @resource[:source]
143-
raise Puppet::Error,
144-
_("No valid installpath found in /etc/pkg.conf and no source was set")
145-
end
146-
else
147-
raise Puppet::Error,
148-
_("You must specify a package source or configure an installpath in /etc/pkg.conf")
149-
end
150-
end
151-
end
152-
153110
def install(latest = false)
154111
cmd = []
155112

156-
parse_pkgconf
157-
158-
if @resource[:source][-1, 1] == ::File::SEPARATOR
159-
e_vars = { 'PKG_PATH' => @resource[:source] }
160-
full_name = get_full_name(latest)
161-
else
162-
e_vars = {}
163-
full_name = @resource[:source]
164-
end
165-
113+
cmd << '-r'
166114
cmd << install_options
167-
cmd << full_name
115+
cmd << get_full_name(latest)
168116

169117
if latest
170-
cmd.unshift('-rz')
118+
cmd.unshift('-z')
171119
end
172120

173-
Puppet::Util.withenv(e_vars) { pkgadd cmd.flatten.compact }
121+
# pkg_add(1) doesn't set the return value upon failure so we have to peek
122+
# at it's output to see if something went wrong.
123+
output = Puppet::Util.withenv({}) { pkgadd cmd.flatten.compact }
124+
require 'pp'
125+
pp output
126+
if output =~ /Can't find /
127+
self.fail "pkg_add returned: #{output.chomp}"
128+
end
174129
end
175130

176131
def get_full_name(latest = false)
@@ -179,11 +134,20 @@ def get_full_name(latest = false)
179134
# installing with 'latest', we do need to handle the flavors. This is
180135
# done so we can feed pkg_add(8) the full package name to install to
181136
# prevent ambiguity.
182-
if latest && resource[:flavor]
183-
"#{resource[:name]}--#{resource[:flavor]}"
184-
elsif latest
185-
# Don't depend on get_version for updates.
186-
@resource[:name]
137+
if resource[:flavor]
138+
# If :ensure contains a version, use that instead of looking it up.
139+
# This allows for installing packages with the same stem, but multiple
140+
# version such as postfix-VERSION-flavor.
141+
if @resource[:ensure].to_s =~ /(\d[^-]*)$/
142+
use_version = @resource[:ensure]
143+
else
144+
use_version = ''
145+
end
146+
"#{resource[:name]}-#{use_version}-#{resource[:flavor]}"
147+
elsif resource[:name].to_s.match(/[a-z0-9]%[0-9a-z]/i)
148+
"#{resource[:name]}"
149+
elsif not latest
150+
"#{resource[:name]}--"
187151
else
188152
# If :ensure contains a version, use that instead of looking it up.
189153
# This allows for installing packages with the same stem, but multiple
@@ -194,33 +158,41 @@ def get_full_name(latest = false)
194158
use_version = get_version
195159
end
196160

197-
[@resource[:name], use_version, @resource[:flavor]].join('-').gsub(/-+$/, '')
161+
if resource[:flavor]
162+
[ @resource[:name], use_version, @resource[:flavor]].join('-').gsub(/-+$/, '')
163+
else
164+
[ @resource[:name], use_version ]
165+
end
198166
end
199167
end
200168

201169
def get_version
202-
execpipe([command(:pkginfo), "-I", @resource[:name]]) do |process|
203-
# our regex for matching pkg_info output
204-
regex = /^(.*)-(\d[^-]*)-?(\w*)(.*)$/
205-
master_version = 0
206-
version = -1
207-
208-
process.each_line do |line|
209-
match = regex.match(line.split[0])
210-
next unless match
211-
170+
pkg_search_name = @resource[:name]
171+
unless pkg_search_name.match(/[a-z0-9]%[0-9a-z]/i) and not @resource[:flavor]
172+
# we are only called when no flavor is specified
173+
# so append '--' to the :name to avoid patch versions on flavors
174+
pkg_search_name << "--"
175+
end
176+
# our regex for matching pkg_info output
177+
regex = /^(.*)-(\d[^-]*)[-]?(\w*)(.*)$/
178+
master_version = 0
179+
version = -1
180+
181+
# pkg_info -I might return multiple lines, i.e. flavors
182+
matching_pkgs = pkginfo("-I", "pkg_search_name")
183+
matching_pkgs.each_line do |line|
184+
if match = regex.match(line.split[0])
212185
# now we return the first version, unless ensure is latest
213186
version = match.captures[1]
214187
return version unless @resource[:ensure] == "latest"
215-
216188
master_version = version unless master_version > version
217189
end
218-
219-
return master_version unless master_version == 0
220-
return '' if version == -1
221-
222-
raise Puppet::Error, _("%{version} is not available for this package") % { version: version }
223190
end
191+
192+
return master_version unless master_version == 0
193+
return '' if version == -1
194+
raise Puppet::Error, _("%{version} is not available for this package") % { version: version }
195+
224196
rescue Puppet::ExecutionFailure
225197
nil
226198
end
@@ -239,7 +211,7 @@ def install_options
239211
end
240212

241213
def uninstall_options
242-
join_options(resource[:uninstall_options])
214+
[join_options(resource[:uninstall_options])]
243215
end
244216

245217
def uninstall

0 commit comments

Comments
 (0)