Class: Chef::Resource
- Inherits:
-
Object
show all
- Extended by:
- Mixin::ConvertToClassName
- Includes:
- Mixin::CheckHelper, Mixin::ConvertToClassName, Mixin::Deprecation, Mixin::Language, Mixin::ParamsValidate
- Defined in:
- lib/chef/resource.rb,
lib/chef/resource/csh.rb,
lib/chef/resource/env.rb,
lib/chef/resource/git.rb,
lib/chef/resource/log.rb,
lib/chef/resource/scm.rb,
lib/chef/resource/bash.rb,
lib/chef/resource/cron.rb,
lib/chef/resource/file.rb,
lib/chef/resource/link.rb,
lib/chef/resource/ohai.rb,
lib/chef/resource/perl.rb,
lib/chef/resource/ruby.rb,
lib/chef/resource/user.rb,
lib/chef/resource/group.rb,
lib/chef/resource/mdadm.rb,
lib/chef/resource/mount.rb,
lib/chef/resource/route.rb,
lib/chef/resource/deploy.rb,
lib/chef/resource/python.rb,
lib/chef/resource/script.rb,
lib/chef/resource/execute.rb,
lib/chef/resource/package.rb,
lib/chef/resource/service.rb,
lib/chef/resource/erl_call.rb,
lib/chef/resource/ifconfig.rb,
lib/chef/resource/template.rb,
lib/chef/resource/directory.rb,
lib/chef/resource/breakpoint.rb,
lib/chef/resource/ruby_block.rb,
lib/chef/resource/subversion.rb,
lib/chef/resource/apt_package.rb,
lib/chef/resource/gem_package.rb,
lib/chef/resource/remote_file.rb,
lib/chef/resource/rpm_package.rb,
lib/chef/resource/yum_package.rb,
lib/chef/resource/dpkg_package.rb,
lib/chef/resource/http_request.rb,
lib/chef/resource/cookbook_file.rb,
lib/chef/resource/pacman_package.rb,
lib/chef/resource/deploy_revision.rb,
lib/chef/resource/freebsd_package.rb,
lib/chef/resource/portage_package.rb,
lib/chef/resource/solaris_package.rb,
lib/chef/resource/macports_package.rb,
lib/chef/resource/remote_directory.rb,
lib/chef/resource/timestamped_deploy.rb,
lib/chef/resource/easy_install_package.rb
Direct Known Subclasses
Breakpoint, Cron, Deploy, Directory, Env, ErlCall, Execute, File, Group, HttpRequest, Ifconfig, Link, Log, Mdadm, Mount, Ohai, Package, Route, RubyBlock, Scm, Service, User
Defined Under Namespace
Classes: AptPackage, Bash, Breakpoint, CookbookFile, Cron, Csh, Deploy, DeployBranch, DeployRevision, Directory, DpkgPackage, EasyInstallPackage, Env, ErlCall, Execute, File, FreebsdPackage, GemPackage, Git, Group, HttpRequest, Ifconfig, Link, Log, MacportsPackage, Mdadm, Mount, Notification, Ohai, Package, PacmanPackage, Perl, PortagePackage, Python, RemoteDirectory, RemoteFile, Route, RpmPackage, Ruby, RubyBlock, Scm, Script, Service, SolarisPackage, Subversion, Template, TimestampedDeploy, User, YumPackage
Constant Summary
collapse
- HIDDEN_IVARS =
[:@allowed_actions, :@resource_name, :@source_line, :@run_context, :@name, :@node]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#action(arg = nil) ⇒ Object
-
#add_notification(action, resources, timing = :delayed) ⇒ Object
-
#epic_fail(arg = nil) ⇒ Object
-
#ignore_failure(arg = nil) ⇒ Object
-
#initialize(name, run_context = nil) ⇒ Resource
constructor
A new instance of Resource.
-
#is(*args) ⇒ Object
-
#load_prior_resource ⇒ Object
-
#method_missing(method_symbol, *args, &block) ⇒ Object
If an unknown method is invoked, determine whether the enclosing Provider’s lexical scope can fulfill the request.
-
#name(name = nil) ⇒ Object
-
#node ⇒ Object
-
#noop(tf = nil) ⇒ Object
-
#not_if(arg = nil, args = {}, &blk) ⇒ Object
-
#notifies(*args) ⇒ Object
-
#notifies_delayed(action, resource_spec) ⇒ Object
-
#notifies_immediately(action, resource_spec) ⇒ Object
-
#only_if(arg = nil, args = {}, &blk) ⇒ Object
-
#resolve_notification_references ⇒ Object
Iterates over all immediate and delayed notifications, calling resolve_resource_reference on each in turn, causing them to resolve lazy/forward references.
-
#resources(*args) ⇒ Object
-
#run_action(action) ⇒ Object
-
#subscribes(action, resources, timing = :delayed) ⇒ Object
-
#supports(args = {}) ⇒ Object
-
#to_hash ⇒ Object
-
#to_json(*a) ⇒ Object
Serialize this object as a hash.
-
#to_s ⇒ Object
-
#to_text ⇒ Object
-
#updated? ⇒ Boolean
-
#updated_by_last_action(true_or_false) ⇒ Object
-
#updated_by_last_action? ⇒ Boolean
convert_to_class_name, convert_to_snake_case, filename_to_qualified_string, snake_case_basename
#deprecated_ivar
#data_bag, #data_bag_item, #platform?, #search, #value_for_platform
#set_or_return, #validate
#set_if_args
Constructor Details
#initialize(name, run_context = nil) ⇒ Resource
Returns a new instance of Resource.
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/chef/resource.rb', line 101
def initialize(name, run_context=nil)
@name = name
@run_context = run_context
@noop = nil
@before = nil
@params = Hash.new
@provider = nil
@allowed_actions = [ :nothing ]
@action = :nothing
@updated = false
@updated_by_last_action = false
@supports = {}
@ignore_failure = false
@not_if = nil
@not_if_args = {}
@only_if = nil
@only_if_args = {}
@immediate_notifications = Array.new
@delayed_notifications = Array.new
@source_line = nil
@node = run_context ? deprecated_ivar(run_context.node, :node, :warn) : nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *args, &block) ⇒ Object
If an unknown method is invoked, determine whether the enclosing Provider’s lexical scope can fulfill the request. E.g. This happens when the Resource’s block invokes new_resource.
140
141
142
143
144
145
146
|
# File 'lib/chef/resource.rb', line 140
def method_missing(method_symbol, *args, &block)
if enclosing_provider && enclosing_provider.respond_to?(method_symbol)
enclosing_provider.send(method_symbol, *args, &block)
else
raise NoMethodError, "undefined method `#{method_symbol.to_s}' for #{self.class.to_s}"
end
end
|
Instance Attribute Details
#allowed_actions ⇒ Object
Returns the value of attribute allowed_actions.
83
84
85
|
# File 'lib/chef/resource.rb', line 83
def allowed_actions
@allowed_actions
end
|
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
85
86
87
|
# File 'lib/chef/resource.rb', line 85
def cookbook_name
@cookbook_name
end
|
#delayed_notifications ⇒ Object
Returns the value of attribute delayed_notifications.
99
100
101
|
# File 'lib/chef/resource.rb', line 99
def delayed_notifications
@delayed_notifications
end
|
#enclosing_provider ⇒ Object
Returns the value of attribute enclosing_provider.
87
88
89
|
# File 'lib/chef/resource.rb', line 87
def enclosing_provider
@enclosing_provider
end
|
Each notify entry is a resource/action pair, modeled as an Struct with a #resource and #action member
98
99
100
|
# File 'lib/chef/resource.rb', line 98
def immediate_notifications
@immediate_notifications
end
|
#not_if_args ⇒ Object
Returns the value of attribute not_if_args.
93
94
95
|
# File 'lib/chef/resource.rb', line 93
def not_if_args
@not_if_args
end
|
#only_if_args ⇒ Object
Returns the value of attribute only_if_args.
94
95
96
|
# File 'lib/chef/resource.rb', line 94
def only_if_args
@only_if_args
end
|
#params ⇒ Object
Returns the value of attribute params.
81
82
83
|
# File 'lib/chef/resource.rb', line 81
def params
@params
end
|
#provider(arg = nil) ⇒ Object
Returns the value of attribute provider.
82
83
84
|
# File 'lib/chef/resource.rb', line 82
def provider
@provider
end
|
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
86
87
88
|
# File 'lib/chef/resource.rb', line 86
def recipe_name
@recipe_name
end
|
#resource_name ⇒ Object
Returns the value of attribute resource_name.
92
93
94
|
# File 'lib/chef/resource.rb', line 92
def resource_name
@resource_name
end
|
#run_context ⇒ Object
Returns the value of attribute run_context.
84
85
86
|
# File 'lib/chef/resource.rb', line 84
def run_context
@run_context
end
|
#source_line ⇒ Object
Returns the value of attribute source_line.
88
89
90
|
# File 'lib/chef/resource.rb', line 88
def source_line
@source_line
end
|
#updated ⇒ Object
Returns the value of attribute updated.
90
91
92
|
# File 'lib/chef/resource.rb', line 90
def updated
@updated
end
|
Class Method Details
.attribute(attr_name, validation_opts = {}) ⇒ Object
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
# File 'lib/chef/resource.rb', line 423
def attribute(attr_name, validation_opts={})
shim_method=<<-SHIM
def #{attr_name}(arg=nil)
_set_or_return_#{attr_name}(arg)
end
SHIM
class_eval(shim_method)
define_method("_set_or_return_#{attr_name.to_s}".to_sym) do |arg|
set_or_return(attr_name.to_sym, arg, validation_opts)
end
end
|
.build_from_file(cookbook_name, filename) ⇒ Object
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
# File 'lib/chef/resource.rb', line 440
def build_from_file(cookbook_name, filename)
rname = filename_to_qualified_string(cookbook_name, filename)
class_name = convert_to_class_name(rname)
overriding = Chef::Resource.const_defined?(class_name)
Chef::Log.info("#{class_name} light-weight resource already initialized -- overriding!") if overriding
new_resource_class = Class.new self do |cls|
def initialize(name, run_context)
super(name, run_context)
end
@actions_to_create = []
class << cls
include Chef::Mixin::FromFile
def actions_to_create
@actions_to_create
end
define_method(:actions) do |*action_names|
actions_to_create.push(*action_names)
end
end
cls.class_from_file(filename)
old_init = instance_method(:initialize)
define_method(:initialize) do |name, *optional_args|
args_run_context = optional_args.shift
@resource_name = rname.to_sym
old_init.bind(self).call(name, args_run_context)
allowed_actions.push(self.class.actions_to_create).flatten!
end
end
class_name = convert_to_class_name(rname)
Chef::Resource.const_set(class_name, new_resource_class)
Chef::Log.debug("Loaded contents of #{filename} into a resource named #{rname} defined in Chef::Resource::#{class_name}")
new_resource_class
end
|
.json_create(o) ⇒ Object
413
414
415
416
417
418
419
|
# File 'lib/chef/resource.rb', line 413
def json_create(o)
resource = self.new(o["instance_vars"]["@name"])
o["instance_vars"].each do |k,v|
resource.instance_variable_set(k.to_sym, v)
end
resource
end
|
.provider_base(arg = nil) ⇒ Object
Resources that want providers namespaced somewhere other than Chef::Provider can set the namespace with provider_base
Ex:
class MyResource < Chef::Resource
provider_base Chef::Provider::Deploy
end
501
502
503
504
|
# File 'lib/chef/resource.rb', line 501
def provider_base(arg=nil)
@provider_base ||= arg
@provider_base ||= Chef::Provider
end
|
Instance Method Details
#action(arg = nil) ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/chef/resource.rb', line 184
def action(arg=nil)
if arg
action_list = arg.kind_of?(Array) ? arg : [ arg ]
action_list = action_list.collect { |a| a.to_sym }
action_list.each do |action|
validate(
{
:action => action,
},
{
:action => { :kind_of => Symbol, :equal_to => @allowed_actions },
}
)
end
@action = action_list
else
@action
end
end
|
#add_notification(action, resources, timing = :delayed) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/chef/resource.rb', line 253
def add_notification(action, resources, timing=:delayed)
resources = [resources].flatten
resources.each do |resource|
case timing.to_s
when 'delayed'
notifies_delayed(action, resource)
when 'immediate', 'immediately'
notifies_immediately(action, resource)
else
raise ArgumentError, "invalid timing: #{timing} for notifies(#{action}, #{resources.inspect}, #{timing}) resource #{self} "\
"Valid timings are: :delayed, :immediate, :immediately"
end
end
true
end
|
#epic_fail(arg = nil) ⇒ Object
226
227
228
|
# File 'lib/chef/resource.rb', line 226
def epic_fail(arg=nil)
ignore_failure(arg)
end
|
#ignore_failure(arg = nil) ⇒ Object
218
219
220
221
222
223
224
|
# File 'lib/chef/resource.rb', line 218
def ignore_failure(arg=nil)
set_or_return(
:ignore_failure,
arg,
:kind_of => [ TrueClass, FalseClass ]
)
end
|
#is(*args) ⇒ Object
298
299
300
301
302
303
304
|
# File 'lib/chef/resource.rb', line 298
def is(*args)
if args.size == 1
args.first
else
return *args
end
end
|
#load_prior_resource ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/chef/resource.rb', line 148
def load_prior_resource
begin
prior_resource = run_context.resource_collection.lookup(self.to_s)
Chef::Log.debug("Setting #{self.to_s} to the state of the prior #{self.to_s}")
prior_resource.instance_variables.each do |iv|
unless iv.to_sym == :@source_line || iv.to_sym == :@action
self.instance_variable_set(iv, prior_resource.instance_variable_get(iv))
end
end
true
rescue Chef::Exceptions::ResourceNotFound => e
true
end
end
|
#name(name = nil) ⇒ Object
204
205
206
207
208
209
|
# File 'lib/chef/resource.rb', line 204
def name(name=nil)
set_if_args(@name, name) do
raise ArgumentError, "name must be a string!" unless name.kind_of?(String)
@name = name
end
end
|
#node ⇒ Object
133
134
135
|
# File 'lib/chef/resource.rb', line 133
def node
run_context && run_context.node
end
|
#noop(tf = nil) ⇒ Object
211
212
213
214
215
216
|
# File 'lib/chef/resource.rb', line 211
def noop(tf=nil)
set_if_args(@noop, tf) do
raise ArgumentError, "noop must be true or false!" unless tf == true || tf == false
@noop = tf
end
end
|
#not_if(arg = nil, args = {}, &blk) ⇒ Object
357
358
359
360
361
362
363
364
365
366
|
# File 'lib/chef/resource.rb', line 357
def not_if(arg=nil, args = {}, &blk)
if Kernel.block_given?
@not_if = blk
@not_if_args = args
else
@not_if = arg if arg
@not_if_args = args if arg
end
@not_if
end
|
#notifies(*args) ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/chef/resource.rb', line 230
def notifies(*args)
unless ( args.size > 0 && args.size < 4)
raise ArgumentError, "Wrong number of arguments for notifies: should be 1-3 arguments, you gave #{args.inspect}"
end
if args.size > 1 add_notification(*args)
else
notifications = args.flatten
notifications.each do |resources_notifications|
resources_notifications.each do |resource, notification|
action, timing = notification[0], notification[1]
Chef::Log.debug "adding notification from resource #{self} to `#{resource.inspect}' => `#{notification.inspect}'"
add_notification(action, resource, timing)
end
end
end
rescue NoMethodError
Chef::Log.fatal("Error processing notifies(#{args.inspect}) on #{self}")
raise
end
|
#notifies_delayed(action, resource_spec) ⇒ Object
282
283
284
|
# File 'lib/chef/resource.rb', line 282
def notifies_delayed(action, resource_spec)
@delayed_notifications << Notification.new(resource_spec, action, self)
end
|
278
279
280
|
# File 'lib/chef/resource.rb', line 278
def notifies_immediately(action, resource_spec)
@immediate_notifications << Notification.new(resource_spec, action, self)
end
|
#only_if(arg = nil, args = {}, &blk) ⇒ Object
346
347
348
349
350
351
352
353
354
355
|
# File 'lib/chef/resource.rb', line 346
def only_if(arg=nil, args = {}, &blk)
if Kernel.block_given?
@only_if = blk
@only_if_args = args
else
@only_if = arg if arg
@only_if_args = args if arg
end
@only_if
end
|
#resolve_notification_references ⇒ Object
Iterates over all immediate and delayed notifications, calling resolve_resource_reference on each in turn, causing them to resolve lazy/forward references.
273
274
275
276
|
# File 'lib/chef/resource.rb', line 273
def resolve_notification_references
@immediate_notifications.each { |n| n.resolve_resource_reference(run_context.resource_collection) }
@delayed_notifications.each {|n| n.resolve_resource_reference(run_context.resource_collection) }
end
|
#resources(*args) ⇒ Object
286
287
288
|
# File 'lib/chef/resource.rb', line 286
def resources(*args)
run_context.resource_collection.find(*args)
end
|
#run_action(action) ⇒ Object
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
# File 'lib/chef/resource.rb', line 368
def run_action(action)
updated_by_last_action(false)
if only_if
unless Chef::Mixin::Command.only_if(only_if, only_if_args)
Chef::Log.debug("Skipping #{self} due to only_if")
return
end
end
if not_if
unless Chef::Mixin::Command.not_if(not_if, not_if_args)
Chef::Log.debug("Skipping #{self} due to not_if")
return
end
end
provider = Chef::Platform.provider_for_resource(self)
provider.load_current_resource
provider.send("action_#{action}")
end
|
#subscribes(action, resources, timing = :delayed) ⇒ Object
290
291
292
293
294
295
296
|
# File 'lib/chef/resource.rb', line 290
def subscribes(action, resources, timing=:delayed)
resources = [resources].flatten
resources.each do |resource|
resource.notifies(action, self, timing)
end
true
end
|
#supports(args = {}) ⇒ Object
163
164
165
166
167
168
169
|
# File 'lib/chef/resource.rb', line 163
def supports(args={})
if args.any?
@supports = args
else
@supports
end
end
|
#to_hash ⇒ Object
337
338
339
340
341
342
343
344
|
# File 'lib/chef/resource.rb', line 337
def to_hash
instance_vars = Hash.new
self.instance_variables.each do |iv|
key = iv.to_s.sub(/^@/,'').to_sym
instance_vars[key] = self.instance_variable_get(iv) unless (key == :run_context) || (key == :node)
end
instance_vars
end
|
#to_json(*a) ⇒ Object
Serialize this object as a hash
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/chef/resource.rb', line 323
def to_json(*a)
instance_vars = Hash.new
self.instance_variables.each do |iv|
unless iv == "@run_context"
instance_vars[iv] = self.instance_variable_get(iv)
end
end
results = {
'json_class' => self.class.name,
'instance_vars' => instance_vars
}
results.to_json(*a)
end
|
#to_s ⇒ Object
306
307
308
|
# File 'lib/chef/resource.rb', line 306
def to_s
"#{@resource_name}[#{@name}]"
end
|
#to_text ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
|
# File 'lib/chef/resource.rb', line 310
def to_text
ivars = instance_variables.map { |ivar| ivar.to_sym } - HIDDEN_IVARS
text = "# Declared in #{@source_line}\n"
text << convert_to_snake_case(self.class.name, 'Chef::Resource') + "(\"#{name}\") do\n"
ivars.each do |ivar|
if (value = instance_variable_get(ivar)) && !(value.respond_to?(:empty?) && value.empty?)
text << " #{ivar.to_s.sub(/^@/,'')}(#{value.inspect})\n"
end
end
text << "end\n"
end
|
#updated? ⇒ Boolean
407
408
409
|
# File 'lib/chef/resource.rb', line 407
def updated?
updated
end
|
#updated_by_last_action(true_or_false) ⇒ Object
398
399
400
401
|
# File 'lib/chef/resource.rb', line 398
def updated_by_last_action(true_or_false)
@updated ||= true_or_false
@updated_by_last_action = true_or_false
end
|
#updated_by_last_action? ⇒ Boolean
403
404
405
|
# File 'lib/chef/resource.rb', line 403
def updated_by_last_action?
@updated_by_last_action
end
|