Class: Puppet::Pops::Loaders Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/loaders.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The name of a private loader should always end with ‘ private’

Defined Under Namespace

Classes: LoaderError, LoaderModuleData, ModuleResolver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, for_agent, load_from_pcore = true) ⇒ Loaders

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Loaders.

Raises:

  • (ArgumentError)


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
# File 'lib/puppet/pops/loaders.rb', line 38

def initialize(environment, for_agent, load_from_pcore = true)
  # Protect against environment havoc
  raise ArgumentError, _("Attempt to redefine already initialized loaders for environment") unless environment.loaders.nil?

  environment.loaders = self
  @environment = environment
  @loaders_by_name = {}

  add_loader_by_name(self.class.static_loader)

  # Create the set of loaders
  # 1. Puppet, loads from the "running" puppet - i.e. bundled functions, types, extension points and extensions
  #    These cannot be cached since a  loaded instance will be bound to its closure scope which holds on to
  #    a compiler and all loaded types. Subsequent request would find remains of the environment that loaded
  #    the content. PUP-4461.
  #
  @puppet_system_loader = create_puppet_system_loader()

  # 2. Cache loader(optional) - i.e. what puppet stores on disk via pluginsync; gate behind the for_agent flag.
  # 3. Environment loader - i.e. what is bound across the environment, may change for each setup
  #    TODO: loaders need to work when also running in an agent doing catalog application. There is no
  #    concept of environment the same way as when running as a master (except when doing apply).
  #    The creation mechanisms should probably differ between the two.
  @private_environment_loader =
    if for_agent
      @puppet_cache_loader = create_puppet_cache_loader
      create_environment_loader(environment, @puppet_cache_loader, load_from_pcore)
    else
      create_environment_loader(environment, @puppet_system_loader, load_from_pcore)
    end

  Pcore.init_env(@private_environment_loader)

  # 4. module loaders are set up from the create_environment_loader, they register themselves
end

Instance Attribute Details

#environmentObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/puppet/pops/loaders.rb', line 25

def environment
  @environment
end

#private_environment_loaderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
# File 'lib/puppet/pops/loaders.rb', line 24

def private_environment_loader
  @private_environment_loader
end

#public_environment_loaderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/puppet/pops/loaders.rb', line 23

def public_environment_loader
  @public_environment_loader
end

#puppet_cache_loaderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Commented out the :static_loader and :puppet_system_loader because of rubocop offenses with duplicated definitions of these generated methods, but keeping them here for visibility on how the loaders are stacked. attr_reader :static_loader attr_reader :puppet_system_loader



22
23
24
# File 'lib/puppet/pops/loaders.rb', line 22

def puppet_cache_loader
  @puppet_cache_loader
end

Class Method Details

.catalog_loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a loader to use when deserializing a catalog and then subsequenlty use user defined types found in that catalog.



168
169
170
171
172
173
174
175
# File 'lib/puppet/pops/loaders.rb', line 168

def self.catalog_loader
  loaders = Puppet.lookup(:loaders) { nil }
  if loaders.nil?
    loaders = Loaders.new(Puppet.lookup(:current_environment), true)
    Puppet.push_context(:loaders => loaders)
  end
  loaders.find_loader(nil)
end

.clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clears the cached static and puppet_system loaders (to enable testing)



82
83
84
85
86
87
# File 'lib/puppet/pops/loaders.rb', line 82

def self.clear
  @@static_loader = nil
  Puppet::Pops::Types::TypeFactory.clear
  Model.class_variable_set(:@@pcore_ast_initialized, false)
  Model.register_pcore_types
end

.find_loader(module_name) ⇒ Loader::Loader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls #loaders to obtain the {Loaders} instance and then uses it to find the appropriate loader for the given ‘module_name`, or for the environment in case `module_name` is `nil` or empty.

Parameters:

  • module_name (String, nil)

    the name of the module

Returns:

Raises:



96
97
98
# File 'lib/puppet/pops/loaders.rb', line 96

def self.find_loader(module_name)
  loaders.find_loader(module_name)
end

.implementation_registryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



119
120
121
122
# File 'lib/puppet/pops/loaders.rb', line 119

def self.implementation_registry
  loaders = Puppet.lookup(:loaders) { nil }
  loaders.nil? ? nil : loaders.implementation_registry
end

.loadersLoaders

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds the ‘Loaders` instance by looking up the :loaders in the global Puppet context

Returns:

  • (Loaders)

    the loaders instance

Raises:



182
183
184
185
186
187
# File 'lib/puppet/pops/loaders.rb', line 182

def self.loaders
  loaders = Puppet.lookup(:loaders) { nil }
  raise Puppet::ParseError, _("Internal Error: Puppet Context ':loaders' missing") if loaders.nil?

  loaders
end

.new(environment, for_agent = false, load_from_pcore = true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet/pops/loaders.rb', line 27

def self.new(environment, for_agent = false, load_from_pcore = true)
  environment.lock.synchronize do
    obj = environment.loaders
    if obj.nil?
      obj = allocate
      obj.send(:initialize, environment, for_agent, load_from_pcore)
    end
    obj
  end
end

.register_implementations_with_loader(obj_classes, name_authority, loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/puppet/pops/loaders.rb', line 133

def self.register_implementations_with_loader(obj_classes, name_authority, loader)
  types = obj_classes.map do |obj_class|
    type = obj_class._pcore_type
    typed_name = Loader::TypedName.new(:type, type.name, name_authority)
    entry = loader.loaded_entry(typed_name)
    loader.set_entry(typed_name, type) if entry.nil? || entry.value.nil?
    type
  end
  # Resolve lazy so that all types can cross reference each other
  types.each { |type| type.resolve(loader) }
end

.register_runtime3_type(name, origin) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register the given type with the Runtime3TypeLoader. The registration will not happen unless the type system has been initialized.

Parameters:

  • name (String, Symbol)

    the name of the entity being set

  • origin (URI)

    the origin or the source where the type is defined



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/puppet/pops/loaders.rb', line 151

def self.register_runtime3_type(name, origin)
  loaders = Puppet.lookup(:loaders) { nil }
  return nil if loaders.nil?

  rt3_loader = loaders.runtime3_type_loader
  return nil if rt3_loader.nil?

  name = name.to_s
  caps_name = Types::TypeFormatter.singleton.capitalize_segments(name)
  typed_name = Loader::TypedName.new(:type, name)
  rt3_loader.set_entry(typed_name, Types::PResourceType.new(caps_name), origin)
  nil
end

.register_static_implementations(obj_classes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register implementations using the global static loader



129
130
131
# File 'lib/puppet/pops/loaders.rb', line 129

def self.register_static_implementations(obj_classes)
  register_implementations_with_loader(obj_classes, Pcore::RUNTIME_NAME_AUTHORITY, static_loader)
end

.static_implementation_registryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



100
101
102
103
104
105
106
107
# File 'lib/puppet/pops/loaders.rb', line 100

def self.static_implementation_registry
  if !class_variable_defined?(:@@static_implementation_registry) || @@static_implementation_registry.nil?
    ir = Types::ImplementationRegistry.new
    Types::TypeParser.type_map.values.each { |t| ir.register_implementation(t.simple_name, t.class.name) }
    @@static_implementation_registry = ir
  end
  @@static_implementation_registry
end

.static_loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



109
110
111
112
113
114
115
116
117
# File 'lib/puppet/pops/loaders.rb', line 109

def self.static_loader
  # The static loader can only be changed after a reboot
  if !class_variable_defined?(:@@static_loader) || @@static_loader.nil?
    @@static_loader = Loader::StaticLoader.new()
    @@static_loader.register_aliases
    Pcore.init(@@static_loader, static_implementation_registry)
  end
  @@static_loader
end

Instance Method Details

#[](loader_name) ⇒ Loader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lookup a loader by its unique name.

Parameters:

  • loader_name (String)

    the name of the loader to lookup

Returns:

  • (Loader)

    the found loader

Raises:

  • (Puppet::ParserError)

    if no loader is found



194
195
196
197
198
199
200
201
202
# File 'lib/puppet/pops/loaders.rb', line 194

def [](loader_name)
  loader = @loaders_by_name[loader_name]
  if loader.nil?
    # Unable to find the module private loader. Try resolving the module
    loader = private_loader_for_module(loader_name[0..-9]) if loader_name.end_with?(' private')
    raise Puppet::ParseError, _("Unable to find loader named '%{loader_name}'") % { loader_name: loader_name } if loader.nil?
  end
  loader
end

#add_loader_by_name(loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



264
265
266
267
268
269
270
271
# File 'lib/puppet/pops/loaders.rb', line 264

def add_loader_by_name(loader)
  name = loader.loader_name
  if @loaders_by_name.include?(name)
    raise Puppet::ParseError, _("Internal Error: Attempt to redefine loader named '%{name}'") % { name: name }
  end

  @loaders_by_name[name] = loader
end

#find_loader(module_name) ⇒ Loader::Loader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds the appropriate loader for the given ‘module_name`, or for the environment in case `module_name` is `nil` or empty.

Parameters:

  • module_name (String, nil)

    the name of the module

Returns:

Raises:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/puppet/pops/loaders.rb', line 211

def find_loader(module_name)
  if module_name.nil? || EMPTY_STRING == module_name
    # Use the public environment loader
    public_environment_loader
  else
    # TODO : Later check if definition is private, and then add it to private_loader_for_module
    #
    loader = public_loader_for_module(module_name)
    if loader.nil?
      raise Puppet::ParseError, _("Internal Error: did not find public loader for module: '%{module_name}'") % { module_name: module_name }
    end

    loader
  end
end

#implementation_registryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



227
228
229
230
# File 'lib/puppet/pops/loaders.rb', line 227

def implementation_registry
  # Environment specific implementation registry
  @implementation_registry ||= Types::ImplementationRegistry.new(self.class.static_implementation_registry)
end

#instantiate_definition(definition, loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add given 4.x definition to the given loader.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/puppet/pops/loaders.rb', line 324

def instantiate_definition(definition, loader)
  case definition
  when Model::PlanDefinition
    instantiate_PlanDefinition(definition, loader)
  when Model::FunctionDefinition
    instantiate_FunctionDefinition(definition, loader)
  when Model::TypeAlias
    instantiate_TypeAlias(definition, loader)
  when Model::TypeMapping
    instantiate_TypeMapping(definition, loader)
  else
    raise Puppet::ParseError, "Internal Error: Unknown type of definition - got '#{definition.class}'"
  end
end

#instantiate_definitions(program, loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add 4.x definitions found in the given program to the given loader.



318
319
320
321
# File 'lib/puppet/pops/loaders.rb', line 318

def instantiate_definitions(program, loader)
  program.definitions.each { |d| instantiate_definition(d, loader) }
  nil
end

#load_main_manifestModel::Program

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load the main manifest for the given environment

There are two sources that can be used for the initial parse:

1. The value of `Puppet[:code]`: Puppet can take a string from
  its settings and parse that as a manifest. This is used by various
  Puppet applications to read in a manifest and pass it to the
  environment as a side effect. This is attempted first.
2. The contents of the environment's +manifest+ attribute: Puppet will
  try to load the environment manifest. The manifest must be a file.

Returns:



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/puppet/pops/loaders.rb', line 285

def load_main_manifest
  parser = Parser::EvaluatingParser.singleton
  parsed_code = Puppet[:code]
  program = if parsed_code != ""
              parser.parse_string(parsed_code, 'unknown-source-location')
            else
              file = @environment.manifest

              # if the manifest file is a reference to a directory, parse and
              # combine all .pp files in that directory
              if file == Puppet::Node::Environment::NO_MANIFEST
                nil
              elsif File.directory?(file)
                raise Puppet::Error, "manifest of environment '#{@environment.name}' appoints directory '#{file}'. It must be a file"
              elsif File.exist?(file)
                parser.parse_file(file)
              else
                raise Puppet::Error, "manifest of environment '#{@environment.name}' appoints '#{file}'. It does not exist"
              end
            end
  instantiate_definitions(program, public_environment_loader) unless program.nil?
  program
rescue Puppet::ParseErrorWithIssue => detail
  detail.environment = @environment.name
  raise
rescue => detail
  msg = _('Could not parse for environment %{env}: %{detail}') % { env: @environment, detail: detail }
  error = Puppet::Error.new(msg)
  error.set_backtrace(detail.backtrace)
  raise error
end

#pre_loadObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called after loader has been added to Puppet Context as :loaders so that dynamic types can be pre-loaded with a fully configured loader system



76
77
78
# File 'lib/puppet/pops/loaders.rb', line 76

def pre_load
  @puppet_system_loader.load(:type, 'error')
end

#private_loader_for_module(module_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



253
254
255
256
257
258
259
260
261
262
# File 'lib/puppet/pops/loaders.rb', line 253

def private_loader_for_module(module_name)
  md = @module_resolver[module_name] || (return nil)
  # Since there is interest in the visibility from the perspective of entities contained in the
  # module, it must be resolved (to provide this visibility).
  # See {#configure_loaders_for_modules}
  unless md.resolved?
    @module_resolver.resolve(md)
  end
  md.private_loader
end

#public_loader_for_module(module_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



244
245
246
247
248
249
250
251
# File 'lib/puppet/pops/loaders.rb', line 244

def public_loader_for_module(module_name)
  md = @module_resolver[module_name] || (return nil)
  # Note, this loader is not resolved until there is interest in the visibility of entities from the
  # perspective of something contained in the module. (Many request may pass through a module loader
  # without it loading anything.
  # See {#private_loader_for_module}, and not in {#configure_loaders_for_modules}
  md.public_loader
end

#puppet_system_loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



236
237
238
# File 'lib/puppet/pops/loaders.rb', line 236

def puppet_system_loader
  @puppet_system_loader
end

#register_implementations(obj_classes, name_authority) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



124
125
126
# File 'lib/puppet/pops/loaders.rb', line 124

def register_implementations(obj_classes, name_authority)
  self.class.register_implementations_with_loader(obj_classes, name_authority, @private_environment_loader)
end

#runtime3_type_loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



240
241
242
# File 'lib/puppet/pops/loaders.rb', line 240

def runtime3_type_loader
  @runtime3_type_loader
end

#static_loaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



232
233
234
# File 'lib/puppet/pops/loaders.rb', line 232

def static_loader
  self.class.static_loader
end