Class: Puppet::Node::Facts::Facter

Inherits:
Indirector::Code show all
Defined in:
lib/puppet/indirector/facts/facter.rb

Constant Summary

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

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

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Instance Attribute Summary

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Indirector::Terminus

abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, #require_environment?, terminus_class, terminus_classes, #terminus_type, #validate, #validate_key, #validate_model

Methods included from Util::InstanceLoader

#instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

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

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Class Method Details

.setup_external_search_paths(request) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet/indirector/facts/facter.rb', line 84

def self.setup_external_search_paths(request)
  # Add any per-module external fact directories to facter's external search path
  dirs = []
  request.environment.modules.each do |m|
    next unless m.has_external_facts?

    dir = m.plugin_fact_directory
    Puppet.debug { "Loading external facts from #{dir}" }
    dirs << dir
  end

  # Add system external fact directory if it exists
  if FileTest.directory?(Puppet[:pluginfactdest])
    dir = Puppet[:pluginfactdest]
    Puppet.debug { "Loading external facts from #{dir}" }
    dirs << dir
  end

  dirs << request.options[:external_dir] if request.options[:external_dir]
  Puppet.runtime[:facter].search_external dirs
end

.setup_search_paths(request) ⇒ Object



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
# File 'lib/puppet/indirector/facts/facter.rb', line 57

def self.setup_search_paths(request)
  # Add any per-module fact directories to facter's search path
  dirs = request.environment.modulepath.collect do |dir|
    %w[lib plugins].map do |subdirectory|
      Dir.glob("#{dir}/*/#{subdirectory}/facter")
    end
  end.flatten + Puppet[:factpath].split(File::PATH_SEPARATOR)

  dirs = dirs.select do |dir|
    next false unless FileTest.directory?(dir)

    # Even through we no longer directly load facts in the terminus,
    # print out each .rb in the facts directory as module
    # developers may find that information useful for debugging purposes
    if Puppet::Util::Log.sendlevel?(:info)
      Puppet.info _("Loading facts")
      Dir.glob("#{dir}/*.rb").each do |file|
        Puppet.debug { "Loading facts from #{file}" }
      end
    end

    true
  end
  dirs << request.options[:custom_dir] if request.options[:custom_dir]
  Puppet.runtime[:facter].search(*dirs)
end

Instance Method Details

#allow_remote_requests?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/puppet/indirector/facts/facter.rb', line 11

def allow_remote_requests?
  false
end

#destroy(facts) ⇒ Object

Raises:



15
16
17
# File 'lib/puppet/indirector/facts/facter.rb', line 15

def destroy(facts)
  raise Puppet::DevError, _('You cannot destroy facts in the code store; it is only used for getting facts from Facter')
end

#find(request) ⇒ Object

Lookup a host’s facts up in Facter.



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
51
52
53
54
55
# File 'lib/puppet/indirector/facts/facter.rb', line 24

def find(request)
  Puppet.runtime[:facter].reset

  # Note: we need to setup puppet's external search paths before adding the puppetversion
  # fact. This is because in Facter 2.x, the first `Puppet.runtime[:facter].add` causes Facter to create
  # its directory loaders which cannot be changed, meaning other external facts won't
  # be resolved. (PUP-4607)
  self.class.setup_external_search_paths(request)
  self.class.setup_search_paths(request)

  # Initialize core Puppet facts, such as puppetversion
  Puppet.initialize_facts

  result = if request.options[:resolve_options]
             raise(Puppet::Error, _("puppet facts show requires version 4.0.40 or greater of Facter.")) unless Facter.respond_to?(:resolve)

             find_with_options(request)
           elsif Puppet[:include_legacy_facts]
             # to_hash returns both structured and legacy facts
             Puppet::Node::Facts.new(request.key, Puppet.runtime[:facter].to_hash)
           else
             # resolve does not return legacy facts unless requested
             facts = Puppet.runtime[:facter].resolve('')
             # some versions of Facter 4 return a Facter::FactCollection instead of
             # a Hash, breaking API compatibility, so force a hash using `to_h`
             Puppet::Node::Facts.new(request.key, facts.to_h)
           end

  result.add_local_facts unless request.options[:resolve_options]
  result.sanitize
  result
end

#save(facts) ⇒ Object

Raises:



19
20
21
# File 'lib/puppet/indirector/facts/facter.rb', line 19

def save(facts)
  raise Puppet::DevError, _('You cannot save facts to the code store; it is only used for getting facts from Facter')
end