Class: Chef::RunContext

Inherits:
Object show all
Includes:
Mixin::LanguageIncludeRecipe
Defined in:
lib/chef/run_context.rb

Overview

Chef::RunContext

Value object that loads and tracks the context of a Chef run

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::LanguageIncludeRecipe

#include_recipe, #load_recipe, #require_recipe

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. :)


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chef/run_context.rb', line 47

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

  # TODO: 5/18/2010 cw/timh - See note on Chef::Node's
  # cookbook_collection attr_accessor
  node.cookbook_collection = cookbook_collection
end

Instance Attribute Details

#cookbook_collectionObject (readonly)

Returns the value of attribute cookbook_collection


34
35
36
# File 'lib/chef/run_context.rb', line 34

def cookbook_collection
  @cookbook_collection
end

#definitionsObject (readonly)

Returns the value of attribute definitions


34
35
36
# File 'lib/chef/run_context.rb', line 34

def definitions
  @definitions
end

#delayed_notification_collectionObject

Needs to be settable so deploy can run a resource_collection independent of any cookbooks.


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

def delayed_notification_collection
  @delayed_notification_collection
end

#eventsObject (readonly)

Returns the value of attribute events


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

def events
  @events
end

#immediate_notification_collectionObject

Needs to be settable so deploy can run a resource_collection independent of any cookbooks.


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

def immediate_notification_collection
  @immediate_notification_collection
end

#nodeObject (readonly)

Returns the value of attribute node


34
35
36
# File 'lib/chef/run_context.rb', line 34

def node
  @node
end

#resource_collectionObject

Needs to be settable so deploy can run a resource_collection independent of any cookbooks.


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

def resource_collection
  @resource_collection
end

Instance Method Details

#delayed_notifications(resource) ⇒ Object


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

def delayed_notifications(resource)
  if resource.instance_of?(Chef::Resource)
    return @delayed_notification_collection[resource.name]
  else
    return @delayed_notification_collection[resource.to_s]
  end
end

#immediate_notifications(resource) ⇒ Object


116
117
118
119
120
121
122
# File 'lib/chef/run_context.rb', line 116

def immediate_notifications(resource)
  if resource.instance_of?(Chef::Resource)
    return @immediate_notification_collection[resource.name]
  else
    return @immediate_notification_collection[resource.to_s]
  end
end

#load(run_list_expansion) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/run_context.rb', line 61

def load(run_list_expansion)
  load_libraries

  load_lwrps
  load_attributes
  load_resource_definitions

  # Precendence rules state that roles' attributes come after
  # cookbooks. Now we've loaded attributes from cookbooks with
  # load_attributes, apply the expansion attributes (loaded from
  # roles) to the node.
  @node.apply_expansion_attributes(run_list_expansion)

  @events.recipe_load_start(run_list_expansion.recipes.size)
  run_list_expansion.recipes.each do |recipe|
    begin
      # TODO: timh/cw, 5-14-2010: It's distasteful to be including
      # the DSL in a class outside the context of the DSL
      include_recipe(recipe)
    rescue Chef::Exceptions::RecipeNotFound => e
      @events.recipe_not_found(e)
      raise
    rescue Exception => e
      path = resolve_recipe(recipe)
      @events.recipe_file_load_failed(path, e)
      raise
    end
  end
  @events.recipe_load_complete
end

#notifies_delayed(notification) ⇒ Object


107
108
109
110
111
112
113
114
# File 'lib/chef/run_context.rb', line 107

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.to_s] << notification
  end
end

#notifies_immediately(notification) ⇒ Object


98
99
100
101
102
103
104
105
# File 'lib/chef/run_context.rb', line 98

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.to_s] << notification
  end
end

#resolve_recipe(recipe_name) ⇒ Object


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

def resolve_recipe(recipe_name)
  cookbook_name, recipe_short_name = Chef::Recipe.parse_recipe_name(recipe_name)
  cookbook = cookbook_collection[cookbook_name]
  cookbook.recipe_filenames_by_name[recipe_short_name]
end