Class: Puppet::Parser::ScriptCompiler

Inherits:
Object
  • Object
show all
Includes:
AbstractCompiler
Defined in:
lib/puppet/parser/script_compiler.rb

Overview

A Script “compiler” that does not support catalog operations

The Script compiler is “one shot” - it does not support rechecking if underlying source has changed or deal with possible errors in a cached environment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AbstractCompiler

#catalog

Constructor Details

#initialize(environment, node_name, for_agent = false) ⇒ ScriptCompiler

Create a script compiler for the given environment where errors are logged as coming from the given node_name



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

def initialize(environment, node_name, for_agent = false)
  @environment = environment
  @node_name = node_name

  # Create the initial scope, it is needed early
  @topscope = Puppet::Parser::Scope.new(self)

  # Initialize loaders and Pcore
  if for_agent
    @loaders = Puppet::Pops::Loaders.new(environment, true)
  else
    @loaders = Puppet::Pops::Loaders.new(environment)
  end

  # Need to compute overrides here, and remember them, because we are about to
  # Expensive entries in the context are bound lazily.
  @context_overrides = context_overrides()

  # Resolutions of fully qualified variable names
  @qualified_variables = {}
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.



28
29
30
# File 'lib/puppet/parser/script_compiler.rb', line 28

def environment
  @environment
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



25
26
27
# File 'lib/puppet/parser/script_compiler.rb', line 25

def loaders
  @loaders
end

#node_nameObject (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.



31
32
33
# File 'lib/puppet/parser/script_compiler.rb', line 31

def node_name
  @node_name
end

#qualified_variablesObject (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.



20
21
22
# File 'lib/puppet/parser/script_compiler.rb', line 20

def qualified_variables
  @qualified_variables
end

#topscopeObject (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.



17
18
19
# File 'lib/puppet/parser/script_compiler.rb', line 17

def topscope
  @topscope
end

Instance Method Details

#compileObject

Evaluates the configured setup for a script + code in an environment with modules



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/parser/script_compiler.rb', line 39

def compile
  Puppet[:strict_variables] = true
  Puppet[:strict] = :error

  # TRANSLATORS, "For running script" is not user facing
  Puppet.override(@context_overrides, "For running script") do
    # TRANSLATORS "main" is a function name and should not be translated
    result = Puppet::Util::Profiler.profile(_("Script: Evaluated main"), [:script, :evaluate_main]) { evaluate_main }
    if block_given?
      yield self
    else
      result
    end
  end
rescue Puppet::ParseErrorWithIssue => detail
  detail.node = node_name
  Puppet.log_exception(detail)
  raise
rescue => detail
  message = "#{detail} on node #{node_name}"
  Puppet.log_exception(detail, message)
  raise Puppet::Error, message, detail.backtrace
end

#context_overridesObject

Constructs the overrides for the context



64
65
66
67
68
69
70
71
# File 'lib/puppet/parser/script_compiler.rb', line 64

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

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

Having multiple named scopes hanging from top scope is not supported when scripting in the regular compiler this is used to create one named scope per class. When scripting, the “main class” is just a container of the top level code to evaluate and it is not evaluated as a class added to a catalog. Since classes are not supported there is no need to support the concept of “named scopes” as all variables are local

or in the top scope itself (notably, the $settings

namespace is initialized

as just a set of variables in that namespace - there is no named scope for ‘settings’ when scripting.

Keeping this method here to get specific error as being unsure if there are functions/logic that will call this. The AbstractCompiler defines this method, but maybe it does not have to (TODO).



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

def newscope(parent, options = {})
  raise _('having multiple named scopes is not supported when scripting')
end

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



33
34
35
# File 'lib/puppet/parser/script_compiler.rb', line 33

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