Method: Puppet::Application::Lookup#generate_scope

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

#generate_scopeObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/puppet/application/lookup.rb', line 341

def generate_scope
  if options[:node]
    node = options[:node]
  else
    node = Puppet[:node_name_value]

    # If we want to lookup the node we are currently on
    # we must returning these settings to their default values
    Puppet.settings[:facts_terminus] = 'facter'
  end

  fact_file = options[:fact_file]

  if fact_file
    if fact_file.end_with?('.json')
      given_facts = Puppet::Util::Json.load_file(fact_file)
    elsif fact_file.end_with?('.yml', '.yaml')
      given_facts = Puppet::Util::Yaml.safe_load_file(fact_file)
    else
      given_facts = Puppet::Util::Json.load_file_if_valid(fact_file)
      given_facts = Puppet::Util::Yaml.safe_load_file_if_valid(fact_file) unless given_facts
    end

    unless given_facts.instance_of?(Hash)
      raise _("Incorrectly formatted data in %{fact_file} given via the --facts flag (only accepts yaml and json files)") % { fact_file: fact_file }
    end

    if TRUSTED_INFORMATION_FACTS.any? { |key| given_facts.key? key }
      unless TRUSTED_INFORMATION_FACTS.all? { |key| given_facts.key? key }
        raise _("When overriding any of the %{trusted_facts_list} facts with %{fact_file} "\
          "given via the --facts flag, they must all be overridden.") % { fact_file: fact_file ,trusted_facts_list: TRUSTED_INFORMATION_FACTS.join(',')}
      end
    end
  end

  unless node.is_a?(Puppet::Node) # to allow unit tests to pass a node instance
    facts = retrieve_node_facts(node, given_facts)
    ni = Puppet::Node.indirection
    tc = ni.terminus_class
    if options[:compile]
      if tc == :plain
        node = ni.find(node, facts: facts, environment: Puppet[:environment])
      else
        begin
          service = Puppet.runtime[:http]
          session = service.create_session
          cert = session.route_to(:ca)

          _, x509 = cert.get_certificate(node)
          cert = OpenSSL::X509::Certificate.new(x509)
          Puppet::SSL::Oids.register_puppet_oids
          trusted = Puppet::Context::TrustedInformation.remote(true, facts.values['certname'] || node, Puppet::SSL::Certificate.from_instance(cert))
          Puppet.override(trusted_information: trusted) do
            node = ni.find(node, facts: facts, environment: Puppet[:environment])
          end
        rescue
          Puppet.warning _("CA is not available, the operation will continue without using trusted facts.")
          node = ni.find(node, facts: facts, environment: Puppet[:environment])
        end
      end
    else
      ni.terminus_class = :plain
      node = ni.find(node, facts: facts, environment: Puppet[:environment])
      ni.terminus_class = tc
    end
  else
    node.add_extra_facts(given_facts) if given_facts
  end
  node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment)
  node.add_server_facts(Puppet::Node::ServerFacts.load)
  Puppet[:code] = 'undef' unless options[:compile]
  compiler = Puppet::Parser::Compiler.new(node)
  if options[:node]
    Puppet::Util.skip_external_facts do
      compiler.compile { |catalog| yield(compiler.topscope); catalog }
    end
  else
    compiler.compile { |catalog| yield(compiler.topscope); catalog }
  end
end