Class: Chef::RunContext
- Inherits:
-
Object
- Object
- Chef::RunContext
- Extended by:
- Forwardable
- Defined in:
- lib/chef/run_context.rb,
lib/chef/run_context/cookbook_compiler.rb
Overview
Value object that loads and tracks the context of a Chef run
Direct Known Subclasses
Defined Under Namespace
Classes: ChildRunContext, CookbookCompiler
Instance Attribute Summary collapse
-
#action_collection ⇒ Chef::ActionCollection
Handle to the global action_collection of executed actions for reporting / data_collector /etc.
-
#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
The set of cookbooks involved in this run.
-
#definitions ⇒ Array[Chef::ResourceDefinition]
readonly
Resource Definitions for this run.
-
#delayed_actions ⇒ Array[Chef::Resource::Notification]
readonly
An Array containing the delayed (end of run) notifications triggered by resources during the converge phase of the chef 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
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.
-
#logger ⇒ Object
readonly
A child of the root Chef::Log logging object.
-
#node ⇒ Chef::Node
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 ⇒ Boolean
If the resource_collection is in unified_mode (no separate converge phase).
-
#rest ⇒ Chef::ServerAPI
Common rest object for using to talk to the Chef Server, this strictly ‘validates’ utf8 and will throw.
-
#rest_clean ⇒ Chef::ServerAPI
Common rest object for using to talk to the Chef Server, this has utf8 sanitization turned on and will replace invalid utf8 with valid characters.
-
#runner ⇒ Object
Pointer back to the Chef::Runner that created this.
-
#updated_resources ⇒ Object
readonly
A Set keyed by the string name, of all the resources that are updated.
Instance Method Summary collapse
-
#add_delayed_action(notification) ⇒ Object
Adds a delayed action to the delayed_actions collection.
-
#before_notifications(resource) ⇒ Array[Notification]
Get the list of before notifications sent by the given resource.
-
#cancel_reboot ⇒ Object
Cancels a pending reboot.
-
#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 = nil, cookbook_collection = nil, events = nil, logger = nil) ⇒ 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
Checks to see if a reboot has been requested.
-
#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.
-
#reverse_immediate_notifications(resource) ⇒ Array[Notification]
Get the list of immeidate notifications pending to the given resource.
-
#root_run_context ⇒ Chef::RunContext
The root run context.
-
#transport ⇒ Train::Plugins::Transport
Remote transport from Train.
-
#transport_connection ⇒ Train::Plugins::Transport::BaseConnection
Remote connection object from Train.
-
#unreachable_cookbook?(cookbook_name) ⇒ Boolean
Find out whether the given cookbook is in the cookbook dependency graph.
Constructor Details
#initialize(node = nil, cookbook_collection = nil, events = nil, logger = nil) ⇒ 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.
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/chef/run_context.rb', line 191 def initialize(node = nil, cookbook_collection = nil, events = nil, logger = nil) @events = events @logger = logger || Chef::Log.with_child self.node = node if node self.cookbook_collection = cookbook_collection if cookbook_collection @definitions = {} @loaded_recipes_hash = {} @loaded_attributes_hash = {} @reboot_info = {} @cookbook_compiler = nil initialize_child_state end |
Instance Attribute Details
#action_collection ⇒ Chef::ActionCollection
Handle to the global action_collection of executed actions for reporting / data_collector /etc
125 126 127 |
# File 'lib/chef/run_context.rb', line 125 def action_collection @action_collection 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.
141 142 143 |
# File 'lib/chef/run_context.rb', line 141 def before_notification_collection @before_notification_collection end |
#cookbook_collection ⇒ Chef::CookbookCollection
The set of cookbooks involved in this run
69 70 71 |
# File 'lib/chef/run_context.rb', line 69 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).
77 78 79 |
# File 'lib/chef/run_context.rb', line 77 def definitions @definitions end |
#delayed_actions ⇒ Array[Chef::Resource::Notification] (readonly)
An Array containing the delayed (end of run) notifications triggered by resources during the converge phase of the chef run.
164 165 166 |
# File 'lib/chef/run_context.rb', line 164 def delayed_actions @delayed_actions 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.
157 158 159 |
# File 'lib/chef/run_context.rb', line 157 def delayed_notification_collection @delayed_notification_collection end |
#events ⇒ Chef::EventDispatch::Dispatcher
Event dispatcher for this run.
83 84 85 |
# File 'lib/chef/run_context.rb', line 83 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.
149 150 151 |
# File 'lib/chef/run_context.rb', line 149 def immediate_notification_collection @immediate_notification_collection end |
#logger ⇒ Object (readonly)
A child of the root Chef::Log logging object.
180 181 182 |
# File 'lib/chef/run_context.rb', line 180 def logger @logger end |
#node ⇒ Chef::Node
The node for this run
62 63 64 |
# File 'lib/chef/run_context.rb', line 62 def node @node end |
#parent_run_context ⇒ Chef::RunContext (readonly)
The parent run context.
100 101 102 |
# File 'lib/chef/run_context.rb', line 100 def parent_run_context @parent_run_context end |
#reboot_info ⇒ Hash
Hash of factoids for a reboot request.
89 90 91 |
# File 'lib/chef/run_context.rb', line 89 def reboot_info @reboot_info end |
#resource_collection ⇒ Boolean
Returns If the resource_collection is in unified_mode (no separate converge phase).
119 120 121 |
# File 'lib/chef/run_context.rb', line 119 def resource_collection @resource_collection end |
#rest ⇒ Chef::ServerAPI
Common rest object for using to talk to the Chef Server, this strictly ‘validates’ utf8 and will throw. (will be nil on solo-legacy runs)
48 49 50 |
# File 'lib/chef/run_context.rb', line 48 def rest @rest end |
#rest_clean ⇒ Chef::ServerAPI
Common rest object for using to talk to the Chef Server, this has utf8 sanitization turned on and will replace invalid utf8 with valid characters. (will be nil on solo-legacy runs)
55 56 57 |
# File 'lib/chef/run_context.rb', line 55 def rest_clean @rest_clean end |
#runner ⇒ Object
Pointer back to the Chef::Runner that created this
129 130 131 |
# File 'lib/chef/run_context.rb', line 129 def runner @runner end |
#updated_resources ⇒ Object (readonly)
A Set keyed by the string name, of all the resources that are updated. We do not track actions or individual resource objects, since this matches the behavior of the notification collections which are keyed by Strings.
170 171 172 |
# File 'lib/chef/run_context.rb', line 170 def updated_resources @updated_resources end |
Instance Method Details
#add_delayed_action(notification) ⇒ Object
Adds a delayed action to the delayed_actions collection
283 284 285 286 287 288 289 290 |
# File 'lib/chef/run_context.rb', line 283 def add_delayed_action(notification) if delayed_actions.any? { |existing_notification| existing_notification.duplicates?(notification) } logger.info( "#{notification.} not queuing delayed action #{notification.action} on #{notification.resource}"\ " (delayed), as it's already been queued") else delayed_actions << notification end end |
#before_notifications(resource) ⇒ Array[Notification]
Get the list of before notifications sent by the given resource.
296 297 298 299 |
# File 'lib/chef/run_context.rb', line 296 def before_notifications(resource) key = resource.is_a?(String) ? resource : resource.declared_key before_notification_collection[key] end |
#cancel_reboot ⇒ Object
Cancels a pending reboot
612 613 614 615 |
# File 'lib/chef/run_context.rb', line 612 def cancel_reboot logger.info "Changing reboot status from #{reboot_info.inspect} to {}" @reboot_info = {} end |
#create_child ⇒ Object
Create a child RunContext.
644 645 646 |
# File 'lib/chef/run_context.rb', line 644 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.
328 329 330 331 |
# File 'lib/chef/run_context.rb', line 328 def delayed_notifications(resource) key = resource.is_a?(String) ? resource : resource.declared_key delayed_notification_collection[key] end |
#has_cookbook_file_in_cookbook?(cookbook, cb_file_name) ⇒ Boolean
Find out if the cookbook has the given file.
555 556 557 558 |
# File 'lib/chef/run_context.rb', line 555 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.
540 541 542 543 |
# File 'lib/chef/run_context.rb', line 540 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.
305 306 307 308 |
# File 'lib/chef/run_context.rb', line 305 def immediate_notifications(resource) key = resource.is_a?(String) ? resource : resource.declared_key immediate_notification_collection[key] end |
#include_recipe(*recipe_names, current_cookbook: nil) ⇒ Object
Evaluates the recipes recipe_names
. Used by DSL::IncludeRecipe
346 347 348 349 350 351 352 353 354 |
# File 'lib/chef/run_context.rb', line 346 def include_recipe(*recipe_names, current_cookbook: nil) result_recipes = [] 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
229 230 231 232 233 234 235 236 |
# File 'lib/chef/run_context.rb', line 229 def initialize_child_state @resource_collection = Chef::ResourceCollection.new(self) @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] = [] } @delayed_actions = [] @updated_resources = Set.new end |
#load(run_list_expansion) ⇒ Object
Triggers the compile phase of the chef run.
221 222 223 224 |
# File 'lib/chef/run_context.rb', line 221 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.
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 |
# File 'lib/chef/run_context.rb', line 371 def load_recipe(recipe_name, current_cookbook: nil) logger.trace("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 logger.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) logger.trace("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.
406 407 408 409 410 411 412 413 414 415 |
# File 'lib/chef/run_context.rb', line 406 def load_recipe_file(recipe_file) unless File.exist?(recipe_file) raise Chef::Exceptions::RecipeNotFound, "could not find recipe file #{recipe_file}" end logger.trace("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.
523 524 525 |
# File 'lib/chef/run_context.rb', line 523 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?
466 467 468 |
# File 'lib/chef/run_context.rb', line 466 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.
513 514 515 |
# File 'lib/chef/run_context.rb', line 513 def loaded_fully_qualified_attribute?(cookbook, attribute_file) loaded_attributes_hash.key?("#{cookbook}::#{attribute_file}") end |
#loaded_fully_qualified_recipe?(cookbook, recipe) ⇒ Boolean
Find out if a given recipe has been loaded.
478 479 480 |
# File 'lib/chef/run_context.rb', line 478 def loaded_fully_qualified_recipe?(cookbook, recipe) loaded_recipes_hash.key?("#{cookbook}::#{recipe}") end |
#loaded_recipe(cookbook, recipe) ⇒ Object
Mark a given recipe as having been loaded.
501 502 503 |
# File 'lib/chef/run_context.rb', line 501 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.
490 491 492 493 |
# File 'lib/chef/run_context.rb', line 490 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?
452 453 454 |
# File 'lib/chef/run_context.rb', line 452 def loaded_recipes loaded_recipes_hash.keys end |
#notifies_before(notification) ⇒ Object
Adds an before notification to the before_notification_collection
.
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/chef/run_context.rb', line 243 def notifies_before(notification) # Note for the future, notification.notifying_resource may be an instance # of Chef::Resource::UnresolvedSubscribes when calling {Resource#subscribes} # with a string value. if unified_mode && updated_resources.include?(notification..declared_key) raise Chef::Exceptions::UnifiedModeBeforeSubscriptionEarlierResource.new(notification) end before_notification_collection[notification..declared_key] << notification end |
#notifies_delayed(notification) ⇒ Object
Adds a delayed notification to the delayed_notification_collection
.
271 272 273 274 275 276 277 278 279 |
# File 'lib/chef/run_context.rb', line 271 def notifies_delayed(notification) # Note for the future, notification.notifying_resource may be an instance # of Chef::Resource::UnresolvedSubscribes when calling {Resource#subscribes} # with a string value. if unified_mode && updated_resources.include?(notification..declared_key) add_delayed_action(notification) end delayed_notification_collection[notification..declared_key] << notification end |
#notifies_immediately(notification) ⇒ Object
Adds an immediate notification to the immediate_notification_collection
.
259 260 261 262 263 264 |
# File 'lib/chef/run_context.rb', line 259 def notifies_immediately(notification) # Note for the future, notification.notifying_resource may be an instance # of Chef::Resource::UnresolvedSubscribes when calling {Resource#subscribes} # with a string value. immediate_notification_collection[notification..declared_key] << notification end |
#open_stream(name: nil, **options) {|stream| ... } ⇒ EventDispatch::EventsOutputStream
Open a stream object that can be printed into and will dispatch to events
584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/chef/run_context.rb', line 584 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
Checks to see if a reboot has been requested
621 622 623 |
# File 'lib/chef/run_context.rb', line 621 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.
-
?
604 605 606 607 |
# File 'lib/chef/run_context.rb', line 604 def request_reboot(reboot_info) logger.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.
430 431 432 433 434 435 436 437 438 |
# File 'lib/chef/run_context.rb', line 430 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 |
#reverse_immediate_notifications(resource) ⇒ Array[Notification]
Get the list of immeidate notifications pending to the given resource
314 315 316 317 318 319 320 321 |
# File 'lib/chef/run_context.rb', line 314 def reverse_immediate_notifications(resource) immediate_notification_collection.map do |k, v| v.select do |n| (n.resource.is_a?(String) && n.resource == resource.declared_key) || n.resource == resource end end.flatten end |
#root_run_context ⇒ Chef::RunContext
The root run context.
106 107 108 109 110 |
# File 'lib/chef/run_context.rb', line 106 def root_run_context rc = self rc = rc.parent_run_context until rc.parent_run_context.nil? rc end |
#transport ⇒ Train::Plugins::Transport
Remote transport from Train
629 630 631 |
# File 'lib/chef/run_context.rb', line 629 def transport @transport ||= Chef::TrainTransport.build_transport(logger) end |
#transport_connection ⇒ Train::Plugins::Transport::BaseConnection
Remote connection object from Train
637 638 639 |
# File 'lib/chef/run_context.rb', line 637 def transport_connection @transport_connection ||= transport&.connection end |
#unreachable_cookbook?(cookbook_name) ⇒ Boolean
Find out whether the given cookbook is in the cookbook dependency graph.
568 569 570 |
# File 'lib/chef/run_context.rb', line 568 def unreachable_cookbook?(cookbook_name) cookbook_compiler.unreachable_cookbook?(cookbook_name) end |