Class: Chef::Resource
- Extended by:
- Mixin::ConvertToClassName
- Defined in:
- lib/chef/resource.rb,
lib/chef/resource/csh.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/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/yum_package.rb,
lib/chef/resource/dpkg_package.rb,
lib/chef/resource/http_request.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/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, ErlCall, Execute, File, Group, HttpRequest, Ifconfig, Link, Log, Mdadm, Mount, Package, Route, RubyBlock, Scm, Service, User
Defined Under Namespace
Classes: AptPackage, Bash, Breakpoint, Cron, Csh, Deploy, DeployBranch, DeployRevision, Directory, DpkgPackage, EasyInstallPackage, ErlCall, Execute, File, FreebsdPackage, GemPackage, Git, Group, HttpRequest, Ifconfig, Link, Log, MacportsPackage, Mdadm, Mount, Package, PacmanPackage, Perl, PortagePackage, Python, RemoteDirectory, RemoteFile, Route, Ruby, RubyBlock, Scm, Script, Service, Subversion, Template, TimestampedDeploy, User, YumPackage
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#allowed_actions ⇒ Object
Returns the value of attribute allowed_actions.
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
-
#enclosing_provider ⇒ Object
Returns the value of attribute enclosing_provider.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#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.
-
#source_line ⇒ Object
readonly
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) ⇒ Object
- .json_create(o) ⇒ Object
-
.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.
Instance Method Summary collapse
- #action(arg = nil) ⇒ Object
- #epic_fail(arg = nil) ⇒ Object
- #ignore_failure(arg = nil) ⇒ Object
-
#initialize(name, collection = nil, node = 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
- #noop(tf = nil) ⇒ Object
- #not_if(arg = nil, args = {}, &blk) ⇒ Object
- #notifies(*args) ⇒ Object
- #only_if(arg = nil, args = {}, &blk) ⇒ Object
- #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
Methods included from Mixin::ConvertToClassName
convert_to_class_name, convert_to_snake_case, filename_to_qualified_string, snake_case_basename
Methods included from Mixin::Language
#data_bag, #data_bag_item, #platform?, #search, #value_for_platform
Methods included from Mixin::ParamsValidate
Methods included from Mixin::CheckHelper
Constructor Details
#initialize(name, collection = nil, node = nil) ⇒ Resource
Returns a new instance of Resource.
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 |
# File 'lib/chef/resource.rb', line 38 def initialize(name, collection=nil, node=nil) @name = name if collection @collection = collection else @collection = Chef::ResourceCollection.new() end @node = node ? node : Chef::Node.new @noop = nil @before = nil @actions = Hash.new @params = Hash.new @provider = nil @allowed_actions = [ :nothing ] @action = :nothing @updated = false @supports = {} @ignore_failure = false @not_if = nil @not_if_args = {} @only_if = nil @only_if_args = {} sline = caller(4).shift if sline @source_line = sline.gsub!(/^(.+):(.+):.+$/, '\1 line \2') @source_line = ::File.(@source_line) if @source_line end 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.
70 71 72 73 74 75 76 |
# File 'lib/chef/resource.rb', line 70 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
#actions ⇒ Object
Returns the value of attribute actions.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def actions @actions end |
#allowed_actions ⇒ Object
Returns the value of attribute allowed_actions.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def allowed_actions @allowed_actions end |
#collection ⇒ Object
Returns the value of attribute collection.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def collection @collection end |
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def cookbook_name @cookbook_name end |
#enclosing_provider ⇒ Object
Returns the value of attribute enclosing_provider.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def enclosing_provider @enclosing_provider end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
36 37 38 |
# File 'lib/chef/resource.rb', line 36 def node @node end |
#not_if_args ⇒ Object (readonly)
Returns the value of attribute not_if_args.
36 37 38 |
# File 'lib/chef/resource.rb', line 36 def not_if_args @not_if_args end |
#only_if_args ⇒ Object (readonly)
Returns the value of attribute only_if_args.
36 37 38 |
# File 'lib/chef/resource.rb', line 36 def only_if_args @only_if_args end |
#params ⇒ Object
Returns the value of attribute params.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def params @params end |
#provider(arg = nil) ⇒ Object
Returns the value of attribute provider.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def provider @provider end |
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def recipe_name @recipe_name end |
#resource_name ⇒ Object (readonly)
Returns the value of attribute resource_name.
36 37 38 |
# File 'lib/chef/resource.rb', line 36 def resource_name @resource_name end |
#source_line ⇒ Object (readonly)
Returns the value of attribute source_line.
36 37 38 |
# File 'lib/chef/resource.rb', line 36 def source_line @source_line end |
#updated ⇒ Object
Returns the value of attribute updated.
35 36 37 |
# File 'lib/chef/resource.rb', line 35 def updated @updated end |
Class Method Details
.attribute(attr_name, validation_opts = {}) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/chef/resource.rb', line 278 def 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=<<-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
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 |
# File 'lib/chef/resource.rb', line 295 def build_from_file(cookbook_name, filename) rname = filename_to_qualified_string(cookbook_name, filename) 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, collection=nil, node=nil) super(name, collection, node) 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 # 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| collection = optional_args.shift node = optional_args.shift @resource_name = rname.to_sym old_init.bind(self).call(name, collection, node) 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
268 269 270 271 272 273 274 |
# File 'lib/chef/resource.rb', line 268 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 |
Instance Method Details
#action(arg = nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/chef/resource.rb', line 114 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 |
#epic_fail(arg = nil) ⇒ Object
156 157 158 |
# File 'lib/chef/resource.rb', line 156 def epic_fail(arg=nil) ignore_failure(arg) end |
#ignore_failure(arg = nil) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/chef/resource.rb', line 148 def ignore_failure(arg=nil) set_or_return( :ignore_failure, arg, :kind_of => [ TrueClass, FalseClass ] ) end |
#is(*args) ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/chef/resource.rb', line 203 def is(*args) if args.size == 1 args.first else return *args end end |
#load_prior_resource ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chef/resource.rb', line 78 def load_prior_resource begin prior_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 ArgumentError => e true end end |
#name(name = nil) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/chef/resource.rb', line 134 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 |
#noop(tf = nil) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/chef/resource.rb', line 141 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
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/chef/resource.rb', line 249 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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/chef/resource.rb', line 160 def notifies(*args) raise ArgumentError, "Wrong number of arguments (should be 1, 2, or 3)" unless ( args.size > 0 && args.size < 4) if args.size > 1 notifies_helper(*args) else # This syntax is so weird. surely people will just give us one hash? notifications = args.flatten notifications.each do |resources_notifications| begin resources_notifications.each do |resource, notification| Chef::Log.error "resource KV: `#{resource.inspect}' => `#{notification.inspect}'" notifies_helper(notification[0], resource, notification[1]) end rescue NoMethodError Chef::Log.fatal("encountered NME processing resource #{resources_notifications.inspect}") Chef::Log.fatal("incoming args: #{args.inspect}") raise end end end end |
#only_if(arg = nil, args = {}, &blk) ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/chef/resource.rb', line 238 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 |
#resources(*args) ⇒ Object
182 183 184 |
# File 'lib/chef/resource.rb', line 182 def resources(*args) @collection.resources(*args) end |
#run_action(action) ⇒ Object
260 261 262 263 264 |
# File 'lib/chef/resource.rb', line 260 def run_action(action) provider = Chef::Platform.provider_for_node(@node, self) provider.load_current_resource provider.send("action_#{action}") end |
#subscribes(action, resources, timing = :delayed) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/chef/resource.rb', line 186 def subscribes(action, resources, timing=:delayed) timing = check_timing(timing) rarray = resources.kind_of?(Array) ? resources : [ resources ] rarray.each do |resource| action_sym = action.to_sym if resource.actions.has_key?(action_sym) resource.actions[action_sym][timing] << self else resource.actions[action_sym] = Hash.new resource.actions[action_sym][:delayed] = Array.new resource.actions[action_sym][:immediate] = Array.new resource.actions[action_sym][timing] << self end end true end |
#supports(args = {}) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/chef/resource.rb', line 93 def supports(args={}) if args.any? @supports = args else @supports end end |
#to_hash ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/chef/resource.rb', line 228 def to_hash instance_vars = Hash.new self.instance_variables.each do |iv| iv = iv.to_s next if iv == "@collection" instance_vars[iv.sub(/^@/,'').to_sym] = self.instance_variable_get(iv) end instance_vars end |
#to_json(*a) ⇒ Object
Serialize this object as a hash
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/chef/resource.rb', line 216 def to_json(*a) instance_vars = Hash.new self.instance_variables.each do |iv| instance_vars[iv] = self.instance_variable_get(iv) unless iv == "@collection" end results = { 'json_class' => self.class.name, 'instance_vars' => instance_vars } results.to_json(*a) end |
#to_s ⇒ Object
211 212 213 |
# File 'lib/chef/resource.rb', line 211 def to_s "#{@resource_name}[#{@name}]" end |