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