Class: Puppet::Parser::Compiler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AbstractCompiler, Puppet::Pops::Evaluator::Runtime3Support, Util, Util::Errors
Defined in:
lib/puppet/parser/compiler.rb,
lib/puppet/parser/compiler/catalog_validator.rb,
lib/puppet/parser/compiler/catalog_validator/relationship_validator.rb

Overview

Abstract class for a catalog validator that can be registered with the compiler to run at a certain stage.

Direct Known Subclasses

CatalogCompiler

Defined Under Namespace

Classes: CatalogValidationError, CatalogValidator

Constant Summary

Constants included from Puppet::Pops::Evaluator::Runtime3Support

Puppet::Pops::Evaluator::Runtime3Support::NAME_SPACE_SEPARATOR

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Puppet::Pops::Evaluator::Runtime3Support

#add_relationship, #call_function, #capitalize_qualified_name, #coerce_numeric, #convert, #create_local_scope_from, #create_match_scope_from, #create_resource_defaults, #create_resource_overrides, #create_resource_parameter, #create_resources, #diagnostic_producer, #external_call_function, #extract_file_line, #fail, #find_resource, #get_resource_parameter_value, #get_scope_nesting_level, #get_variable_value, #is_parameter_of_resource?, #is_true?, #optionally_fail, #runtime_issue, #set_match_data, #set_scope_nesting_level, #set_variable, #variable_bound?, #variable_exists?

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(node, code_id: nil) ⇒ Compiler

Returns a new instance of Compiler.



276
277
278
279
280
281
282
283
# File 'lib/puppet/parser/compiler.rb', line 276

def initialize(node, code_id: nil)
  @node = sanitize_node(node)
  @code_id = code_id
  initvars
  add_catalog_validators
  # Resolutions of fully qualified variable names
  @qualified_variables = {}
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def catalog
  @catalog
end

#code_idObject

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.

The id of code input to the compiler.



57
58
59
# File 'lib/puppet/parser/compiler.rb', line 57

def code_id
  @code_id
end

#collectionsObject (readonly)

Returns the value of attribute collections.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def collections
  @collections
end

#factsObject (readonly)

Returns the value of attribute facts.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def facts
  @facts
end

#loadersPuppet::Pops::Loader::Loaders (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.

Access to the configured loaders for 4x

Returns:

  • (Puppet::Pops::Loader::Loaders)

    the configured loaders



53
54
55
# File 'lib/puppet/parser/compiler.rb', line 53

def loaders
  @loaders
end

#nodeObject (readonly)

Returns the value of attribute node.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def node
  @node
end

#qualified_variablesObject (readonly)

Returns the value of attribute qualified_variables.



48
49
50
# File 'lib/puppet/parser/compiler.rb', line 48

def qualified_variables
  @qualified_variables
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def relationships
  @relationships
end

#resourcesObject (readonly)

Returns the value of attribute resources.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def resources
  @resources
end

#topscopeObject (readonly)

Returns the value of attribute topscope.



47
48
49
# File 'lib/puppet/parser/compiler.rb', line 47

def topscope
  @topscope
end

Class Method Details

.compile(node, code_id = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppet/parser/compiler.rb', line 23

def self.compile(node, code_id = nil)
  node.environment.check_for_reparse

  errors = node.environment.validation_errors
  unless errors.empty?
    errors.each { |e| Puppet.err(e) } if errors.size > 1
    errmsg = [
      _("Compilation has been halted because: %{error}") % { error: errors.first },
      _("For more information, see https://puppet.com/docs/puppet/latest/environments_about.html")
    ]
    raise(Puppet::Error, errmsg.join(' '))
  end

  new(node, :code_id => code_id).compile(&:to_resource)
rescue Puppet::ParseErrorWithIssue => detail
  detail.node = node.name
  Puppet.log_exception(detail)
  raise
rescue => detail
  message = _("%{message} on node %{node}") % { message: detail, node: node.name }
  Puppet.log_exception(detail, message)
  raise Puppet::Error, message, detail.backtrace
end

Instance Method Details

#add_catalog_validator(catalog_validators) ⇒ Object

Add a catalog validator that will run at some stage to this compiler

Parameters:

  • catalog_validators (Class<CatalogValidator>)

    The catalog validator class to add



106
107
108
109
# File 'lib/puppet/parser/compiler.rb', line 106

def add_catalog_validator(catalog_validators)
  @catalog_validators << catalog_validators
  nil
end

#add_catalog_validatorsObject



111
112
113
# File 'lib/puppet/parser/compiler.rb', line 111

def add_catalog_validators
  add_catalog_validator(CatalogValidator::RelationshipValidator)
end

#add_class(name) ⇒ Object

Store the fact that we’ve evaluated a class



100
101
102
# File 'lib/puppet/parser/compiler.rb', line 100

def add_class(name)
  @catalog.add_class(name) unless name == ""
end

#add_override(override) ⇒ Object

Store a resource override.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet/parser/compiler.rb', line 64

def add_override(override)
  # If possible, merge the override in immediately.
  resource = @catalog.resource(override.ref)
  if resource
    resource.merge(override)
  else
    # Otherwise, store the override for later; these
    # get evaluated in Resource#finish.
    @resource_overrides[override.ref] << override
  end
end

#add_resource(scope, resource) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/puppet/parser/compiler.rb', line 76

def add_resource(scope, resource)
  @resources << resource

  # Note that this will fail if the resource is not unique.
  @catalog.add_resource(resource)

  if !resource.class? and resource[:stage]
    # TRANSLATORS "stage" is a keyword in Puppet and should not be translated
    raise ArgumentError, _("Only classes can set 'stage'; normal resources like %{resource} cannot change run stage") % { resource: resource }
  end

  # Stages should not be inside of classes.  They are always a
  # top-level container, regardless of where they appear in the
  # manifest.
  return if resource.stage?

  # This adds a resource to the class it lexically appears in in the
  # manifest.
  unless resource.class?
    @catalog.add_edge(scope.resource, resource)
  end
end

#compileObject

Compiler our catalog. This mostly revolves around finding and evaluating classes. This is the main entry into our catalog.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/puppet/parser/compiler.rb', line 124

def compile
  Puppet.override(@context_overrides, _("For compiling %{node}") % { node: node.name }) do
    @catalog.environment_instance = environment

    # Set the client's parameters into the top scope.
    Puppet::Util::Profiler.profile(_("Compile: Set node parameters"), [:compiler, :set_node_params]) { set_node_parameters }

    Puppet::Util::Profiler.profile(_("Compile: Created settings scope"), [:compiler, :create_settings_scope]) { create_settings_scope }

    # TRANSLATORS "main" is a function name and should not be translated
    Puppet::Util::Profiler.profile(_("Compile: Evaluated main"), [:compiler, :evaluate_main]) { evaluate_main }

    Puppet::Util::Profiler.profile(_("Compile: Evaluated AST node"), [:compiler, :evaluate_ast_node]) { evaluate_ast_node }

    Puppet::Util::Profiler.profile(_("Compile: Evaluated node classes"), [:compiler, :evaluate_node_classes]) { evaluate_node_classes }

    Puppet::Util::Profiler.profile(_("Compile: Evaluated generators"), [:compiler, :evaluate_generators]) { evaluate_generators }

    Puppet::Util::Profiler.profile(_("Compile: Validate Catalog pre-finish"), [:compiler, :validate_pre_finish]) do
      validate_catalog(CatalogValidator::PRE_FINISH)
    end

    Puppet::Util::Profiler.profile(_("Compile: Finished catalog"), [:compiler, :finish_catalog]) { finish }

    fail_on_unevaluated

    Puppet::Util::Profiler.profile(_("Compile: Validate Catalog final"), [:compiler, :validate_final]) do
      validate_catalog(CatalogValidator::FINAL)
    end

    if block_given?
      yield @catalog
    else
      @catalog
    end
  end
end

#context_overridesObject

Constructs the overrides for the context



167
168
169
170
171
172
173
# File 'lib/puppet/parser/compiler.rb', line 167

def context_overrides
  {
    :current_environment => environment,
    :global_scope => @topscope, # 4x placeholder for new global scope
    :loaders => @loaders, # 4x loaders
  }
end

#environmentObject

Return the node’s environment.



178
179
180
# File 'lib/puppet/parser/compiler.rb', line 178

def environment
  node.environment
end

#evaluate_ast_nodeObject

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.

If ast nodes are enabled, then see if we can find and evaluate one.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/puppet/parser/compiler.rb', line 208

def evaluate_ast_node
  krt = environment.known_resource_types
  return unless krt.nodes? # ast_nodes?

  # Now see if we can find the node.
  astnode = nil
  @node.names.each do |name|
    astnode = krt.node(name.to_s.downcase)
    break if astnode
  end

  unless astnode ||= krt.node("default")
    raise Puppet::ParseError, _("Could not find node statement with name 'default' or '%{names}'") % { names: node.names.join(", ") }
  end

  # Create a resource to model this node, and then add it to the list
  # of resources.
  resource = astnode.ensure_in_catalog(topscope)

  resource.evaluate

  @node_scope = topscope.class_scope(astnode)
end

#evaluate_classes(classes, scope, lazy_evaluate = true) ⇒ Object

Evaluates each specified class in turn. If there are any classes that can’t be found, an error is raised. This method really just creates resource objects that point back to the classes, and then the resources are themselves evaluated later in the process.

Raises:



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/puppet/parser/compiler.rb', line 237

def evaluate_classes(classes, scope, lazy_evaluate = true)
  raise Puppet::DevError, _("No source for scope passed to evaluate_classes") unless scope.source

  class_parameters = nil
  # if we are a param class, save the classes hash
  # and transform classes to be the keys
  if classes.instance_of?(Hash)
    class_parameters = classes
    classes = classes.keys
  end

  hostclasses = classes.collect do |name|
    environment.known_resource_types.find_hostclass(name) or raise Puppet::Error, _("Could not find class %{name} for %{node}") % { name: name, node: node.name }
  end

  if class_parameters
    resources = ensure_classes_with_parameters(scope, hostclasses, class_parameters)
    unless lazy_evaluate
      resources.each(&:evaluate)
    end

    resources
  else
    already_included, newly_included = ensure_classes_without_parameters(scope, hostclasses)
    unless lazy_evaluate
      newly_included.each(&:evaluate)
    end

    already_included + newly_included
  end
end

#evaluate_node_classesObject

Evaluate all of the classes specified by the node. Classes with parameters are evaluated as if they were declared. Classes without parameters or with an empty set of parameters are evaluated as if they were included. This means classes with an empty set of parameters won’t conflict even if the class has already been included.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/puppet/parser/compiler.rb', line 187

def evaluate_node_classes
  if @node.classes.is_a? Hash
    classes_with_params, classes_without_params = @node.classes.partition { |_name, params| params and !params.empty? }

    # The results from Hash#partition are arrays of pairs rather than hashes,
    # so we have to convert to the forms evaluate_classes expects (Hash, and
    # Array of class names)
    classes_with_params = classes_with_params.to_h
    classes_without_params.map!(&:first)
  else
    classes_with_params = {}
    classes_without_params = @node.classes
  end

  evaluate_classes(classes_with_params, @node_scope || topscope)
  evaluate_classes(classes_without_params, @node_scope || topscope)
end

#evaluate_relationshipsObject



269
270
271
# File 'lib/puppet/parser/compiler.rb', line 269

def evaluate_relationships
  @relationships.each { |rel| rel.evaluate(catalog) }
end

#newscope(parent, options = {}) ⇒ Object

Create a new scope, with either a specified parent scope or using the top scope.



287
288
289
290
291
292
# File 'lib/puppet/parser/compiler.rb', line 287

def newscope(parent, options = {})
  parent ||= topscope
  scope = Puppet::Parser::Scope.new(self, **options)
  scope.parent = parent
  scope
end

#resource_overrides(resource) ⇒ Object

Return any overrides for the given resource.



295
296
297
# File 'lib/puppet/parser/compiler.rb', line 295

def resource_overrides(resource)
  @resource_overrides[resource.ref]
end

#validate_catalog(validation_stage) ⇒ Object



162
163
164
# File 'lib/puppet/parser/compiler.rb', line 162

def validate_catalog(validation_stage)
  @catalog_validators.select { |vclass| vclass.validation_stage?(validation_stage) }.each { |vclass| vclass.new(@catalog).validate }
end

#with_context_overrides(description = '', &block) ⇒ Object



118
119
120
# File 'lib/puppet/parser/compiler.rb', line 118

def with_context_overrides(description = '', &block)
  Puppet.override(@context_overrides, description, &block)
end