Class: PuppetX::Puppetlabs::Lookup
- Inherits:
-
Object
- Object
- PuppetX::Puppetlabs::Lookup
- Defined in:
- lib/puppet_x/puppetlabs/lookup.rb
Overview
Output a class parameter defined in Hiera and/or the Classifier.
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Instance Method Summary collapse
-
#hiera_classifier_overrides(certname, settings) ⇒ Object
Extract the beating heart of a puppet compiler for lookup purposes.
-
#hiera_classifier_settings(certname, setting_names) ⇒ Object
Interface to Puppet::Util::Pe_conf::Recover.
-
#initialize(options) ⇒ Lookup
constructor
Initialize command options, class variables, and objects.
-
#output(info) ⇒ Object
Output.
-
#output_current_setting(certname, setting_name) ⇒ Object
Output the specified class parameter defined in Hiera and/or the Classifier.
-
#output_data(info) ⇒ Object
Output highlighted output, from ‘puppet/util/colors’.
-
#output_error_and_exit(message) ⇒ Object
Output an error and exit.
- #output_line ⇒ Object
-
#recover_with_instance_method? ⇒ Boolean
PE-24106 changes Recover to a class with instance methods.
-
#recover_with_node_terminus_method? ⇒ Boolean
In some versions, Puppet::Util::Pe_conf::Recover does not implement get_node_terminus() and implements find_hiera_overrides(params, facts, environment).
Constructor Details
#initialize(options) ⇒ Lookup
Initialize command options, class variables, and objects.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 12 def initialize() unless [:param] output_error_and_exit('The --param option is required') end unless [:node] output_error_and_exit('The --node option is required') end unless [:pe_environment] output_error_and_exit('The --pe_environment option is required') end @environment = [:pe_environment] end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
9 10 11 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 9 def environment @environment end |
Instance Method Details
#hiera_classifier_overrides(certname, settings) ⇒ Object
Extract the beating heart of a puppet compiler for lookup purposes.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 80 def hiera_classifier_overrides(certname, settings) if recover_with_instance_method? recover = Puppet::Util::Pe_conf::Recover.new recover_node_facts = recover.facts_for_node(certname, @environment) node_terminus = recover.get_node_terminus overrides_hiera = recover.find_hiera_overrides(certname, settings, recover_node_facts, @environment, node_terminus) overrides_classifier = recover.classifier_overrides_for_node(certname, recover_node_facts, recover_node_facts['::trusted']) else recover_node_facts = Puppet::Util::Pe_conf::Recover.facts_for_node(certname, @environment) if recover_with_node_terminus_method? node_terminus = Puppet::Util::Pe_conf::Recover.get_node_terminus overrides_hiera = Puppet::Util::Pe_conf::Recover.find_hiera_overrides(certname, settings, recover_node_facts, @environment, node_terminus) else overrides_hiera = Puppet::Util::Pe_conf::Recover.find_hiera_overrides(settings, recover_node_facts, @environment) end overrides_classifier = Puppet::Util::Pe_conf::Recover.classifier_overrides_for_node(certname, recover_node_facts, recover_node_facts['::trusted']) end [overrides_hiera, overrides_classifier] end |
#hiera_classifier_settings(certname, setting_names) ⇒ Object
Interface to Puppet::Util::Pe_conf::Recover.
68 69 70 71 72 73 74 75 76 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 68 def hiera_classifier_settings(certname, setting_names) overrides_hiera, overrides_classifier = hiera_classifier_overrides(certname, setting_names) Puppet.debug("Settings: #{setting_names}") Puppet.debug("Settings from Hiera for: #{certname}: #{overrides_hiera}") Puppet.debug("Settings from Classifier for: #{certname}: #{overrides_classifier}") return { 'hiera' => overrides_hiera, 'classifier' => overrides_classifier } rescue Puppet::Error return nil end |
#output(info) ⇒ Object
Output
116 117 118 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 116 def output(info) puts "# #{info}" end |
#output_current_setting(certname, setting_name) ⇒ Object
Output the specified class parameter defined in Hiera and/or the Classifier.
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 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 29 def output_current_setting(certname, setting_name) current_settings = hiera_classifier_settings(certname, [setting_name]) output_error_and_exit('Unable to query Hiera or the Classifier') if current_settings.nil? current_settings['hiera'] = current_settings['hiera'].select { |k, _v| k == setting_name } current_settings['classifier'] = current_settings['classifier'].select { |k, _v| k == setting_name } output 'Node: %{certname}' % { certname: certname } output 'Parameter: %{setting_name}' % { setting_name: setting_name } output_line found_in_hiera = current_settings['hiera'].key?(setting_name) if found_in_hiera output 'Parameter found in Hiera:' output_line output_data(current_settings['hiera'].to_yaml) else output 'Parameter not found in Hiera' end output_line found_in_classifier = current_settings['classifier'].key?(setting_name) if found_in_classifier output 'Parameter found in the Classifier:' output_line output_data JSON.pretty_generate(current_settings['classifier']) else output 'Parameter not found in the Classifier' end output_line return unless found_in_hiera && found_in_classifier output 'Classifier settings take precedence over Hiera settings.' output_line end |
#output_data(info) ⇒ Object
Output highlighted output, from ‘puppet/util/colors’.
126 127 128 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 126 def output_data(info) puts "\e[0;32m#{info}\e[0m" end |
#output_error_and_exit(message) ⇒ Object
Output an error and exit.
132 133 134 135 136 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 132 def output_error_and_exit() Puppet.err() Puppet.err("Rerun this command with '--debug' or '--help' for more information") exit 1 end |
#output_line ⇒ Object
120 121 122 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 120 def output_line puts "\n" end |
#recover_with_instance_method? ⇒ Boolean
PE-24106 changes Recover to a class with instance methods.
102 103 104 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 102 def recover_with_instance_method? defined?(Puppet::Util::Pe_conf::Recover.facts_for_node) != 'method' end |
#recover_with_node_terminus_method? ⇒ Boolean
In some versions, Puppet::Util::Pe_conf::Recover does not implement get_node_terminus() and implements find_hiera_overrides(params, facts, environment)
108 109 110 |
# File 'lib/puppet_x/puppetlabs/lookup.rb', line 108 def recover_with_node_terminus_method? defined?(Puppet::Util::Pe_conf::Recover.get_node_terminus) == 'method' end |