Method: Puppet::Application::Apply#main

Defined in:
lib/puppet/application/apply.rb

#mainObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/puppet/application/apply.rb', line 204

def main
  manifest          = get_manifest() # Get either a manifest or nil if apply should use content of Puppet[:code]
  splay                              # splay if needed
  facts             = get_facts()    # facts or nil
  node              = get_node()     # node or error
  apply_environment = get_configured_environment(node, manifest)

  # TRANSLATORS "puppet apply" is a program command and should not be translated
  Puppet.override({:current_environment => apply_environment, :loaders => create_loaders(apply_environment)}, _("For puppet apply")) do
    configure_node_facts(node, facts)

    # Allow users to load the classes that puppet agent creates.
    if options[:loadclasses]
      file = Puppet[:classfile]
      if Puppet::FileSystem.exist?(file)
        unless FileTest.readable?(file)
          $stderr.puts _("%{file} is not readable") % { file: file }
          exit(63)
        end
        node.classes = Puppet::FileSystem.read(file, :encoding => 'utf-8').split(/[\s]+/)
      end
    end

    begin
      # Compile the catalog
      starttime = Time.now

      # When compiling, the compiler traps and logs certain errors
      # Those that do not lead to an immediate exit are caught by the general
      # rule and gets logged.
      #
      catalog =
      begin
        Puppet::Resource::Catalog.indirection.find(node.name, :use_node => node)
      rescue Puppet::Error
        # already logged and handled by the compiler, including Puppet::ParseErrorWithIssue
        exit(1)
      end

      # Resolve all deferred values and replace them / mutate the catalog
      Puppet::Pops::Evaluator::DeferredResolver.resolve_and_replace(node.facts, catalog, apply_environment, Puppet[:preprocess_deferred])

      # Translate it to a RAL catalog
      catalog = catalog.to_ral

      catalog.finalize

      catalog.retrieval_duration = Time.now - starttime

      # We accept either the global option `--write_catalog_summary`
      # corresponding to the new setting, or the application option
      # `--write-catalog-summary`. The latter is needed to maintain backwards
      # compatibility.
      #
      # Puppet settings parse global options using PuppetOptionParser, but it
      # only recognizes underscores, not dashes.
      # The base application parses app specific options using ruby's builtin
      # OptionParser. As of ruby 2.4, it will accept either underscores or
      # dashes, but prefer dashes.
      #
      # So if underscores are used, the PuppetOptionParser will parse it and
      # store that in Puppet[:write_catalog_summary]. If dashes are used,
      # OptionParser will parse it, and set Puppet[:write_catalog_summary]. In
      # either case, settings will contain the correct value.
      if Puppet[:write_catalog_summary]
        catalog.write_class_file
        catalog.write_resource_file
      end

      exit_status = apply_catalog(catalog)

      if not exit_status
        exit(1)
      elsif options[:detailed_exitcodes] then
        exit(exit_status)
      else
        exit(0)
      end
    rescue => detail
      Puppet.log_exception(detail)
      exit(1)
    end
  end
end