Skip to content

Commit 3a7cf67

Browse files
(PUP-11379) Agent no longer calls local enc script
This commit adds a new setting that allows to choose between using the environment data from the `last_run_summary.yaml` file or just do the node request and ignore all the other environment data.
1 parent c3ddb95 commit 3a7cf67

File tree

3 files changed

+102
-59
lines changed

3 files changed

+102
-59
lines changed

lib/puppet/configurer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def run_internal(options)
304304
Puppet.debug(_("Environment not passed via CLI and no catalog was given, attempting to find out the last server-specified environment"))
305305
initial_environment, loaded_last_environment = last_server_specified_environment
306306

307-
unless loaded_last_environment
307+
unless Puppet[:use_last_environment] && loaded_last_environment
308308
Puppet.debug(_("Requesting environment from the server"))
309309
initial_environment = current_server_specified_environment(@environment, configured_environment, options)
310310
end

lib/puppet/defaults.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,17 @@ def self.initialize_default_settings!(settings)
442442
<https://puppet.com/docs/puppet/latest/environments_about.html>",
443443
:type => :path,
444444
},
445+
:use_last_environment => {
446+
:type => :boolean,
447+
:default => true,
448+
:desc => <<-'EOT'
449+
Puppet saves both the initial and converged environment in the last_run_summary file.
450+
If they differ, and this setting is set to true, we will use the last converged
451+
environment and skip the node request.
452+
453+
When set to false, we will do the node request and ignore the environment data from the last_run_summary file.
454+
EOT
455+
},
445456
:always_retry_plugins => {
446457
:type => :boolean,
447458
:default => true,

spec/unit/configurer_spec.rb

Lines changed: 90 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,88 +1128,120 @@ def expects_neither_new_or_cached_catalog
11281128
converged_environment: #{last_server_specified_environment}
11291129
run_mode: agent
11301130
SUMMARY
1131+
end
11311132

1132-
expect(Puppet::Node.indirection).not_to receive(:find)
1133+
describe "when the use_last_environment is set to true" do
1134+
before do
1135+
expect(Puppet::Node.indirection).not_to receive(:find)
11331136
.with(anything, hash_including(:ignore_cache => true, :fail_on_404 => true))
1134-
end
1137+
end
11351138

1136-
it "prefers the environment set via cli" do
1137-
Puppet.settings.handlearg('--environment', 'usethis')
1138-
configurer.run
1139+
it "prefers the environment set via cli" do
1140+
Puppet.settings.handlearg('--environment', 'usethis')
1141+
configurer.run
11391142

1140-
expect(configurer.environment).to eq('usethis')
1141-
end
1143+
expect(configurer.environment).to eq('usethis')
1144+
end
11421145

1143-
it "prefers the environment set via lastrunfile over config" do
1144-
FileUtils.mkdir_p(Puppet[:confdir])
1145-
set_puppet_conf(Puppet[:confdir], <<~CONF)
1146-
[main]
1147-
environment = usethis
1148-
lastrunfile = #{Puppet[:lastrunfile]}
1149-
CONF
1146+
it "prefers the environment set via lastrunfile over config" do
1147+
FileUtils.mkdir_p(Puppet[:confdir])
1148+
set_puppet_conf(Puppet[:confdir], <<~CONF)
1149+
[main]
1150+
environment = usethis
1151+
lastrunfile = #{Puppet[:lastrunfile]}
1152+
CONF
11501153

1151-
Puppet.initialize_settings
1152-
configurer.run
1154+
Puppet.initialize_settings
1155+
configurer.run
11531156

1154-
expect(configurer.environment).to eq(last_server_specified_environment)
1155-
end
1157+
expect(configurer.environment).to eq(last_server_specified_environment)
1158+
end
11561159

1157-
it "uses the environment from Puppet[:environment] if given a catalog" do
1158-
configurer.run(catalog: catalog)
1160+
it "uses the environment from Puppet[:environment] if given a catalog" do
1161+
configurer.run(catalog: catalog)
11591162

1160-
expect(configurer.environment).to eq(Puppet[:environment])
1161-
end
1163+
expect(configurer.environment).to eq(Puppet[:environment])
1164+
end
11621165

1163-
it "uses the environment from Puppet[:environment] if use_cached_catalog = true" do
1164-
Puppet[:use_cached_catalog] = true
1165-
expects_cached_catalog_only(catalog)
1166-
configurer.run
1166+
it "uses the environment from Puppet[:environment] if use_cached_catalog = true" do
1167+
Puppet[:use_cached_catalog] = true
1168+
expects_cached_catalog_only(catalog)
1169+
configurer.run
11671170

1168-
expect(configurer.environment).to eq(Puppet[:environment])
1169-
end
1171+
expect(configurer.environment).to eq(Puppet[:environment])
1172+
end
11701173

1171-
describe "when the environment is not set via CLI" do
1172-
it "uses the environment found in lastrunfile if the key exists" do
1173-
configurer.run
1174+
describe "when the environment is not set via CLI" do
1175+
it "uses the environment found in lastrunfile if the key exists" do
1176+
configurer.run
11741177

1175-
expect(configurer.environment).to eq(last_server_specified_environment)
1178+
expect(configurer.environment).to eq(last_server_specified_environment)
1179+
end
1180+
1181+
it "pushes the converged environment found in lastrunfile over the existing context" do
1182+
initial_env = Puppet::Node::Environment.remote('production')
1183+
Puppet.push_context(
1184+
current_environment: initial_env,
1185+
loaders: Puppet::Pops::Loaders.new(initial_env, true))
1186+
1187+
expect(Puppet).to receive(:push_context).with(
1188+
hash_including(:current_environment, :loaders),
1189+
"Local node environment #{last_server_specified_environment} for configurer transaction"
1190+
).once.and_call_original
1191+
1192+
configurer.run
1193+
end
1194+
1195+
it "uses the environment from Puppet[:environment] if strict_environment_mode is set" do
1196+
Puppet[:strict_environment_mode] = true
1197+
configurer.run
1198+
1199+
expect(configurer.environment).to eq(Puppet[:environment])
1200+
end
1201+
1202+
it "uses the environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1203+
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1204+
---
1205+
version:
1206+
config: 1624882680
1207+
puppet: 6.24.0
1208+
application:
1209+
initial_environment: development
1210+
converged_environment: development
1211+
run_mode: agent
1212+
SUMMARY
1213+
configurer.run
1214+
1215+
expect(configurer.environment).to eq(Puppet[:environment])
1216+
end
11761217
end
1218+
end
11771219

1178-
it "pushes the converged environment found in lastrunfile over the existing context" do
1179-
initial_env = Puppet::Node::Environment.remote('production')
1180-
Puppet.push_context(
1181-
current_environment: initial_env,
1182-
loaders: Puppet::Pops::Loaders.new(initial_env, true))
1220+
describe "when the use_last_environment setting is set to false" do
1221+
let(:node_environment) { Puppet::Node::Environment.remote(:salam) }
1222+
let(:node) { Puppet::Node.new(Puppet[:node_name_value]) }
11831223

1184-
expect(Puppet).to receive(:push_context).with(
1185-
hash_including(:current_environment, :loaders),
1186-
"Local node environment #{last_server_specified_environment} for configurer transaction"
1187-
).once.and_call_original
1224+
before do
1225+
Puppet[:use_last_environment] = false
1226+
node.environment = node_environment
11881227

1189-
configurer.run
1228+
allow(Puppet::Node.indirection).to receive(:find)
1229+
allow(Puppet::Node.indirection).to receive(:find)
1230+
.with(anything, hash_including(:ignore_cache => true, :fail_on_404 => true))
1231+
.and_return(node)
11901232
end
11911233

1192-
it "uses the environment from Puppet[:environment] if strict_environment_mode is set" do
1193-
Puppet[:strict_environment_mode] = true
1194-
configurer.run
1234+
it "does a node request" do
1235+
expect(Puppet::Node.indirection).to receive(:find)
1236+
.with(anything, hash_including(:ignore_cache => true, :fail_on_404 => true))
11951237

1196-
expect(configurer.environment).to eq(Puppet[:environment])
1238+
configurer.run
11971239
end
11981240

1199-
it "uses the environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1200-
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1201-
---
1202-
version:
1203-
config: 1624882680
1204-
puppet: 6.24.0
1205-
application:
1206-
initial_environment: development
1207-
converged_environment: development
1208-
run_mode: agent
1209-
SUMMARY
1241+
it "uses the node environment from the node request" do
12101242
configurer.run
12111243

1212-
expect(configurer.environment).to eq(Puppet[:environment])
1244+
expect(configurer.environment).to eq(node_environment.name.to_s)
12131245
end
12141246
end
12151247
end

0 commit comments

Comments
 (0)