Class: Puppet::Pops::Loaders

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

Overview

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) ⇒ Loaders

Returns a new instance of Loaders.

Raises:

  • (ArgumentError)


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

def initialize(environment)
  # Protect against environment havoc
  raise ArgumentError.new("Attempt to redefine already initialized loaders for environment") unless environment.loaders.nil?
  environment.loaders = self
  @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. 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 = create_environment_loader(environment)

  # 3. The implementation registry maintains mappings between Puppet types and Runtime types for
  #    the current environment
  @implementation_registry = Types::ImplementationRegistry.new(@private_environment_loader)
  Pcore.init(@puppet_system_loader, @implementation_registry)

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

Instance Attribute Details

#implementation_registryObject (readonly)



18
19
20
# File 'lib/puppet/pops/loaders.rb', line 18

def implementation_registry
  @implementation_registry
end

#private_environment_loaderObject (readonly)



17
18
19
# File 'lib/puppet/pops/loaders.rb', line 17

def private_environment_loader
  @private_environment_loader
end

#public_environment_loaderObject (readonly)



16
17
18
# File 'lib/puppet/pops/loaders.rb', line 16

def public_environment_loader
  @public_environment_loader
end

#puppet_system_loaderObject (readonly)



15
16
17
# File 'lib/puppet/pops/loaders.rb', line 15

def puppet_system_loader
  @puppet_system_loader
end

#static_loaderObject (readonly)



14
15
16
# File 'lib/puppet/pops/loaders.rb', line 14

def static_loader
  @static_loader
end

Class Method Details

.clearObject

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



62
63
64
# File 'lib/puppet/pops/loaders.rb', line 62

def self.clear
  @@static_loader = nil
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:



73
74
75
# File 'lib/puppet/pops/loaders.rb', line 73

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

.implementation_registryObject



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

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:



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

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

.new(environment) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/puppet/pops/loaders.rb', line 20

def self.new(environment)
  obj = environment.loaders
  if obj.nil?
    obj = self.allocate
    obj.send(:initialize, environment)
  end
  obj
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



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

def self.register_runtime3_type(name, origin)
  loaders = Puppet.lookup(:loaders) { nil }
  unless loaders.nil?
    name = name.to_s
    caps_name = Types::TypeFormatter.singleton.capitalize_segments(name)
    typed_name = Loader::TypedName.new(:type, name.downcase)
    loaders.runtime3_type_loader.set_entry(typed_name, Types::PResourceType.new(caps_name), origin)
  end
  nil
end

.static_loaderObject



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

def self.static_loader
  # The static loader can only be changed after a reboot
  @@static_loader ||= Loader::StaticLoader.new()
end

Instance Method Details

#[](loader_name) ⇒ Loader

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



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

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}'" if loader.nil?
  end
  loader
end

#add_loader_by_name(loader) ⇒ Object

Raises:



202
203
204
205
206
# File 'lib/puppet/pops/loaders.rb', line 202

def add_loader_by_name(loader)
  name = loader.loader_name
  raise Puppet::ParseError, "Internal Error: Attempt to redefine loader named '#{name}'" if @loaders_by_name.include?(name)
  @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:



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

def find_loader(module_name)
  if module_name.nil? || module_name == ''
    # TODO : Later when fdefinition can be private, a decision is needed regarding what that means.
    #        A private environment loader could be used for logic outside of modules, then only that logic
    #        would see the definition.
    #
    # Use the private loader, this definition may see the environment's dependencies (currently, all modules)
    loader = private_environment_loader()
    raise Puppet::ParseError, 'Internal Error: did not find public loader' if loader.nil?
    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)
    raise Puppet::ParseError, "Internal Error: did not find public loader for module: '#{module_name}'" if loader.nil?
    loader
  end
end

#private_loader_for_module(module_name) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/puppet/pops/loaders.rb', line 191

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



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

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

#register_implementations(obj_classes, name_authority) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/pops/loaders.rb', line 87

def register_implementations(obj_classes, name_authority)
  loader = @private_environment_loader
  types = obj_classes.map do |obj_class|
    type = obj_class._ptype
    typed_name = Loader::TypedName.new(:type, type.name.downcase, name_authority)
    entry = loader.loaded_entry(typed_name)
    loader.set_entry(typed_name, type, obj_class._plocation) if entry.nil? || entry.value.nil?
    type
  end
  # Resolve lazy so that all types can cross reference each other
  parser = Types::TypeParser.singleton
  types.each { |type| type.resolve(parser, loader) }
end

#runtime3_type_loaderObject



178
179
180
# File 'lib/puppet/pops/loaders.rb', line 178

def runtime3_type_loader
  @runtime3_type_loader
end