Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 7.x] Add server facts when looking up values #9399

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/puppet/application/lookup.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,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
@@ -406,6 +407,7 @@ def generate_scope
node.add_extra_facts(given_facts) if given_facts
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]
38 changes: 2 additions & 36 deletions lib/puppet/indirector/catalog/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative '../../../puppet/environments'
require_relative '../../../puppet/node'
require_relative '../../../puppet/node/server_facts'
require_relative '../../../puppet/resource/catalog'
require_relative '../../../puppet/indirector/code'
require_relative '../../../puppet/util/profiler'
@@ -425,41 +426,6 @@ def node_from_request(facts, request)
#
# See also set_server_facts in Puppet::Server::Compiler in puppetserver.
def set_server_facts
@server_facts = {}

# Add our server Puppet Enterprise version, if available.
pe_version_file = '/opt/puppetlabs/server/pe_version'
if File.readable?(pe_version_file) and !File.zero?(pe_version_file)
@server_facts['pe_serverversion'] = File.read(pe_version_file).chomp
end

# Add our server version to the fact list
@server_facts["serverversion"] = Puppet.version.to_s

# And then add the server name and IP
{"servername" => "fqdn",
"serverip" => "ipaddress",
"serverip6" => "ipaddress6"
}.each do |var, fact|
value = Puppet.runtime[:facter].value(fact)
if !value.nil?
@server_facts[var] = value
end
end

if @server_facts["servername"].nil?
host = Puppet.runtime[:facter].value(:hostname)
if host.nil?
Puppet.warning _("Could not retrieve fact servername")
elsif domain = Puppet.runtime[:facter].value(:domain) #rubocop:disable Lint/AssignmentInCondition
@server_facts["servername"] = [host, domain].join(".")
else
@server_facts["servername"] = host
end
end

if @server_facts["serverip"].nil? && @server_facts["serverip6"].nil?
Puppet.warning _("Could not retrieve either serverip or serverip6 fact")
end
@server_facts = Puppet::Node::ServerFacts.load
end
end
43 changes: 43 additions & 0 deletions lib/puppet/node/server_facts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

class Puppet::Node::ServerFacts
def self.load
server_facts = {}

# Add our server Puppet Enterprise version, if available.
pe_version_file = '/opt/puppetlabs/server/pe_version'
if File.readable?(pe_version_file) and !File.zero?(pe_version_file)
server_facts['pe_serverversion'] = File.read(pe_version_file).chomp
end

# Add our server version to the fact list
server_facts["serverversion"] = Puppet.version.to_s

# And then add the server name and IP
{"servername" => "fqdn",
"serverip" => "ipaddress",
"serverip6" => "ipaddress6"}.each do |var, fact|
value = Puppet.runtime[:facter].value(fact)
if !value.nil?
server_facts[var] = value
end
end

if server_facts["servername"].nil?
host = Puppet.runtime[:facter].value(:hostname)
if host.nil?
Puppet.warning _("Could not retrieve fact servername")
elsif domain = Puppet.runtime[:facter].value(:domain) #rubocop:disable Lint/AssignmentInCondition
server_facts["servername"] = [host, domain].join(".")
else
server_facts["servername"] = host
end
end

if server_facts["serverip"].nil? && server_facts["serverip6"].nil?
Puppet.warning _("Could not retrieve either serverip or serverip6 fact")
end

server_facts
end
end
Original file line number Diff line number Diff line change
@@ -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
@@ -543,6 +543,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