Sha256: 84d200e2b4a6a6480e0c3bd5b2a47b84915ce89e53bcdf7f29eb052df4601b30

Contents?: true

Size: 1.87 KB

Versions: 134

Compression:

Stored size: 1.87 KB

Contents

require 'puppet/node'
require 'puppet/indirector/exec'

class Puppet::Node::Exec < Puppet::Indirector::Exec
  desc "Call an external program to get node information.  See
  the [External Nodes](https://docs.puppetlabs.com/guides/external_nodes.html) page for more information."
  include Puppet::Util

  def command
    command = Puppet[:external_nodes]
    raise ArgumentError, "You must set the 'external_nodes' parameter to use the external node terminus" unless command != "none"
    command.split
  end

  # Look for external node definitions.
  def find(request)
    output = super or return nil

    # Translate the output to ruby.
    result = translate(request.key, output)

    # Set the requested environment if it wasn't overridden
    # If we don't do this it gets set to the local default
    result[:environment] ||= request.environment

    create_node(request.key, result)
  end

  private

  # Proxy the execution, so it's easier to test.
  def execute(command, arguments)
    Puppet::Util::Execution.execute(command,arguments)
  end

  # Turn our outputted objects into a Puppet::Node instance.
  def create_node(name, result)
    node = Puppet::Node.new(name)
    set = false
    [:parameters, :classes, :environment].each do |param|
      if value = result[param]
        node.send(param.to_s + "=", value)
        set = true
      end
    end

    node.fact_merge
    node
  end

  # Translate the yaml string into Ruby objects.
  def translate(name, output)
    YAML.load(output).inject({}) do |hash, data|
      case data[0]
      when String
        hash[data[0].intern] = data[1]
      when Symbol
        hash[data[0]] = data[1]
      else
        raise Puppet::Error, "key is a #{data[0].class}, not a string or symbol"
      end

      hash
    end

  rescue => detail
      raise Puppet::Error, "Could not load external node results for #{name}: #{detail}", detail.backtrace
  end
end

Version data entries

134 entries across 134 versions & 2 rubygems

Version Path
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/indirector/node/exec.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/indirector/node/exec.rb
puppet-4.10.12 lib/puppet/indirector/node/exec.rb
puppet-4.10.12-x86-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.12-x64-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.12-universal-darwin lib/puppet/indirector/node/exec.rb
puppet-4.10.11 lib/puppet/indirector/node/exec.rb
puppet-4.10.11-x86-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.11-x64-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.11-universal-darwin lib/puppet/indirector/node/exec.rb
puppet-4.10.10 lib/puppet/indirector/node/exec.rb
puppet-4.10.10-x86-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.10-x64-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.10-universal-darwin lib/puppet/indirector/node/exec.rb
puppet-retrospec-1.6.1 vendor/pup410/lib/puppet/indirector/node/exec.rb
puppet-retrospec-1.6.0 vendor/pup410/lib/puppet/indirector/node/exec.rb
puppet-4.10.9 lib/puppet/indirector/node/exec.rb
puppet-4.10.9-x86-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.9-x64-mingw32 lib/puppet/indirector/node/exec.rb
puppet-4.10.9-universal-darwin lib/puppet/indirector/node/exec.rb