Class: Puppet::Parser::Compiler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Resource::TypeCollectionHelper, Util, Util::Errors, Util::MethodHelper
Defined in:
lib/puppet/parser/compiler.rb

Overview

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

Constant Summary

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource::TypeCollectionHelper

#known_resource_types

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, deterministic_rand, execfail, execpipe, execute, exit_on_fail, logmethods, memory, path_to_uri, pretty_backtrace, proxy, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Constructor Details

#initialize(node, options = {}) ⇒ Compiler

Returns a new instance of Compiler.



236
237
238
239
240
# File 'lib/puppet/parser/compiler.rb', line 236

def initialize(node, options = {})
  @node = node
  set_options(options)
  initvars
end

Instance Attribute Details

#boot_injectorPuppet::Pops::Binder::Injector?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The injector that provides lookup services during the creation of the #injector.



51
52
53
# File 'lib/puppet/parser/compiler.rb', line 51

def boot_injector
  @boot_injector
end

#catalogObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def catalog
  @catalog
end

#collectionsObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def collections
  @collections
end

#factsObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def facts
  @facts
end

#injectorPuppet::Pops::Binder::Injector?

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.



38
39
40
# File 'lib/puppet/parser/compiler.rb', line 38

def injector
  @injector
end

#loadersPuppet::Pops::Loader::Loaders (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Access to the configured loaders for 4x



43
44
45
# File 'lib/puppet/parser/compiler.rb', line 43

def loaders
  @loaders
end

#nodeObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def node
  @node
end

#relationshipsObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def relationships
  @relationships
end

#resourcesObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def resources
  @resources
end

#topscopeObject (readonly)



30
31
32
# File 'lib/puppet/parser/compiler.rb', line 30

def topscope
  @topscope
end

Class Method Details

.compile(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet/parser/compiler.rb', line 19

def self.compile(node)
   $env_module_directories = nil
   node.environment.check_for_reparse

   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

Instance Method Details

#add_class(name) ⇒ Object

Store the fact that we’ve evaluated a class



95
96
97
# File 'lib/puppet/parser/compiler.rb', line 95

def add_class(name)
  @catalog.add_class(name) unless name == ""
end

#add_override(override) ⇒ Object

Store a resource override.



58
59
60
61
62
63
64
65
66
67
# File 'lib/puppet/parser/compiler.rb', line 58

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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/parser/compiler.rb', line 69

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

#compileObject

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/parser/compiler.rb', line 105

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") { set_node_parameters }

    Puppet::Util::Profiler.profile("Compile: Created 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") { injector }
    end

    Puppet::Util::Profiler.profile("Compile: Evaluated main") { evaluate_main }

    Puppet::Util::Profiler.profile("Compile: Evaluated AST node") { evaluate_ast_node }

    Puppet::Util::Profiler.profile("Compile: Evaluated node classes") { evaluate_node_classes }

    Puppet::Util::Profiler.profile("Compile: Evaluated generators") { evaluate_generators }

    Puppet::Util::Profiler.profile("Compile: Finished catalog") { finish }

    fail_on_unevaluated

    @catalog
  end
end

#context_overridesObject

Constructs the overrides for the context



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet/parser/compiler.rb', line 137

def context_overrides()
  if Puppet[:parser] == 'future'
    require 'puppet/loaders'
    {
      :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) ⇒ Puppet::Pops::Binder::Injector

Creates the boot injector from registered system, default, and injector config.



274
275
276
277
278
279
280
281
# File 'lib/puppet/parser/compiler.rb', line 274

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

#environmentObject

Return the node’s environment.



156
157
158
159
160
161
# File 'lib/puppet/parser/compiler.rb', line 156

def environment
  unless node.environment.is_a? Puppet::Node::Environment
    raise Puppet::DevError, "node #{node} has an invalid environment!"
  end
  node.environment
end

#evaluate_classes(classes, scope, lazy_evaluate = true, fqname = false) ⇒ Object

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

Raises:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/puppet/parser/compiler.rb', line 198

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_classesObject

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.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/puppet/parser/compiler.rb', line 168

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_without_params, @node_scope || topscope)

  evaluate_classes(classes_with_params, @node_scope || topscope)
end

#evaluate_relationshipsObject



229
230
231
# File 'lib/puppet/parser/compiler.rb', line 229

def evaluate_relationships
  @relationships.each { |rel| rel.evaluate(catalog) }
end

#is_binder_active?Boolean

Answers if Puppet Binder should be active or not, and if it should and is not active, then it is activated.



285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/puppet/parser/compiler.rb', line 285

def is_binder_active?
  should_be_active = Puppet[:binder] || Puppet[:parser] == 'future'
  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 = {}) ⇒ Object

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



244
245
246
247
248
249
# File 'lib/puppet/parser/compiler.rb', line 244

def newscope(parent, options = {})
  parent ||= topscope
  scope = Puppet::Parser::Scope.new(self, options)
  scope.parent = parent
  scope
end

#resource_overrides(resource) ⇒ Object

Return any overrides for the given resource.



252
253
254
# File 'lib/puppet/parser/compiler.rb', line 252

def resource_overrides(resource)
  @resource_overrides[resource.ref]
end