class Puppet::Resource::TypeCollection

@api private

Constants

COLON_COLON

Attributes

environment[R]
parse_failed[RW]

Public Class Methods

new(env) click to toggle source
   # File lib/puppet/resource/type_collection.rb
20 def initialize(env)
21   @environment = env
22   @hostclasses = {}
23   @definitions = {}
24   @nodes = {}
25   @notfound = {}
26   @lock = Puppet::Concurrent::Lock.new
27 
28   # So we can keep a list and match the first-defined regex
29   @node_list = []
30 end

Public Instance Methods

<<(thing) click to toggle source

@api private

   # File lib/puppet/resource/type_collection.rb
47 def <<(thing)
48   add(thing)
49   self
50 end
add(instance) click to toggle source
   # File lib/puppet/resource/type_collection.rb
52 def add(instance)
53   # return a merged instance, or the given
54   catch(:merged) {
55     send("add_#{instance.type}", instance)
56     instance.resource_type_collection = self
57     instance
58   }
59 end
add_definition(instance) click to toggle source
    # File lib/puppet/resource/type_collection.rb
131 def add_definition(instance)
132   dupe_check(instance, @hostclasses) { |dupe| _("'%{name}' is already defined%{error} as a class; cannot redefine as a definition") % { name: instance.name, error: dupe.error_context } }
133   dupe_check(instance, @definitions) { |dupe| _("Definition '%{name}' is already defined%{error}; cannot be redefined") % { name: instance.name, error: dupe.error_context } }
134 
135   @definitions[instance.name] = instance
136 end
add_hostclass(instance) click to toggle source
   # File lib/puppet/resource/type_collection.rb
61 def add_hostclass(instance)
62   handle_hostclass_merge(instance)
63   dupe_check(instance, @hostclasses) { |dupe| _("Class '%{klass}' is already defined%{error}; cannot redefine") % { klass: instance.name, error: dupe.error_context } }
64   dupe_check(instance, @nodes)       { |dupe| _("Node '%{klass}' is already defined%{error}; cannot be redefined as a class") % { klass: instance.name, error: dupe.error_context } }
65   dupe_check(instance, @definitions) { |dupe| _("Definition '%{klass}' is already defined%{error}; cannot be redefined as a class") % { klass: instance.name, error: dupe.error_context } }
66 
67   @hostclasses[instance.name] = instance
68   instance
69 end
add_node(instance) click to toggle source
    # File lib/puppet/resource/type_collection.rb
 95 def add_node(instance)
 96   dupe_check(instance, @nodes) { |dupe| _("Node '%{name}' is already defined%{error}; cannot redefine") % { name: instance.name, error: dupe.error_context } }
 97   dupe_check(instance, @hostclasses) { |dupe| _("Class '%{klass}' is already defined%{error}; cannot be redefined as a node") % { klass: instance.name, error: dupe.error_context } }
 98 
 99   @node_list << instance
100   @nodes[instance.name] = instance
101   instance
102 end
clear() click to toggle source
   # File lib/puppet/resource/type_collection.rb
13 def clear
14   @hostclasses.clear
15   @definitions.clear
16   @nodes.clear
17   @notfound.clear
18 end
definition(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
138 def definition(name)
139   @definitions[munge_name(name)]
140 end
find_definition(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
150 def find_definition(name)
151   find_or_load(name, :definition)
152 end
find_hostclass(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
146 def find_hostclass(name)
147   find_or_load(name, :hostclass)
148 end
find_node(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
142 def find_node(name)
143   @nodes[munge_name(name)]
144 end
handle_hostclass_merge(instance) click to toggle source
   # File lib/puppet/resource/type_collection.rb
71 def handle_hostclass_merge(instance)
72   # Only main class (named '') can be merged (for purpose of merging top-scopes).
73   return instance unless instance.name == ''
74   if instance.type == :hostclass && (other = @hostclasses[instance.name]) && other.type == :hostclass
75     other.merge(instance)
76     # throw is used to signal merge - avoids dupe checks and adding it to hostclasses
77     throw :merged, other
78   end
79 end
hostclass(name) click to toggle source
   # File lib/puppet/resource/type_collection.rb
91 def hostclass(name)
92   @hostclasses[munge_name(name)]
93 end
import_ast(ast, modname) click to toggle source
   # File lib/puppet/resource/type_collection.rb
32 def import_ast(ast, modname)
33   ast.instantiate(modname).each do |instance|
34     add(instance)
35   end
36 end
inspect() click to toggle source
   # File lib/puppet/resource/type_collection.rb
38 def inspect
39   "TypeCollection" + {
40     :hostclasses => @hostclasses.keys,
41     :definitions => @definitions.keys,
42     :nodes => @nodes.keys
43   }.inspect
44 end
loader() click to toggle source
    # File lib/puppet/resource/type_collection.rb
104 def loader
105   @loader ||= Puppet::Parser::TypeLoader.new(environment)
106 end
node(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
108 def node(name)
109   name = munge_name(name)
110 
111   node = @nodes[name]
112   if node
113     return node
114   end
115 
116   @node_list.each do |n|
117     next unless n.name_is_regex?
118     return n if n.match(name)
119   end
120   nil
121 end
node_exists?(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
123 def node_exists?(name)
124   @nodes[munge_name(name)]
125 end
nodes?() click to toggle source
    # File lib/puppet/resource/type_collection.rb
127 def nodes?
128   @nodes.length > 0
129 end
parse_failed?() click to toggle source
    # File lib/puppet/resource/type_collection.rb
162 def parse_failed?
163   @parse_failed
164 end
replace_settings(instance) click to toggle source

Replaces the known settings with a new instance (that must be named 'settings'). This is primarily needed for testing purposes. Also see PUP-5954 as it makes it illegal to merge classes other than the '' (main) class. Prior to this change settings where always merged rather than being defined from scratch for many testing scenarios not having a complete side effect free setup for compilation.

   # File lib/puppet/resource/type_collection.rb
87 def replace_settings(instance)
88   @hostclasses['settings'] = instance
89 end
version() click to toggle source
    # File lib/puppet/resource/type_collection.rb
166 def version
167   if !defined?(@version)
168     if environment.config_version.nil? || environment.config_version == ""
169       @version = Time.now.to_i
170     else
171       @version = Puppet::Util::Execution.execute([environment.config_version]).to_s.strip
172     end
173   end
174 
175   @version
176 rescue Puppet::ExecutionFailure => e
177   raise Puppet::ParseError, _("Execution of config_version command `%{cmd}` failed: %{message}") % { cmd: environment.config_version, message: e.message }, e.backtrace
178 end

Private Instance Methods

dupe_check(instance, hash) { |dupe| ... } click to toggle source
    # File lib/puppet/resource/type_collection.rb
214 def dupe_check(instance, hash)
215   dupe = hash[instance.name]
216   return unless dupe
217   message = yield dupe
218   instance.fail Puppet::ParseError, message
219 end
dupe_check_singleton(instance, set) { |set| ... } click to toggle source
    # File lib/puppet/resource/type_collection.rb
221 def dupe_check_singleton(instance, set)
222   return if set.empty?
223   message = yield set[0]
224   instance.fail Puppet::ParseError, message
225 end
find_or_load(name, type) click to toggle source

Resolve namespaces and find the given object. Autoload it if necessary.

    # File lib/puppet/resource/type_collection.rb
186 def find_or_load(name, type)
187   @lock.synchronize do
188     # Name is always absolute, but may start with :: which must be removed
189     fqname = (name[0,2] == COLON_COLON ? name[2..-1] : name)
190 
191     result = send(type, fqname)
192     unless result
193       if @notfound[ fqname ] && Puppet[ :ignoremissingtypes ]
194         # do not try to autoload if we already tried and it wasn't conclusive
195         # as this is a time consuming operation. Warn the user.
196         # Check first if debugging is on since the call to debug_once is expensive
197         if Puppet[:debug]
198           debug_once _("Not attempting to load %{type} %{fqname} as this object was missing during a prior compilation") % { type: type, fqname: fqname }
199         end
200       else
201         fqname = munge_name(fqname)
202         result = loader.try_load_fqname(type, fqname)
203         @notfound[ fqname ] = result.nil?
204       end
205     end
206     result
207   end
208 end
munge_name(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
210 def munge_name(name)
211   name.to_s.downcase
212 end