~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to lib/puppet/resource/type_collection.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
class Puppet::Resource::TypeCollection
2
2
  attr_reader :environment
 
3
  attr_accessor :parse_failed
3
4
 
4
5
  def clear
5
6
    @hostclasses.clear
6
7
    @definitions.clear
7
8
    @nodes.clear
 
9
    @watched_files.clear
8
10
  end
9
11
 
10
12
  def initialize(env)
19
21
    @watched_files = {}
20
22
  end
21
23
 
 
24
  def import_ast(ast, modname)
 
25
    ast.instantiate(modname).each do |instance|
 
26
      add(instance)
 
27
    end
 
28
  end
 
29
 
22
30
  def inspect
23
31
    "TypeCollection" + { :hostclasses => @hostclasses.keys, :definitions => @definitions.keys, :nodes => @nodes.keys }.inspect
24
32
  end
96
104
    @definitions[munge_name(name)]
97
105
  end
98
106
 
99
 
  def find(namespaces, name, type)
100
 
    #Array("") == [] for some reason
101
 
    namespaces = [namespaces] unless namespaces.is_a?(Array)
102
 
 
103
 
    if name =~ /^::/
104
 
      return send(type, name.sub(/^::/, ''))
105
 
    end
106
 
 
107
 
    namespaces.each do |namespace|
108
 
      ary = namespace.split("::")
109
 
 
110
 
      while ary.length > 0
111
 
        tmp_namespace = ary.join("::")
112
 
        if r = find_partially_qualified(tmp_namespace, name, type)
113
 
          return r
114
 
        end
115
 
 
116
 
        # Delete the second to last object, which reduces our namespace by one.
117
 
        ary.pop
118
 
      end
119
 
 
120
 
      if result = send(type, name)
121
 
        return result
122
 
      end
123
 
    end
124
 
    nil
125
 
  end
126
 
 
127
 
  def find_or_load(namespaces, name, type)
128
 
    name      = name.downcase
129
 
    namespaces = [namespaces] unless namespaces.is_a?(Array)
130
 
    namespaces = namespaces.collect { |ns| ns.downcase }
131
 
 
132
 
    # This could be done in the load_until, but the knowledge seems to
133
 
    # belong here.
134
 
    if r = find(namespaces, name, type)
135
 
      return r
136
 
    end
137
 
 
138
 
    loader.load_until(namespaces, name) { find(namespaces, name, type) }
139
 
  end
140
 
 
141
107
  def find_node(namespaces, name)
142
 
    find("", name, :node)
 
108
    @nodes[munge_name(name)]
143
109
  end
144
110
 
145
111
  def find_hostclass(namespaces, name)
156
122
    end
157
123
  end
158
124
 
159
 
  def perform_initial_import
160
 
    parser = Puppet::Parser::Parser.new(environment)
161
 
    if code = Puppet.settings.uninterpolated_value(:code, environment.to_s) and code != ""
162
 
      parser.string = code
163
 
    else
164
 
      file = Puppet.settings.value(:manifest, environment.to_s)
165
 
      parser.file = file
166
 
    end
167
 
    parser.parse
168
 
  rescue => detail
169
 
    @parse_failed = true
170
 
 
171
 
    msg = "Could not parse for environment #{environment}: #{detail}"
172
 
    error = Puppet::Error.new(msg)
173
 
    error.set_backtrace(detail.backtrace)
174
 
    raise error
175
 
  end
176
 
 
177
125
  def require_reparse?
178
126
    @parse_failed || stale?
179
127
  end
206
154
 
207
155
  private
208
156
 
209
 
  def find_partially_qualified(namespace, name, type)
210
 
    send(type, [namespace, name].join("::"))
 
157
  # Return a list of all possible fully-qualified names that might be
 
158
  # meant by the given name, in the context of namespaces.
 
159
  def resolve_namespaces(namespaces, name)
 
160
    name      = name.downcase
 
161
    if name =~ /^::/
 
162
      # name is explicitly fully qualified, so just return it, sans
 
163
      # initial "::".
 
164
      return [name.sub(/^::/, '')]
 
165
    end
 
166
    if name == ""
 
167
      # The name "" has special meaning--it always refers to a "main"
 
168
      # hostclass which contains all toplevel resources.
 
169
      return [""]
 
170
    end
 
171
 
 
172
    namespaces = [namespaces] unless namespaces.is_a?(Array)
 
173
    namespaces = namespaces.collect { |ns| ns.downcase }
 
174
 
 
175
    result = []
 
176
    namespaces.each do |namespace|
 
177
      ary = namespace.split("::")
 
178
 
 
179
      # Search each namespace nesting in innermost-to-outermost order.
 
180
      while ary.length > 0
 
181
        result << "#{ary.join("::")}::#{name}"
 
182
        ary.pop
 
183
      end
 
184
 
 
185
      # Finally, search the toplevel namespace.
 
186
      result << name
 
187
    end
 
188
 
 
189
    return result.uniq
 
190
  end
 
191
 
 
192
  # Resolve namespaces and find the given object.  Autoload it if
 
193
  # necessary.
 
194
  def find_or_load(namespaces, name, type)
 
195
    resolve_namespaces(namespaces, name).each do |fqname|
 
196
      if result = send(type, fqname) || loader.try_load_fqname(type, fqname)
 
197
        return result
 
198
      end
 
199
    end
 
200
 
 
201
    # Nothing found.
 
202
    return nil
211
203
  end
212
204
 
213
205
  def munge_name(name)