Class: Puppet::Node
- Extended by:
- Indirector
- Includes:
- Util::PsychSupport
- Defined in:
- lib/puppet/node/environment.rb,
lib/puppet/node.rb
Overview
A class for managing nodes, including their facts and environment.
Defined Under Namespace
Classes: Environment, Exec, Facts, Ldap, Memory, Msgpack, Plain, Rest, StoreConfigs, WriteOnlyYaml, Yaml
Constant Summary collapse
- ENVIRONMENT =
'environment'.freeze
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_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.
- #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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/puppet/node.rb', line 91 def initialize(name, = {}) raise ArgumentError, _("Node names cannot be nil") unless name @name = name if classes = [:classes] if classes.is_a?(String) @classes = [classes] else @classes = classes end else @classes = [] end @parameters = [:parameters] || {} @facts = [:facts] @server_facts = {} if env = [:environment] self.environment = env end @time = Time.now end |
Instance Attribute Details
#classes ⇒ Object
Returns the value of attribute classes.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def classes @classes end |
#environment_name ⇒ Object
Returns the value of attribute environment_name.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def environment_name @environment_name end |
#facts ⇒ Object (readonly)
Returns the value of attribute facts.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def facts @facts end |
#ipaddress ⇒ Object
Returns the value of attribute ipaddress.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def ipaddress @ipaddress end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def name @name end |
#parameters ⇒ Object
Returns the value of attribute parameters.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def parameters @parameters end |
#server_facts ⇒ Object (readonly)
Returns the value of attribute server_facts.
22 23 24 |
# File 'lib/puppet/node.rb', line 22 def server_facts @server_facts end |
#source ⇒ Object
Returns the value of attribute source.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def source @source end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
20 21 22 |
# File 'lib/puppet/node.rb', line 20 def time @time end |
#trusted_data ⇒ Object
Returns the value of attribute trusted_data.
19 20 21 |
# File 'lib/puppet/node.rb', line 19 def trusted_data @trusted_data end |
Class Method Details
.from_data_hash(data) ⇒ Object
36 37 38 39 40 |
# File 'lib/puppet/node.rb', line 36 def self.from_data_hash(data) node = new(name) node.initialize_from_hash(data) node end |
Instance Method Details
#add_server_facts(facts) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/puppet/node.rb', line 152 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/puppet/node.rb', line 52 def environment if @environment @environment else if env = parameters[ENVIRONMENT] 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 # Tt 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/puppet/node.rb', line 72 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? @parameters[ENVIRONMENT] = @environment.name.to_s if @parameters.include?(ENVIRONMENT) self.environment_name = @environment.name if instance_variable_defined?(:@environment_name) end @environment end |
#fact_merge(facts = nil) ⇒ nil
Merge the node facts with parameters from the node source.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/puppet/node.rb', line 124 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 merge(@facts.values) end end |
#has_environment_instance? ⇒ Boolean
87 88 89 |
# File 'lib/puppet/node.rb', line 87 def has_environment_instance? !@environment.nil? end |
#initialize_from_hash(data) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/puppet/node.rb', line 26 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'] env_name = env_name.intern unless env_name.nil? @environment_name = env_name environment = env_name end |
#merge(params) ⇒ Object
Merge any random parameters into our parameter list.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/puppet/node.rb', line 140 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 @parameters[ENVIRONMENT] ||= self.environment.name.to_s end |
#names ⇒ Object
Calculate the list of names we might use for looking up our node. This is only used for AST nodes.
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 |
# File 'lib/puppet/node.rb', line 162 def names return [name] if Puppet.settings[:strict_hostname_checking] names = [] names += split_name(name) if name.include?(".") # First, get the fqdn unless fqdn = parameters["fqdn"] if parameters["hostname"] and parameters["domain"] fqdn = parameters["hostname"] + "." + parameters["domain"] else Puppet.warning _("Host is missing hostname and/or domain: %{name}") % { name: name } end end # Now that we (might) have the fqdn, add each piece to the name # list to search, in order of longest to shortest. names += split_name(fqdn) if fqdn # And make sure the node name is first, since that's the most # likely usage. # The name is usually the Certificate CN, but it can be # set to the 'facter' hostname instead. if Puppet[:node_name] == 'cert' names.unshift name else names.unshift parameters["hostname"] end names.uniq end |
#split_name(name) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/puppet/node.rb', line 194 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
42 43 44 45 46 47 48 49 50 |
# File 'lib/puppet/node.rb', line 42 def to_data_hash result = { 'name' => name, 'environment' => environment.name.to_s, } result['classes'] = classes unless classes.empty? result['parameters'] = parameters unless parameters.empty? result end |