Class: Chef::Node
- Inherits:
-
Object
- Object
- Chef::Node
- Extended by:
- Forwardable
- Defined in:
- lib/chef/node.rb,
lib/chef/node/attribute.rb,
lib/chef/node/attribute_collections.rb,
lib/chef/node/immutable_collections.rb
Defined Under Namespace
Modules: Immutablize Classes: AttrArray, Attribute, ImmutableArray, ImmutableMash, MultiMash, VividMash
Instance Attribute Summary collapse
-
#chef_server_rest ⇒ Object
Returns the value of attribute chef_server_rest.
-
#override_runlist(*args) ⇒ Object
Returns the value of attribute override_runlist.
-
#recipe_list ⇒ Object
Returns the value of attribute recipe_list.
-
#run_context ⇒ Object
RunContext will set itself as run_context via this setter when initialized.
-
#run_state ⇒ Object
Returns the value of attribute run_state.
Class Method Summary collapse
- .build(node_name) ⇒ Object
- .find_or_create(node_name) ⇒ Object
-
.json_create(o) ⇒ Object
Create a Chef::Node from JSON.
- .list(inflate = false) ⇒ Object
- .list_by_environment(environment, inflate = false) ⇒ Object
-
.load(name) ⇒ Object
Load a node by name.
Instance Method Summary collapse
- #<=>(other_node) ⇒ Object
-
#[](attrib) ⇒ Object
Return an attribute of this node.
-
#apply_expansion_attributes(expansion) ⇒ Object
Apply the default and overrides attributes from the expansion passed in, which came from roles.
-
#attribute?(attrib) ⇒ Boolean
Return true if this Node has a given attribute, false if not.
- #attributes ⇒ Object (also: #attribute, #construct_attributes)
- #automatic_attrs ⇒ Object
- #automatic_attrs=(new_values) ⇒ Object
- #chef_environment(arg = nil) ⇒ Object (also: #environment)
- #chef_environment=(environment) ⇒ Object
-
#consume_attributes(attrs) ⇒ Object
Consumes the combined run_list and other attributes in
attrs. -
#consume_external_attrs(ohai_data, json_cli_attrs) ⇒ Object
Consume data from ohai and Attributes provided as JSON on the command line.
-
#consume_run_list(attrs) ⇒ Object
Extracts the run list from
attrsand applies it. -
#create ⇒ Object
Create the node via the REST API.
-
#default ⇒ Object
(also: #default_attrs)
Set a default of this node, but auto-vivify any Mashes that might be missing.
- #default_attrs=(new_values) ⇒ Object
-
#default_unless ⇒ Object
Set a default attribute of this node, auto-vivifying any mashes that are missing, but if the final value already exists, don’t set it.
-
#destroy ⇒ Object
Remove this node via the REST API.
- #display_hash ⇒ Object
-
#each(&block) ⇒ Object
Yield each key of the top level to the block.
-
#each_attribute(&block) ⇒ Object
Iterates over each attribute, passing the attribute and value to the block.
-
#expand!(data_source = 'server') ⇒ Object
Expands the node’s run list and sets the default and override attributes.
- #for_json ⇒ Object
-
#initialize(chef_server_rest: nil) ⇒ Node
constructor
Create a new Chef::Node object.
-
#loaded_recipe(cookbook, recipe) ⇒ Object
used by include_recipe to add recipes to the expanded run_list to be saved back to the node and be searchable.
-
#method_missing(symbol, *args) ⇒ Object
Only works for attribute fetches, setting is no longer supported.
-
#name(arg = nil) ⇒ Object
Set the name of this Node, or return the current name.
-
#node ⇒ Object
Used by DSL.
-
#normal ⇒ Object
(also: #set, #normal_attrs)
Set a normal attribute of this node, but auto-vivify any Mashes that might be missing.
- #normal_attrs=(new_values) ⇒ Object
-
#normal_unless ⇒ Object
(also: #set_unless)
Set a normal attribute of this node, auto-vivifying any mashes that are missing, but if the final value already exists, don’t set it.
-
#override ⇒ Object
(also: #override_attrs)
Set an override attribute of this node, but auto-vivify any Mashes that might be missing.
- #override_attrs=(new_values) ⇒ Object
-
#override_unless ⇒ Object
Set an override attribute of this node, auto-vivifying any mashes that are missing, but if the final value already exists, don’t set it.
- #primary_runlist ⇒ Object
-
#recipe?(recipe_name) ⇒ Boolean
Returns true if this Node expects a given recipe, false if not.
-
#reset_defaults_and_overrides ⇒ Object
Clear defaults and overrides, so that any deleted attributes between runs are still gone.
-
#role?(role_name) ⇒ Boolean
Returns true if this Node expects a given role, false if not.
-
#run_list(*args) ⇒ Object
Returns an Array of roles and recipes, in the order they will be applied.
- #run_list=(list) ⇒ Object
-
#run_list?(item) ⇒ Boolean
Returns true if this Node expects a given role, false if not.
-
#save ⇒ Object
Save this node via the REST API.
- #select_run_list ⇒ Object
- #tag(*tags) ⇒ Object
-
#tags ⇒ Object
Lazy initializer for tags attribute.
-
#to_hash ⇒ Object
Transform the node to a Hash.
-
#to_json(*a) ⇒ Object
Serialize this object as a hash.
- #to_s ⇒ Object
- #update_from!(o) ⇒ Object
Methods included from Mixin::ParamsValidate
#lazy, #set_or_return, #validate
Methods included from DSL::PlatformIntrospection
#platform?, #platform_family?, #value_for_platform, #value_for_platform_family
Methods included from DSL::IncludeAttribute
#include_attribute, #parse_attribute_file_spec
Methods included from Mixin::FromFile
Constructor Details
#initialize(chef_server_rest: nil) ⇒ Node
Create a new Chef::Node object.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chef/node.rb', line 67 def initialize(chef_server_rest: nil) @chef_server_rest = chef_server_rest @name = nil @chef_environment = '_default' @primary_runlist = Chef::RunList.new @override_runlist = Chef::RunList.new @attributes = Chef::Node::Attribute.new({}, {}, {}, {}) @run_state = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
Only works for attribute fetches, setting is no longer supported
228 229 230 |
# File 'lib/chef/node.rb', line 228 def method_missing(symbol, *args) attributes.send(symbol, *args) end |
Instance Attribute Details
#chef_server_rest ⇒ Object
Returns the value of attribute chef_server_rest.
50 51 52 |
# File 'lib/chef/node.rb', line 50 def chef_server_rest @chef_server_rest end |
#override_runlist(*args) ⇒ Object
Returns the value of attribute override_runlist.
48 49 50 |
# File 'lib/chef/node.rb', line 48 def override_runlist @override_runlist end |
#recipe_list ⇒ Object
Returns the value of attribute recipe_list.
48 49 50 |
# File 'lib/chef/node.rb', line 48 def recipe_list @recipe_list end |
#run_context ⇒ Object
RunContext will set itself as run_context via this setter when initialized. This is needed so DSL::IncludeAttribute (in particular, #include_recipe) can access the run_context to determine if an attributes file has been seen yet. – TODO: This is a pretty ugly way to solve that problem.
58 59 60 |
# File 'lib/chef/node.rb', line 58 def run_context @run_context end |
#run_state ⇒ Object
Returns the value of attribute run_state.
48 49 50 |
# File 'lib/chef/node.rb', line 48 def run_state @run_state end |
Class Method Details
.build(node_name) ⇒ Object
490 491 492 493 494 495 |
# File 'lib/chef/node.rb', line 490 def self.build(node_name) node = new node.name(node_name) node.chef_environment(Chef::Config[:environment]) unless Chef::Config[:environment].nil? || Chef::Config[:environment].chomp.empty? node end |
.find_or_create(node_name) ⇒ Object
482 483 484 485 486 487 488 |
# File 'lib/chef/node.rb', line 482 def self.find_or_create(node_name) load(node_name) rescue Net::HTTPServerException => e raise unless e.response.code == '404' node = build(node_name) node.create end |
.json_create(o) ⇒ Object
Create a Chef::Node from JSON
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/chef/node.rb', line 440 def self.json_create(o) node = new node.name(o["name"]) node.chef_environment(o["chef_environment"]) if o.has_key?("attributes") node.normal_attrs = o["attributes"] end node.automatic_attrs = Mash.new(o["automatic"]) if o.has_key?("automatic") node.normal_attrs = Mash.new(o["normal"]) if o.has_key?("normal") node.default_attrs = Mash.new(o["default"]) if o.has_key?("default") node.override_attrs = Mash.new(o["override"]) if o.has_key?("override") if o.has_key?("run_list") node.run_list.reset!(o["run_list"]) else o["recipes"].each { |r| node.recipes << r } end node end |
.list(inflate = false) ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/chef/node.rb', line 470 def self.list(inflate=false) if inflate response = Hash.new Chef::Search::Query.new.search(:node) do |n| response[n.name] = n unless n.nil? end response else Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("nodes") end end |
.list_by_environment(environment, inflate = false) ⇒ Object
460 461 462 463 464 465 466 467 468 |
# File 'lib/chef/node.rb', line 460 def self.list_by_environment(environment, inflate=false) if inflate response = Hash.new Chef::Search::Query.new.search(:node, "chef_environment:#{environment}") {|n| response[n.name] = n unless n.nil?} response else Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("environments/#{environment}/nodes") end end |
Instance Method Details
#<=>(other_node) ⇒ Object
534 535 536 |
# File 'lib/chef/node.rb', line 534 def <=>(other_node) self.name <=> other_node.name end |
#[](attrib) ⇒ Object
Return an attribute of this node. Returns nil if the attribute is not found.
126 127 128 |
# File 'lib/chef/node.rb', line 126 def [](attrib) attributes[attrib] end |
#apply_expansion_attributes(expansion) ⇒ Object
Apply the default and overrides attributes from the expansion passed in, which came from roles.
367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/chef/node.rb', line 367 def apply_expansion_attributes(expansion) loaded_environment = if chef_environment == "_default" Chef::Environment.new.tap {|e| e.name("_default")} else Chef::Environment.load(chef_environment) end attributes.env_default = loaded_environment.default_attributes attributes.env_override = loaded_environment.override_attributes attribute.role_default = expansion.default_attrs attributes.role_override = expansion.override_attrs end |
#attribute?(attrib) ⇒ Boolean
Return true if this Node has a given attribute, false if not. Takes either a symbol or a string.
Only works on the top level. Preferred way is to use the normal [] style lookup and call attribute?()
213 214 215 |
# File 'lib/chef/node.rb', line 213 def attribute?(attrib) attributes.attribute?(attrib) end |
#attributes ⇒ Object Also known as: attribute, construct_attributes
118 119 120 |
# File 'lib/chef/node.rb', line 118 def attributes @attributes end |
#automatic_attrs ⇒ Object
198 199 200 201 202 |
# File 'lib/chef/node.rb', line 198 def automatic_attrs attributes. = nil attributes.set_unless_value_present = false attributes.automatic end |
#automatic_attrs=(new_values) ⇒ Object
204 205 206 |
# File 'lib/chef/node.rb', line 204 def automatic_attrs=(new_values) attributes.automatic = new_values end |
#chef_environment(arg = nil) ⇒ Object Also known as: environment
104 105 106 107 108 109 110 |
# File 'lib/chef/node.rb', line 104 def chef_environment(arg=nil) set_or_return( :chef_environment, arg, { :regex => /^[\-[:alnum:]_]+$/, :kind_of => String } ) end |
#chef_environment=(environment) ⇒ Object
112 113 114 |
# File 'lib/chef/node.rb', line 112 def chef_environment=(environment) chef_environment(environment) end |
#consume_attributes(attrs) ⇒ Object
Consumes the combined run_list and other attributes in attrs
298 299 300 301 302 303 |
# File 'lib/chef/node.rb', line 298 def consume_attributes(attrs) normal_attrs_to_merge = consume_run_list(attrs) Chef::Log.debug("Applying attributes from json file") self.normal_attrs = Chef::Mixin::DeepMerge.merge(normal_attrs,normal_attrs_to_merge) self. # make sure they're defined end |
#consume_external_attrs(ohai_data, json_cli_attrs) ⇒ Object
Consume data from ohai and Attributes provided as JSON on the command line.
285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/chef/node.rb', line 285 def consume_external_attrs(ohai_data, json_cli_attrs) Chef::Log.debug("Extracting run list from JSON attributes provided on command line") consume_attributes(json_cli_attrs) self.automatic_attrs = ohai_data platform, version = Chef::Platform.find_platform_and_version(self) Chef::Log.debug("Platform is #{platform} version #{version}") self.automatic[:platform] = platform self.automatic[:platform_version] = version end |
#consume_run_list(attrs) ⇒ Object
Extracts the run list from attrs and applies it. Returns the remaining attributes
320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/chef/node.rb', line 320 def consume_run_list(attrs) attrs = attrs ? attrs.dup : {} if new_run_list = attrs.delete("recipes") || attrs.delete("run_list") if attrs.key?("recipes") || attrs.key?("run_list") raise Chef::Exceptions::AmbiguousRunlistSpecification, "please set the node's run list using the 'run_list' attribute only." end Chef::Log.info("Setting the run_list to #{new_run_list.inspect} from CLI options") run_list(new_run_list) end attrs end |
#create ⇒ Object
Create the node via the REST API
525 526 527 528 |
# File 'lib/chef/node.rb', line 525 def create chef_server_rest.post_rest("nodes", data_for_save) self end |
#default ⇒ Object Also known as: default_attrs
Set a default of this node, but auto-vivify any Mashes that might be missing
152 153 154 155 156 |
# File 'lib/chef/node.rb', line 152 def default attributes. = nil attributes.set_unless_value_present = false attributes.default end |
#default_attrs=(new_values) ⇒ Object
190 191 192 |
# File 'lib/chef/node.rb', line 190 def default_attrs=(new_values) attributes.default = new_values end |
#default_unless ⇒ Object
Set a default attribute of this node, auto-vivifying any mashes that are missing, but if the final value already exists, don’t set it
160 161 162 163 164 |
# File 'lib/chef/node.rb', line 160 def default_unless attributes. = nil attributes.set_unless_value_present = true attributes.default end |
#destroy ⇒ Object
Remove this node via the REST API
503 504 505 |
# File 'lib/chef/node.rb', line 503 def destroy chef_server_rest.delete_rest("nodes/#{name}") end |
#display_hash ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/chef/node.rb', line 396 def display_hash display = {} display["name"] = name display["chef_environment"] = chef_environment display["automatic"] = automatic_attrs display["normal"] = normal_attrs display["default"] = attributes.combined_default display["override"] = attributes.combined_override display["run_list"] = run_list.run_list_items display end |
#each(&block) ⇒ Object
Yield each key of the top level to the block.
218 219 220 |
# File 'lib/chef/node.rb', line 218 def each(&block) attributes.each(&block) end |
#each_attribute(&block) ⇒ Object
Iterates over each attribute, passing the attribute and value to the block.
223 224 225 |
# File 'lib/chef/node.rb', line 223 def each_attribute(&block) attributes.each_attribute(&block) end |
#expand!(data_source = 'server') ⇒ Object
Expands the node’s run list and sets the default and override attributes. Also applies stored attributes (from json provided on the command line)
Returns the fully-expanded list of recipes, a RunListExpansion.
– TODO: timh/cw, 5-14-2010: Should this method exist? Should we instead modify default_attrs and override_attrs whenever our run_list is mutated? Or perhaps do something smarter like on-demand generation of default_attrs and override_attrs, invalidated only when run_list is mutated?
351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/chef/node.rb', line 351 def (data_source = 'server') expansion = run_list.(chef_environment, data_source) raise Chef::Exceptions::MissingRole, expansion if expansion.errors? self. # make sure they're defined automatic_attrs[:recipes] = expansion.recipes automatic_attrs[:roles] = expansion.roles apply_expansion_attributes(expansion) expansion end |
#for_json ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/chef/node.rb', line 413 def for_json result = { "name" => name, "chef_environment" => chef_environment, 'json_class' => self.class.name, "automatic" => attributes.automatic, "normal" => attributes.normal, "chef_type" => "node", "default" => attributes.combined_default, "override" => attributes.combined_override, #Render correctly for run_list items so malformed json does not result "run_list" => @primary_runlist.run_list.map { |item| item.to_s } } result end |
#loaded_recipe(cookbook, recipe) ⇒ Object
used by include_recipe to add recipes to the expanded run_list to be saved back to the node and be searchable
245 246 247 248 |
# File 'lib/chef/node.rb', line 245 def loaded_recipe(cookbook, recipe) fully_qualified_recipe = "#{cookbook}::#{recipe}" automatic_attrs[:recipes] << fully_qualified_recipe unless Array(self[:recipes]).include?(fully_qualified_recipe) end |
#name(arg = nil) ⇒ Object
Set the name of this Node, or return the current name.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/chef/node.rb', line 90 def name(arg=nil) if arg != nil validate( {:name => arg }, {:name => { :kind_of => String, :cannot_be => :blank, :regex => /^[\-[:alnum:]_:.]+$/} }) @name = arg else @name end end |
#node ⇒ Object
Used by DSL
81 82 83 |
# File 'lib/chef/node.rb', line 81 def node self end |
#normal ⇒ Object Also known as: set, normal_attrs
Set a normal attribute of this node, but auto-vivify any Mashes that might be missing
132 133 134 135 136 |
# File 'lib/chef/node.rb', line 132 def normal attributes. = nil attributes.set_unless_value_present = false attributes.normal end |
#normal_attrs=(new_values) ⇒ Object
194 195 196 |
# File 'lib/chef/node.rb', line 194 def normal_attrs=(new_values) attributes.normal = new_values end |
#normal_unless ⇒ Object Also known as: set_unless
Set a normal attribute of this node, auto-vivifying any mashes that are missing, but if the final value already exists, don’t set it
142 143 144 145 146 |
# File 'lib/chef/node.rb', line 142 def normal_unless attributes. = nil attributes.set_unless_value_present = true attributes.normal end |
#override ⇒ Object Also known as: override_attrs
Set an override attribute of this node, but auto-vivify any Mashes that might be missing
168 169 170 171 172 |
# File 'lib/chef/node.rb', line 168 def override attributes. = nil attributes.set_unless_value_present = false attributes.override end |
#override_attrs=(new_values) ⇒ Object
186 187 188 |
# File 'lib/chef/node.rb', line 186 def override_attrs=(new_values) attributes.override = new_values end |
#override_unless ⇒ Object
Set an override attribute of this node, auto-vivifying any mashes that are missing, but if the final value already exists, don’t set it
176 177 178 179 180 |
# File 'lib/chef/node.rb', line 176 def override_unless attributes. = nil attributes.set_unless_value_present = true attributes.override end |
#primary_runlist ⇒ Object
255 256 257 |
# File 'lib/chef/node.rb', line 255 def primary_runlist @primary_runlist end |
#recipe?(recipe_name) ⇒ Boolean
Returns true if this Node expects a given recipe, false if not.
First, the run list is consulted to see whether the recipe is explicitly included. If it’s not there, it looks in ‘node`, which is populated when the run_list is expanded
NOTE: It’s used by cookbook authors
239 240 241 |
# File 'lib/chef/node.rb', line 239 def recipe?(recipe_name) run_list.include?(recipe_name) || Array(self[:recipes]).include?(recipe_name) end |
#reset_defaults_and_overrides ⇒ Object
Clear defaults and overrides, so that any deleted attributes between runs are still gone.
334 335 336 337 |
# File 'lib/chef/node.rb', line 334 def reset_defaults_and_overrides self.default.clear self.override.clear end |
#role?(role_name) ⇒ Boolean
Returns true if this Node expects a given role, false if not.
251 252 253 |
# File 'lib/chef/node.rb', line 251 def role?(role_name) run_list.include?("role[#{role_name}]") end |
#run_list(*args) ⇒ Object
Returns an Array of roles and recipes, in the order they will be applied. If you call it with arguments, they will become the new list of roles and recipes.
269 270 271 272 |
# File 'lib/chef/node.rb', line 269 def run_list(*args) rl = select_run_list args.length > 0 ? rl.reset!(args) : rl end |
#run_list=(list) ⇒ Object
274 275 276 277 |
# File 'lib/chef/node.rb', line 274 def run_list=(list) rl = select_run_list rl = list end |
#run_list?(item) ⇒ Boolean
Returns true if this Node expects a given role, false if not.
280 281 282 |
# File 'lib/chef/node.rb', line 280 def run_list?(item) run_list.detect { |r| r == item } ? true : false end |
#save ⇒ Object
Save this node via the REST API
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/chef/node.rb', line 508 def save # Try PUT. If the node doesn't yet exist, PUT will return 404, # so then POST to create. begin if Chef::Config[:why_run] Chef::Log.warn("In whyrun mode, so NOT performing node save.") else chef_server_rest.put_rest("nodes/#{name}", data_for_save) end rescue Net::HTTPServerException => e raise e unless e.response.code == "404" chef_server_rest.post_rest("nodes", data_for_save) end self end |
#select_run_list ⇒ Object
263 264 265 |
# File 'lib/chef/node.rb', line 263 def select_run_list @override_runlist.empty? ? @primary_runlist : @override_runlist end |
#tag(*tags) ⇒ Object
311 312 313 314 315 316 317 |
# File 'lib/chef/node.rb', line 311 def tag(*) .each do |tag| self.normal[:tags].push(tag.to_s) unless self[:tags].include? tag.to_s end self[:tags] end |
#tags ⇒ Object
Lazy initializer for tags attribute
306 307 308 309 |
# File 'lib/chef/node.rb', line 306 def normal[:tags] = [] unless attribute?(:tags) normal[:tags] end |
#to_hash ⇒ Object
Transform the node to a Hash
382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/chef/node.rb', line 382 def to_hash index_hash = Hash.new index_hash["chef_type"] = "node" index_hash["name"] = name index_hash["chef_environment"] = chef_environment attribute.each do |key, value| index_hash[key] = value end index_hash["recipe"] = run_list.recipe_names if run_list.recipe_names.length > 0 index_hash["role"] = run_list.role_names if run_list.role_names.length > 0 index_hash["run_list"] = run_list.run_list_items index_hash end |
#to_json(*a) ⇒ Object
Serialize this object as a hash
409 410 411 |
# File 'lib/chef/node.rb', line 409 def to_json(*a) Chef::JSONCompat.to_json(for_json, *a) end |
#to_s ⇒ Object
530 531 532 |
# File 'lib/chef/node.rb', line 530 def to_s "node[#{name}]" end |
#update_from!(o) ⇒ Object
429 430 431 432 433 434 435 436 437 |
# File 'lib/chef/node.rb', line 429 def update_from!(o) run_list.reset!(o.run_list) self.automatic_attrs = o.automatic_attrs self.normal_attrs = o.normal_attrs self.override_attrs = o.override_attrs self.default_attrs = o.default_attrs chef_environment(o.chef_environment) self end |