class Puppet::Parser::Compiler

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

Attributes

catalog[R]
collections[R]
facts[R]
node[R]
relationships[R]
resources[R]
topscope[R]

Public Class Methods

compile(node) click to toggle source
# File lib/puppet/parser/compiler.rb, line 19
 def self.compile(node)
   # We get these from the environment and only cache them in a thread
   # variable for the duration of the compilation.  If nothing else is using
   # the thread, though, we can leave 'em hanging round with no ill effects,
   # and this is safer than cleaning them at the end and assuming that will
   # stick until the next entry to this function.
   Thread.current[:known_resource_types] = nil
   Thread.current[:env_module_directories] = nil

   # ...and we actually do the compile now we have caching ready.
   new(node).compile.to_resource
 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 179
def initialize(node, options = {})
  @node = node
  set_options(options)
  initvars
end

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 81
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 43
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

Store a resource in our resource table.

# File lib/puppet/parser/compiler.rb, line 55
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() 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 91
def compile
  # Set the client's parameters into the top scope.
  set_node_parameters
  create_settings_scope

  evaluate_main

  evaluate_ast_node

  evaluate_node_classes

  evaluate_generators

  finish

  fail_on_unevaluated

  @catalog
end
environment() click to toggle source

Return the node’s environment.

# File lib/puppet/parser/compiler.rb, line 114
def environment
  unless defined?(@environment)
    unless node.environment.is_a? Puppet::Node::Environment
      raise Puppet::DevError, "node #{node} has an invalid environment!"
    end
    @environment = node.environment
  end
  Puppet::Node::Environment.current = @environment
  @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 it’s 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 141
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
  classes.each do |name|
    # If we can find the class, then make a resource that will evaluate it.
    if klass = scope.find_hostclass(name, :assume_fqname => fqname)

      # If parameters are passed, then attempt to create a duplicate resource
      # so the appropriate error is thrown.
      if class_parameters
        resource = klass.ensure_in_catalog(scope, class_parameters[name] || {})
      else
        next if scope.class_scope(klass)
        resource = klass.ensure_in_catalog(scope)
      end

      # If they've disabled lazy evaluation (which the :include function does),
      # then evaluate our resource immediately.
      resource.evaluate unless lazy_evaluate
    else
      raise Puppet::Error, "Could not find class #{name} for #{node.name}"
    end
  end
end
evaluate_node_classes() click to toggle source

Evaluate all of the classes specified by the node.

# File lib/puppet/parser/compiler.rb, line 126
def evaluate_node_classes
  evaluate_classes(@node.classes, @node_scope || topscope)
end
evaluate_relationships() click to toggle source
# File lib/puppet/parser/compiler.rb, line 172
def evaluate_relationships
  @relationships.each { |rel| rel.evaluate(catalog) }
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 187
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 195
def resource_overrides(resource)
  @resource_overrides[resource.ref]
end