Skip to content

Commit

Permalink
Add server facts when looking up values
Browse files Browse the repository at this point in the history
During normal catalog compilation, server facts are added by the `compiler`
terminus prior to calling `Puppet::Parser::Compiler.compile`[1]. However, the
lookup application directly calls `Compiler.compile`, bypassing the `compiler`
terminus[2]. Therefore, server facts weren't being added when running the lookup
command.

Ideally, catalog compilation and the lookup command would compile the catalog in
the same way, but changing that is risky. For that to work, we would need to
pass the already resolved node and facts to the `compiler` terminus and the
terminus would need to add server facts to the node. However, the terminus
doesn't add server facts if the node is passed in. It only does that if it
resolves the node using the indirector[3].

Rather than mess with the terminus and break compilation, just load server facts
in the same way that the `compiler` terminus does.

[1] https://github.com/puppetlabs/puppet/blob/8.7.0/lib/puppet/indirector/catalog/compiler.rb#L56
[2] https://github.com/puppetlabs/puppet/blob/8.7.0/lib/puppet/application/lookup.rb#L407
[3] https://github.com/puppetlabs/puppet/blob/8.7.0/lib/puppet/indirector/catalog/compiler.rb#L390
  • Loading branch information
joshcooper committed Jun 12, 2024
1 parent 8068bce commit 719efae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/puppet/application/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative '../../puppet/application'
require_relative '../../puppet/pops'
require_relative '../../puppet/node'
require_relative '../../puppet/node/server_facts'
require_relative '../../puppet/parser/compiler'

class Puppet::Application::Lookup < Puppet::Application
Expand Down Expand Up @@ -403,6 +404,7 @@ def generate_scope
end
end
node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment)
node.add_server_facts(Puppet::Node::ServerFacts.load)
Puppet[:code] = 'undef' unless options[:compile]
compiler = Puppet::Parser::Compiler.new(node)
if options[:node]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ ab: "%{hiera('a')} and %{hiera('b')}"

g: "This is%{facts.cx} in facts hash"

h: "server version is %{server_facts.serverversion}"

lookup_options:
a: first
7 changes: 7 additions & 0 deletions spec/unit/application/lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,13 @@ def run_lookup(lookup)
expect(run_lookup(lookup)).to eql("This is G from facts in facts hash")
end

it 'looks up server facts' do
lookup.options[:node] = node
lookup.options[:render_as] = :s
allow(lookup.command_line).to receive(:args).and_return(['h'])
expect(run_lookup(lookup)).to eql("server version is #{Puppet.version}")
end

describe 'when retrieving given facts' do
before do
lookup.options[:node] = node
Expand Down

0 comments on commit 719efae

Please sign in to comment.