Class: Chef::Cookbook

Inherits:
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, #convert_to_snake_case, #filename_to_qualified_string

Constructor Details

#initialize(name) ⇒ Cookbook

Creates a new Chef::Cookbook object.

Returns

object<Chef::Cookbook>

Duh. :)



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

def initialize(name)
  @name = name
  @attribute_files = Array.new
  @attribute_names = Hash.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.



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

def attribute_files
  @attribute_files
end

#definition_filesObject

Returns the value of attribute definition_files.



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

def definition_files
  @definition_files
end

#lib_filesObject

Returns the value of attribute lib_files.



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

def lib_files
  @lib_files
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#provider_filesObject

Returns the value of attribute provider_files.



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

def provider_files
  @provider_files
end

#recipe_filesObject

Returns the value of attribute recipe_files.



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

def recipe_files
  @recipe_files
end

#remote_filesObject

Returns the value of attribute remote_files.



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

def remote_files
  @remote_files
end

#resource_filesObject

Returns the value of attribute resource_files.



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

def resource_files
  @resource_files
end

#template_filesObject

Returns the value of attribute template_files.



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

def template_files
  @template_files
end

Instance Method Details

#load_attribute(name, node) ⇒ Object



86
87
88
89
90
91
# File 'lib/chef/cookbook.rb', line 86

def load_attribute(name, node)
  attr_name = shorten_name(name)
  file = @attribute_files[@attribute_names[attr_name]]
  load_attribute_file(file, node)
  node
end

#load_attribute_file(file, node) ⇒ Object



81
82
83
84
# File 'lib/chef/cookbook.rb', line 81

def load_attribute_file(file, node)
  Chef::Log.debug("Loading attributes from #{file}")
  node.from_file(file)
end

#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>



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

def load_attributes(node)
  @attribute_files.each do |file|
    load_attribute_file(file, node)
  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.



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

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



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

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



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

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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/cookbook.rb', line 158

def load_recipe(name, node, collection=nil, definitions=nil, cookbook_loader=nil)
  cookbook_name = @name
  recipe_name = shorten_name(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



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

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)


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

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



150
151
152
153
154
155
156
# File 'lib/chef/cookbook.rb', line 150

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