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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
# The container class for implementations.
class Puppet::Provider
include Puppet::Util
include Puppet::Util::Errors
include Puppet::Util::Warnings
extend Puppet::Util::Warnings
Puppet::Util.logmethods(self, true)
class << self
# Include the util module so we have access to things like 'binary'
include Puppet::Util, Puppet::Util::Docs
include Puppet::Util::Logging
attr_accessor :name
# The source parameter exists so that providers using the same
# source can specify this, so reading doesn't attempt to read the
# same package multiple times.
attr_writer :source
# LAK 2007-05-09: Keep the model stuff around for backward compatibility
attr_reader :model
attr_accessor :resource_type
attr_writer :doc
end
# LAK 2007-05-09: Keep the model stuff around for backward compatibility
attr_reader :model
attr_accessor :resource
def self.command(name)
name = symbolize(name)
if defined?(@commands) and command = @commands[name]
# nothing
elsif superclass.respond_to? :command and command = superclass.command(name)
# nothing
else
raise Puppet::DevError, "No command %s defined for provider %s" %
[name, self.name]
end
if command == :missing
return nil
end
command
end
# Define commands that are not optional.
def self.commands(hash)
optional_commands(hash) do |name, path|
confine :exists => path
end
end
def self.confine(hash)
hash.each do |p,v|
if v.is_a? Array
@confines[p] += v
else
@confines[p] << v
end
end
end
# Does this implementation match all of the default requirements? If
# defaults are empty, we return false.
def self.default?
return false if @defaults.empty?
if @defaults.find do |fact, values|
values = [values] unless values.is_a? Array
if fval = Facter.value(fact).to_s and fval != ""
fval = fval.to_s.downcase.intern
else
return false
end
# If any of the values match, we're a default.
if values.find do |value| fval == value.to_s.downcase.intern end
false
else
true
end
end
return false
else
return true
end
end
# Store how to determine defaults.
def self.defaultfor(hash)
hash.each do |d,v|
@defaults[d] = v
end
end
def self.defaultnum
@defaults.length
end
def self.initvars
@defaults = {}
@commands = {}
@origcommands = {}
@confines = Hash.new do |hash, key|
hash[key] = []
end
end
# The method for returning a list of provider instances. Note that it returns providers, preferably with values already
# filled in, not resources.
def self.instances
raise Puppet::DevError, "Provider %s has not defined the 'instances' class method" % self.name
end
# Create the methods for a given command.
def self.make_command_methods(name)
# Now define a method for that command
unless metaclass.method_defined? name
meta_def(name) do |*args|
unless command(name)
raise Puppet::Error, "Command %s is missing" % name
end
if args.empty?
cmd = [command(name)]
else
cmd = [command(name)] + args
end
# This might throw an ExecutionFailure, but the system above
# will catch it, if so.
return execute(cmd)
end
# And then define an instance method that just calls the class method.
# We need both, so both instances and classes can easily run the commands.
unless method_defined? name
define_method(name) do |*args|
self.class.send(name, *args)
end
end
end
end
# Create getter/setter methods for each property our resource type supports.
# They all get stored in @property_hash. This method is useful
# for those providers that use prefetch and flush.
def self.mkmodelmethods
warnonce "Provider.mkmodelmethods is deprecated; use Provider.mk_resource_methods"
mk_resource_methods
end
# Create getter/setter methods for each property our resource type supports.
# They all get stored in @property_hash. This method is useful
# for those providers that use prefetch and flush.
def self.mk_resource_methods
[resource_type.validproperties, resource_type.parameters].flatten.each do |attr|
attr = symbolize(attr)
next if attr == :name
define_method(attr) do
@property_hash[attr] || :absent
end
define_method(attr.to_s + "=") do |val|
@property_hash[attr] = val
end
end
end
self.initvars
# Define one or more binaries we'll be using. If a block is passed, yield the name
# and path to the block (really only used by 'commands').
def self.optional_commands(hash)
hash.each do |name, path|
name = symbolize(name)
@origcommands[name] = path
# Try to find the full path (or verify already-full paths); otherwise
# store that the command is missing so we know it's defined but absent.
if tmp = binary(path)
path = tmp
@commands[name] = path
else
@commands[name] = :missing
end
if block_given?
yield(name, path)
end
# Now define the class and instance methods.
make_command_methods(name)
end
end
# Retrieve the data source. Defaults to the provider name.
def self.source
unless defined? @source
@source = self.name
end
@source
end
# Check whether this implementation is suitable for our platform.
def self.suitable?(short = true)
# A single false result is sufficient to turn the whole thing down.
# We don't return 'true' until the very end, though, so that every
# confine is tested.
missing = {}
@confines.each do |check, values|
case check
when :exists:
values.each do |value|
unless value and FileTest.exists? value
debug "Not suitable: missing %s" % value
return false if short
missing[:exists] ||= []
missing[:exists] << value
end
end
when :true:
values.each do |v|
debug "Not suitable: false value"
unless v
return false if short
missing[:true] ||= 0
missing[:true] += 1
end
end
when :false:
values.each do |v|
debug "Not suitable: true value"
if v and short
return false if short
missing[:false] ||= 0
missing[:false] += 1
end
end
else # Just delegate everything else to facter
if result = Facter.value(check)
result = result.to_s.downcase.intern
found = values.find do |v|
result == v.to_s.downcase.intern
end
unless found
debug "Not suitable: %s not in %s" % [check, values]
return false if short
missing[:facter] ||= {}
missing[:facter][check] = values
end
else
return false if short
missing[:facter] ||= {}
missing[:facter][check] = values
end
end
end
if short
return true
else
return missing
end
end
# Does this provider support the specified parameter?
def self.supports_parameter?(param)
if param.is_a?(Class)
klass = param
else
unless klass = resource_type.attrclass(param)
raise Puppet::DevError, "'%s' is not a valid parameter for %s" % [param, resource_type.name]
end
end
return true unless features = klass.required_features
if satisfies?(*features)
return true
else
return false
end
end
def self.to_s
unless defined? @str
if self.resource_type
@str = "%s provider %s" % [resource_type.name, self.name]
else
@str = "unattached provider %s" % [self.name]
end
end
@str
end
dochook(:defaults) do
if @defaults.length > 0
return " Default for " + @defaults.collect do |f, v|
"``#{f}`` == ``#{v}``"
end.join(" and ") + "."
end
end
dochook(:commands) do
if @origcommands.length > 0
return " Required binaries: " + @origcommands.collect do |n, c|
"``#{c}``"
end.join(", ") + "."
end
end
dochook(:features) do
if features().length > 0
return " Supported features: " + features().collect do |f|
"``#{f}``"
end.join(", ") + "."
end
end
# Remove the reference to the resource, so GC can clean up.
def clear
@resource = nil
@model = nil
end
# Retrieve a named command.
def command(name)
self.class.command(name)
end
# Get a parameter value.
def get(param)
@property_hash[symbolize(param)] || :absent
end
def initialize(resource = nil)
if resource.is_a?(Hash)
# We don't use a duplicate here, because some providers (ParsedFile, at least)
# use the hash here for later events.
@property_hash = resource
elsif resource
@resource = resource if resource
# LAK 2007-05-09: Keep the model stuff around for backward compatibility
@model = resource
@property_hash = {}
else
@property_hash = {}
end
end
def name
if n = @property_hash[:name]
return n
elsif self.resource
resource.name
else
raise Puppet::DevError, "No resource and no name in property hash in %s instance" % self.class.name
end
end
# Set passed params as the current values.
def set(params)
params.each do |param, value|
@property_hash[symbolize(param)] = value
end
end
def to_s
"%s(provider=%s)" % [@resource.to_s, self.class.name]
end
end
|