class Puppet::Parser::ScriptCompiler
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.
Attributes
@api private
Access to the configured loaders for 4x @return [Puppet::Pops::Loader::Loaders] the configured loaders @api private
@api private
@api private
@api private
Public Class Methods
Create a script compiler for the given environment where errors are logged as coming from the given node_name
# File lib/puppet/parser/script_compiler.rb 76 def initialize(environment, node_name, for_agent=false) 77 @environment = environment 78 @node_name = node_name 79 80 # Create the initial scope, it is needed early 81 @topscope = Puppet::Parser::Scope.new(self) 82 83 # Initialize loaders and Pcore 84 if for_agent 85 @loaders = Puppet::Pops::Loaders.new(environment, true) 86 else 87 @loaders = Puppet::Pops::Loaders.new(environment) 88 end 89 90 # Need to compute overrides here, and remember them, because we are about to 91 # Expensive entries in the context are bound lazily. 92 @context_overrides = context_overrides() 93 94 # Resolutions of fully qualified variable names 95 @qualified_variables = {} 96 end
Public Instance Methods
Evaluates the configured setup for a script + code in an environment with modules
# File lib/puppet/parser/script_compiler.rb 37 def compile 38 Puppet[:strict_variables] = true 39 Puppet[:strict] = :error 40 41 # TRANSLATORS, "For running script" is not user facing 42 Puppet.override( @context_overrides , "For running script") do 43 44 #TRANSLATORS "main" is a function name and should not be translated 45 result = Puppet::Util::Profiler.profile(_("Script: Evaluated main"), [:script, :evaluate_main]) { evaluate_main } 46 if block_given? 47 yield self 48 else 49 result 50 end 51 end 52 53 rescue Puppet::ParseErrorWithIssue => detail 54 detail.node = node_name 55 Puppet.log_exception(detail) 56 raise 57 rescue => detail 58 message = "#{detail} on node #{node_name}" 59 Puppet.log_exception(detail, message) 60 raise Puppet::Error, message, detail.backtrace 61 end
Constructs the overrides for the context
# File lib/puppet/parser/script_compiler.rb 64 def context_overrides() 65 { 66 :current_environment => environment, 67 :global_scope => @topscope, # 4x placeholder for new global scope 68 :loaders => @loaders, # 4x loaders 69 :rich_data => true, 70 } 71 end
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).
# File lib/puppet/parser/script_compiler.rb 111 def newscope(parent, options = {}) 112 raise _('having multiple named scopes is not supported when scripting') 113 end
# File lib/puppet/parser/script_compiler.rb 31 def with_context_overrides(description = '', &block) 32 Puppet.override( @context_overrides , description, &block) 33 end
Private Instance Methods
Find and evaluate the top level code.
# File lib/puppet/parser/script_compiler.rb 118 def evaluate_main 119 @loaders.pre_load 120 program = @loaders.load_main_manifest 121 return program.nil? ? nil : Puppet::Pops::Parser::EvaluatingParser.singleton.evaluator.evaluate(program, @topscope) 122 end