Skip to content

Commit 075162f

Browse files
authored
Revert "(PUP-10216) Use last environment instead of requesting the node's environment"
1 parent 93c17e0 commit 075162f

File tree

5 files changed

+103
-182
lines changed

5 files changed

+103
-182
lines changed

lib/puppet/configurer.rb

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def retrieve_catalog(facts, query_options)
9191

9292
if result
9393
# don't use use cached catalog if it doesn't match server specified environment
94-
if result.environment != @environment
94+
if @node_environment && result.environment != @environment
9595
Puppet.err _("Not using cached catalog because its environment '%{catalog_env}' does not match '%{local_env}'") % { catalog_env: result.environment, local_env: @environment }
9696
return nil
9797
end
@@ -296,11 +296,50 @@ def run_internal(options)
296296
configured_environment = Puppet[:environment] if Puppet.settings.set_by_config?(:environment)
297297

298298
# We only need to find out the environment to run in if we don't already have a catalog
299-
unless (cached_catalog || options[:catalog] || configured_environment)
300-
Puppet.debug(_("No environment configured, attempting to find out the last used environment"))
301-
if (env = last_used_environment)
302-
@environment = env
303-
report.environment = env
299+
unless (cached_catalog || options[:catalog] || Puppet[:strict_environment_mode])
300+
begin
301+
node = nil
302+
node_retr_time = thinmark do
303+
node = Puppet::Node.indirection.find(Puppet[:node_name_value],
304+
:environment => Puppet::Node::Environment.remote(@environment),
305+
:configured_environment => configured_environment,
306+
:ignore_cache => true,
307+
:transaction_uuid => @transaction_uuid,
308+
:fail_on_404 => true)
309+
end
310+
options[:report].add_times(:node_retrieval, node_retr_time)
311+
312+
if node
313+
# If we have deserialized a node from a rest call, we want to set
314+
# an environment instance as a simple 'remote' environment reference.
315+
if !node.has_environment_instance? && node.environment_name
316+
node.environment = Puppet::Node::Environment.remote(node.environment_name)
317+
end
318+
319+
@node_environment = node.environment.to_s
320+
321+
if node.environment.to_s != @environment
322+
Puppet.notice _("Local environment: '%{local_env}' doesn't match server specified node environment '%{node_env}', switching agent to '%{node_env}'.") % { local_env: @environment, node_env: node.environment }
323+
@environment = node.environment.to_s
324+
report.environment = @environment
325+
query_options = nil
326+
facts = nil
327+
328+
new_env = Puppet::Node::Environment.remote(@environment)
329+
Puppet.push_context(
330+
{
331+
current_environment: new_env,
332+
loaders: Puppet::Pops::Loaders.new(new_env, true)
333+
},
334+
"Local node environment #{@environment} for configurer transaction"
335+
)
336+
else
337+
Puppet.info _("Using configured environment '%{env}'") % { env: @environment }
338+
end
339+
end
340+
rescue StandardError => detail
341+
Puppet.warning(_("Unable to fetch my node definition, but the agent run will continue:"))
342+
Puppet.warning(detail)
304343
end
305344
end
306345

@@ -319,6 +358,7 @@ def run_internal(options)
319358

320359
query_options, facts = get_facts(options) unless query_options
321360
query_options[:configured_environment] = configured_environment
361+
options[:convert_for_node] = node
322362

323363
catalog = prepare_and_retrieve_catalog(cached_catalog, facts, options, query_options)
324364
unless catalog
@@ -432,22 +472,6 @@ def find_functional_server
432472
end
433473
private :find_functional_server
434474

435-
def last_used_environment
436-
if Puppet::FileSystem.exist?(Puppet[:lastrunfile])
437-
last_run_environment = Puppet::Util::Yaml.safe_load_file(Puppet[:lastrunfile]).dig('version', 'environment') rescue nil
438-
end
439-
440-
if !last_run_environment && Puppet::FileSystem.exist?(Puppet[:lastrunreport])
441-
last_run_environment = Puppet::Util::Yaml.safe_load_file(Puppet[:lastrunreport], [Puppet::Transaction::Report]).environment
442-
end
443-
Puppet.debug(_("Found last used environment: %{environment}") % { environment: last_run_environment }) if last_run_environment
444-
last_run_environment
445-
rescue => detail
446-
Puppet.debug(_("Unable to get last used environment: %{detail}") % { detail: detail })
447-
nil
448-
end
449-
private :last_used_environment
450-
451475
def send_report(report)
452476
puts report.summary if Puppet[:summarize]
453477
save_last_run_summary(report)

lib/puppet/transaction/report.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,7 @@ def summary
377377
# @api public
378378
#
379379
def raw_summary
380-
report = {
381-
"version" => {
382-
"config" => configuration_version,
383-
"environment" => environment,
384-
"puppet" => Puppet.version
385-
}
386-
}
380+
report = { "version" => { "config" => configuration_version, "puppet" => Puppet.version } }
387381

388382
@metrics.each do |name, metric|
389383
key = metric.name.to_s

spec/fixtures/unit/configurer/last_run_report.yaml

Lines changed: 0 additions & 56 deletions
This file was deleted.

spec/integration/application/agent_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@
572572
).and matching(
573573
/Notify\[a message\]\/message:/
574574
)).to_stdout
575-
.and output(/Could not retrieve catalog from remote server/).to_stderr
575+
.and output(/the agent run will continue/).to_stderr
576576
end
577577

578578
it 'preserves the old cached catalog if validation fails with the old one' do
@@ -591,7 +591,7 @@
591591
agent.command_line.args << '--test'
592592
agent.run
593593
}.to exit_with(1)
594-
.and output(/Retrieving plugin/).to_stdout
594+
.and output(/Using configured environment/).to_stdout
595595
.and output(%r{Validation of Exec\[unqualified_command\] failed: 'unqualified_command' is not qualified and no path was specified}).to_stderr
596596
end
597597

spec/unit/configurer_spec.rb

Lines changed: 54 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
configurer.run(:pluginsync => false)
7979
end
8080

81+
it "should carry on when it can't fetch its node definition" do
82+
error = Net::HTTPError.new(400, 'dummy server communication error')
83+
expect(Puppet::Node.indirection).to receive(:find).and_raise(error)
84+
expect(configurer.run).to eq(0)
85+
end
86+
8187
it "fails the run if pluginsync fails when usecacheonfailure is false" do
8288
Puppet[:ignore_plugin_errors] = false
8389

@@ -119,6 +125,7 @@
119125
it "applies a cached catalog when it can't connect to the master" do
120126
error = Errno::ECONNREFUSED.new('Connection refused - connect(2)')
121127

128+
expect(Puppet::Node.indirection).to receive(:find).and_raise(error)
122129
expect(Puppet::Resource::Catalog.indirection).to receive(:find).with(anything, hash_including(:ignore_cache => true)).and_raise(error)
123130
expect(Puppet::Resource::Catalog.indirection).to receive(:find).with(anything, hash_including(:ignore_terminus => true)).and_return(catalog)
124131

@@ -546,6 +553,24 @@ def method_missing(*args)
546553
end
547554
end
548555

556+
describe "when requesting a node" do
557+
it "uses the transaction uuid in the request" do
558+
expect(Puppet::Node.indirection).to receive(:find).with(anything, hash_including(transaction_uuid: anything)).twice
559+
configurer.run
560+
end
561+
562+
it "sends an explicitly configured environment request" do
563+
expect(Puppet.settings).to receive(:set_by_config?).with(:environment).and_return(true)
564+
expect(Puppet::Node.indirection).to receive(:find).with(anything, hash_including(configured_environment: Puppet[:environment])).twice
565+
configurer.run
566+
end
567+
568+
it "does not send a configured_environment when using the default" do
569+
expect(Puppet::Node.indirection).to receive(:find).with(anything, hash_including(configured_environment: nil)).twice
570+
configurer.run
571+
end
572+
end
573+
549574
def expects_pluginsync
550575
metadata = "[{\"path\":\"/etc/puppetlabs/code\",\"relative_path\":\".\",\"links\":\"follow\",\"owner\":0,\"group\":0,\"mode\":420,\"checksum\":{\"type\":\"ctime\",\"value\":\"{ctime}2020-07-10 14:00:00 -0700\"},\"type\":\"directory\",\"destination\":null}]"
551576
stub_request(:get, %r{/puppet/v3/file_metadatas/(plugins|locales)}).to_return(status: 200, body: metadata, headers: {'Content-Type' => 'application/json'})
@@ -597,13 +622,21 @@ def expects_neither_new_or_cached_catalog
597622
configurer.run
598623
end
599624

600-
it "should not pluginsync when a cached catalog is successfully retrieved" do
625+
it "should not make a node request or pluginsync when a cached catalog is successfully retrieved" do
626+
expect(Puppet::Node.indirection).not_to receive(:find)
601627
expects_cached_catalog_only(catalog)
602628
expect(configurer).not_to receive(:download_plugins)
603629

604630
configurer.run
605631
end
606632

633+
it "should make a node request and pluginsync when a cached catalog cannot be retrieved" do
634+
expect(Puppet::Node.indirection).to receive(:find).and_return(nil)
635+
expects_fallback_to_new_catalog(catalog)
636+
637+
configurer.run
638+
end
639+
607640
it "should set its cached_catalog_status to 'explicitly_requested'" do
608641
expects_cached_catalog_only(catalog)
609642

@@ -635,6 +668,7 @@ def expects_neither_new_or_cached_catalog
635668
end
636669

637670
it "should not attempt to retrieve a cached catalog again if the first attempt failed" do
671+
expect(Puppet::Node.indirection).to receive(:find).and_return(nil)
638672
expects_neither_new_or_cached_catalog
639673
expects_pluginsync
640674

@@ -690,6 +724,16 @@ def expects_neither_new_or_cached_catalog
690724
Puppet.settings[:strict_environment_mode] = true
691725
end
692726

727+
it "should not make a node request" do
728+
stub_request(:get, %r{/puppet/v3/file_metadatas?/plugins}).to_return(:status => 404)
729+
stub_request(:get, %r{/puppet/v3/file_metadatas?/pluginfacts}).to_return(:status => 404)
730+
expects_new_catalog_only(catalog)
731+
732+
expect(Puppet::Node.indirection).not_to receive(:find)
733+
734+
configurer.run
735+
end
736+
693737
it "should return nil when the catalog's environment doesn't match the agent specified environment" do
694738
Puppet[:environment] = 'second_env'
695739
configurer = Puppet::Configurer.new
@@ -1063,105 +1107,20 @@ def expects_neither_new_or_cached_catalog
10631107
expect(configurer.run(options)).to eq(0)
10641108
expect(options[:report].server_used).to be_nil
10651109
end
1066-
end
1067-
1068-
describe "when selecting an environment" do
1069-
include PuppetSpec::Files
1070-
include PuppetSpec::Settings
1071-
1072-
describe "when the last used environment is available" do
1073-
let(:last_used_environment) { 'development' }
1074-
let(:default_environment) { 'production' }
1075-
1076-
before do
1077-
Puppet[:lastrunreport] = my_fixture('last_run_report.yaml')
1078-
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1079-
---
1080-
version:
1081-
config: 1624882680
1082-
environment: development
1083-
puppet: 6.24.0
1084-
SUMMARY
1085-
end
10861110

1087-
it "prefers the environment set via cli" do
1088-
Puppet.settings.handlearg('--environment', 'usethis')
1089-
configurer.run
1090-
1091-
expect(configurer.environment).to eq('usethis')
1092-
end
1093-
1094-
it "prefers the environment set via config" do
1095-
FileUtils.mkdir_p(Puppet[:confdir])
1096-
set_puppet_conf(Puppet[:confdir], <<~CONF)
1097-
[main]
1098-
environment = usethis
1099-
CONF
1100-
1101-
Puppet.initialize_settings
1102-
configurer.run
1103-
1104-
expect(configurer.environment).to eq('usethis')
1105-
end
1106-
1107-
it "uses the default environment if given a catalog" do
1108-
configurer.run(catalog: catalog)
1109-
1110-
expect(configurer.environment).to eq(default_environment)
1111-
end
1112-
1113-
it "uses the default environment if use_cached_catalog = true" do
1114-
Puppet[:use_cached_catalog] = true
1115-
expects_cached_catalog_only(catalog)
1116-
configurer.run
1117-
1118-
expect(configurer.environment).to eq(default_environment)
1119-
end
1120-
1121-
describe "when the environment is not configured" do
1122-
it "uses the environment found in lastrunfile if the key exists" do
1123-
configurer.run
1124-
1125-
expect(configurer.environment).to eq(last_used_environment)
1126-
end
1127-
1128-
it "falls back to the environment from lastrunreport if lastrunfile does not contain the environment" do
1129-
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1130-
---
1131-
version:
1132-
config: 1624882680
1133-
puppet: 6.24.0
1134-
SUMMARY
1135-
configurer.run
1136-
1137-
expect(configurer.environment).to eq(last_used_environment)
1138-
end
1139-
1140-
it "falls back to the environment from lastrunreport if lastrunfile is invalid YAML" do
1141-
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1142-
Key: 'this is my very very very ' +
1143-
'long string'
1144-
SUMMARY
1145-
configurer.run
1146-
1147-
expect(configurer.environment).to eq(last_used_environment)
1148-
end
1111+
it "should not make multiple node requests when the server is found" do
1112+
Puppet.settings[:server_list] = ["myserver:123"]
11491113

1150-
it "falls back to the environment from lastrunreport if lastrunfile exists but is empty" do
1151-
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', '')
1152-
configurer.run
1114+
Puppet::Node.indirection.terminus_class = :rest
1115+
Puppet::Resource::Catalog.indirection.terminus_class = :rest
11531116

1154-
expect(configurer.environment).to eq(last_used_environment)
1155-
end
1117+
stub_request(:get, 'https://myserver:123/status/v1/simple/master').to_return(status: 200)
1118+
stub_request(:post, %r{https://myserver:123/puppet/v3/catalog}).to_return(status: 200)
1119+
node_request = stub_request(:get, %r{https://myserver:123/puppet/v3/node/}).to_return(status: 200)
11561120

1157-
it "uses the default environment if the last used one cannot be found" do
1158-
Puppet[:lastrunfile] = tmpfile('last_run_summary.yaml')
1159-
Puppet[:lastrunreport] = tmpfile('last_run_report.yaml')
1160-
configurer.run
1121+
configurer.run
11611122

1162-
expect(configurer.environment).to eq(default_environment)
1163-
end
1164-
end
1123+
expect(node_request).to have_been_requested.once
11651124
end
11661125
end
11671126
end

0 commit comments

Comments
 (0)