Class: Chef::Cookbook

Inherits:
Object
  • Object
show all
Includes:
Mixin::ConvertToClassName
Defined in:
lib/chef/cookbook.rb,
lib/chef/cookbook/metadata.rb

Defined Under Namespace

Classes: Metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Constructor Details

#initialize(name) ⇒ Cookbook

Creates a new Chef::Cookbook object.

Returns

object<Chef::Cookbook>

Duh. :)



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/cookbook.rb', line 37

def initialize(name)
  @name = name
  @attribute_files = Array.new
  @definition_files = Array.new
  @template_files = Array.new
  @remote_files = Array.new
  @recipe_files = Array.new
  @recipe_names = Hash.new
  @lib_files = Array.new
  @resource_files = Array.new
  @provider_files = Array.new
end

Instance Attribute Details

#attribute_filesObject

Returns the value of attribute attribute_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def attribute_files
  @attribute_files
end

#definition_filesObject

Returns the value of attribute definition_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def definition_files
  @definition_files
end

#lib_filesObject

Returns the value of attribute lib_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def lib_files
  @lib_files
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def name
  @name
end

#provider_filesObject

Returns the value of attribute provider_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def provider_files
  @provider_files
end

#recipe_filesObject

Returns the value of attribute recipe_files.



31
32
33
# File 'lib/chef/cookbook.rb', line 31

def recipe_files
  @recipe_files
end

#remote_filesObject

Returns the value of attribute remote_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def remote_files
  @remote_files
end

#resource_filesObject

Returns the value of attribute resource_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def resource_files
  @resource_files
end

#template_filesObject

Returns the value of attribute template_files.



29
30
31
# File 'lib/chef/cookbook.rb', line 29

def template_files
  @template_files
end

Instance Method Details

#load_attributes(node) ⇒ Object

Loads all the attribute files in this cookbook within a particular <Chef::Node>.

Parameters

node<Chef::Node>

The Chef::Node to apply the attributes to

Returns

node<Chef::Node>

The updated Chef::Node object

Raises

<ArgumentError>

If the argument is not a kind_of? <Chef::Node>



72
73
74
75
76
77
78
79
80
81
# File 'lib/chef/cookbook.rb', line 72

def load_attributes(node)
  unless node.kind_of?(Chef::Node)
    raise ArgumentError, "You must pass a Chef::Node to load_attributes!"
  end
  @attribute_files.each do |file|
    Chef::Log.debug("Loading attributes from #{file}")
    node.from_file(file)
  end
  node
end

#load_definitionsObject

Loads all the resource definitions in this cookbook.

Returns

definitions<Hash>: A hash of <Chef::ResourceDefinition> objects, keyed by name.



87
88
89
90
91
92
93
94
95
96
# File 'lib/chef/cookbook.rb', line 87

def load_definitions
  results = Hash.new
  @definition_files.each do |file|
    Chef::Log.debug("Loading cookbook #{name}'s definitions from #{file}")
    resourcedef = Chef::ResourceDefinition.new
    resourcedef.from_file(file)
    results[resourcedef.name] = resourcedef
  end
  results
end

#load_librariesObject

Loads all the library files in this cookbook via require.

Returns

true

Always returns true



54
55
56
57
58
59
60
# File 'lib/chef/cookbook.rb', line 54

def load_libraries
  @lib_files.each do |file|
    Chef::Log.debug("Loading cookbook #{name} library file: #{file}")
    require file
  end
  true
end

#load_providersObject

Loads all the providers in this cookbook.

Returns

true

Always returns true



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

def load_providers
  @provider_files.each do |file|
    Chef::Log.debug("Loading cookbook #{name}'s providers from #{file}")
    Chef::Provider.build_from_file(name, file)
  end
end

#load_recipe(name, node, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/chef/cookbook.rb', line 154

def load_recipe(name, node, collection=nil, definitions=nil, cookbook_loader=nil)
  cookbook_name = @name
  recipe_name = nil
  nmatch = name.match(/^(.+?)::(.+)$/)
  recipe_name = nmatch ? nmatch[2] : name
  
  unless @recipe_names.has_key?(recipe_name)
    raise ArgumentError, "Cannot find a recipe matching #{recipe_name} in cookbook #{@name}"
  end
  Chef::Log.debug("Found recipe #{recipe_name} in cookbook #{cookbook_name}") if Chef::Log.debug?
  recipe = Chef::Recipe.new(cookbook_name, recipe_name, node, 
                            collection, definitions, cookbook_loader)
  recipe.from_file(@recipe_files[@recipe_names[recipe_name]])
  recipe
end

#load_resourcesObject

Loads all the resources in this cookbook.

Returns

true

Always returns true



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

def load_resources
  @resource_files.each do |file|
    Chef::Log.debug("Loading cookbook #{name}'s resources from #{file}")
    Chef::Resource.build_from_file(name, file)
  end
end

#recipe?(name) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
# File 'lib/chef/cookbook.rb', line 136

def recipe?(name)
  lookup_name = name
  if name =~ /(.+)::(.+)/
    cookbook_name = $1
    lookup_name = $2
    return false unless cookbook_name == @name
  end
  @recipe_names.has_key?(lookup_name)
end

#recipesObject



146
147
148
149
150
151
152
# File 'lib/chef/cookbook.rb', line 146

def recipes
  results = Array.new
  @recipe_names.each_key do |rname|
    results << "#{@name}::#{rname}"
  end
  results
end