Class: Chef::Recipe

Inherits:
Object
  • Object
show all
Includes:
Mixin::FromFile, Mixin::Language, Mixin::RecipeDefinitionDSLCore
Defined in:
lib/chef/recipe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #filename_to_qualified_string

Methods included from Mixin::Language

#platform?, #value_for_platform

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initialize(cookbook_name, recipe_name, node, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Recipe

Returns a new instance of Recipe.



40
41
42
43
44
45
46
47
48
# File 'lib/chef/recipe.rb', line 40

def initialize(cookbook_name, recipe_name, node, collection=nil, definitions=nil, cookbook_loader=nil)
  @cookbook_name = cookbook_name
  @recipe_name = recipe_name
  @node = node
  @collection = collection || Chef::ResourceCollection.new
  @definitions = definitions || Hash.new
  @cookbook_loader = cookbook_loader || Chef::CookbookLoader.new
  @params = Hash.new      
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def collection
  @collection
end

#cookbook_loaderObject

Returns the value of attribute cookbook_loader.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def cookbook_loader
  @cookbook_loader
end

#cookbook_nameObject

Returns the value of attribute cookbook_name.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def cookbook_name
  @cookbook_name
end

#definitionsObject

Returns the value of attribute definitions.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def definitions
  @definitions
end

#nodeObject

Returns the value of attribute node.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def node
  @node
end

#paramsObject

Returns the value of attribute params.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def params
  @params
end

#recipeObject

Returns the value of attribute recipe.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def recipe
  @recipe
end

#recipe_nameObject

Returns the value of attribute recipe_name.



37
38
39
# File 'lib/chef/recipe.rb', line 37

def recipe_name
  @recipe_name
end

Instance Method Details

#include_recipe(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chef/recipe.rb', line 50

def include_recipe(*args)
  args.flatten.each do |recipe|
    if @node.run_state[:seen_recipes].has_key?(recipe)
      Chef::Log.debug("I am not loading #{recipe}, because I have already seen it.")
      next
    end        

    Chef::Log.debug("Loading Recipe #{recipe} via include_recipe")
    @node.run_state[:seen_recipes][recipe] = true
    
    rmatch = recipe.match(/(.+?)::(.+)/)
    if rmatch
      cookbook = @cookbook_loader[rmatch[1]]
      cookbook.load_recipe(rmatch[2], @node, @collection, @definitions, @cookbook_loader)
    else
      cookbook = @cookbook_loader[recipe]
      cookbook.load_recipe("default", @node, @collection, @definitions, @cookbook_loader)
    end
  end
end

#require_recipe(*args) ⇒ Object



71
72
73
# File 'lib/chef/recipe.rb', line 71

def require_recipe(*args)
  include_recipe(*args)
end

#resources(*args) ⇒ Object



75
76
77
# File 'lib/chef/recipe.rb', line 75

def resources(*args)
  @collection.resources(*args)
end

#search(type, query, attributes = [], &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/recipe.rb', line 79

def search(type, query, attributes=[], &block)
  Chef::Log.debug("Searching #{type} index with #{query}")
  r = Chef::REST.new(Chef::Config[:search_url])

  results = r.get_rest("search/#{type}?q=#{query}&a=#{attributes.join(',')}")
  Chef::Log.debug("Searching #{type} index with #{query} returned #{results.length} entries")
  if block
    results.each do |sr|
      block.call(sr)
    end
  else
    results
  end
end

#tag(*args) ⇒ Object

Sets a tag, or list of tags, for this node. Syntactic sugar for @node.

With no arguments, returns the list of tags.

Parameters

tags<Array>

A list of tags to add - can be a single string

Returns

tags<Array>

The contents of @node



104
105
106
107
108
109
110
111
112
113
# File 'lib/chef/recipe.rb', line 104

def tag(*args)
  if args.length > 0
    args.each do |tag|
      @node[:tags] << tag unless @node[:tags].include?(tag)
    end
    @node[:tags]
  else
    @node[:tags]
  end
end

#tagged?(*args) ⇒ Boolean

Returns true if the node is tagged with the supplied list of tags.

Parameters

tags<Array>

A list of tags

Returns

true<TrueClass>

If all the parameters are present

false<FalseClass>

If any of the parameters are missing

Returns:

  • (Boolean)


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

def tagged?(*args)
  args.each do |tag|
    return false unless @node[:tags].include?(tag)
  end
  true
end

#untag(*args) ⇒ Object

Removes the list of tags from the node.

Parameters

tags<Array>

A list of tags

Returns

tags<Array>

The current list of @node



137
138
139
140
141
# File 'lib/chef/recipe.rb', line 137

def untag(*args)
  args.each do |tag|
    @node[:tags].delete(tag)
  end
end