class Puppet::Parser::Compiler

Maintain a graph of scopes, along with a bunch of data about the individual catalog we’re compiling.

Attributes

boot_injector[RW]

The injector that provides lookup services during the creation of the {injector}. @return [Puppet::Pops::Binder::Injector, nil] The injector that provides lookup services during injector creation

for this compiler/environment

@api private

catalog[R]
collections[R]
facts[R]
injector[RW]

The injector that provides lookup services, or nil if accessed before the compiler has started compiling and bootstrapped. The injector is initialized and available before any manifests are evaluated.

@return [Puppet::Pops::Binder::Injector, nil] The injector that provides lookup services for this compiler/environment @api public

loaders[R]

Access to the configured loaders for 4x @return [Puppet::Pops::Loader::Loaders] the configured loaders @api private

node[R]
relationships[R]
resources[R]
topscope[R]

Public Instance Methods

add_class(name) click to toggle source

Store the fact that we’ve evaluated a class

# File lib/puppet/parser/compiler.rb, line 110
def add_class(name)
  @catalog.add_class(name) unless name == ""
end
add_override(override) click to toggle source

Store a resource override.

# File lib/puppet/parser/compiler.rb, line 73
def add_override(override)
  # If possible, merge the override in immediately.
  if resource = @catalog.resource(override.ref)
    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) click to toggle source
# File lib/puppet/parser/compiler.rb, line 84
def add_resource(scope, resource)
  @resources << resource

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

  if not resource.class? and resource[:stage]
    raise ArgumentError, "Only classes can set 'stage'; normal resources like #{resource} cannot change run stage"
  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?
    return @catalog.add_edge(scope.resource, resource)
  end
end
compile() { |catalog| ... } click to toggle source

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

# File lib/puppet/parser/compiler.rb, line 120
def compile
  Puppet.override( @context_overrides , "For compiling #{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 }

    if is_binder_active?
      # create injector, if not already created - this is for 3x that does not trigger
      # lazy loading of injector via context
      Puppet::Util::Profiler.profile("Compile: Created injector", [:compiler, :create_injector]) { injector }
    end

    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: Finished catalog", [:compiler, :finish_catalog]) { finish }

    fail_on_unevaluated

    if block_given?
      yield @catalog
    else
      @catalog
    end
  end
end
context_overrides() click to toggle source

Constructs the overrides for the context

# File lib/puppet/parser/compiler.rb, line 156
def context_overrides()
  if Puppet.future_parser?(environment)
    {
      :current_environment => environment,
      :global_scope => @topscope,             # 4x placeholder for new global scope
      :loaders  => lambda {|| loaders() },    # 4x loaders
      :injector => lambda {|| injector() }    # 4x API - via context instead of via compiler
    }
  else
    {
      :current_environment => environment,
    }
  end
end
create_boot_injector(env_boot_bindings) click to toggle source

Creates the boot injector from registered system, default, and injector config. @return [Puppet::Pops::Binder::Injector] the created boot injector @api private Cannot be ‘private’ since it is called from the BindingsComposer.

# File lib/puppet/parser/compiler.rb, line 288
def create_boot_injector(env_boot_bindings)
  assert_binder_active()
  pb = Puppet::Pops::Binder
  boot_contribution = pb::SystemBindings.injector_boot_contribution(env_boot_bindings)
  final_contribution = pb::SystemBindings.final_contribution
  binder = pb::Binder.new(pb::BindingsFactory.layered_bindings(final_contribution, boot_contribution))
  @boot_injector = pb::Injector.new(binder)
end
environment() click to toggle source

Return the node’s environment.

# File lib/puppet/parser/compiler.rb, line 174
def environment
  node.environment
end
evaluate_classes(classes, scope, lazy_evaluate = true, fqname = false) click to toggle source

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

Sometimes we evaluate classes with a fully qualified name already, in which case, we tell scope.find_hostclass we’ve pre-qualified the name so it doesn’t need to search its namespaces again. This gets around a weird edge case of duplicate class names, one at top scope and one nested in our namespace and the wrong one (or both!) getting selected. See ticket #13349 for more detail. –jeffweiss 26 apr 2012

# File lib/puppet/parser/compiler.rb, line 212
def evaluate_classes(classes, scope, lazy_evaluate = true, fqname = false)
  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.class == Hash
    class_parameters = classes
    classes = classes.keys
  end

  hostclasses = classes.collect do |name|
    scope.find_hostclass(name, :assume_fqname => fqname) or raise Puppet::Error, "Could not find class #{name} for #{node.name}"
  end

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

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

    already_included + newly_included
  end
end
evaluate_node_classes() click to toggle source

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.

# File lib/puppet/parser/compiler.rb, line 183
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 = Hash[classes_with_params]
    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_relationships() click to toggle source
# File lib/puppet/parser/compiler.rb, line 243
def evaluate_relationships
  @relationships.each { |rel| rel.evaluate(catalog) }
end
is_binder_active?() click to toggle source

Answers if Puppet Binder should be active or not, and if it should and is not active, then it is activated. @return [Boolean] true if the Puppet Binder should be activated

# File lib/puppet/parser/compiler.rb, line 299
def is_binder_active?
  should_be_active = Puppet[:binder] || Puppet.future_parser?
  if should_be_active
    # TODO: this should be in a central place, not just for ParserFactory anymore...
    Puppet::Parser::ParserFactory.assert_rgen_installed()
    @@binder_loaded ||= false
    unless @@binder_loaded
      require 'puppet/pops'
      require 'puppetx'
      @@binder_loaded = true
    end
  end
  should_be_active
end
newscope(parent, options = {}) click to toggle source

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

# File lib/puppet/parser/compiler.rb, line 258
def newscope(parent, options = {})
  parent ||= topscope
  scope = Puppet::Parser::Scope.new(self, options)
  scope.parent = parent
  scope
end
resource_overrides(resource) click to toggle source

Return any overrides for the given resource.

# File lib/puppet/parser/compiler.rb, line 266
def resource_overrides(resource)
  @resource_overrides[resource.ref]
end

Public Class Methods

compile(node) click to toggle source
# File lib/puppet/parser/compiler.rb, line 19
 def self.compile(node)
   $env_module_directories = nil
   node.environment.check_for_reparse

   if node.environment.conflicting_manifest_settings?
     errmsg = [
       "The 'disable_per_environment_manifest' setting is true, and this '#{node.environment}'",
       "has an environment.conf manifest that conflicts with the 'default_manifest' setting.",
       "Compilation has been halted in order to avoid running a catalog which may be using",
       "unexpected manifests. For more information, see",
       "http://docs.puppetlabs.com/puppet/latest/reference/environments.html",
     ]
     raise(Puppet::Error, errmsg.join(' '))
   end

   new(node).compile {|resulting_catalog| resulting_catalog.to_resource }
 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
new(node, options = {}) click to toggle source
# File lib/puppet/parser/compiler.rb, line 250
def initialize(node, options = {})
  @node = node
  set_options(options)
  initvars
end