Class: Chef::RunContext

Inherits:
Object
  • Object
show all
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

Defined Under Namespace

Classes: CookbookCompiler

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Returns

object<Chef::RunContext>

Duh. :)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/run_context.rb', line 75

def initialize(node, cookbook_collection, events)
  @node = node
  @cookbook_collection = cookbook_collection
  @resource_collection = Chef::ResourceCollection.new
  @audits = {}
  @immediate_notification_collection = Hash.new {|h,k| h[k] = []}
  @delayed_notification_collection = Hash.new {|h,k| h[k] = []}
  @definitions = Hash.new
  @loaded_recipes = {}
  @loaded_attributes = {}
  @events = events
  @reboot_info = {}

  @node.run_context = self
  @cookbook_compiler = nil
end

Instance Attribute Details

#auditsObject

The list of audits (control groups) to execute during the audit phase



54
55
56
# File 'lib/chef/run_context.rb', line 54

def audits
  @audits
end

#cookbook_collectionObject (readonly)

Chef::CookbookCollection for this run



39
40
41
# File 'lib/chef/run_context.rb', line 39

def cookbook_collection
  @cookbook_collection
end

#definitionsObject (readonly)

Resource Definitions for this run. Populated when the files in definitions/ are evaluated (this is triggered by #load).



43
44
45
# File 'lib/chef/run_context.rb', line 43

def definitions
  @definitions
end

#delayed_notification_collectionObject

A Hash containing the delayed (end of run) notifications triggered by resources during the converge phase of the chef run.



62
63
64
# File 'lib/chef/run_context.rb', line 62

def delayed_notification_collection
  @delayed_notification_collection
end

#eventsObject (readonly)

Event dispatcher for this run.



65
66
67
# File 'lib/chef/run_context.rb', line 65

def events
  @events
end

#immediate_notification_collectionObject

A Hash containing the immediate notifications triggered by resources during the converge phase of the chef run.



58
59
60
# File 'lib/chef/run_context.rb', line 58

def immediate_notification_collection
  @immediate_notification_collection
end

#nodeObject (readonly)

Chef::Node object for this run



36
37
38
# File 'lib/chef/run_context.rb', line 36

def node
  @node
end

#reboot_infoObject (readonly)

Hash of factoids for a reboot request.



68
69
70
# File 'lib/chef/run_context.rb', line 68

def reboot_info
  @reboot_info
end

#resource_collectionObject

The Chef::ResourceCollection for this run. Populated by evaluating recipes, which is triggered by #load. (See also: CookbookCompiler)



51
52
53
# File 'lib/chef/run_context.rb', line 51

def resource_collection
  @resource_collection
end

Instance Method Details

#cancel_rebootObject



293
294
295
296
# File 'lib/chef/run_context.rb', line 293

def cancel_reboot
  Chef::Log::info "Changing reboot status from #{@reboot_info.inspect} to {}"
  @reboot_info = {}
end

#delayed_notifications(resource) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/chef/run_context.rb', line 130

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

Returns:

  • (Boolean)


248
249
250
251
# File 'lib/chef/run_context.rb', line 248

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

Cookbook File Introspection

Returns:

  • (Boolean)


243
244
245
246
# File 'lib/chef/run_context.rb', line 243

def has_template_in_cookbook?(cookbook, template_name)
  cookbook = cookbook_collection[cookbook]
  cookbook.has_template_for_node?(node, template_name)
end

#immediate_notifications(resource) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/chef/run_context.rb', line 122

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



139
140
141
142
143
144
145
146
147
# File 'lib/chef/run_context.rb', line 139

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

#load(run_list_expansion) ⇒ Object

Triggers the compile phase of the chef run. Implemented by Chef::RunContext::CookbookCompiler



94
95
96
97
# File 'lib/chef/run_context.rb', line 94

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/chef/run_context.rb', line 150

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) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/chef/run_context.rb', line 177

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



236
237
238
# File 'lib/chef/run_context.rb', line 236

def loaded_attribute(cookbook, attribute_file)
  @loaded_attributes["#{cookbook}::#{attribute_file}"] = true
end

#loaded_attributesObject

An Array of all attributes files that have been loaded. Stored internally using a Hash, so order is predictable.

Attribute file names are given in fully qualified form, e.g., “nginx::default” instead of “nginx”.



216
217
218
# File 'lib/chef/run_context.rb', line 216

def loaded_attributes
  @loaded_attributes.keys
end

#loaded_fully_qualified_attribute?(cookbook, attribute_file) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/chef/run_context.rb', line 232

def loaded_fully_qualified_attribute?(cookbook, attribute_file)
  @loaded_attributes.has_key?("#{cookbook}::#{attribute_file}")
end

#loaded_fully_qualified_recipe?(cookbook, recipe) ⇒ Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/chef/run_context.rb', line 220

def loaded_fully_qualified_recipe?(cookbook, recipe)
  @loaded_recipes.has_key?("#{cookbook}::#{recipe}")
end

#loaded_recipe?(recipe) ⇒ Boolean

Returns true if recipe has been loaded, false otherwise. Default recipe names are expanded, so ‘loaded_recipe?(“nginx”)` and `loaded_recipe?(“nginx::default”)` are valid and give identical results.

Returns:

  • (Boolean)


227
228
229
230
# File 'lib/chef/run_context.rb', line 227

def loaded_recipe?(recipe)
  cookbook, recipe_name = Chef::Recipe.parse_recipe_name(recipe)
  loaded_fully_qualified_recipe?(cookbook, recipe_name)
end

#loaded_recipesObject

An Array of all recipes that have been loaded. This is stored internally as a Hash, so ordering is predictable.

Recipe names are given in fully qualified form, e.g., the recipe “nginx” will be given as “nginx::default”

To determine if a particular recipe has been loaded, use #loaded_recipe?



207
208
209
# File 'lib/chef/run_context.rb', line 207

def loaded_recipes
  @loaded_recipes.keys
end

#notifies_delayed(notification) ⇒ Object

Adds a delayed notification to the delayed_notification_collection. The notification should be a Chef::Resource::Notification or duck type.



113
114
115
116
117
118
119
120
# File 'lib/chef/run_context.rb', line 113

def notifies_delayed(notification)
  nr = notification.notifying_resource
  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. The notification should be a Chef::Resource::Notification or duck type.



102
103
104
105
106
107
108
109
# File 'lib/chef/run_context.rb', line 102

def notifies_immediately(notification)
  nr = notification.notifying_resource
  if nr.instance_of?(Chef::Resource)
    @immediate_notification_collection[nr.name] << notification
  else
    @immediate_notification_collection[nr.declared_key] << notification
  end
end

#open_stream(options = {}) ⇒ Object

Open a stream object that can be printed into and will dispatch to events

Arguments

options is a hash with these possible options:

  • name: a string that identifies the stream to the user. Preferably short.

Pass a block and the stream will be yielded to it, and close on its own at the end of the block.



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/chef/run_context.rb', line 268

def open_stream(options = {})
  stream = EventDispatch::EventsOutputStream.new(events, options)
  if block_given?
    begin
      yield stream
    ensure
      stream.close
    end
  else
    stream
  end
end

#reboot_requested?Boolean

Returns:

  • (Boolean)


298
299
300
# File 'lib/chef/run_context.rb', line 298

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:

  1. first call always wins (never change @reboot_info once set).

  2. last call always wins (happily change @reboot_info whenever).

  3. raise an exception on the first conflict.

  4. disable reboot after this run if anyone ever calls :cancel.

  5. raise an exception on any second call.

  6. ?



288
289
290
291
# File 'lib/chef/run_context.rb', line 288

def request_reboot(reboot_info)
  Chef::Log::info "Changing reboot status from #{@reboot_info.inspect} to #{reboot_info.inspect}"
  @reboot_info = reboot_info
end

#resolve_attribute(cookbook_name, attr_file_name) ⇒ Object

Looks up an attribute file given the cookbook_name and attr_file_name. Used by DSL::IncludeAttribute



190
191
192
193
194
195
196
197
198
# File 'lib/chef/run_context.rb', line 190

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

Delegates to CookbookCompiler#unreachable_cookbook? Used to raise an error when attempting to load a recipe belonging to a cookbook that is not in the dependency graph. See also: CHEF-4367

Returns:

  • (Boolean)


256
257
258
# File 'lib/chef/run_context.rb', line 256

def unreachable_cookbook?(cookbook_name)
  @cookbook_compiler.unreachable_cookbook?(cookbook_name)
end