Class: Chef::Resource
- 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/chef_gem.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/conditional.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_platform_map.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/smartos_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, ChefGem, Conditional, 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, PlatformMap, PortagePackage, Python, RemoteDirectory, RemoteFile, Route, RpmPackage, Ruby, RubyBlock, Scm, Script, Service, SmartOSPackage, SolarisPackage, Subversion, Template, TimestampedDeploy, User, YumPackage
Constant Summary collapse
- FORBIDDEN_IVARS =
[:@run_context, :@node, :@not_if, :@only_if]
- HIDDEN_IVARS =
[:@allowed_actions, :@resource_name, :@source_line, :@run_context, :@name, :@node]
Instance Attribute Summary collapse
-
#allowed_actions ⇒ Object
Returns the value of attribute allowed_actions.
-
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
-
#delayed_notifications ⇒ Object
readonly
Returns the value of attribute delayed_notifications.
-
#enclosing_provider ⇒ Object
Returns the value of attribute enclosing_provider.
-
#immediate_notifications ⇒ Object
readonly
Each notify entry is a resource/action pair, modeled as an Struct with a #resource and #action member.
-
#not_if_args ⇒ Object
readonly
Returns the value of attribute not_if_args.
-
#only_if_args ⇒ Object
readonly
Returns the value of attribute only_if_args.
-
#params ⇒ Object
Returns the value of attribute params.
-
#provider(arg = nil) ⇒ Object
Returns the value of attribute provider.
-
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
-
#resource_name ⇒ Object
readonly
Returns the value of attribute resource_name.
-
#retries(arg = nil) ⇒ Object
Returns the value of attribute retries.
-
#retry_delay(arg = nil) ⇒ Object
Returns the value of attribute retry_delay.
-
#run_context ⇒ Object
Returns the value of attribute run_context.
-
#source_line ⇒ Object
Returns the value of attribute source_line.
-
#updated ⇒ Object
Returns the value of attribute updated.
Class Method Summary collapse
- .attribute(attr_name, validation_opts = {}) ⇒ Object
- .build_from_file(cookbook_name, filename, run_context) ⇒ Object
- .json_create(o) ⇒ Object
- .platform_map ⇒ Object
-
.provider_base(arg = nil) ⇒ Object
Resources that want providers namespaced somewhere other than Chef::Provider can set the namespace with
provider_baseEx: class MyResource < Chef::Resource provider_base Chef::Provider::Deploy # …other stuff end. -
.provides(short_name, opts = {}) ⇒ Object
Maps a short_name (and optionally a platform and version) to a Chef::Resource.
-
.resource_for_node(short_name, node) ⇒ Object
Returns a resource based on a short_name and a node’s platform and version.
-
.resource_for_platform(short_name, platform = nil, version = nil) ⇒ Object
Returns a resource based on a short_name anda platform and version.
Instance Method Summary collapse
- #action(arg = nil) ⇒ Object
- #add_notification(action, resources, timing = :delayed) ⇒ Object
-
#after_created ⇒ Object
Hook to allow a resource to run specific code after creation.
-
#as_json(*a) ⇒ Object
as_json does most of the to_json heavy lifted.
- #defined_at ⇒ Object
- #epic_fail(arg = nil) ⇒ Object
- #ignore_failure(arg = nil) ⇒ Object
-
#initialize(name, run_context = nil) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
- #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(command = nil, opts = {}, &block) ⇒ Object
If command is a block, returns false if the block returns true, true if it returns false.
- #notifies(*args) ⇒ Object
- #notifies_delayed(action, resource_spec) ⇒ Object
- #notifies_immediately(action, resource_spec) ⇒ Object
-
#only_if(command = nil, opts = {}, &block) ⇒ Object
If command is a block, returns true if the block returns true, false if it returns false.
-
#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
-
#should_skip? ⇒ Boolean
Evaluates not_if and only_if conditionals.
- #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
Methods included from Mixin::ConvertToClassName
convert_to_class_name, convert_to_snake_case, filename_to_qualified_string, snake_case_basename
Methods included from Mixin::Deprecation
Methods included from Mixin::Language
#data_bag, #data_bag_item, #platform?, #platform_family?, #search, #value_for_platform, #value_for_platform_family
Methods included from Mixin::ParamsValidate
Methods included from Mixin::CheckHelper
Constructor Details
#initialize(name, run_context = nil) ⇒ Resource
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/chef/resource.rb', line 105 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 @retries = 0 @retry_delay = 2 @not_if = [] @only_if = [] @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.
144 145 146 147 148 149 150 |
# File 'lib/chef/resource.rb', line 144 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.
85 86 87 |
# File 'lib/chef/resource.rb', line 85 def allowed_actions @allowed_actions end |
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
87 88 89 |
# File 'lib/chef/resource.rb', line 87 def cookbook_name @cookbook_name end |
#delayed_notifications ⇒ Object (readonly)
Returns the value of attribute delayed_notifications.
103 104 105 |
# File 'lib/chef/resource.rb', line 103 def delayed_notifications @delayed_notifications end |
#enclosing_provider ⇒ Object
Returns the value of attribute enclosing_provider.
89 90 91 |
# File 'lib/chef/resource.rb', line 89 def enclosing_provider @enclosing_provider end |
#immediate_notifications ⇒ Object (readonly)
Each notify entry is a resource/action pair, modeled as an Struct with a #resource and #action member
102 103 104 |
# File 'lib/chef/resource.rb', line 102 def immediate_notifications @immediate_notifications end |
#not_if_args ⇒ Object (readonly)
Returns the value of attribute not_if_args.
97 98 99 |
# File 'lib/chef/resource.rb', line 97 def not_if_args @not_if_args end |
#only_if_args ⇒ Object (readonly)
Returns the value of attribute only_if_args.
98 99 100 |
# File 'lib/chef/resource.rb', line 98 def only_if_args @only_if_args end |
#params ⇒ Object
Returns the value of attribute params.
83 84 85 |
# File 'lib/chef/resource.rb', line 83 def params @params end |
#provider(arg = nil) ⇒ Object
Returns the value of attribute provider.
84 85 86 |
# File 'lib/chef/resource.rb', line 84 def provider @provider end |
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
88 89 90 |
# File 'lib/chef/resource.rb', line 88 def recipe_name @recipe_name end |
#resource_name ⇒ Object (readonly)
Returns the value of attribute resource_name.
96 97 98 |
# File 'lib/chef/resource.rb', line 96 def resource_name @resource_name end |
#retries(arg = nil) ⇒ Object
Returns the value of attribute retries.
91 92 93 |
# File 'lib/chef/resource.rb', line 91 def retries @retries end |
#retry_delay(arg = nil) ⇒ Object
Returns the value of attribute retry_delay.
92 93 94 |
# File 'lib/chef/resource.rb', line 92 def retry_delay @retry_delay end |
#run_context ⇒ Object
Returns the value of attribute run_context.
86 87 88 |
# File 'lib/chef/resource.rb', line 86 def run_context @run_context end |
#source_line ⇒ Object
Returns the value of attribute source_line.
90 91 92 |
# File 'lib/chef/resource.rb', line 90 def source_line @source_line end |
#updated ⇒ Object
Returns the value of attribute updated.
94 95 96 |
# File 'lib/chef/resource.rb', line 94 def updated @updated end |
Class Method Details
.attribute(attr_name, validation_opts = {}) ⇒ Object
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/chef/resource.rb', line 517 def self.attribute(attr_name, validation_opts={}) # This atrocity is the only way to support 1.8 and 1.9 at the same time # When you're ready to drop 1.8 support, do this: # define_method attr_name.to_sym do |arg=nil| # etc. shim_method=" def \#{attr_name}(arg=nil)\n _set_or_return_\#{attr_name}(arg)\n end\n SHIM\n class_eval(shim_method)\n\n define_method(\"_set_or_return_\#{attr_name.to_s}\".to_sym) do |arg|\n set_or_return(attr_name.to_sym, arg, validation_opts)\n end\nend\n" |
.build_from_file(cookbook_name, filename, run_context) ⇒ Object
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/chef/resource.rb', line 534 def self.build_from_file(cookbook_name, filename, run_context) rname = filename_to_qualified_string(cookbook_name, filename) # Add log entry if we override an existing light-weight resource. 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| # default initialize method that ensures that when initialize is finally # wrapped (see below), super is called in the event that the resource # definer does not implement initialize def initialize(name, run_context) super(name, run_context) end @actions_to_create = [] class << cls include Chef::Mixin::FromFile attr_accessor :run_context attr_reader :action_to_set_default def node self.run_context.node end def actions_to_create @actions_to_create end define_method(:default_action) do |action_name| actions_to_create.push(action_name) @action_to_set_default = action_name end define_method(:actions) do |*action_names| actions_to_create.push(*action_names) end end # set the run context in the class instance variable cls.run_context = run_context # load resource definition from file cls.class_from_file(filename) # create a new constructor that wraps the old one and adds the actions # specified in the DSL 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) @action = self.class.action_to_set_default || @action allowed_actions.push(self.class.actions_to_create).flatten! end end # register new class as a Chef::Resource 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
502 503 504 505 506 507 508 |
# File 'lib/chef/resource.rb', line 502 def self.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 |
.platform_map ⇒ Object
616 617 618 |
# File 'lib/chef/resource.rb', line 616 def self.platform_map @@platform_map ||= PlatformMap.new 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
# ...other stuff
end
611 612 613 614 |
# File 'lib/chef/resource.rb', line 611 def self.provider_base(arg=nil) @provider_base ||= arg @provider_base ||= Chef::Provider end |
.provides(short_name, opts = {}) ⇒ Object
Maps a short_name (and optionally a platform and version) to a Chef::Resource. This allows finer grained per platform resource attributes and the end of overloaded resource definitions (I’m looking at you Chef::Resource::Package) Ex:
class WindowsFile < Chef::Resource
provides :file, :on_platforms => ["windows"]
# ...other stuff
end
TODO: 2011-11-02 schisamo - platform_version support
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/chef/resource.rb', line 631 def self.provides(short_name, opts={}) short_name_sym = short_name if short_name.kind_of?(String) short_name.downcase! short_name.gsub!(/\s/, "_") short_name_sym = short_name.to_sym end if opts.has_key?(:on_platforms) platforms = [opts[:on_platforms]].flatten platforms.each do |p| p = :default if :all == p.to_sym platform_map.set( :platform => p.to_sym, :short_name => short_name_sym, :resource => self ) end else platform_map.set( :short_name => short_name_sym, :resource => self ) end end |
.resource_for_node(short_name, node) ⇒ Object
Returns a resource based on a short_name and a node’s platform and version.
Parameters
- short_name<Symbol>
-
short_name of the resource (ie :directory)
- node<Chef::Node>
-
Node object to look up platform and version in
Returns
- <Chef::Resource>
-
returns the proper Chef::Resource class
679 680 681 682 683 684 685 686 |
# File 'lib/chef/resource.rb', line 679 def self.resource_for_node(short_name, node) begin platform, version = Chef::Platform.find_platform_and_version(node) rescue ArgumentError end resource = resource_for_platform(short_name, platform, version) resource end |
.resource_for_platform(short_name, platform = nil, version = nil) ⇒ Object
Returns a resource based on a short_name anda platform and version.
Parameters
- short_name<Symbol>
-
short_name of the resource (ie :directory)
- platform<Symbol,String>
-
platform name
- version<String>
-
platform version
Returns
- <Chef::Resource>
-
returns the proper Chef::Resource class
666 667 668 |
# File 'lib/chef/resource.rb', line 666 def self.resource_for_platform(short_name, platform=nil, version=nil) platform_map.get(short_name, platform, version) end |
Instance Method Details
#action(arg = nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/chef/resource.rb', line 188 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
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/chef/resource.rb', line 273 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 |
#after_created ⇒ Object
Hook to allow a resource to run specific code after creation
511 512 513 |
# File 'lib/chef/resource.rb', line 511 def after_created nil end |
#as_json(*a) ⇒ Object
as_json does most of the to_json heavy lifted. It exists here in case activesupport is loaded. activesupport will call as_json and skip over to_json. This ensure json is encoded as expected
352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/chef/resource.rb', line 352 def as_json(*a) safe_ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS instance_vars = Hash.new safe_ivars.each do |iv| instance_vars[iv.to_s.sub(/^@/, '')] = instance_variable_get(iv) end results = { 'json_class' => self.class.name, 'instance_vars' => instance_vars } end |
#defined_at ⇒ Object
422 423 424 425 426 427 428 429 430 431 |
# File 'lib/chef/resource.rb', line 422 def defined_at if cookbook_name && recipe_name && source_line "#{cookbook_name}::#{recipe_name} line #{source_line.split(':')[1]}" elsif source_line file, line_no = source_line.split(':') "#{file} line #{line_no}" else "dynamically defined" end end |
#epic_fail(arg = nil) ⇒ Object
246 247 248 |
# File 'lib/chef/resource.rb', line 246 def epic_fail(arg=nil) ignore_failure(arg) end |
#ignore_failure(arg = nil) ⇒ Object
222 223 224 225 226 227 228 |
# File 'lib/chef/resource.rb', line 222 def ignore_failure(arg=nil) set_or_return( :ignore_failure, arg, :kind_of => [ TrueClass, FalseClass ] ) end |
#inspect ⇒ Object
342 343 344 345 346 347 |
# File 'lib/chef/resource.rb', line 342 def inspect ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS ivars.inject("<#{to_s}") do |str, ivar| str << " #{ivar}: #{instance_variable_get(ivar).inspect}" end << ">" end |
#is(*args) ⇒ Object
318 319 320 321 322 323 324 |
# File 'lib/chef/resource.rb', line 318 def is(*args) if args.size == 1 args.first else return *args end end |
#load_prior_resource ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/chef/resource.rb', line 152 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
208 209 210 211 212 213 |
# File 'lib/chef/resource.rb', line 208 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
137 138 139 |
# File 'lib/chef/resource.rb', line 137 def node run_context && run_context.node end |
#noop(tf = nil) ⇒ Object
215 216 217 218 219 220 |
# File 'lib/chef/resource.rb', line 215 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(command = nil, opts = {}, &block) ⇒ Object
If command is a block, returns false if the block returns true, true if it returns false. (“Do not run this resource if the block is true”)
If the command is not a block, executes the command. If it returns a 0 exitstatus, returns false. (“Do not run this resource if the command returns 0”)
Parameters
- command<String>
-
A a string to execute.
- opts<Hash>
-
Options control the execution of the command
- block<Proc>
-
A ruby block to run. Ignored if a command is given.
Evaluation
-
evaluates to true if the block is false, or if the command returns a non-zero exit status.
-
evaluates to false if the block is true, or if the command returns a 0 exit status.
415 416 417 418 419 420 |
# File 'lib/chef/resource.rb', line 415 def not_if(command=nil, opts={}, &block) if command || block_given? @not_if << Conditional.not_if(command, opts, &block) end @not_if end |
#notifies(*args) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/chef/resource.rb', line 250 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 # notifies(:action, resource) OR notifies(:action, resource, :immediately) add_notification(*args) else # This syntax is so weird. surely people will just give us one hash? 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
302 303 304 |
# File 'lib/chef/resource.rb', line 302 def notifies_delayed(action, resource_spec) @delayed_notifications << Notification.new(resource_spec, action, self) end |
#notifies_immediately(action, resource_spec) ⇒ Object
298 299 300 |
# File 'lib/chef/resource.rb', line 298 def notifies_immediately(action, resource_spec) @immediate_notifications << Notification.new(resource_spec, action, self) end |
#only_if(command = nil, opts = {}, &block) ⇒ Object
If command is a block, returns true if the block returns true, false if it returns false. (“Only run this resource if the block is true”)
If the command is not a block, executes the command. If it returns any status other than 0, it returns false (clearly, a 0 status code is true)
Parameters
- command<String>
-
A a string to execute.
- opts<Hash>
-
Options control the execution of the command
- block<Proc>
-
A ruby block to run. Ignored if a command is given.
Evaluation
-
evaluates to true if the block is true, or if the command returns 0
-
evaluates to false if the block is false, or if the command returns a non-zero exit code.
394 395 396 397 398 399 |
# File 'lib/chef/resource.rb', line 394 def only_if(command=nil, opts={}, &block) if command || block_given? @only_if << Conditional.only_if(command, opts, &block) 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.
293 294 295 296 |
# File 'lib/chef/resource.rb', line 293 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
306 307 308 |
# File 'lib/chef/resource.rb', line 306 def resources(*args) run_context.resource_collection.find(*args) end |
#run_action(action) ⇒ Object
433 434 435 436 437 438 439 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 |
# File 'lib/chef/resource.rb', line 433 def run_action(action) if Chef::Config[:verbose_logging] || Chef::Log.level == :debug # This can be noisy Chef::Log.info("Processing #{self} action #{action} (#{defined_at})") end # ensure that we don't leave @updated_by_last_action set to true # on accident updated_by_last_action(false) begin return if should_skip? # leverage new platform => short_name => resource # which requires explicitly setting provider in # resource class if self.provider provider = self.provider.new(self, self.run_context) else # fall back to old provider resolution provider = Chef::Platform.provider_for_resource(self) end provider.load_current_resource provider.send("action_#{action}") rescue => e if ignore_failure Chef::Log.error("#{self} (#{defined_at}) had an error: #{e.message}") else Chef::Log.error("#{self} (#{defined_at}) has had an error") new_exception = e.exception("#{self} (#{defined_at}) had an error: #{e.class.name}: #{e.message}") new_exception.set_backtrace(e.backtrace) raise new_exception end end end |
#should_skip? ⇒ Boolean
Evaluates not_if and only_if conditionals. Returns a falsey value if any of the conditionals indicate that this resource should be skipped, i.e., if an only_if evaluates to false or a not_if evaluates to true.
If this resource should be skipped, returns the first conditional that “fails” its check. Subsequent conditionals are not evaluated, so in general it’s not a good idea to rely on side effects from not_if or only_if commands/blocks being evaluated.
475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/chef/resource.rb', line 475 def should_skip? conditionals = only_if + not_if return false if conditionals.empty? conditionals.find do |conditional| if conditional.continue? false else Chef::Log.debug("Skipping #{self} due to #{conditional.description}") true end end end |
#subscribes(action, resources, timing = :delayed) ⇒ Object
310 311 312 313 314 315 316 |
# File 'lib/chef/resource.rb', line 310 def subscribes(action, resources, timing=:delayed) resources = [resources].flatten resources.each do |resource| resource.notifies(action, self, timing) end true end |
#supports(args = {}) ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/chef/resource.rb', line 167 def supports(args={}) if args.any? @supports = args else @supports end end |
#to_hash ⇒ Object
370 371 372 373 374 375 376 377 378 |
# File 'lib/chef/resource.rb', line 370 def to_hash safe_ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS instance_vars = Hash.new safe_ivars.each do |iv| key = iv.to_s.sub(/^@/,'').to_sym instance_vars[key] = instance_variable_get(iv) end instance_vars end |
#to_json(*a) ⇒ Object
Serialize this object as a hash
365 366 367 368 |
# File 'lib/chef/resource.rb', line 365 def to_json(*a) results = as_json results.to_json(*a) end |
#to_s ⇒ Object
326 327 328 |
# File 'lib/chef/resource.rb', line 326 def to_s "#{@resource_name}[#{@name}]" end |
#to_text ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/chef/resource.rb', line 330 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
498 499 500 |
# File 'lib/chef/resource.rb', line 498 def updated? updated end |
#updated_by_last_action(true_or_false) ⇒ Object
489 490 491 492 |
# File 'lib/chef/resource.rb', line 489 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
494 495 496 |
# File 'lib/chef/resource.rb', line 494 def updated_by_last_action? @updated_by_last_action end |