Class: Chef::RunContext

Inherits:
Object
  • 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, #require_recipe

Constructor Details

#initialize(node, cookbook_collection) ⇒ 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. :)



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

def initialize(node, cookbook_collection)
  @node = node
  @cookbook_collection = cookbook_collection
  @resource_collection = Chef::ResourceCollection.new
  @definitions = Hash.new
  
  # TODO: 5/18/2010 cw/timh - See note on Chef::Node's
  # cookbook_collection attr_accessor
  node.cookbook_collection = cookbook_collection

  load
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

#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

#loadObject



58
59
60
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
91
92
93
94
95
96
# File 'lib/chef/run_context.rb', line 58

def load
  foreach_cookbook_load_segment(:libraries) do |cookbook_name, filename|
    Chef::Log.debug("Loading cookbook #{cookbook_name}'s library file: #{filename}")
    require filename
  end
  
  foreach_cookbook_load_segment(:providers) do |cookbook_name, filename|
    Chef::Log.debug("Loading cookbook #{cookbook_name}'s providers from #{filename}")
    Chef::Provider.build_from_file(cookbook_name, filename)
  end
  
  foreach_cookbook_load_segment(:resources) do |cookbook_name, filename|
    Chef::Log.debug("Loading cookbook #{cookbook_name}'s resources from #{filename}")
    Chef::Resource.build_from_file(cookbook_name, filename)
  end

  node.load_attributes

  foreach_cookbook_load_segment(:definitions) do |cookbook_name, filename|
    Chef::Log.debug("Loading cookbook #{cookbook_name}'s definitions from #{filename}")
    resourcelist = Chef::ResourceDefinitionList.new
    resourcelist.from_file(filename)
    definitions.merge!(resourcelist.defines) do |key, oldval, newval|
      Chef::Log.info("Overriding duplicate definition #{key}, new found in #{filename}")
      newval
    end
  end

  # Retrieve the fully expanded list of recipes for the node by
  # resolving roles; this step also merges attributes into the
  # node from the roles/recipes included.
  recipe_names = node.expand!

  recipe_names.each do |recipe_name|
    # 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_name)
  end
end