File: fact_handler.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (50 lines) | stat: -rw-r--r-- 2,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require_relative '../../puppet/indirector/facts/facter'

require_relative '../../puppet/configurer'
require_relative '../../puppet/configurer/downloader'

# Break out the code related to facts.  This module is
# just included into the agent, but having it here makes it
# easier to test.
module Puppet::Configurer::FactHandler
  def find_facts
    # This works because puppet agent configures Facts to use 'facter' for
    # finding facts and the 'rest' terminus for caching them.  Thus, we'll
    # compile them and then "cache" them on the server.
    begin
      facts = Puppet::Node::Facts.indirection.find(Puppet[:node_name_value], :environment => Puppet::Node::Environment.remote(@environment))
      unless Puppet[:node_name_fact].empty?
        Puppet[:node_name_value] = facts.values[Puppet[:node_name_fact]]
        facts.name = Puppet[:node_name_value]
      end
      facts
    rescue SystemExit,NoMemoryError
      raise
    rescue Exception => detail
      message = _("Could not retrieve local facts: %{detail}") % { detail: detail }
      Puppet.log_exception(detail, message)
      raise Puppet::Error, message, detail.backtrace
    end
  end

  def facts_for_uploading
    encode_facts(find_facts)
  end

  def encode_facts(facts)
    #facts = find_facts

    # NOTE: :facts specified as parameters are URI encoded here,
    # then  encoded for a second time depending on their length:
    #
    # <= 1024 characters sent via query string of a HTTP GET, additionally query string encoded
    # > 1024 characters sent in POST data, additionally x-www-form-urlencoded
    # so it's only important that encoding method here return original values
    # correctly when CGI.unescape called against it (in compiler code)
    if Puppet[:preferred_serialization_format] == "pson"
      {:facts_format => :pson, :facts => Puppet::Util.uri_query_encode(facts.render(:pson)) }
    else
      {:facts_format => 'application/json', :facts => Puppet::Util.uri_query_encode(facts.render(:json)) }
    end
  end
end