# class Node

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

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

### Public Class Methods

from_pson(pson) click to toggle source

    # File lib/puppet/node.rb, line 24
    def self.from_pson(pson)
      raise ArgumentError, "No name provided in pson data" unless name = pson['name']

      node = new(name)
      node.classes = pson['classes']
      node.parameters = pson['parameters']
      node.environment = pson['environment']
      node
    end

new(name, options = {}) click to toggle source

    # File lib/puppet/node.rb, line 59
    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

### Public Instance Methods

environment() click to toggle source
Calls superclass method Object#environment

    # File lib/puppet/node.rb, line 47
    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_merge() click to toggle source

Merge the node facts with parameters from the node source.

    # File lib/puppet/node.rb, line 85
    def fact_merge
      if @facts = Puppet::Node::Facts.indirection.find(name, :environment => environment)
        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) click to toggle source

Merge any random parameters into our parameter list.

    # File lib/puppet/node.rb, line 96
    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 106
    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 138
    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_pson(*args) click to toggle source

    # File lib/puppet/node.rb, line 34
    def to_pson(*args)
      result = {
        'document_type' => "Node",
        'data' => {}
      }
      result['data']['name'] = name
      result['data']['classes'] = classes unless classes.empty?
      result['data']['parameters'] = parameters unless parameters.empty?
      result['data']['environment'] = environment.name

      result.to_pson(*args)
    end