Skip to content

Commit 3f1bbd2

Browse files
authored
Merge pull request #8948 from AriaXLi/pup-11527_puppet_lookup_enc
(PUP-11527) puppet lookup -E doesn't execute ENC
2 parents adcc339 + 4afb802 commit 3f1bbd2

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

lib/puppet/application/lookup.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ def generate_scope
376376
facts = retrieve_node_facts(node, given_facts)
377377
ni = Puppet::Node.indirection
378378
tc = ni.terminus_class
379-
if options[:compile] && !Puppet.settings.set_by_cli?('environment')
379+
if options[:compile]
380380
if tc == :plain
381-
node = ni.find(node, facts: facts)
381+
node = ni.find(node, facts: facts, environment: Puppet[:environment])
382382
else
383383
begin
384384
service = Puppet.runtime[:http]
@@ -390,11 +390,11 @@ def generate_scope
390390
Puppet::SSL::Oids.register_puppet_oids
391391
trusted = Puppet::Context::TrustedInformation.remote(true, facts.values['certname'] || node, Puppet::SSL::Certificate.from_instance(cert))
392392
Puppet.override(trusted_information: trusted) do
393-
node = ni.find(node, facts: facts)
393+
node = ni.find(node, facts: facts, environment: Puppet[:environment])
394394
end
395395
rescue
396396
Puppet.warning _("CA is not available, the operation will continue without using trusted facts.")
397-
node = ni.find(node, facts: facts)
397+
node = ni.find(node, facts: facts, environment: Puppet[:environment])
398398
end
399399
end
400400
else
@@ -405,7 +405,7 @@ def generate_scope
405405
else
406406
node.add_extra_facts(given_facts) if given_facts
407407
end
408-
408+
node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment)
409409
Puppet[:code] = 'undef' unless options[:compile]
410410
compiler = Puppet::Parser::Compiler.new(node)
411411
if options[:node]

spec/integration/application/lookup_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,61 @@ def expect_lookup_with_output(exitcode, out)
9191
expect_lookup_with_output(0, /--- value from per node data/)
9292
end
9393

94+
it "resolves hiera data using a top-level node parameter from enc" do
95+
Puppet.settings[:node_terminus] = 'exec'
96+
enc = tmpfile('enc.sh')
97+
Puppet.settings[:external_nodes] = enc
98+
File.write(File.join(env_dir, env_name, 'hiera.yaml'), <<~YAML)
99+
---
100+
version: 5
101+
hierarchy:
102+
- name: "Node parameters"
103+
data_hash: yaml_data
104+
path: "%{site}.yaml"
105+
YAML
106+
107+
File.write(File.join(env_dir, env_name, 'data', "pdx.yaml"), <<~YAML)
108+
---
109+
key: value
110+
YAML
111+
allow(Puppet::Util::Execution).to receive(:execute).with([enc, fqdn], anything).and_return(<<~YAML)
112+
parameters:
113+
site: pdx
114+
YAML
115+
app.command_line.args << 'key' << '--compile'
116+
Puppet.initialize_settings(['-E', env_name])
117+
expect_lookup_with_output(0, /--- value/)
118+
end
119+
120+
it "prefers the environment specified on the commandline over the enc environment" do
121+
Puppet.settings[:node_terminus] = 'exec'
122+
enc = tmpfile('enc.sh')
123+
Puppet.settings[:external_nodes] = enc
124+
File.write(File.join(env_dir, env_name, 'hiera.yaml'), <<~YAML)
125+
---
126+
version: 5
127+
hierarchy:
128+
- name: "Node parameters"
129+
data_hash: yaml_data
130+
path: "%{site}.yaml"
131+
YAML
132+
133+
File.write(File.join(env_dir, env_name, 'data', "pdx.yaml"), <<~YAML)
134+
---
135+
key: value
136+
YAML
137+
allow(Puppet::Util::Execution).to receive(:execute).with([enc, fqdn], anything).and_return(<<~YAML)
138+
---
139+
# return 'someother' environment because it doesn't have any hiera data
140+
environment: someother
141+
parameters:
142+
site: pdx
143+
YAML
144+
app.command_line.args << 'key' << '--compile'
145+
Puppet.initialize_settings(['-E', env_name])
146+
expect_lookup_with_output(0, /--- value/)
147+
end
148+
94149
it 'loads trusted information from the node certificate' do
95150
Puppet.settings[:node_terminus] = 'exec'
96151
expect_any_instance_of(Puppet::Node::Exec).to receive(:find) do |args|

0 commit comments

Comments
 (0)