Class: Puppet::Node
- Extended by:
- Indirector
- Includes:
- Util::PsychSupport
- Defined in:
- lib/puppet/node.rb,
lib/puppet/node/environment.rb
Overview
Just define it, so this class has fewer load dependencies.
Defined Under Namespace
Classes: Environment, Exec, Facts, Json, Memory, Msgpack, Plain, Rest, StoreConfigs, Yaml
Constant Summary collapse
- ENVIRONMENT =
'environment'
Constants included from Indirector
Instance Attribute Summary collapse
-
#classes ⇒ Object
Returns the value of attribute classes.
-
#environment_name ⇒ Object
Returns the value of attribute environment_name.
-
#facts ⇒ Object
readonly
Returns the value of attribute facts.
-
#ipaddress ⇒ Object
Returns the value of attribute ipaddress.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#server_facts ⇒ Object
readonly
Returns the value of attribute server_facts.
-
#source ⇒ Object
Returns the value of attribute source.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#trusted_data ⇒ Object
Returns the value of attribute trusted_data.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_extra_facts(extra_facts) ⇒ Object
private
Add extra facts, such as facts given to lookup on the command line The extra facts will override existing ones.
- #add_server_facts(facts) ⇒ Object
- #environment ⇒ Object
- #environment=(env) ⇒ Object
-
#fact_merge(facts = nil) ⇒ nil
Merge the node facts with parameters from the node source.
- #has_environment_instance? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ Node
constructor
A new instance of Node.
- #initialize_from_hash(data) ⇒ Object
-
#merge(params) ⇒ Object
Merge any random parameters into our parameter list.
-
#names ⇒ Object
Calculate the list of names we might use for looking up our node.
-
#sanitize ⇒ Object
Resurrects and sanitizes trusted information in the node by modifying it and setting the trusted_data in the node from parameters.
- #serializable_parameters ⇒ Object
- #split_name(name) ⇒ Object
- #to_data_hash ⇒ Object
Methods included from Indirector
Methods included from Util::PsychSupport
Constructor Details
#initialize(name, options = {}) ⇒ Node
Returns a new instance of Node.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/puppet/node.rb', line 102 def initialize(name, = {}) raise ArgumentError, _("Node names cannot be nil") unless name @name = name classes = [:classes] if classes if classes.is_a?(String) @classes = [classes] else @classes = classes end else @classes = [] end @parameters = [:parameters] || {} @facts = [:facts] @server_facts = {} env = [:environment] if env self.environment = env end @time = Time.now end |
Instance Attribute Details
#classes ⇒ Object
Returns the value of attribute classes.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def classes @classes end |
#environment_name ⇒ Object
Returns the value of attribute environment_name.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def environment_name @environment_name end |
#facts ⇒ Object (readonly)
Returns the value of attribute facts.
21 22 23 |
# File 'lib/puppet/node.rb', line 21 def facts @facts end |
#ipaddress ⇒ Object
Returns the value of attribute ipaddress.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def ipaddress @ipaddress end |
#name ⇒ Object
Returns the value of attribute name.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def name @name end |
#parameters ⇒ Object
Returns the value of attribute parameters.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def parameters @parameters end |
#server_facts ⇒ Object (readonly)
Returns the value of attribute server_facts.
23 24 25 |
# File 'lib/puppet/node.rb', line 23 def server_facts @server_facts end |
#source ⇒ Object
Returns the value of attribute source.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def source @source end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
21 22 23 |
# File 'lib/puppet/node.rb', line 21 def time @time end |
#trusted_data ⇒ Object
Returns the value of attribute trusted_data.
21 22 23 |
# File 'lib/puppet/node.rb', line 21 def trusted_data @trusted_data end |
Class Method Details
.from_data_hash(data) ⇒ Object
38 39 40 41 42 |
# File 'lib/puppet/node.rb', line 38 def self.from_data_hash(data) node = new(name) node.initialize_from_hash(data) node end |
Instance Method Details
#add_extra_facts(extra_facts) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add extra facts, such as facts given to lookup on the command line The extra facts will override existing ones.
170 171 172 173 174 |
# File 'lib/puppet/node.rb', line 170 def add_extra_facts(extra_facts) @facts.add_extra_values(extra_facts) @parameters.merge!(extra_facts) nil end |
#add_server_facts(facts) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/puppet/node.rb', line 176 def add_server_facts(facts) # Append the current environment to the list of server facts @server_facts = facts.merge({ "environment" => self.environment.name.to_s}) # Merge the server facts into the parameters for the node merge(facts) end |
#environment ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/puppet/node.rb', line 61 def environment if @environment @environment else env = parameters[ENVIRONMENT] if env self.environment = env elsif environment_name self.environment = environment_name else # This should not be :current_environment, this is the default # for a node when it has not specified its environment # it will be used to establish what the current environment is. # self.environment = Puppet.lookup(:environments).get!(Puppet[:environment]) end @environment end end |
#environment=(env) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/puppet/node.rb', line 82 def environment=(env) if env.is_a?(String) or env.is_a?(Symbol) @environment = Puppet.lookup(:environments).get!(env) else @environment = env end # Keep environment_name attribute and parameter in sync if they have been set unless @environment.nil? # always set the environment parameter. It becomes top scope $environment for a manifest during catalog compilation. @parameters[ENVIRONMENT] = @environment.name.to_s self.environment_name = @environment.name end @environment end |
#fact_merge(facts = nil) ⇒ nil
Merge the node facts with parameters from the node source.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/puppet/node.rb', line 137 def fact_merge(facts = nil) begin @facts = facts.nil? ? Puppet::Node::Facts.indirection.find(name, :environment => environment) : facts rescue => detail error = Puppet::Error.new(_("Could not retrieve facts for %{name}: %{detail}") % { name: name, detail: detail }, detail) error.set_backtrace(detail.backtrace) raise error end if !@facts.nil? @facts.sanitize # facts should never modify the environment parameter orig_param_env = @parameters[ENVIRONMENT] merge(@facts.values) @parameters[ENVIRONMENT] = orig_param_env end end |
#has_environment_instance? ⇒ Boolean
98 99 100 |
# File 'lib/puppet/node.rb', line 98 def has_environment_instance? !@environment.nil? end |
#initialize_from_hash(data) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/puppet/node.rb', line 27 def initialize_from_hash(data) @name = data['name'] || (raise ArgumentError, _("No name provided in serialized data")) @classes = data['classes'] || [] @parameters = data['parameters'] || {} env_name = data['environment'] || @parameters[ENVIRONMENT] unless env_name.nil? @parameters[ENVIRONMENT] = env_name @environment_name = env_name.intern end end |
#merge(params) ⇒ Object
Merge any random parameters into our parameter list.
156 157 158 159 160 161 162 163 164 |
# File 'lib/puppet/node.rb', line 156 def merge(params) params.each do |name, value| if @parameters.include?(name) Puppet::Util::Warnings.warnonce(_("The node parameter '%{param_name}' for node '%{node_name}' was already set to '%{value}'. It could not be set to '%{desired_value}'") % { param_name: name, node_name: @name, value: @parameters[name], desired_value: value }) else @parameters[name] = value end end end |
#names ⇒ Object
Calculate the list of names we might use for looking up our node. This is only used for AST nodes.
186 187 188 |
# File 'lib/puppet/node.rb', line 186 def names @names ||= [name] end |
#sanitize ⇒ Object
Resurrects and sanitizes trusted information in the node by modifying it and setting the trusted_data in the node from parameters. This modifies the node
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 |
# File 'lib/puppet/node.rb', line 210 def sanitize # Resurrect "trusted information" that comes from node/fact terminus. # The current way this is done in puppet db (currently the only one) # is to store the node parameter 'trusted' as a hash of the trusted information. # # Thus here there are two main cases: # 1. This terminus was used in a real agent call (only meaningful if someone curls the request as it would # fail since the result is a hash of two catalogs). # 2 It is a command line call with a given node that use a terminus that: # 2.1 does not include a 'trusted' fact - use local from node trusted information # 2.2 has a 'trusted' fact - this in turn could be # 2.2.1 puppet db having stored trusted node data as a fact (not a great design) # 2.2.2 some other terminus having stored a fact called "trusted" (most likely that would have failed earlier, but could # be spoofed). # # For the reasons above, the resurrection of trusted node data with authenticated => true is only performed # if user is running as root, else it is resurrected as unauthenticated. # trusted_param = @parameters['trusted'] if trusted_param # Blows up if it is a parameter as it will be set as $trusted by the compiler as if it was a variable @parameters.delete('trusted') unless trusted_param.is_a?(Hash) && %w{authenticated certname extensions}.all? {|key| trusted_param.has_key?(key) } # trusted is some kind of garbage, do not resurrect trusted_param = nil end else # trusted may be Boolean false if set as a fact by someone trusted_param = nil end # The options for node.trusted_data in priority order are: # 1) node came with trusted_data so use that # 2) else if there is :trusted_information in the puppet context # 3) else if the node provided a 'trusted' parameter (parsed out above) # 4) last, fallback to local node trusted information # # Note that trusted_data should be a hash, but (2) and (4) are not # hashes, so we to_h at the end if !trusted_data trusted = Puppet.lookup(:trusted_information) do trusted_param || Puppet::Context::TrustedInformation.local(self) end self.trusted_data = trusted.to_h end end |
#serializable_parameters ⇒ Object
55 56 57 58 59 |
# File 'lib/puppet/node.rb', line 55 def serializable_parameters new_params = parameters.dup new_params.delete(ENVIRONMENT) new_params end |
#split_name(name) ⇒ Object
190 191 192 193 194 195 196 197 |
# File 'lib/puppet/node.rb', line 190 def split_name(name) list = name.split(".") tmp = [] list.each_with_index do |short, i| tmp << list[0..i].join(".") end tmp.reverse end |
#to_data_hash ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/puppet/node.rb', line 44 def to_data_hash result = { 'name' => name, 'environment' => environment.name.to_s, } result['classes'] = classes unless classes.empty? serialized_params = self.serializable_parameters result['parameters'] = serialized_params unless serialized_params.empty? result end |