module Puppet::Pops::Evaluator::Runtime3Support
A module with bindings between the new evaluator and the 3x runtime. The intention is to separate all calls into scope, compiler, resource, etc. in this module to make it easier to later refactor the evaluator for better implementations of the 3x classes.
@api private
Constants
- NAME_SPACE_SEPARATOR
Public Instance Methods
Adds a relationship between the given `source` and `target` of the given `relationship_type` @param source [Puppet:Pops::Types::PCatalogEntryType] the source end of the relationship (from) @param target [Puppet:Pops::Types::PCatalogEntryType] the target end of the relationship (to) @param relationship_type [:relationship, :subscription] the type of the relationship
# File lib/puppet/pops/evaluator/runtime3_support.rb 173 def add_relationship(source, target, relationship_type, scope) 174 # The 3x way is to record a Puppet::Parser::Relationship that is evaluated at the end of the compilation. 175 # This means it is not possible to detect any duplicates at this point (and signal where an attempt is made to 176 # add a duplicate. There is also no location information to signal the original place in the logic. The user will have 177 # to go fish. 178 # The 3.x implementation is based on Strings :-o, so the source and target must be transformed. The resolution is 179 # done by Catalog#resource(type, title). To do that, it creates a Puppet::Resource since it is responsible for 180 # translating the name/type/title and create index-keys used by the catalog. The Puppet::Resource has bizarre parsing of 181 # the type and title (scan for [] that is interpreted as type/title (but it gets it wrong). 182 # Moreover if the type is "" or "component", the type is Class, and if the type is :main, it is :main, all other cases 183 # undergo capitalization of name-segments (foo::bar becomes Foo::Bar). (This was earlier done in the reverse by the parser). 184 # Further, the title undergoes the same munging !!! 185 # 186 # That bug infested nest of messy logic needs serious Exorcism! 187 # 188 # Unfortunately it is not easy to simply call more intelligent methods at a lower level as the compiler evaluates the recorded 189 # Relationship object at a much later point, and it is responsible for invoking all the messy logic. 190 # 191 # TODO: Revisit the below logic when there is a sane implementation of the catalog, compiler and resource. For now 192 # concentrate on transforming the type references to what is expected by the wacky logic. 193 # 194 # HOWEVER, the Compiler only records the Relationships, and the only method it calls is @relationships.each{|x| x.evaluate(catalog) } 195 # Which means a smarter Relationship class could do this right. Instead of obtaining the resource from the catalog using 196 # the borked resource(type, title) which creates a resource for the purpose of looking it up, it needs to instead 197 # scan the catalog's resources 198 # 199 # GAAAH, it is even worse! 200 # It starts in the parser, which parses "File['foo']" into an AST::ResourceReference with type = File, and title = foo 201 # This AST is evaluated by looking up the type/title in the scope - causing it to be loaded if it exists, and if not, the given 202 # type name/title is used. It does not search for resource instances, only classes and types. It returns symbolic information 203 # [type, [title, title]]. From this, instances of Puppet::Resource are created and returned. These only have type/title information 204 # filled out. One or an array of resources are returned. 205 # This set of evaluated (empty reference) Resource instances are then passed to the relationship operator. It creates a 206 # Puppet::Parser::Relationship giving it a source and a target that are (empty reference) Resource instances. These are then remembered 207 # until the relationship is evaluated by the compiler (at the end). When evaluation takes place, the (empty reference) Resource instances 208 # are converted to String (!?! WTF) on the simple format "#{type}[#{title}]", and the catalog is told to find a resource, by giving 209 # it this string. If it cannot find the resource it fails, else the before/notify parameter is appended with the target. 210 # The search for the resource begin with (you guessed it) again creating an (empty reference) resource from type and title (WTF?!?!). 211 # The catalog now uses the reference resource to compute a key [r.type, r.title.to_s] and also gets a uniqueness key from the 212 # resource (This is only a reference type created from title and type). If it cannot find it with the first key, it uses the 213 # uniqueness key to lookup. 214 # 215 # This is probably done to allow a resource type to munge/translate the title in some way (but it is quite unclear from the long 216 # and convoluted path of evaluation. 217 # In order to do this in a way that is similar to 3.x two resources are created to be used as keys. 218 # 219 # And if that is not enough, a source/target may be a Collector (a baked query that will be evaluated by the 220 # compiler - it is simply passed through here for processing by the compiler at the right time). 221 # 222 if source.is_a?(Collectors::AbstractCollector) 223 # use verbatim - behavior defined by 3x 224 source_resource = source 225 else 226 # transform into the wonderful String representation in 3x 227 type, title = Runtime3Converter.instance.catalog_type_to_split_type_title(source) 228 type = Runtime3ResourceSupport.find_resource_type(scope, type) unless type == 'class' || type == 'node' 229 source_resource = Puppet::Resource.new(type, title) 230 end 231 if target.is_a?(Collectors::AbstractCollector) 232 # use verbatim - behavior defined by 3x 233 target_resource = target 234 else 235 # transform into the wonderful String representation in 3x 236 type, title = Runtime3Converter.instance.catalog_type_to_split_type_title(target) 237 type = Runtime3ResourceSupport.find_resource_type(scope, type) unless type == 'class' || type == 'node' 238 target_resource = Puppet::Resource.new(type, title) 239 end 240 # Add the relationship to the compiler for later evaluation. 241 scope.compiler.add_relationship(Puppet::Parser::Relationship.new(source_resource, target_resource, relationship_type)) 242 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 299 def call_function(name, args, o, scope, &block) 300 file, line = extract_file_line(o) 301 loader = Adapters::LoaderAdapter.loader_for_model_object(o, file) 302 func = loader.load(:function, name) if loader 303 if func 304 Puppet::Util::Profiler.profile(name, [:functions, name]) do 305 # Add stack frame when calling. 306 return Puppet::Pops::PuppetStack.stack(file || '', line, func, :call, [scope, *args], &block) 307 end 308 end 309 # Call via 3x API if function exists there without having been autoloaded 310 fail(Issues::UNKNOWN_FUNCTION, o, {:name => name}) unless Puppet::Parser::Functions.function(name) 311 312 # Arguments must be mapped since functions are unaware of the new and magical creatures in 4x. 313 # NOTE: Passing an empty string last converts nil/:undef to empty string 314 mapped_args = Runtime3FunctionArgumentConverter.map_args(args, scope, '') 315 # The 3x function performs return value mapping and returns nil if it is not of rvalue type 316 Puppet::Pops::PuppetStack.stack(file, line, scope, "function_#{name}", [mapped_args], &block) 317 end
Capitalizes each segment of a qualified name
# File lib/puppet/pops/evaluator/runtime3_support.rb 357 def capitalize_qualified_name(name) 358 name.split(/::/).map(&:capitalize).join(NAME_SPACE_SEPARATOR) 359 end
Coerce value `v` to numeric or fails. The given value `v` is coerced to Numeric, and if that fails the operation calls {#fail}. @param v [Object] the value to convert @param o [Object] originating instruction @param scope [Object] the (runtime specific) scope where evaluation of o takes place @return [Numeric] value `v` converted to Numeric.
# File lib/puppet/pops/evaluator/runtime3_support.rb 252 def coerce_numeric(v, o, scope) 253 if v.is_a?(Numeric) 254 return v 255 end 256 n = Utils.to_n(v) 257 unless n 258 fail(Issues::NOT_NUMERIC, o, {:value => v}) 259 end 260 # this point is reached if there was a conversion 261 optionally_fail(Issues::NUMERIC_COERCION, o, {:before => v, :after => n}) 262 n 263 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 330 def convert(value, scope, undef_value) 331 Runtime3Converter.instance.convert(value, scope, undef_value) 332 end
Creates a local scope with vairalbes set from a hash of variable name to value
# File lib/puppet/pops/evaluator/runtime3_support.rb 144 def create_local_scope_from(hash, scope) 145 # two dummy values are needed since the scope tries to give an error message (can not happen in this 146 # case - it is just wrong, the error should be reported by the caller who knows in more detail where it 147 # is in the source. 148 # 149 raise ArgumentError, _("Internal error - attempt to create a local scope without a hash") unless hash.is_a?(Hash) 150 scope.ephemeral_from(hash) 151 end
Creates a nested match scope
# File lib/puppet/pops/evaluator/runtime3_support.rb 154 def create_match_scope_from(scope) 155 # Create a transparent match scope (for future matches) 156 scope.new_match_scope(nil) 157 end
Defines default parameters for a type with the given name.
# File lib/puppet/pops/evaluator/runtime3_support.rb 346 def create_resource_defaults(o, scope, type_name, evaluated_parameters) 347 # Note that name must be capitalized in this 3x call 348 # The 3x impl creates a Resource instance with a bogus title and then asks the created resource 349 # for the type of the name. 350 # Note, locations are available per parameter. 351 # 352 scope.define_settings(capitalize_qualified_name(type_name), evaluated_parameters.flatten) 353 end
Creates resource overrides for all resource type objects in evaluated_resources. The same set of evaluated parameters are applied to all.
# File lib/puppet/pops/evaluator/runtime3_support.rb 364 def create_resource_overrides(o, scope, evaluated_resources, evaluated_parameters) 365 # Not 100% accurate as this is the resource expression location and each title is processed separately 366 # The titles are however the result of evaluation and they have no location at this point (an array 367 # of positions for the source expressions are required for this to work. 368 # TODO: Revisit and possible improve the accuracy. 369 # 370 file, line = extract_file_line(o) 371 # A *=> results in an array of arrays 372 evaluated_parameters = evaluated_parameters.flatten 373 evaluated_resources.each do |r| 374 unless r.is_a?(Types::PResourceType) && r.type_name != 'class' 375 fail(Issues::ILLEGAL_OVERRIDDEN_TYPE, o, {:actual => r} ) 376 end 377 t = Runtime3ResourceSupport.find_resource_type(scope, r.type_name) 378 resource = Puppet::Parser::Resource.new( 379 t, r.title, { 380 :parameters => evaluated_parameters, 381 :file => file, 382 :line => line, 383 # WTF is this? Which source is this? The file? The name of the context ? 384 :source => scope.source, 385 :scope => scope 386 }, false # defaults should not override 387 ) 388 389 scope.compiler.add_override(resource) 390 end 391 end
The o is used for source reference
# File lib/puppet/pops/evaluator/runtime3_support.rb 320 def create_resource_parameter(o, scope, name, value, operator) 321 file, line = extract_file_line(o) 322 Puppet::Parser::Resource::Param.new( 323 :name => name, 324 :value => convert(value, scope, nil), # converted to 3x since 4x supports additional objects / types 325 :source => scope.source, :line => line, :file => file, 326 :add => operator == '+>' 327 ) 328 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 334 def create_resources(o, scope, virtual, exported, type_name, resource_titles, evaluated_parameters) 335 # Not 100% accurate as this is the resource expression location and each title is processed separately 336 # The titles are however the result of evaluation and they have no location at this point (an array 337 # of positions for the source expressions are required for this to work). 338 # TODO: Revisit and possible improve the accuracy. 339 # 340 file, line = extract_file_line(o) 341 Runtime3ResourceSupport.create_resources(file, line, scope, virtual, exported, type_name, resource_titles, evaluated_parameters) 342 end
Creates a diagnostic producer
# File lib/puppet/pops/evaluator/runtime3_support.rb 474 def diagnostic_producer 475 Validation::DiagnosticProducer.new( 476 ExceptionRaisingAcceptor.new(), # Raises exception on all issues 477 SeverityProducer.new(), # All issues are errors 478 Model::ModelLabelProvider.new()) 479 end
Provides the ability to call a 3.x or 4.x function from the perspective of a 3.x function or ERB template. The arguments to the function must be an Array containing values compliant with the 4.x calling convention. If the targeted function is a 3.x function, the values will be transformed. @param name [String] the name of the function (without the 'function_' prefix used by scope) @param args [Array] arguments, may be empty @param scope [Object] the (runtime specific) scope where evaluation takes place @raise [ArgumentError] 'unknown function' if the function does not exist
# File lib/puppet/pops/evaluator/runtime3_support.rb 273 def external_call_function(name, args, scope, &block) 274 # Call via 4x API if the function exists there 275 loaders = scope.compiler.loaders 276 # Since this is a call from non puppet code, it is not possible to relate it to a module loader 277 # It is known where the call originates by using the scope associated module - but this is the calling scope 278 # and it does not defined the visibility of functions from a ruby function's perspective. Instead, 279 # this is done from the perspective of the environment. 280 loader = loaders.private_environment_loader 281 func = loader.load(:function, name) if loader 282 if func 283 Puppet::Util::Profiler.profile(name, [:functions, name]) do 284 return func.call(scope, *args, &block) 285 end 286 end 287 288 # Call via 3x API if function exists there 289 raise ArgumentError, _("Unknown function '%{name}'") % { name: name } unless Puppet::Parser::Functions.function(name) 290 291 # Arguments must be mapped since functions are unaware of the new and magical creatures in 4x. 292 # NOTE: Passing an empty string last converts nil/:undef to empty string 293 mapped_args = Runtime3FunctionArgumentConverter.map_args(args, scope, '') 294 result = scope.send("function_#{name}", mapped_args, &block) 295 # Prevent non r-value functions from leaking their result (they are not written to care about this) 296 Puppet::Parser::Functions.rvalue?(name) ? result : nil 297 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 469 def extract_file_line(o) 470 o.is_a?(Model::Positioned) ? [o.file, o.line] : [nil, -1] 471 end
Fails the evaluation of semantic with a given issue.
@param issue [Issue] the issue to report @param semantic [ModelPopsObject] the object for which evaluation failed in some way. Used to determine origin. @param options [Hash] hash of optional named data elements for the given issue @return [!] this method does not return @raise [Puppet::ParseError] an evaluation error initialized from the arguments (TODO: Change to EvaluationError?)
# File lib/puppet/pops/evaluator/runtime3_support.rb 21 def fail(issue, semantic, options={}, except=nil) 22 optionally_fail(issue, semantic, options, except) 23 # an error should have been raised since fail always fails 24 raise ArgumentError, _("Internal Error: Configuration of runtime error handling wrong: should have raised exception") 25 end
Finds a resource given a type and a title.
# File lib/puppet/pops/evaluator/runtime3_support.rb 395 def find_resource(scope, type_name, title) 396 scope.compiler.findresource(type_name, title) 397 end
Returns the value of a resource's parameter by first looking up the parameter in the resource and then in the defaults for the resource. Since the resource exists (it must in order to look up its parameters, any overrides have already been applied). Defaults are not applied to a resource until it has been finished (which typically has not taken place when this is evaluated; hence the dual lookup).
# File lib/puppet/pops/evaluator/runtime3_support.rb 404 def get_resource_parameter_value(scope, resource, parameter_name) 405 # This gets the parameter value, or nil (for both valid parameters and parameters that do not exist). 406 val = resource[parameter_name] 407 408 # Sometimes the resource is a Puppet::Parser::Resource and sometimes it is 409 # a Puppet::Resource. The Puppet::Resource case occurs when puppet language 410 # is evaluated against an already completed catalog (where all instances of 411 # Puppet::Parser::Resource are converted to Puppet::Resource instances). 412 # Evaluating against an already completed catalog is really only found in 413 # the language specification tests, where the puppet language is used to 414 # test itself. 415 if resource.is_a?(Puppet::Parser::Resource) 416 # The defaults must be looked up in the scope where the resource was created (not in the given 417 # scope where the lookup takes place. 418 resource_scope = resource.scope 419 defaults = resource_scope.lookupdefaults(resource.type) if val.nil? && resource_scope 420 if defaults 421 # NOTE: 3x resource keeps defaults as hash using symbol for name as key to Parameter which (again) holds 422 # name and value. 423 # NOTE: meta parameters that are unset ends up here, and there are no defaults for those encoded 424 # in the defaults, they may receive hardcoded defaults later (e.g. 'tag'). 425 param = defaults[parameter_name.to_sym] 426 # Some parameters (meta parameters like 'tag') does not return a param from which the value can be obtained 427 # at all times. Instead, they return a nil param until a value has been set. 428 val = param.nil? ? nil : param.value 429 end 430 end 431 val 432 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 159 def get_scope_nesting_level(scope) 160 scope.ephemeral_level 161 end
Returns the value of the variable (nil is returned if variable has no value, or if variable does not exist)
# File lib/puppet/pops/evaluator/runtime3_support.rb 98 def get_variable_value(name, o, scope) 99 # Puppet 3x stores all variables as strings (then converts them back to numeric with a regexp... to see if it is a match variable) 100 # Not ideal, scope should support numeric lookup directly instead. 101 # TODO: consider fixing scope 102 catch(:undefined_variable) { 103 x = scope.lookupvar(name.to_s) 104 # Must convert :undef back to nil - this can happen when an undefined variable is used in a 105 # parameter's default value expression - there nil must be :undef to work with the rest of 3x. 106 # Now that the value comes back to 4x it is changed to nil. 107 return :undef.equal?(x) ? nil : x 108 } 109 # It is always ok to reference numeric variables even if they are not assigned. They are always undef 110 # if not set by a match expression. 111 # 112 unless name =~ Puppet::Pops::Patterns::NUMERIC_VAR_NAME 113 optionally_fail(Puppet::Pops::Issues::UNKNOWN_VARIABLE, o, {:name => name}) 114 end 115 nil # in case unknown variable is configured as a warning or ignore 116 end
Utility method for TrueClass || FalseClass @param x [Object] the object to test if it is instance of TrueClass or FalseClass
# File lib/puppet/pops/evaluator/runtime3_support.rb 465 def is_boolean? x 466 x.is_a?(TrueClass) || x.is_a?(FalseClass) 467 end
Returns true, if the given name is the name of a resource parameter.
# File lib/puppet/pops/evaluator/runtime3_support.rb 436 def is_parameter_of_resource?(scope, resource, name) 437 return false unless name.is_a?(String) 438 resource.valid_parameter?(name) 439 end
This is the same type of “truth” as used in the current Puppet DSL.
# File lib/puppet/pops/evaluator/runtime3_support.rb 449 def is_true?(value, o) 450 # Is the value true? This allows us to control the definition of truth 451 # in one place. 452 case value 453 # Support :undef since it may come from a 3x structure 454 when :undef 455 false 456 when String 457 true 458 else 459 !!value 460 end 461 end
Optionally (based on severity) Fails the evaluation of semantic with a given issue If the given issue is configured to be of severity < :error it is only reported, and the function returns.
@param issue [Issue] the issue to report @param semantic [ModelPopsObject] the object for which evaluation failed in some way. Used to determine origin. @param options [Hash] hash of optional named data elements for the given issue @return [!] this method does not return @raise [Puppet::ParseError] an evaluation error initialized from the arguments (TODO: Change to EvaluationError?)
# File lib/puppet/pops/evaluator/runtime3_support.rb 36 def optionally_fail(issue, semantic, options={}, except=nil) 37 if except.nil? && diagnostic_producer.severity_producer[issue] == :error 38 # Want a stacktrace, and it must be passed as an exception 39 begin 40 raise EvaluationError.new() 41 rescue EvaluationError => e 42 except = e 43 end 44 end 45 diagnostic_producer.accept(issue, semantic, options, except) 46 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 441 def resource_to_ptype(resource) 442 nil if resource.nil? 443 # inference returns the meta type since the 3x Resource is an alternate way to describe a type 444 type_calculator.infer(resource).type 445 end
Optionally (based on severity) Fails the evaluation with a given issue If the given issue is configured to be of severity < :error it is only reported, and the function returns. The location the issue is reported against is found is based on the top file/line in the puppet call stack
@param issue [Issue] the issue to report @param options [Hash] hash of optional named data elements for the given issue @return [!] this method may not return, nil if it does @raise [Puppet::ParseError] an evaluation error initialized from the arguments
# File lib/puppet/pops/evaluator/runtime3_support.rb 57 def runtime_issue(issue, options={}) 58 # Get position from puppet runtime stack 59 file, line = Puppet::Pops::PuppetStack.top_of_stack 60 61 # Use a SemanticError as the sourcepos 62 semantic = Puppet::Pops::SemanticError.new(issue, nil, options.merge({:file => file, :line => line})) 63 optionally_fail(issue, semantic) 64 nil 65 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 131 def set_match_data(match_data, scope) 132 # See set_variable for rationale for not passing file and line to ephemeral_from. 133 # NOTE: The 3x scope adds one ephemeral(match) to its internal stack per match that succeeds ! It never 134 # clears anything. Thus a context that performs many matches will get very deep (there simply is no way to 135 # clear the match variables without rolling back the ephemeral stack.) 136 # This implementation does not attempt to fix this, it behaves the same bad way. 137 unless match_data.nil? 138 scope.ephemeral_from(match_data) 139 end 140 end
# File lib/puppet/pops/evaluator/runtime3_support.rb 163 def set_scope_nesting_level(scope, level) 164 # 3x uses this method to reset the level, 165 scope.pop_ephemerals(level) 166 end
Binds the given variable name to the given value in the given scope. The reference object `o` is intended to be used for origin information - the 3x scope implementation only makes use of location when there is an error. This is now handled by other mechanisms; first a check is made if a variable exists and an error is raised if attempting to change an immutable value. Errors in name, numeric variable assignment etc. have also been validated prior to this call. In the event the scope.setvar still raises an error, the general exception handling for evaluation of the assignment expression knows about its location. Because of this, there is no need to extract the location for each setting (extraction is somewhat expensive since 3x requires line instead of offset).
# File lib/puppet/pops/evaluator/runtime3_support.rb 76 def set_variable(name, value, o, scope) 77 # Scope also checks this but requires that location information are passed as options. 78 # Those are expensive to calculate and a test is instead made here to enable failing with better information. 79 # The error is not specific enough to allow catching it - need to check the actual message text. 80 # TODO: Improve the messy implementation in Scope. 81 # 82 if name == "server_facts" 83 fail(Issues::ILLEGAL_RESERVED_ASSIGNMENT, o, {:name => name} ) 84 end 85 86 if scope.bound?(name) 87 if Puppet::Parser::Scope::RESERVED_VARIABLE_NAMES.include?(name) 88 fail(Issues::ILLEGAL_RESERVED_ASSIGNMENT, o, {:name => name} ) 89 else 90 fail(Issues::ILLEGAL_REASSIGNMENT, o, {:name => name} ) 91 end 92 end 93 scope.setvar(name, value) 94 end
Returns true if the variable of the given name is set in the given most nested scope. True is returned even if variable is bound to nil.
# File lib/puppet/pops/evaluator/runtime3_support.rb 121 def variable_bound?(name, scope) 122 scope.bound?(name.to_s) 123 end
Returns true if the variable is bound to a value or nil, in the scope or it's parent scopes.
# File lib/puppet/pops/evaluator/runtime3_support.rb 127 def variable_exists?(name, scope) 128 scope.exist?(name.to_s) 129 end