Class: Puppet::Node

Inherits:
Object show all
Extended by:
Indirector
Includes:
Environment::Helper
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: ActiveRecord, Environment, Exec, Facts, Ldap, Memory, Plain, Rest, Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indirector

indirects

Methods included from Environment::Helper

#environment=

Constructor Details

#initialize(name, options = {}) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/node.rb', line 34

def initialize(name, options = {})
  raise ArgumentError, "Node names cannot be nil" unless name
  @name = name

  if classes = options[:classes]
    if classes.is_a?(String)
      @classes = [classes]
    else
      @classes = classes
    end
  else
    @classes = []
  end

  @parameters = options[:parameters] || {}

  if env = options[:environment]
    self.environment = env
  end

  @time = Time.now
end

Instance Attribute Details

#classesObject

Returns the value of attribute classes.



19
20
21
# File 'lib/puppet/node.rb', line 19

def classes
  @classes
end

#ipaddressObject

Returns the value of attribute ipaddress.



19
20
21
# File 'lib/puppet/node.rb', line 19

def ipaddress
  @ipaddress
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/puppet/node.rb', line 19

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



19
20
21
# File 'lib/puppet/node.rb', line 19

def parameters
  @parameters
end

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/puppet/node.rb', line 19

def source
  @source
end

#timeObject (readonly)

Returns the value of attribute time.



20
21
22
# File 'lib/puppet/node.rb', line 20

def time
  @time
end

Instance Method Details

#environmentObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/node.rb', line 22

def environment
  return super if @environment

  if env = parameters["environment"]
    self.environment = env
    return super
  end

  # Else, return the default
  Puppet::Node::Environment.new
end

#fact_mergeObject

Merge the node facts with parameters from the node source.



58
59
60
61
62
63
64
65
66
# File 'lib/puppet/node.rb', line 58

def fact_merge
    if facts = Puppet::Node::Facts.find(name)
      merge(facts.values)
    end
rescue => detail
    error = Puppet::Error.new("Could not retrieve facts for #{name}: #{detail}")
    error.set_backtrace(detail.backtrace)
    raise error
end

#merge(params) ⇒ Object

Merge any random parameters into our parameter list.



69
70
71
72
73
74
75
# File 'lib/puppet/node.rb', line 69

def merge(params)
  params.each do |name, value|
    @parameters[name] = value unless @parameters.include?(name)
  end

  @parameters["environment"] ||= self.environment.name.to_s if self.environment
end

#namesObject

Calculate the list of names we might use for looking up our node. This is only used for AST nodes.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/node.rb', line 79

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}"
    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



111
112
113
114
115
116
117
118
# File 'lib/puppet/node.rb', line 111

def split_name(name)
  list = name.split(".")
  tmp = []
  list.each_with_index do |short, i|
    tmp << list[0..i].join(".")
  end
  tmp.reverse
end