Class: Chef::RunContext
- Inherits:
-
Object
- Object
- Chef::RunContext
- Includes:
- Deprecated
- Defined in:
- lib/chef/run_context.rb,
lib/chef/run_context/cookbook_compiler.rb
Overview
Chef::RunContext
Value object that loads and tracks the context of a Chef run
Direct Known Subclasses
Defined Under Namespace
Modules: Deprecated Classes: ChildRunContext, CookbookCompiler
Instance Attribute Summary collapse
-
#audits ⇒ Object
readonly
The list of control groups to execute during the audit phase.
-
#before_notification_collection ⇒ Hash[String, Array[Chef::Resource::Notification]]
readonly
A Hash containing the before notifications triggered by resources during the converge phase of the chef run.
-
#cookbook_collection ⇒ Chef::CookbookCollection
readonly
The set of cookbooks involved in this run.
-
#definitions ⇒ Array[Chef::ResourceDefinition]
readonly
Resource Definitions for this run.
-
#delayed_notification_collection ⇒ Hash[String, Array[Chef::Resource::Notification]]
readonly
A Hash containing the delayed (end of run) notifications triggered by resources during the converge phase of the chef run.
-
#events ⇒ Chef::EventDispatch::Dispatcher
readonly
Event dispatcher for this run.
-
#immediate_notification_collection ⇒ Hash[String, Array[Chef::Resource::Notification]]
readonly
A Hash containing the immediate notifications triggered by resources during the converge phase of the chef run.
-
#node ⇒ Chef::Node
readonly
The node for this run.
-
#parent_run_context ⇒ Chef::RunContext
readonly
The parent run context.
-
#reboot_info ⇒ Hash
Hash of factoids for a reboot request.
-
#resource_collection ⇒ Chef::ResourceCollection
The collection of resources intended to be converged (and able to be notified).
Instance Method Summary collapse
-
#before_notifications(resource) ⇒ Array[Notification]
Get the list of before notifications sent by the given resource.
- #cancel_reboot ⇒ Object
-
#create_child ⇒ Object
Create a child RunContext.
-
#delayed_notifications(resource) ⇒ Array[Notification]
Get the list of delayed (end of run) notifications sent by the given resource.
-
#has_cookbook_file_in_cookbook?(cookbook, cb_file_name) ⇒ Boolean
Find out if the cookbook has the given file.
-
#has_template_in_cookbook?(cookbook, template_name) ⇒ Boolean
Find out if the cookbook has the given template.
-
#immediate_notifications(resource) ⇒ Array[Notification]
Get the list of immediate notifications sent by the given resource.
-
#include_recipe(*recipe_names, current_cookbook: nil) ⇒ Object
Evaluates the recipes
recipe_names
. -
#initialize(node, cookbook_collection, events) ⇒ RunContext
constructor
Creates a new Chef::RunContext object and populates its fields.
-
#initialize_child_state ⇒ Object
Initialize state that applies to both Chef::RunContext and Chef::ChildRunContext.
-
#load(run_list_expansion) ⇒ Object
Triggers the compile phase of the chef run.
-
#load_recipe(recipe_name, current_cookbook: nil) ⇒ Object
Evaluates the recipe
recipe_name
. -
#load_recipe_file(recipe_file) ⇒ Chef::Recipe
Load the given recipe from a filename.
-
#loaded_attribute(cookbook, attribute_file) ⇒ Object
Mark a given attribute file as having been loaded.
-
#loaded_attributes ⇒ Array[String]
A list of all attributes files that have been loaded.
-
#loaded_fully_qualified_attribute?(cookbook, attribute_file) ⇒ Boolean
Find out if a given attribute file has been loaded.
-
#loaded_fully_qualified_recipe?(cookbook, recipe) ⇒ Boolean
Find out if a given recipe has been loaded.
-
#loaded_recipe(cookbook, recipe) ⇒ Object
Mark a given recipe as having been loaded.
-
#loaded_recipe?(recipe) ⇒ Boolean
Find out if a given recipe has been loaded.
-
#loaded_recipes ⇒ Array[String]
A list of all recipes that have been loaded.
-
#notifies_before(notification) ⇒ Object
Adds an before notification to the
before_notification_collection
. -
#notifies_delayed(notification) ⇒ Object
Adds a delayed notification to the
delayed_notification_collection
. -
#notifies_immediately(notification) ⇒ Object
Adds an immediate notification to the
immediate_notification_collection
. -
#open_stream(name: nil, **options) {|stream| ... } ⇒ EventDispatch::EventsOutputStream
Open a stream object that can be printed into and will dispatch to events.
- #reboot_requested? ⇒ Boolean
-
#request_reboot(reboot_info) ⇒ Object
there are options for how to handle multiple calls to these functions: 1.
-
#resolve_attribute(cookbook_name, attr_file_name) ⇒ String
Look up an attribute filename.
-
#unreachable_cookbook?(cookbook_name) ⇒ Boolean
Find out whether the given cookbook is in the cookbook dependency graph.
Methods included from Deprecated
#audits=, #delayed_notification_collection=, #immediate_notification_collection=
Constructor Details
#initialize(node, cookbook_collection, events) ⇒ RunContext
Creates a new Chef::RunContext object and populates its fields. This object gets used by the Chef Server to generate a fully compiled recipe list for a node.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/chef/run_context.rb', line 142 def initialize(node, cookbook_collection, events) @node = node @cookbook_collection = cookbook_collection @events = events node.run_context = self node.set_cookbook_attribute @definitions = Hash.new @loaded_recipes_hash = {} @loaded_attributes_hash = {} @reboot_info = {} @cookbook_compiler = nil initialize_child_state end |
Instance Attribute Details
#audits ⇒ Object (readonly)
The list of control groups to execute during the audit phase
100 101 102 |
# File 'lib/chef/run_context.rb', line 100 def audits @audits end |
#before_notification_collection ⇒ Hash[String, Array[Chef::Resource::Notification]] (readonly)
A Hash containing the before notifications triggered by resources during the converge phase of the chef run.
113 114 115 |
# File 'lib/chef/run_context.rb', line 113 def before_notification_collection @before_notification_collection end |
#cookbook_collection ⇒ Chef::CookbookCollection (readonly)
The set of cookbooks involved in this run
51 52 53 |
# File 'lib/chef/run_context.rb', line 51 def cookbook_collection @cookbook_collection end |
#definitions ⇒ Array[Chef::ResourceDefinition] (readonly)
Resource Definitions for this run. Populated when the files in definitions/
are evaluated (this is triggered by #load).
59 60 61 |
# File 'lib/chef/run_context.rb', line 59 def definitions @definitions end |
#delayed_notification_collection ⇒ Hash[String, Array[Chef::Resource::Notification]] (readonly)
A Hash containing the delayed (end of run) notifications triggered by resources during the converge phase of the chef run.
131 132 133 |
# File 'lib/chef/run_context.rb', line 131 def delayed_notification_collection @delayed_notification_collection end |
#events ⇒ Chef::EventDispatch::Dispatcher (readonly)
Event dispatcher for this run.
66 67 68 |
# File 'lib/chef/run_context.rb', line 66 def events @events end |
#immediate_notification_collection ⇒ Hash[String, Array[Chef::Resource::Notification]] (readonly)
A Hash containing the immediate notifications triggered by resources during the converge phase of the chef run.
122 123 124 |
# File 'lib/chef/run_context.rb', line 122 def immediate_notification_collection @immediate_notification_collection end |
#node ⇒ Chef::Node (readonly)
The node for this run
44 45 46 |
# File 'lib/chef/run_context.rb', line 44 def node @node end |
#parent_run_context ⇒ Chef::RunContext (readonly)
The parent run context.
85 86 87 |
# File 'lib/chef/run_context.rb', line 85 def parent_run_context @parent_run_context end |
#reboot_info ⇒ Hash
Hash of factoids for a reboot request.
73 74 75 |
# File 'lib/chef/run_context.rb', line 73 def reboot_info @reboot_info end |
#resource_collection ⇒ Chef::ResourceCollection
The collection of resources intended to be converged (and able to be notified).
95 96 97 |
# File 'lib/chef/run_context.rb', line 95 def resource_collection @resource_collection end |
Instance Method Details
#before_notifications(resource) ⇒ Array[Notification]
Get the list of before notifications sent by the given resource.
TODO seriously, this is actually wrong. resource.name is not unique, you need the type as well.
231 232 233 234 235 236 237 |
# File 'lib/chef/run_context.rb', line 231 def before_notifications(resource) if resource.instance_of?(Chef::Resource) return before_notification_collection[resource.name] else return before_notification_collection[resource.declared_key] end end |
#cancel_reboot ⇒ Object
548 549 550 551 |
# File 'lib/chef/run_context.rb', line 548 def cancel_reboot Chef::Log.info "Changing reboot status from #{reboot_info.inspect} to {}" @reboot_info = {} end |
#create_child ⇒ Object
Create a child RunContext.
560 561 562 |
# File 'lib/chef/run_context.rb', line 560 def create_child ChildRunContext.new(self) end |
#delayed_notifications(resource) ⇒ Array[Notification]
Get the list of delayed (end of run) notifications sent by the given resource.
TODO seriously, this is actually wrong. resource.name is not unique, you need the type as well.
264 265 266 267 268 269 270 |
# File 'lib/chef/run_context.rb', line 264 def delayed_notifications(resource) if resource.instance_of?(Chef::Resource) return delayed_notification_collection[resource.name] else return delayed_notification_collection[resource.declared_key] end end |
#has_cookbook_file_in_cookbook?(cookbook, cb_file_name) ⇒ Boolean
Find out if the cookbook has the given file.
494 495 496 497 |
# File 'lib/chef/run_context.rb', line 494 def has_cookbook_file_in_cookbook?(cookbook, cb_file_name) cookbook = cookbook_collection[cookbook] cookbook.has_cookbook_file_for_node?(node, cb_file_name) end |
#has_template_in_cookbook?(cookbook, template_name) ⇒ Boolean
Find out if the cookbook has the given template.
479 480 481 482 |
# File 'lib/chef/run_context.rb', line 479 def has_template_in_cookbook?(cookbook, template_name) cookbook = cookbook_collection[cookbook] cookbook.has_template_for_node?(node, template_name) end |
#immediate_notifications(resource) ⇒ Array[Notification]
Get the list of immediate notifications sent by the given resource.
TODO seriously, this is actually wrong. resource.name is not unique, you need the type as well.
247 248 249 250 251 252 253 |
# File 'lib/chef/run_context.rb', line 247 def immediate_notifications(resource) if resource.instance_of?(Chef::Resource) return immediate_notification_collection[resource.name] else return immediate_notification_collection[resource.declared_key] end end |
#include_recipe(*recipe_names, current_cookbook: nil) ⇒ Object
Evaluates the recipes recipe_names
. Used by DSL::IncludeRecipe
285 286 287 288 289 290 291 292 293 |
# File 'lib/chef/run_context.rb', line 285 def include_recipe(*recipe_names, current_cookbook: nil) result_recipes = Array.new recipe_names.flatten.each do |recipe_name| if result = load_recipe(recipe_name, current_cookbook: current_cookbook) result_recipes << result end end result_recipes end |
#initialize_child_state ⇒ Object
Initialize state that applies to both Chef::RunContext and Chef::ChildRunContext
173 174 175 176 177 178 179 |
# File 'lib/chef/run_context.rb', line 173 def initialize_child_state @audits = {} @resource_collection = Chef::ResourceCollection.new @before_notification_collection = Hash.new { |h, k| h[k] = [] } @immediate_notification_collection = Hash.new { |h, k| h[k] = [] } @delayed_notification_collection = Hash.new { |h, k| h[k] = [] } end |
#load(run_list_expansion) ⇒ Object
Triggers the compile phase of the chef run.
165 166 167 168 |
# File 'lib/chef/run_context.rb', line 165 def load(run_list_expansion) @cookbook_compiler = CookbookCompiler.new(self, run_list_expansion, events) cookbook_compiler.compile end |
#load_recipe(recipe_name, current_cookbook: nil) ⇒ Object
Evaluates the recipe recipe_name
. Used by DSL::IncludeRecipe
TODO I am sort of confused why we have both this and include_recipe …
I don't see anything different beyond accepting and returning an
array of recipes.
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 |
# File 'lib/chef/run_context.rb', line 310 def load_recipe(recipe_name, current_cookbook: nil) Chef::Log.debug("Loading recipe #{recipe_name} via include_recipe") cookbook_name, recipe_short_name = Chef::Recipe.parse_recipe_name(recipe_name, current_cookbook: current_cookbook) if unreachable_cookbook?(cookbook_name) # CHEF-4367 Chef::Log.warn(<<-ERROR_MESSAGE) MissingCookbookDependency: Recipe `#{recipe_name}` is not in the run_list, and cookbook '#{cookbook_name}' is not a dependency of any cookbook in the run_list. To load this recipe, first add a dependency on cookbook '#{cookbook_name}' in the cookbook you're including it from in that cookbook's metadata. ERROR_MESSAGE end if loaded_fully_qualified_recipe?(cookbook_name, recipe_short_name) Chef::Log.debug("I am not loading #{recipe_name}, because I have already seen it.") false else loaded_recipe(cookbook_name, recipe_short_name) node.loaded_recipe(cookbook_name, recipe_short_name) cookbook = cookbook_collection[cookbook_name] cookbook.load_recipe(recipe_short_name, self) end end |
#load_recipe_file(recipe_file) ⇒ Chef::Recipe
Load the given recipe from a filename.
345 346 347 348 349 350 351 352 353 354 |
# File 'lib/chef/run_context.rb', line 345 def load_recipe_file(recipe_file) if !File.exist?(recipe_file) raise Chef::Exceptions::RecipeNotFound, "could not find recipe file #{recipe_file}" end Chef::Log.debug("Loading recipe file #{recipe_file}") recipe = Chef::Recipe.new("@recipe_files", recipe_file, self) recipe.from_file(recipe_file) recipe end |
#loaded_attribute(cookbook, attribute_file) ⇒ Object
Mark a given attribute file as having been loaded.
462 463 464 |
# File 'lib/chef/run_context.rb', line 462 def loaded_attribute(cookbook, attribute_file) loaded_attributes_hash["#{cookbook}::#{attribute_file}"] = true end |
#loaded_attributes ⇒ Array[String]
A list of all attributes files that have been loaded.
Stored internally using a Hash, so order is predictable.
TODO is the above statement true in a 1.9+ ruby world? Is it relevant?
405 406 407 |
# File 'lib/chef/run_context.rb', line 405 def loaded_attributes loaded_attributes_hash.keys end |
#loaded_fully_qualified_attribute?(cookbook, attribute_file) ⇒ Boolean
Find out if a given attribute file has been loaded.
452 453 454 |
# File 'lib/chef/run_context.rb', line 452 def loaded_fully_qualified_attribute?(cookbook, attribute_file) loaded_attributes_hash.has_key?("#{cookbook}::#{attribute_file}") end |
#loaded_fully_qualified_recipe?(cookbook, recipe) ⇒ Boolean
Find out if a given recipe has been loaded.
417 418 419 |
# File 'lib/chef/run_context.rb', line 417 def loaded_fully_qualified_recipe?(cookbook, recipe) loaded_recipes_hash.has_key?("#{cookbook}::#{recipe}") end |
#loaded_recipe(cookbook, recipe) ⇒ Object
Mark a given recipe as having been loaded.
440 441 442 |
# File 'lib/chef/run_context.rb', line 440 def loaded_recipe(cookbook, recipe) loaded_recipes_hash["#{cookbook}::#{recipe}"] = true end |
#loaded_recipe?(recipe) ⇒ Boolean
Find out if a given recipe has been loaded.
429 430 431 432 |
# File 'lib/chef/run_context.rb', line 429 def loaded_recipe?(recipe) cookbook, recipe_name = Chef::Recipe.parse_recipe_name(recipe) loaded_fully_qualified_recipe?(cookbook, recipe_name) end |
#loaded_recipes ⇒ Array[String]
A list of all recipes that have been loaded.
This is stored internally as a Hash, so ordering is predictable.
TODO is the above statement true in a 1.9+ ruby world? Is it relevant?
391 392 393 |
# File 'lib/chef/run_context.rb', line 391 def loaded_recipes loaded_recipes_hash.keys end |
#notifies_before(notification) ⇒ Object
Adds an before notification to the before_notification_collection
.
186 187 188 189 190 191 192 193 |
# File 'lib/chef/run_context.rb', line 186 def notifies_before(notification) nr = notification. if nr.instance_of?(Chef::Resource) before_notification_collection[nr.name] << notification else before_notification_collection[nr.declared_key] << notification end end |
#notifies_delayed(notification) ⇒ Object
Adds a delayed notification to the delayed_notification_collection
.
214 215 216 217 218 219 220 221 |
# File 'lib/chef/run_context.rb', line 214 def notifies_delayed(notification) nr = notification. if nr.instance_of?(Chef::Resource) delayed_notification_collection[nr.name] << notification else delayed_notification_collection[nr.declared_key] << notification end end |
#notifies_immediately(notification) ⇒ Object
Adds an immediate notification to the immediate_notification_collection
.
200 201 202 203 204 205 206 207 |
# File 'lib/chef/run_context.rb', line 200 def notifies_immediately(notification) nr = notification. if nr.instance_of?(Chef::Resource) immediate_notification_collection[nr.name] << notification else immediate_notification_collection[nr.declared_key] << notification end end |
#open_stream(name: nil, **options) {|stream| ... } ⇒ EventDispatch::EventsOutputStream
Open a stream object that can be printed into and will dispatch to events
523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/chef/run_context.rb', line 523 def open_stream(name: nil, **) stream = EventDispatch::EventsOutputStream.new(events, name: name, **) if block_given? begin yield stream ensure stream.close end else stream end end |
#reboot_requested? ⇒ Boolean
553 554 555 |
# File 'lib/chef/run_context.rb', line 553 def reboot_requested? reboot_info.size > 0 end |
#request_reboot(reboot_info) ⇒ Object
there are options for how to handle multiple calls to these functions:
-
first call always wins (never change reboot_info once set).
-
last call always wins (happily change reboot_info whenever).
-
raise an exception on the first conflict.
-
disable reboot after this run if anyone ever calls :cancel.
-
raise an exception on any second call.
-
?
543 544 545 546 |
# File 'lib/chef/run_context.rb', line 543 def request_reboot(reboot_info) Chef::Log.info "Changing reboot status from #{self.reboot_info.inspect} to #{reboot_info.inspect}" @reboot_info = reboot_info end |
#resolve_attribute(cookbook_name, attr_file_name) ⇒ String
Look up an attribute filename.
369 370 371 372 373 374 375 376 377 |
# File 'lib/chef/run_context.rb', line 369 def resolve_attribute(cookbook_name, attr_file_name) cookbook = cookbook_collection[cookbook_name] raise Chef::Exceptions::CookbookNotFound, "could not find cookbook #{cookbook_name} while loading attribute #{name}" unless cookbook attribute_filename = cookbook.attribute_filenames_by_short_filename[attr_file_name] raise Chef::Exceptions::AttributeNotFound, "could not find filename for attribute #{attr_file_name} in cookbook #{cookbook_name}" unless attribute_filename attribute_filename end |
#unreachable_cookbook?(cookbook_name) ⇒ Boolean
Find out whether the given cookbook is in the cookbook dependency graph.
507 508 509 |
# File 'lib/chef/run_context.rb', line 507 def unreachable_cookbook?(cookbook_name) cookbook_compiler.unreachable_cookbook?(cookbook_name) end |