summaryrefslogtreecommitdiff
path: root/chef/lib/chef/recipe.rb
blob: 1a0ded5e17f2efc378eb59d9e1631789a9fcc995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/resource'
Dir[File.join(File.dirname(__FILE__), 'resource/**/*.rb')].sort.each { |lib| require lib }
require 'chef/mixin/from_file'
require 'chef/mixin/language'
require 'chef/resource_collection'
require 'chef/cookbook_loader'
require 'chef/rest'

class Chef
  class Recipe
    
    include Chef::Mixin::FromFile
    include Chef::Mixin::Language
        
    attr_accessor :cookbook_name, :recipe_name, :recipe, :node, :collection, 
                  :definitions, :params, :cookbook_loader
    
    def initialize(cookbook_name, recipe_name, node, collection=nil, definitions=nil, cookbook_loader=nil)
      @cookbook_name = cookbook_name
      @recipe_name = recipe_name
      @node = node
      
      if collection
        @collection = collection
      else
        @collection = Chef::ResourceCollection.new()
      end
      
      if definitions
        @definitions = definitions
      else
        @definitions = Hash.new
      end
      
      if cookbook_loader
        @cookbook_loader = cookbook_loader
      else
        @cookbook_loader = Chef::CookbookLoader.new()
      end
      
      @params = Hash.new
      
      @@seen_recipes ||= Hash.new
    end
    
    def include_recipe(*args)
      args.flatten.each do |recipe|
        if @@seen_recipes.has_key?(recipe)
          Chef::Log.debug("I am not loading #{recipe}, because I have already seen it.")
          next
        end
        Chef::Log.debug("#{@@seen_recipes.inspect}")
        @@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
    
    def require_recipe(*args)
      include_recipe(*args)
    end
    
    def resources(*args)
      @collection.resources(*args)
    end
    
    def search(type, query, &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}")
      Chef::Log.debug("Searching #{type} index with #{query} returned #{results.length} entries")
      results.each do |sr|
        block.call(sr)
      end
    end
    
    # Sets a tag, or list of tags, for this node.  Syntactic sugar for
    # @node[:tags].  
    #
    # 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[:tags]
    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
    
    # 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
    def tagged?(*args)
      args.each do |tag|
        return false unless @node[:tags].include?(tag)
      end
      true
    end
    
    # Removes the list of tags from the node.
    #
    # === Parameters
    # tags<Array>:: A list of tags
    #
    # === Returns
    # tags<Array>:: The current list of @node[:tags]
    def untag(*args)
      args.each do |tag|
        @node[:tags].delete(tag)
      end
    end
        
    def method_missing(method_symbol, *args, &block)
      resource = nil
      # If we have a definition that matches, we want to use that instead.  This should
      # let you do some really crazy over-riding of "native" types, if you really want
      # to. 
      if @definitions.has_key?(method_symbol)
        # This dupes the high level object, but we still need to dup the params
        new_def = @definitions[method_symbol].dup
        new_def.params = new_def.params.dup
        # This sets up the parameter overrides
        new_def.instance_eval(&block) if block
        new_recipe = Chef::Recipe.new(@cookbook_name, @recipe_name, @node, @collection, @definitions, @cookbook_loader)
        new_recipe.params = new_def.params
        new_recipe.params[:name] = args[0]
        new_recipe.instance_eval(&new_def.recipe)
      else
        method_name = method_symbol.to_s
      # Otherwise, we're rocking the regular resource call route.
        rname = nil
        mn = method_name.match(/^(.+)_(.+)$/)
        if mn
          rname = "Chef::Resource::#{mn[1].capitalize}#{mn[2].capitalize}"
        else
          short_match = method_name.match(/^(.+)$/)
          if short_match
            rname = "Chef::Resource::#{short_match[1].capitalize}"
          end
        end
        begin
          args << @collection
          args << @node
          resource = eval(rname).new(*args)
          resource.cookbook_name = @cookbook_name
          resource.recipe_name = @recipe_name
          resource.params = @params
          resource.instance_eval(&block) if block
        rescue Exception => e
          if e.kind_of?(NameError) && e.to_s =~ /Chef::Resource/
            raise NameError, "Cannot find #{rname} for #{method_name}\nOriginal: #{e.to_s}"
          else
            raise e
          end
        end
        @collection << resource
        resource
      end
    end
  end
end