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