class Node
A class for managing nodes, including their facts and environment.
Just define it, so this class has fewer load dependencies.
Constants
- ENVIRONMENT
Attributes
Public Class Methods
# File lib/puppet/node.rb 38 def self.from_data_hash(data) 39 node = new(name) 40 node.initialize_from_hash(data) 41 node 42 end
# File lib/puppet/node.rb 102 def initialize(name, options = {}) 103 raise ArgumentError, _("Node names cannot be nil") unless name 104 @name = name 105 106 classes = options[:classes] 107 if classes 108 if classes.is_a?(String) 109 @classes = [classes] 110 else 111 @classes = classes 112 end 113 else 114 @classes = [] 115 end 116 117 @parameters = options[:parameters] || {} 118 119 @facts = options[:facts] 120 121 @server_facts = {} 122 123 env = options[:environment] 124 if env 125 self.environment = env 126 end 127 128 @time = Time.now 129 end
Public Instance Methods
Add extra facts, such as facts given to lookup on the command line The extra facts will override existing ones. @param extra_facts [Hash{String=>Object}] the facts to tadd @api private
# File lib/puppet/node.rb 170 def add_extra_facts(extra_facts) 171 @facts.add_extra_values(extra_facts) 172 @parameters.merge!(extra_facts) 173 nil 174 end
# File lib/puppet/node.rb 176 def add_server_facts(facts) 177 # Append the current environment to the list of server facts 178 @server_facts = facts.merge({ "environment" => self.environment.name.to_s}) 179 180 # Merge the server facts into the parameters for the node 181 merge(facts) 182 end
# File lib/puppet/node.rb 61 def environment 62 if @environment 63 @environment 64 else 65 env = parameters[ENVIRONMENT] 66 if env 67 self.environment = env 68 elsif environment_name 69 self.environment = environment_name 70 else 71 # This should not be :current_environment, this is the default 72 # for a node when it has not specified its environment 73 # it will be used to establish what the current environment is. 74 # 75 self.environment = Puppet.lookup(:environments).get!(Puppet[:environment]) 76 end 77 78 @environment 79 end 80 end
# File lib/puppet/node.rb 82 def environment=(env) 83 if env.is_a?(String) or env.is_a?(Symbol) 84 @environment = Puppet.lookup(:environments).get!(env) 85 else 86 @environment = env 87 end 88 89 # Keep environment_name attribute and parameter in sync if they have been set 90 unless @environment.nil? 91 # always set the environment parameter. It becomes top scope $environment for a manifest during catalog compilation. 92 @parameters[ENVIRONMENT] = @environment.name.to_s 93 self.environment_name = @environment.name 94 end 95 @environment 96 end
Merge the node facts with parameters from the node source. @api public @param facts [optional, Puppet::Node::Facts] facts to merge into node parameters.
Will query Facts indirection if not supplied.
@raise [Puppet::Error] Raise on failure to retrieve facts if not supplied @return [nil]
# File lib/puppet/node.rb 137 def fact_merge(facts = nil) 138 begin 139 @facts = facts.nil? ? Puppet::Node::Facts.indirection.find(name, :environment => environment) : facts 140 rescue => detail 141 error = Puppet::Error.new(_("Could not retrieve facts for %{name}: %{detail}") % { name: name, detail: detail }, detail) 142 error.set_backtrace(detail.backtrace) 143 raise error 144 end 145 146 if !@facts.nil? 147 @facts.sanitize 148 # facts should never modify the environment parameter 149 orig_param_env = @parameters[ENVIRONMENT] 150 merge(@facts.values) 151 @parameters[ENVIRONMENT] = orig_param_env 152 end 153 end
# File lib/puppet/node.rb 98 def has_environment_instance? 99 !@environment.nil? 100 end
# File lib/puppet/node.rb 27 def initialize_from_hash(data) 28 @name = data['name'] || (raise ArgumentError, _("No name provided in serialized data")) 29 @classes = data['classes'] || [] 30 @parameters = data['parameters'] || {} 31 env_name = data['environment'] || @parameters[ENVIRONMENT] 32 unless env_name.nil? 33 @parameters[ENVIRONMENT] = env_name 34 @environment_name = env_name.intern 35 end 36 end
Merge any random parameters into our parameter list.
# File lib/puppet/node.rb 156 def merge(params) 157 params.each do |name, value| 158 if @parameters.include?(name) 159 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 }) 160 else 161 @parameters[name] = value 162 end 163 end 164 end
Calculate the list of names we might use for looking up our node. This is only used for AST nodes.
# File lib/puppet/node.rb 186 def names 187 @names ||= [name] 188 end
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
# File lib/puppet/node.rb 210 def sanitize 211 # Resurrect "trusted information" that comes from node/fact terminus. 212 # The current way this is done in puppet db (currently the only one) 213 # is to store the node parameter 'trusted' as a hash of the trusted information. 214 # 215 # Thus here there are two main cases: 216 # 1. This terminus was used in a real agent call (only meaningful if someone curls the request as it would 217 # fail since the result is a hash of two catalogs). 218 # 2 It is a command line call with a given node that use a terminus that: 219 # 2.1 does not include a 'trusted' fact - use local from node trusted information 220 # 2.2 has a 'trusted' fact - this in turn could be 221 # 2.2.1 puppet db having stored trusted node data as a fact (not a great design) 222 # 2.2.2 some other terminus having stored a fact called "trusted" (most likely that would have failed earlier, but could 223 # be spoofed). 224 # 225 # For the reasons above, the resurrection of trusted node data with authenticated => true is only performed 226 # if user is running as root, else it is resurrected as unauthenticated. 227 # 228 trusted_param = @parameters['trusted'] 229 if trusted_param 230 # Blows up if it is a parameter as it will be set as $trusted by the compiler as if it was a variable 231 @parameters.delete('trusted') 232 unless trusted_param.is_a?(Hash) && %w{authenticated certname extensions}.all? {|key| trusted_param.has_key?(key) } 233 # trusted is some kind of garbage, do not resurrect 234 trusted_param = nil 235 end 236 else 237 # trusted may be Boolean false if set as a fact by someone 238 trusted_param = nil 239 end 240 241 # The options for node.trusted_data in priority order are: 242 # 1) node came with trusted_data so use that 243 # 2) else if there is :trusted_information in the puppet context 244 # 3) else if the node provided a 'trusted' parameter (parsed out above) 245 # 4) last, fallback to local node trusted information 246 # 247 # Note that trusted_data should be a hash, but (2) and (4) are not 248 # hashes, so we to_h at the end 249 if !trusted_data 250 trusted = Puppet.lookup(:trusted_information) do 251 trusted_param || Puppet::Context::TrustedInformation.local(self) 252 end 253 254 self.trusted_data = trusted.to_h 255 end 256 end
# File lib/puppet/node.rb 55 def serializable_parameters 56 new_params = parameters.dup 57 new_params.delete(ENVIRONMENT) 58 new_params 59 end
# File lib/puppet/node.rb 190 def split_name(name) 191 list = name.split(".") 192 tmp = [] 193 list.each_with_index do |short, i| 194 tmp << list[0..i].join(".") 195 end 196 tmp.reverse 197 end
# File lib/puppet/node.rb 44 def to_data_hash 45 result = { 46 'name' => name, 47 'environment' => environment.name.to_s, 48 } 49 result['classes'] = classes unless classes.empty? 50 serialized_params = self.serializable_parameters 51 result['parameters'] = serialized_params unless serialized_params.empty? 52 result 53 end
Ensures the data is frozen
# File lib/puppet/node.rb 201 def trusted_data=(data) 202 Puppet.warning(_("Trusted node data modified for node %{name}") % { name: name }) unless @trusted_data.nil? 203 @trusted_data = data.freeze 204 end