Class: Puppet::Parser::EnvironmentCompiler

Inherits:
Compiler show all
Defined in:
lib/puppet/parser/environment_compiler.rb

Constant Summary

Constants included from Puppet::Pops::Evaluator::Runtime3Support

Puppet::Pops::Evaluator::Runtime3Support::NAME_SPACE_SEPARATOR

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

Attributes inherited from Compiler

#catalog, #code_id, #collections, #facts, #loaders, #node, #qualified_variables, #relationships, #topscope

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Compiler

#add_catalog_validator, #add_class, #add_override, #assert_app_in_site, #context_overrides, #environment, #evaluate_node_classes, #evaluate_relationships, #evaluate_site, #newscope, #prune_node_catalog, #resource_overrides, #validate_catalog, #with_context_overrides

Methods included from Puppet::Pops::Evaluator::Runtime3Support

#add_relationship, #call_function, #capitalize_qualified_name, #coerce_numeric, #convert, #create_local_scope_from, #create_match_scope_from, #create_resource_defaults, #create_resource_overrides, #create_resource_parameter, #create_resources, #diagnostic_producer, #external_call_function, #extract_file_line, #fail, #find_resource, #get_resource_parameter_value, #get_scope_nesting_level, #get_variable_value, #is_boolean?, #is_parameter_of_resource?, #is_true?, #optionally_fail, #resource_to_ptype, #runtime_issue, #set_match_data, #set_scope_nesting_level, #set_variable, #variable_bound?, #variable_exists?

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?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, 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 = {}) ⇒ EnvironmentCompiler

Returns a new instance of EnvironmentCompiler.



18
19
20
21
# File 'lib/puppet/parser/environment_compiler.rb', line 18

def initialize(node, options = {})
  super
  @overridden_functions = {}
end

Class Method Details

.compile(env, code_id = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/puppet/parser/environment_compiler.rb', line 4

def self.compile(env, code_id=nil)
  begin
    env.check_for_reparse

    node = Puppet::Node.new(env)
    node.environment = env
    new(node, :code_id => code_id).compile
  rescue => detail
    message = _("%{detail} in environment %{env}") % { detail: detail, env: env.name }
    Puppet.log_exception(detail, message)
    raise Puppet::Error, message, detail.backtrace
  end
end

Instance Method Details

#add_catalog_validatorsObject



50
51
52
53
54
# File 'lib/puppet/parser/environment_compiler.rb', line 50

def add_catalog_validators
  super
  add_catalog_validator(CatalogValidator::SiteValidator)
  add_catalog_validator(CatalogValidator::EnvironmentRelationshipValidator)
end

#add_function_override(func_name, override) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/parser/environment_compiler.rb', line 27

def add_function_override(func_name, override)
  typed_name = Puppet::Pops::Loader::TypedName.new(:function, func_name)
  loader = loaders.puppet_system_loader

  # Remove and preserve existing entry. A `nil` is also preserved to indicate
  # an override that didn't replace a loaded function.
  entry = loader.get_entry(typed_name)
  existing = entry.nil? ? nil : entry.value
  loader.remove_entry(typed_name) unless existing.nil?
  @overridden_functions[typed_name] = existing

  # Add the override to the loader
  loader.set_entry(typed_name, override)
end

#add_function_overridesObject



23
24
25
# File 'lib/puppet/parser/environment_compiler.rb', line 23

def add_function_overrides
  add_function_override('hiera_include', proc { Puppet.debug "Ignoring hiera_include() during environment catalog compilation" })
end

#add_resource(scope, resource) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/puppet/parser/environment_compiler.rb', line 129

def add_resource(scope, resource)
  @resources << resource
  @catalog.add_resource(resource)

  if !resource.class? && resource[:stage]
    raise ArgumentError, _("Only classes can set 'stage'; normal resources like %{resource} cannot change run stage") % { resource: resource }
  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?
    @catalog.add_edge(scope.resource, resource)
  end
  resource.mark_unevaluated_consumer if is_capability_consumer?(resource)
  assert_app_in_site(scope, resource)
end

#compileObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/parser/environment_compiler.rb', line 56

def compile
  add_function_overrides
  begin
    Puppet.override(@context_overrides, _("For compiling environment catalog %{env}") % { env: environment.name }) do
      @catalog.environment_instance = environment

      Puppet::Util::Profiler.profile(_("Env Compile: Created settings scope"), [:compiler, :create_settings_scope]) { create_settings_scope }

      Puppet::Util::Profiler.profile(_("Env Compile: Evaluated main"), [:compiler, :evaluate_main]) { evaluate_main }

      Puppet::Util::Profiler.profile(_("Env Compile: Evaluated site"), [:compiler, :evaluate_site]) { evaluate_site }

      Puppet::Util::Profiler.profile(_("Env Compile: Evaluated application instances"), [:compiler, :evaluate_applications]) { evaluate_applications }

      Puppet::Util::Profiler.profile(_("Env Compile: Prune"), [:compiler, :prune_catalog]) { prune_catalog }

      Puppet::Util::Profiler.profile(_("Env Compile: Validate Catalog pre-finish"), [:compiler, :validate_pre_finish]) do
        validate_catalog(CatalogValidator::PRE_FINISH)
      end

      Puppet::Util::Profiler.profile(_("Env Compile: Finished catalog"), [:compiler, :finish_catalog]) { finish }

      fail_on_unevaluated

      Puppet::Util::Profiler.profile(_("Env Compile: Validate Catalog final"), [:compiler, :validate_final]) do
        validate_catalog(CatalogValidator::FINAL)
      end

      if block_given?
        yield @catalog
      else
        @catalog
      end
    end
  ensure
    remove_function_overrides
  end
end

#evaluate_applicationsObject



159
160
161
162
163
164
165
166
167
# File 'lib/puppet/parser/environment_compiler.rb', line 159

def evaluate_applications
  exceptwrap do
    resources.select { |resource| type = resource.resource_type; type.is_a?(Puppet::Resource::Type) && type.application? }.each do |resource|
      Puppet::Util::Profiler.profile(_("Evaluated application %{resource}") % { resource: resource }, [:compiler, :evaluate_resource, resource]) do
        resource.evaluate
      end
    end
  end
end

#evaluate_ast_nodeObject



151
152
153
# File 'lib/puppet/parser/environment_compiler.rb', line 151

def evaluate_ast_node()
  # Do nothing, the environment catalog is not built for a particular node.
end

#evaluate_classes(titles, scope, lazy) ⇒ Object



169
170
171
172
# File 'lib/puppet/parser/environment_compiler.rb', line 169

def evaluate_classes(titles, scope, lazy)
  # Always lazy in an Environment compilation
  super(titles, scope, true)
end

#on_empty_siteObject



155
156
157
# File 'lib/puppet/parser/environment_compiler.rb', line 155

def on_empty_site
  Puppet.warning(_("Environment Compiler: Could not find a site definition to evaluate"))
end

#prune_catalogObject

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.



96
97
98
# File 'lib/puppet/parser/environment_compiler.rb', line 96

def prune_catalog
  prune_env_catalog
end

#prune_env_catalogObject

Prunes the catalog by dropping all resources that are not contained under the Site (if a site expression is used). As a consequence all edges to/from dropped resources are also dropped. Once the pruning is performed, this compiler returns the pruned list when calling the #resources method. The pruning does not alter the order of resources in the resources list.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/puppet/parser/environment_compiler.rb', line 105

def prune_env_catalog
  # Everything under Class[main], that is not under (inclusive of) Site[site] should be pruned as those resources
  # are intended for nodes in a node catalog.
  #
  the_main_class_resource = @catalog.resource('Class', '')
  the_site_resource = @catalog.resource('Site', 'site')

  # Get downstream vertexes returns a hash where the keys are the resources and values nesting level
  rooted_in_main = @catalog.downstream_from_vertex(the_main_class_resource).keys

  to_be_removed =
  if the_site_resource
    keep_from_site = @catalog.downstream_from_vertex(the_site_resource).keys
    keep_from_site << the_site_resource
    rooted_in_main - keep_from_site
  else
    rooted_in_main
  end

  @catalog.remove_resource(*to_be_removed)
  # The compiler keeps a list of added resources, this shadows that list with the now pruned result
  @pruned_resources = @catalog.resources
end

#remove_function_overridesObject



42
43
44
45
46
47
48
# File 'lib/puppet/parser/environment_compiler.rb', line 42

def remove_function_overrides
  loader = loaders.puppet_system_loader
  @overridden_functions.each_pair do |typed_name, overridden|
    loader.remove_entry(typed_name)
    loader.set_entry(typed_name, overridden) unless overridden.nil?
  end
end

#resourcesObject

Overrides the regular compiler to be able to return the list of resources after a prune has taken place in the graph representation. Before a prune, the list is the same as in the regular compiler



178
179
180
# File 'lib/puppet/parser/environment_compiler.rb', line 178

def resources
  @pruned_resources || super
end