Class: Puppet::Application::UploadFacts

Inherits:
Puppet::Application show all
Defined in:
ext/upload_facts.rb

Constant Summary

Constants inherited from Puppet::Application

DOCPATTERN

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 Puppet::Application

#command_line, #options

Instance Method Summary collapse

Methods inherited from Puppet::Application

[], #app_defaults, available_application_names, banner, clear!, clear?, clear_everything_for_tests, #configure_indirector_routes, controlled_run, #deprecate, #deprecated?, exit, find, #handle_logdest_arg, #handlearg, #initialize, #initialize_app_defaults, interrupted?, #log_runtime_environment, #name, option, option_parser_commands, #parse_options, #preinit, restart!, restart_requested?, #run, #run_command, run_mode, #set_log_level, #setup_logs, stop!, stop_requested?, #summary, try_load_class

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

This class inherits a constructor from Puppet::Application

Instance Method Details

#helpObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'ext/upload_facts.rb', line 28

def help
  print <<HELP
== Synopsis

Upload cached facts to the inventory service.

= Usage

upload_facts [-d|--debug] [-v|--verbose] [-m|--minutes <minutes>]
             [-l|--logdest syslog|<file>|console]

= Description

This command will read YAML facts from the puppet master's YAML directory, and
save them to the configured facts_terminus. It is intended to be used with the
facts_terminus set to inventory_service, in order to ensure facts which have
been cached locally due to a temporary failure are still uploaded to the
inventory service.

= Usage Notes

upload_facts is intended to be run from cron, with the facts_terminus set to
inventory_service. The +--minutes+ argument should be set to the length of time
between upload_facts runs. This will ensure that only new YAML files are
uploaded.

= Options

Note that any setting that's valid in the configuration file
is also a valid long argument.  For example, 'server' is a valid configuration
parameter, so you can specify '--server <servername>' as an argument.

See the configuration file documentation at
https://docs.puppetlabs.com/puppet/latest/reference/configuration.html for
the full list of acceptable parameters. A commented list of all
configuration options can also be generated by running puppet agent with
'--genconfig'.

minutes::
Limit the upload only to YAML files which have been added within the last n
minutes.
HELP

  exit
end

#mainObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ext/upload_facts.rb', line 89

def main
  dir = Pathname.new(Puppet[:yamldir]) + 'facts'

  cutoff = options[:time_limit] ? Time.now - options[:time_limit] : Time.at(0)

  files = dir.children.select do |file|
    file.extname == '.yaml' && file.mtime > cutoff
  end

  failed = false

  terminus = Puppet::Node::Facts.indirection.terminus

  files.each do |file|
    facts = YAML.load_file(file)

    request = Puppet::Indirector::Request.new(:facts, :save, facts)

    # The terminus warns for us if we fail.
    if terminus.save(request)
      Puppet.info "Uploaded facts for #{facts.name} to inventory service"
    else
      failed = true
    end
  end

  exit !failed
end

#setupObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/upload_facts.rb', line 74

def setup
  # Handle the logging settings.
  if options[:debug] or options[:verbose]
    if options[:debug]
      Puppet::Util::Log.level = :debug
    else
      Puppet::Util::Log.level = :info
    end

    Puppet::Util::Log.newdestination(:console) unless options[:setdest]
  end

  exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?
end