Class: Puppet::Application::Lookup
Constant Summary
collapse
- RUN_HELP =
_("Run 'puppet lookup --help' for more details").freeze
- DEEP_MERGE_OPTIONS =
'--knock-out-prefix, --sort-merged-arrays, and --merge-hash-arrays'
- TRUSTED_INFORMATION_FACTS =
%w[hostname domain fqdn clientcert].freeze
DOCPATTERN
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
Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit
Instance Attribute Summary
#command_line, #options
Instance Method Summary
collapse
[], available_application_names, banner, clear!, clear?, clear_everything_for_tests, #configure_indirector_routes, controlled_run, #deprecate, #deprecated?, environment_mode, exit, find, get_environment_mode, #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, stop!, stop_requested?, try_load_class
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
#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Instance Method Details
#app_defaults ⇒ Object
63
64
65
66
67
|
# File 'lib/puppet/application/lookup.rb', line 63
def app_defaults
super.merge({
:facts_terminus => 'yaml'
})
end
|
#generate_scope ⇒ Object
338
339
340
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
|
# File 'lib/puppet/application/lookup.rb', line 338
def generate_scope
if options[:node]
node = options[:node]
else
node = Puppet[:node_name_value]
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)
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
if node.is_a?(Puppet::Node)
node.(given_facts) if given_facts
else
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
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
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
|
# File 'lib/puppet/application/lookup.rb', line 107
def help
"\n puppet-lookup(8) -- \#{summary}\n ========\n\n SYNOPSIS\n --------\n Does Hiera lookups from the command line.\n\n Since this command needs access to your Hiera data, make sure to run it on a\n node that has a copy of that data. This usually means logging into a Puppet\n Server node and running 'puppet lookup' with sudo.\n\n The most common version of this command is:\n\n 'puppet lookup <KEY> --node <NAME> --environment <ENV> --explain'\n\n USAGE\n -----\n puppet lookup [--help] [--type <TYPESTRING>] [--merge first|unique|hash|deep]\n [--knock-out-prefix <PREFIX-STRING>] [--sort-merged-arrays]\n [--merge-hash-arrays] [--explain] [--environment <ENV>]\n [--default <VALUE>] [--node <NODE-NAME>] [--facts <FILE>]\n [--compile]\n [--render-as s|json|yaml|binary|msgpack] <keys>\n\n DESCRIPTION\n -----------\n The lookup command is a CLI for Puppet's 'lookup()' function. It searches your\n Hiera data and returns a value for the requested lookup key, so you can test and\n explore your data. It is a modern replacement for the 'hiera' command.\n Lookup uses the setting for global hiera.yaml from puppet's config,\n and the environment to find the environment level hiera.yaml as well as the\n resulting modulepath for the environment (for hiera.yaml files in modules).\n Hiera usually relies on a node's facts to locate the relevant data sources. By\n default, 'puppet lookup' uses facts from the node you run the command on, but\n you can get data for any other node with the '--node <NAME>' option. If\n possible, the lookup command will use the requested node's real stored facts\n from PuppetDB; if PuppetDB isn't configured or you want to provide arbitrary\n fact values, you can pass alternate facts as a JSON or YAML file with '--facts\n <FILE>'.\n\n If you're debugging your Hiera data and want to see where values are coming\n from, use the '--explain' option.\n\n If '--explain' isn't specified, lookup exits with 0 if a value was found and 1\n otherwise. With '--explain', lookup always exits with 0 unless there is a major\n error.\n\n You can provide multiple lookup keys to this command, but it only returns a\n value for the first found key, omitting the rest.\n\n For more details about how Hiera works, see the Hiera documentation:\n https://puppet.com/docs/puppet/latest/hiera_intro.html\n\n OPTIONS\n -------\n\n * --help:\n Print this help message.\n\n * --explain\n Explain the details of how the lookup was performed and where the final value\n came from (or the reason no value was found).\n\n * --node <NODE-NAME>\n Specify which node to look up data for; defaults to the node where the command\n is run. Since Hiera's purpose is to provide different values for different\n nodes (usually based on their facts), you'll usually want to use some specific\n node's facts to explore your data. If the node where you're running this\n command is configured to talk to PuppetDB, the command will use the requested\n node's most recent facts. Otherwise, you can override facts with the '--facts'\n option.\n\n * --facts <FILE>\n Specify a .json or .yaml file of key => value mappings to override the facts\n for this lookup. Any facts not specified in this file maintain their\n original value.\n\n * --environment <ENV>\n Like with most Puppet commands, you can specify an environment on the command\n line. This is important for lookup because different environments can have\n different Hiera data. This environment will be always be the one used regardless\n of any other factors.\n\n * --merge first|unique|hash|deep:\n Specify the merge behavior, overriding any merge behavior from the data's\n lookup_options. 'first' returns the first value found. 'unique' appends\n everything to a merged, deduplicated array. 'hash' performs a simple hash\n merge by overwriting keys of lower lookup priority. 'deep' performs a deep\n merge on values of Array and Hash type. There are additional options that can\n be used with 'deep'.\n\n * --knock-out-prefix <PREFIX-STRING>\n Can be used with the 'deep' merge strategy. Specifies a prefix to indicate a\n value should be removed from the final result.\n\n * --sort-merged-arrays\n Can be used with the 'deep' merge strategy. When this flag is used, all\n merged arrays are sorted.\n\n * --merge-hash-arrays\n Can be used with the 'deep' merge strategy. When this flag is used, hashes\n WITHIN arrays are deep-merged with their counterparts by position.\n\n * --explain-options\n Explain whether a lookup_options hash affects this lookup, and how that hash\n was assembled. (lookup_options is how Hiera configures merge behavior in data.)\n\n * --default <VALUE>\n A value to return if Hiera can't find a value in data. For emulating calls to\n the 'lookup()' function that include a default.\n\n * --type <TYPESTRING>:\n Assert that the value has the specified type. For emulating calls to the\n 'lookup()' function that include a data type.\n\n * --compile\n Perform a full catalog compilation prior to the lookup. If your hierarchy and\n data only use the $facts, $trusted, and $server_facts variables, you don't\n need this option; however, if your Hiera configuration uses arbitrary\n variables set by a Puppet manifest, you might need this option to get accurate\n data. No catalog compilation takes place unless this flag is given.\n\n * --render-as s|json|yaml|binary|msgpack\n Specify the output format of the results; \"s\" means plain text. The default\n when producing a value is yaml and the default when producing an explanation\n is s.\n\n EXAMPLE\n -------\n To look up 'key_name' using the Puppet Server node's facts:\n $ puppet lookup key_name\n\n To look up 'key_name' using the Puppet Server node's arbitrary variables from a manifest, and\n classify the node if applicable:\n $ puppet lookup key_name --compile\n\n To look up 'key_name' using the Puppet Server node's facts, overridden by facts given in a file:\n $ puppet lookup key_name --facts fact_file.yaml\n\n To look up 'key_name' with agent.local's facts:\n $ puppet lookup --node agent.local key_name\n\n To get the first value found for 'key_name_one' and 'key_name_two'\n with agent.local's facts while merging values and knocking out\n the prefix 'foo' while merging:\n $ puppet lookup --node agent.local --merge deep --knock-out-prefix foo key_name_one key_name_two\n\n To lookup 'key_name' with agent.local's facts, and return a default value of\n 'bar' if nothing was found:\n $ puppet lookup --node agent.local --default bar key_name\n\n To see an explanation of how the value for 'key_name' would be found, using\n agent.local's facts:\n $ puppet lookup --node agent.local --explain key_name\n\n COPYRIGHT\n ---------\n Copyright (c) 2015 Puppet Inc., LLC Licensed under the Apache 2.0 License\n\n\n HELP\nend\n"
|
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# File 'lib/puppet/application/lookup.rb', line 273
def main
keys = command_line.args
if (options[:sort_merged_arrays] || options[:merge_hash_arrays] || options[:prefix]) && options[:merge] != 'deep'
raise _("The options %{deep_merge_opts} are only available with '--merge deep'\n%{run_help}") % { deep_merge_opts: DEEP_MERGE_OPTIONS, run_help: RUN_HELP }
end
use_default_value = !options[:default_value].nil?
merge_options = nil
merge = options[:merge]
unless merge.nil?
strategies = Puppet::Pops::MergeStrategy.strategy_keys
unless strategies.include?(merge.to_sym)
strategies = strategies.map { |k| "'#{k}'" }
raise _("The --merge option only accepts %{strategies}, or %{last_strategy}\n%{run_help}") % { strategies: strategies[0...-1].join(', '), last_strategy: strategies.last, run_help: RUN_HELP }
end
if merge == 'deep'
merge_options = { 'strategy' => 'deep',
'sort_merged_arrays' => !options[:sort_merged_arrays].nil?,
'merge_hash_arrays' => !options[:merge_hash_arrays].nil? }
if options[:prefix]
merge_options['knockout_prefix'] = options[:prefix]
end
else
merge_options = { 'strategy' => merge }
end
end
explain_data = !!options[:explain]
explain_options = !!options[:explain_options]
only_explain_options = explain_options && !explain_data
if keys.empty?
if only_explain_options
keys = Puppet::Pops::Lookup::GLOBAL
else
raise _('No keys were given to lookup.')
end
end
explain = explain_data || explain_options
format = options[:render_as] || (explain ? :s : :yaml)
renderer = Puppet::Network::FormatHandler.format(format)
raise _("Unknown rendering format '%{format}'") % { format: format } if renderer.nil?
generate_scope do |scope|
lookup_invocation = Puppet::Pops::Lookup::Invocation.new(scope, {}, {}, explain ? Puppet::Pops::Lookup::Explainer.new(explain_options, only_explain_options) : nil)
begin
type = options.include?(:type) ? Puppet::Pops::Types::TypeParser.singleton.parse(options[:type], scope) : nil
result = Puppet::Pops::Lookup.lookup(keys, type, options[:default_value], use_default_value, merge_options, lookup_invocation)
puts renderer.render(result) unless explain
rescue Puppet::DataBinding::LookupError => e
lookup_invocation.report_text { e.message }
exit(1) unless explain
end
puts format == :s ? lookup_invocation.explainer.explain : renderer.render(lookup_invocation.explainer.to_hash) if explain
end
exit(0)
end
|
#retrieve_node_facts(node, given_facts) ⇒ Object
419
420
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/puppet/application/lookup.rb', line 419
def retrieve_node_facts(node, given_facts)
facts = Puppet::Node::Facts.indirection.find(node, :environment => Puppet.lookup(:current_environment))
facts = Puppet::Node::Facts.new(node, {}) if facts.nil?
facts.(given_facts) if given_facts
if facts.values.empty?
raise _("No facts available for target node: %{node}") % { node: node }
end
facts
end
|
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/puppet/application/lookup.rb', line 87
def setup
setup_logs
exit(Puppet.settings.print_configs ? 0 : 1) if Puppet.settings.print_configs?
if options[:node]
Puppet::Util.skip_external_facts do
Puppet.settings.use :main, :server, :ssl, :metrics
end
else
Puppet.settings.use :main, :server, :ssl, :metrics
end
setup_terminuses
end
|
#setup_logs ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/puppet/application/lookup.rb', line 69
def setup_logs
set_log_level
Puppet::Util::Log.newdestination(:console)
end
|
#setup_terminuses ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/puppet/application/lookup.rb', line 77
def setup_terminuses
require_relative '../../puppet/file_serving/content'
require_relative '../../puppet/file_serving/metadata'
Puppet::FileServing::Content.indirection.terminus_class = :file_server
Puppet::FileServing::Metadata.indirection.terminus_class = :file_server
Puppet::FileBucket::File.indirection.terminus_class = :file
end
|
103
104
105
|
# File 'lib/puppet/application/lookup.rb', line 103
def summary
_("Interactive Hiera lookup")
end
|