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
21 def initialize(env)
22   @environment = env
23   @hostclasses = {}
24   @definitions = {}
25   @nodes = {}
26   @notfound = {}
27   # always lock the environment before acquiring this lock
28   @lock = Puppet::Concurrent::Lock.new
29 
30   # So we can keep a list and match the first-defined regex
31   @node_list = []
32 end

Public Instance Methods

<<(thing) click to toggle source

@api private

   # File lib/puppet/resource/type_collection.rb
49 def <<(thing)
50   add(thing)
51   self
52 end
add(instance) click to toggle source
   # File lib/puppet/resource/type_collection.rb
54 def add(instance)
55   # return a merged instance, or the given
56   catch(:merged) {
57     send("add_#{instance.type}", instance)
58     instance.resource_type_collection = self
59     instance
60   }
61 end
add_definition(instance) click to toggle source
    # File lib/puppet/resource/type_collection.rb
133 def add_definition(instance)
134   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 } }
135   dupe_check(instance, @definitions) { |dupe| _("Definition '%{name}' is already defined%{error}; cannot be redefined") % { name: instance.name, error: dupe.error_context } }
136 
137   @definitions[instance.name] = instance
138 end
add_hostclass(instance) click to toggle source
   # File lib/puppet/resource/type_collection.rb
63 def add_hostclass(instance)
64   handle_hostclass_merge(instance)
65   dupe_check(instance, @hostclasses) { |dupe| _("Class '%{klass}' is already defined%{error}; cannot redefine") % { klass: instance.name, error: dupe.error_context } }
66   dupe_check(instance, @nodes)       { |dupe| _("Node '%{klass}' is already defined%{error}; cannot be redefined as a class") % { klass: instance.name, error: dupe.error_context } }
67   dupe_check(instance, @definitions) { |dupe| _("Definition '%{klass}' is already defined%{error}; cannot be redefined as a class") % { klass: instance.name, error: dupe.error_context } }
68 
69   @hostclasses[instance.name] = instance
70   instance
71 end
add_node(instance) click to toggle source
    # File lib/puppet/resource/type_collection.rb
 97 def add_node(instance)
 98   dupe_check(instance, @nodes) { |dupe| _("Node '%{name}' is already defined%{error}; cannot redefine") % { name: instance.name, error: dupe.error_context } }
 99   dupe_check(instance, @hostclasses) { |dupe| _("Class '%{klass}' is already defined%{error}; cannot be redefined as a node") % { klass: instance.name, error: dupe.error_context } }
100 
101   @node_list << instance
102   @nodes[instance.name] = instance
103   instance
104 end
clear() click to toggle source
   # File lib/puppet/resource/type_collection.rb
14 def clear
15   @hostclasses.clear
16   @definitions.clear
17   @nodes.clear
18   @notfound.clear
19 end
definition(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
140 def definition(name)
141   @definitions[munge_name(name)]
142 end
find_definition(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
152 def find_definition(name)
153   find_or_load(name, :definition)
154 end
find_hostclass(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
148 def find_hostclass(name)
149   find_or_load(name, :hostclass)
150 end
find_node(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
144 def find_node(name)
145   @nodes[munge_name(name)]
146 end
handle_hostclass_merge(instance) click to toggle source
   # File lib/puppet/resource/type_collection.rb
73 def handle_hostclass_merge(instance)
74   # Only main class (named '') can be merged (for purpose of merging top-scopes).
75   return instance unless instance.name == ''
76   if instance.type == :hostclass && (other = @hostclasses[instance.name]) && other.type == :hostclass
77     other.merge(instance)
78     # throw is used to signal merge - avoids dupe checks and adding it to hostclasses
79     throw :merged, other
80   end
81 end
hostclass(name) click to toggle source
   # File lib/puppet/resource/type_collection.rb
93 def hostclass(name)
94   @hostclasses[munge_name(name)]
95 end
import_ast(ast, modname) click to toggle source
   # File lib/puppet/resource/type_collection.rb
34 def import_ast(ast, modname)
35   ast.instantiate(modname).each do |instance|
36     add(instance)
37   end
38 end
inspect() click to toggle source
   # File lib/puppet/resource/type_collection.rb
40 def inspect
41   "TypeCollection" + {
42     :hostclasses => @hostclasses.keys,
43     :definitions => @definitions.keys,
44     :nodes => @nodes.keys
45   }.inspect
46 end
loader() click to toggle source
    # File lib/puppet/resource/type_collection.rb
106 def loader
107   @loader ||= Puppet::Parser::TypeLoader.new(environment)
108 end
node(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
110 def node(name)
111   name = munge_name(name)
112 
113   node = @nodes[name]
114   if node
115     return node
116   end
117 
118   @node_list.each do |n|
119     next unless n.name_is_regex?
120     return n if n.match(name)
121   end
122   nil
123 end
node_exists?(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
125 def node_exists?(name)
126   @nodes[munge_name(name)]
127 end
nodes?() click to toggle source
    # File lib/puppet/resource/type_collection.rb
129 def nodes?
130   @nodes.length > 0
131 end
parse_failed?() click to toggle source
    # File lib/puppet/resource/type_collection.rb
164 def parse_failed?
165   @parse_failed
166 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
89 def replace_settings(instance)
90   @hostclasses['settings'] = instance
91 end
version() click to toggle source
    # File lib/puppet/resource/type_collection.rb
168 def version
169   if !defined?(@version)
170     if environment.config_version.nil? || environment.config_version == ""
171       @version = Time.now.to_i
172     else
173       @version = Puppet::Util::Execution.execute([environment.config_version]).to_s.strip
174     end
175   end
176 
177   @version
178 rescue Puppet::ExecutionFailure => e
179   raise Puppet::ParseError, _("Execution of config_version command `%{cmd}` failed: %{message}") % { cmd: environment.config_version, message: e.message }, e.backtrace
180 end

Private Instance Methods

dupe_check(instance, hash) { |dupe| ... } click to toggle source
    # File lib/puppet/resource/type_collection.rb
219 def dupe_check(instance, hash)
220   dupe = hash[instance.name]
221   return unless dupe
222   message = yield dupe
223   instance.fail Puppet::ParseError, message
224 end
dupe_check_singleton(instance, set) { |set| ... } click to toggle source
    # File lib/puppet/resource/type_collection.rb
226 def dupe_check_singleton(instance, set)
227   return if set.empty?
228   message = yield set[0]
229   instance.fail Puppet::ParseError, message
230 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
188 def find_or_load(name, type)
189   # always lock the environment before locking the type collection
190   @environment.lock.synchronize do
191     @lock.synchronize do
192       # Name is always absolute, but may start with :: which must be removed
193       fqname = (name[0,2] == COLON_COLON ? name[2..-1] : name)
194 
195       result = send(type, fqname)
196       unless result
197         if @notfound[ fqname ] && Puppet[ :ignoremissingtypes ]
198           # do not try to autoload if we already tried and it wasn't conclusive
199           # as this is a time consuming operation. Warn the user.
200           # Check first if debugging is on since the call to debug_once is expensive
201           if Puppet[:debug]
202             debug_once _("Not attempting to load %{type} %{fqname} as this object was missing during a prior compilation") % { type: type, fqname: fqname }
203           end
204         else
205           fqname = munge_name(fqname)
206           result = loader.try_load_fqname(type, fqname)
207           @notfound[ fqname ] = result.nil?
208         end
209       end
210       result
211     end
212   end
213 end
munge_name(name) click to toggle source
    # File lib/puppet/resource/type_collection.rb
215 def munge_name(name)
216   name.to_s.downcase
217 end