class Puppet::Node

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

A class for managing nodes, including their facts and environment.

Public Instance Methods

environment() click to toggle source
# File lib/puppet/node.rb, line 59
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) click to toggle source
# File lib/puppet/node.rb, line 79
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 if @parameters.include?(ENVIRONMENT)
    self.environment_name = @environment.name if instance_variable_defined?(:@environment_name)
  end
  @environment
end
fact_merge() click to toggle source

Merge the node facts with parameters from the node source.

# File lib/puppet/node.rb, line 124
def fact_merge
  if @facts = Puppet::Node::Facts.indirection.find(name, :environment => environment)
    @facts.sanitize
    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
has_environment_instance?() click to toggle source
# File lib/puppet/node.rb, line 94
def has_environment_instance?
  !@environment.nil?
end
merge(params) click to toggle source

Merge any random parameters into our parameter list.

# File lib/puppet/node.rb, line 136
def merge(params)
  params.each do |name, value|
    @parameters[name] = value unless @parameters.include?(name)
  end

  @parameters[ENVIRONMENT] ||= self.environment.name.to_s
end
names() click to toggle source

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, line 146
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) click to toggle source
# File lib/puppet/node.rb, line 178
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() click to toggle source
# File lib/puppet/node.rb, line 38
def to_data_hash
  result = {
    'name' => name,
    'environment' => environment.name,
  }
  result['classes'] = classes unless classes.empty?
  result['parameters'] = parameters unless parameters.empty?
  result
end
to_pson(*args) click to toggle source
# File lib/puppet/node.rb, line 55
def to_pson(*args)
  to_pson_data_hash.to_pson(*args)
end
to_pson_data_hash(*args) click to toggle source
# File lib/puppet/node.rb, line 48
def to_pson_data_hash(*args)
  {
    'document_type' => "Node",
    'data' =>  to_data_hash,
  }
end
trusted_data=(data) click to toggle source

Ensures the data is frozen

# File lib/puppet/node.rb, line 189
def trusted_data=(data)
  Puppet.warning("Trusted node data modified for node #{name}") unless @trusted_data.nil?
  @trusted_data = data.freeze
end

Public Class Methods

from_data_hash(data) click to toggle source
# File lib/puppet/node.rb, line 23
def self.from_data_hash(data)
  raise ArgumentError, "No name provided in serialized data" unless name = data['name']

  node = new(name)
  node.classes = data['classes']
  node.parameters = data['parameters']
  node.environment_name = data['environment']
  node
end
from_pson(pson) click to toggle source
# File lib/puppet/node.rb, line 33
def self.from_pson(pson)
  Puppet.deprecation_warning("from_pson is being removed in favour of from_data_hash.")
  self.from_data_hash(pson)
end
new(name, options = {}) click to toggle source
# File lib/puppet/node.rb, line 98
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] || {}

  @facts = options[:facts]

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

  @time = Time.now
end