-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
pkg.rb
300 lines (262 loc) · 11.8 KB
/
pkg.rb
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# frozen_string_literal: true
require_relative '../../../puppet/provider/package'
Puppet::Type.type(:package).provide :pkg, :parent => Puppet::Provider::Package do
desc "OpenSolaris image packaging system. See pkg(5) for more information.
This provider supports the `install_options` attribute, which allows
command-line flags to be passed to pkg. These options should be specified as an
array where each element is either a string or a hash."
# https://docs.oracle.com/cd/E19963-01/html/820-6572/managepkgs.html
# A few notes before we start:
# Opensolaris pkg has two slightly different formats (as of now.)
# The first one is what is distributed with the Solaris 11 Express 11/10 dvd
# The latest one is what you get when you update package.
# To make things more interesting, pkg version just returns a sha sum.
# dvd: pkg version => 052adf36c3f4
# updated: pkg version => 630e1ffc7a19
# Thankfully, Solaris has not changed the commands to be used.
# TODO: We still have to allow packages to specify a preferred publisher.
has_feature :versionable
has_feature :upgradable
has_feature :holdable
has_feature :install_options
commands :pkg => "/usr/bin/pkg"
confine 'os.family' => :solaris
defaultfor 'os.family' => :solaris, :kernelrelease => ['5.11', '5.12']
def self.instances
pkg(:list, '-Hv').split("\n").map { |l| new(parse_line(l)) }
end
# The IFO flag field is just what it names, the first field can have either
# i_nstalled or -, and second field f_rozen or -, and last
# o_bsolate or r_rename or -
# so this checks if the installed field is present, and also verifies that
# if not the field is -, else we don't know what we are doing and exit with
# out doing more damage.
def self.ifo_flag(flags)
(
case flags[0..0]
when 'i'
{ :status => 'installed' }
when '-'
{ :status => 'known' }
else
raise ArgumentError, _('Unknown format %{resource_name}: %{full_flags}[%{bad_flag}]') %
{ resource_name: name, full_flags: flags, bad_flag: flags[0..0] }
end
).merge(
case flags[1..1]
when 'f'
{ :mark => :hold }
when '-'
{}
else
raise ArgumentError, _('Unknown format %{resource_name}: %{full_flags}[%{bad_flag}]') %
{ resource_name: name, full_flags: flags, bad_flag: flags[1..1] }
end
)
end
# The UFOXI field is the field present in the older pkg
# (solaris 2009.06 - snv151a)
# similar to IFO, UFOXI is also an either letter or -
# u_pdate indicates that an update for the package is available.
# f_rozen(n/i) o_bsolete x_cluded(n/i) i_constrained(n/i)
# note that u_pdate flag may not be trustable due to constraints.
# so we dont rely on it
# Frozen was never implemented in UFOXI so skipping frozen here.
def self.ufoxi_flag(flags)
{}
end
# pkg state was present in the older version of pkg (with UFOXI) but is
# no longer available with the IFO field version. When it was present,
# it was used to indicate that a particular version was present (installed)
# and later versions were known. Note that according to the pkg man page,
# known never lists older versions of the package. So we can rely on this
# field to make sure that if a known is present, then the pkg is upgradable.
def self.pkg_state(state)
case state
when /installed/
{ :status => 'installed' }
when /known/
{ :status => 'known' }
else
raise ArgumentError, _('Unknown format %{resource_name}: %{state}') % { resource_name: name, state: state }
end
end
# Here is (hopefully) the only place we will have to deal with multiple
# formats of output for different pkg versions.
def self.parse_line(line)
(case line.chomp
# FMRI IFO
# pkg://omnios/SUNWcs@0.5.11,5.11-0.151008:20131204T022241Z ---
when %r{^pkg://([^/]+)/([^@]+)@(\S+) +(...)$}
{ :publisher => Regexp.last_match(1), :name => Regexp.last_match(2), :ensure => Regexp.last_match(3) }.merge ifo_flag(Regexp.last_match(4))
# FMRI STATE UFOXI
# pkg://solaris/SUNWcs@0.5.11,5.11-0.151.0.1:20101105T001108Z installed u----
when %r{^pkg://([^/]+)/([^@]+)@(\S+) +(\S+) +(.....)$}
{ :publisher => Regexp.last_match(1), :name => Regexp.last_match(2), :ensure => Regexp.last_match(3) }.merge pkg_state(Regexp.last_match(4)).merge(ufoxi_flag(Regexp.last_match(5)))
else
raise ArgumentError, _('Unknown line format %{resource_name}: %{parse_line}') % { resource_name: name, parse_line: line }
end).merge({ :provider => name })
end
def hold
pkg(:freeze, @resource[:name])
end
def unhold
r = exec_cmd(command(:pkg), 'unfreeze', @resource[:name])
raise Puppet::Error, _("Unable to unfreeze %{package}") % { package: r[:out] } unless [0, 4].include? r[:exit]
end
def insync?(is)
# this is called after the generic version matching logic (insync? for the
# type), so we only get here if should != is, and 'should' is a version
# number. 'is' might not be, though.
should = @resource[:ensure]
# NB: it is apparently possible for repository administrators to publish
# packages which do not include build or branch versions, but component
# version must always be present, and the timestamp is added by pkgsend
# publish.
if /^[0-9.]+(,[0-9.]+)?(-[0-9.]+)?:[0-9]+T[0-9]+Z$/ !~ should
# We have a less-than-explicit version string, which we must accept for
# backward compatibility. We can find the real version this would match
# by asking pkg for the all matching versions, and selecting the first
# installable one [0]; this can change over time when remote repositories
# are updated, but the principle of least astonishment should still hold:
# if we allow users to specify less-than-explicit versions, the
# functionality should match that of the package manager.
#
# [0]: we could simply get the newest matching version with 'pkg list
# -n', but that isn't always correct, since it might not be installable.
# If that were the case we could potentially end up returning false for
# insync? here but not actually changing the package version in install
# (ie. if the currently installed version is the latest matching version
# that is installable, we would falsely conclude here that since the
# installed version is not the latest matching version, we're not in
# sync). 'pkg list -a' instead of '-n' would solve this, but
# unfortunately it doesn't consider downgrades 'available' (eg. with
# installed foo@1.0, list -a foo@0.9 would fail).
name = @resource[:name]
potential_matches = pkg(:list, '-Hvfa', "#{name}@#{should}").split("\n").map { |l| self.class.parse_line(l) }
n = potential_matches.length
if n > 1
warning(_("Implicit version %{should} has %{n} possible matches") % { should: should, n: n })
end
potential_matches.each { |p|
command = is == :absent ? 'install' : 'update'
options = ['-n']
options.concat(join_options(@resource[:install_options])) if @resource[:install_options]
begin
unhold if properties[:mark] == :hold
status = exec_cmd(command(:pkg), command, *options, "#{name}@#{p[:ensure]}")[:exit]
ensure
hold if properties[:mark] == :hold
end
case status
when 4
# if the first installable match would cause no changes, we're in sync
return true
when 0
warning(_("Selecting version '%{version}' for implicit '%{should}'") % { version: p[:ensure], should: should })
@resource[:ensure] = p[:ensure]
return false
end
}
raise Puppet::DevError, _("No version of %{name} matching %{should} is installable, even though the package is currently installed") %
{ name: name, should: should }
end
false
end
# Return the version of the package. Note that the bug
# http://defect.opensolaris.org/bz/show_bug.cgi?id=19159%
# notes that we can't use -Ha for the same even though the manual page reads that way.
def latest
# Refresh package metadata before looking for latest versions
pkg(:refresh)
lines = pkg(:list, "-Hvn", @resource[:name]).split("\n")
# remove certificate expiration warnings from the output, but report them
cert_warnings = lines.select { |line| line =~ /^Certificate/ }
unless cert_warnings.empty?
Puppet.warning(_("pkg warning: %{warnings}") % { warnings: cert_warnings.join(', ') })
end
lst = lines.select { |line| line !~ /^Certificate/ }.map { |line| self.class.parse_line(line) }
# Now we know there is a newer version. But is that installable? (i.e are there any constraints?)
# return the first known we find. The only way that is currently available is to do a dry run of
# pkg update and see if could get installed (`pkg update -n res`).
known = lst.find { |p| p[:status] == 'known' }
if known
options = ['-n']
options.concat(join_options(@resource[:install_options])) if @resource[:install_options]
return known[:ensure] if exec_cmd(command(:pkg), 'update', *options, @resource[:name])[:exit].zero?
end
# If not, then return the installed, else nil
(lst.find { |p| p[:status] == 'installed' } || {})[:ensure]
end
# install the package and accept all licenses.
def install(nofail = false)
name = @resource[:name]
should = @resource[:ensure]
is = query
if is[:ensure].to_sym == :absent
command = 'install'
else
command = 'update'
end
args = ['--accept']
if Puppet::Util::Package.versioncmp(Puppet.runtime[:facter].value('os.release.full'), '11.2') >= 0
args.push('--sync-actuators-timeout', '900')
end
args.concat(join_options(@resource[:install_options])) if @resource[:install_options]
unless should.is_a? Symbol
name += "@#{should}"
end
unhold if properties[:mark] == :hold
begin
tries = 1
# pkg install exits with code 7 when the image is currently in use by another process and cannot be modified
r = exec_cmd(command(:pkg), command, *args, name)
while r[:exit] == 7
if tries > 4
raise Puppet::Error, _("Pkg could not install %{name} after %{tries} tries. Aborting run") % { name: name, tries: tries }
end
sleep 2**tries
tries += 1
r = exec_cmd(command(:pkg), command, *args, name)
end
ensure
hold if @resource[:mark] == :hold
end
return r if nofail
raise Puppet::Error, _("Unable to update %{package}") % { package: r[:out] } if r[:exit] != 0
end
# uninstall the package. The complication comes from the -r_ecursive flag which is no longer
# present in newer package version.
def uninstall
cmd = [:uninstall]
case (pkg :version).chomp
when /052adf36c3f4/
cmd << '-r'
end
cmd << @resource[:name]
unhold if properties[:mark] == :hold
begin
pkg cmd
rescue StandardError, LoadError => e
hold if properties[:mark] == :hold
raise e
end
end
# update the package to the latest version available
def update
r = install(true)
# 4 == /No updates available for this image./
return if [0, 4].include? r[:exit]
raise Puppet::Error, _("Unable to update %{package}") % { package: r[:out] }
end
# list a specific package
def query
r = exec_cmd(command(:pkg), 'list', '-Hv', @resource[:name])
return { :ensure => :absent, :name => @resource[:name] } if r[:exit] != 0
self.class.parse_line(r[:out])
end
def exec_cmd(*cmd)
output = Puppet::Util::Execution.execute(cmd, :failonfail => false, :combine => true)
{ :out => output, :exit => output.exitstatus }
end
end