Class: Puppet::Node

Inherits:
Object show all
Extended by:
Indirector
Includes:
Environment::Helper
Defined in:
lib/vendor/puppet/node.rb,
lib/vendor/puppet/node/environment.rb

Overview

Just define it, so this class has fewer load dependencies.

Defined Under Namespace

Classes: ActiveRecord, Environment, Exec, Facts, Inventory, Ldap, Memory, Plain, Rest, StoreConfigs, Yaml

Constant Summary

Constants included from Indirector

Indirector::BadNameRegexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indirector

configure_routes, indirects

Methods included from Environment::Helper

#environment=

Constructor Details

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

Returns a new instance of Node.

Raises:

  • (ArgumentError)


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

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.



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

def classes
  @classes
end

#ipaddressObject

Returns the value of attribute ipaddress.



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

def ipaddress
  @ipaddress
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#timeObject (readonly)

Returns the value of attribute time.



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

def time
  @time
end

Instance Method Details

#environmentObject



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

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.



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

def fact_merge
    if facts = Puppet::Node::Facts.indirection.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.



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

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.



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
110
# File 'lib/vendor/puppet/node.rb', line 80

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



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

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