Module: PuppetRepl::Support::Node

Included in:
PuppetRepl::Support
Defined in:
lib/puppet-repl/support/node.rb

Instance Method Summary collapse

Instance Method Details

#convert_remote_node(remote_node) ⇒ Object

this is a hack to get around that the puppet node fact face does not return a proper node object with the facts hash populated returns a node object with a proper facts hash



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet-repl/support/node.rb', line 55

def convert_remote_node(remote_node)
  options = {}
  # remove trusted data as it will later get populated during compilation
  parameters = remote_node.parameters.dup
  trusted_data = parameters.delete('trusted')
  options[:parameters] = parameters || {}
  options[:facts] = Puppet::Node::Facts.new(remote_node.name,remote_node.parameters)
  options[:classes] = remote_node.classes
  options[:environment] = puppet_environment
  node_object = Puppet::Node.new(remote_node.name, options)
  node_object.add_server_facts(server_facts) if node_object.respond_to?(:add_server_facts)
  node_object.trusted_data = trusted_data
  node_object
end

#create_nodeObject

creates a node object using defaults or gets the remote node object if the remote_node_name is defined



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/puppet-repl/support/node.rb', line 8

def create_node
  Puppet[:trusted_server_facts] = true if Puppet.version.to_f >= 4.1

  if remote_node_name
    # refetch
    node_obj = set_node_from_name(remote_node_name)
  end
  unless node_obj
    options = {}
    options[:parameters] = default_facts.values
    options[:facts] = default_facts
    options[:classes] = []
    options[:environment] = puppet_environment
    node_obj = Puppet::Node.new(default_facts.values['fqdn'], options)
    node_obj.add_server_facts(server_facts) if node_obj.respond_to?(:add_server_facts)
    node_obj
  end
  node_obj
end

#get_remote_node(name) ⇒ Object



45
46
47
48
49
50
# File 'lib/puppet-repl/support/node.rb', line 45

def get_remote_node(name)
  indirection = Puppet::Indirector::Indirection.instance(:node)
  indirection.terminus_class = 'rest'
  remote_node = indirection.find(name, :environment => puppet_environment)
  remote_node
end

#nodenode

Returns puppet node object.

Returns:

  • (node)

    puppet node object



41
42
43
# File 'lib/puppet-repl/support/node.rb', line 41

def node
  @node ||= create_node
end

#remote_node_nameObject



36
37
38
# File 'lib/puppet-repl/support/node.rb', line 36

def remote_node_name
  @remote_node_name
end

#remote_node_name=(name) ⇒ Object



32
33
34
# File 'lib/puppet-repl/support/node.rb', line 32

def remote_node_name=(name)
  @remote_node_name = name
end

#set_node(value) ⇒ Object



84
85
86
# File 'lib/puppet-repl/support/node.rb', line 84

def set_node(value)
  @node = value
end

#set_node_from_name(name) ⇒ Object

query the remote puppet server and retrieve the node object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet-repl/support/node.rb', line 72

def set_node_from_name(name)
  out_buffer.puts ("Fetching node #{name}")
  remote_node = get_remote_node(name)
  if remote_node and remote_node.parameters.empty?
    remote_node_name = nil  # clear out the remote name
    raise PuppetRepl::Exception::UndefinedNode.new(:name => remote_node.name)
  end
  remote_node_name = remote_node.name
  node_object = convert_remote_node(remote_node)
  set_node(node_object)
end

#set_remote_node_name(name) ⇒ Object



28
29
30
# File 'lib/puppet-repl/support/node.rb', line 28

def set_remote_node_name(name)
  @remote_node_name = name
end