Skip to content

Commit c8aa8b2

Browse files
(maint) Merge up c13689d to main
Generated by CI * commit 'c13689d9dfda6e125d0d66c5ab278bb2e530dd22': (PUP-11201) fix test to use puppet helper (packaging) Updating the puppet.pot file (PUP-11201) Refine when the last used environment is used (maint) Fix a bug with the logger in clean.rb
2 parents 285d89b + c13689d commit c8aa8b2

File tree

5 files changed

+172
-31
lines changed

5 files changed

+172
-31
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test_name "Agent should use set environment after running with specified environment" do
2+
require 'puppet/acceptance/environment_utils'
3+
extend Puppet::Acceptance::EnvironmentUtils
4+
5+
tag 'audit:high',
6+
'server'
7+
8+
# Remove all traces of the last used environment
9+
teardown do
10+
agents.each do |agent|
11+
on(agent, puppet('config print lastrunfile')) do |command_result|
12+
agent.rm_rf(command_result.stdout)
13+
end
14+
end
15+
end
16+
17+
tmp_environment = mk_tmp_environment_with_teardown(master, 'special')
18+
agents.each do |agent|
19+
on(agent, puppet("agent -t --environment #{tmp_environment}")) do |result|
20+
assert_match(/Info: Using environment 'special_\w+'/, result.stdout)
21+
end
22+
23+
on(agent, puppet('agent -t')) do |result|
24+
assert_match(/Info: Using environment 'production'/, result.stdout)
25+
end
26+
end
27+
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
test_name "Agent should use the last server-specified environment if server is authoritative" do
2+
require 'puppet/acceptance/environment_utils'
3+
extend Puppet::Acceptance::EnvironmentUtils
4+
5+
tag 'audit:high',
6+
'server'
7+
8+
# Remove all traces of the last used environment
9+
teardown do
10+
agents.each do |agent|
11+
on(agent, puppet('config print lastrunfile')) do |command_result|
12+
agent.rm_rf(command_result.stdout)
13+
end
14+
end
15+
end
16+
17+
testdir = create_tmpdir_for_user(master, 'use_enc_env')
18+
19+
create_remote_file(master, "#{testdir}/enc.rb", <<END)
20+
#!#{master['privatebindir'] || '/opt/puppetlabs/puppet/bin'}/ruby
21+
puts <<YAML
22+
parameters:
23+
environment: special
24+
YAML
25+
END
26+
on(master, "chmod 755 '#{testdir}/enc.rb'")
27+
28+
apply_manifest_on(master, <<-MANIFEST, :catch_failures => true)
29+
File {
30+
ensure => directory,
31+
mode => "0770",
32+
owner => #{master.puppet['user']},
33+
group => #{master.puppet['group']},
34+
}
35+
file {
36+
'#{testdir}/environments':;
37+
'#{testdir}/environments/production':;
38+
'#{testdir}/environments/production/manifests':;
39+
'#{testdir}/environments/special/':;
40+
'#{testdir}/environments/special/manifests':;
41+
}
42+
file { '#{testdir}/environments/production/manifests/site.pp':
43+
ensure => file,
44+
mode => "0640",
45+
content => 'notify { "production environment": }',
46+
}
47+
file { '#{testdir}/environments/special/manifests/different.pp':
48+
ensure => file,
49+
mode => "0640",
50+
content => 'notify { "special environment": }',
51+
}
52+
MANIFEST
53+
54+
master_opts = {
55+
'main' => {
56+
'environmentpath' => "#{testdir}/environments",
57+
},
58+
}
59+
master_opts['master'] = {
60+
'node_terminus' => 'exec',
61+
'external_nodes' => "#{testdir}/enc.rb",
62+
} if !master.is_pe?
63+
64+
with_puppet_running_on(master, master_opts, testdir) do
65+
agents.each do |agent|
66+
run_agent_on(agent, '--no-daemonize --onetime --verbose') do |result|
67+
assert_match(/Info: Using environment 'production'/, result.stdout)
68+
assert_match(/Local environment: 'production' doesn't match server specified environment 'special', restarting agent run with environment 'special'/, result.stdout)
69+
assert_match(/Notice: special environment/, result.stdout)
70+
end
71+
72+
run_agent_on(agent, '--no-daemonize --onetime --verbose') do |result|
73+
assert_match(/Info: Using environment 'special'/, result.stdout)
74+
assert_match(/Notice: special environment/, result.stdout)
75+
end
76+
end
77+
end
78+
end

lib/puppet/configurer.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ def run(options = {})
345345

346346
def run_internal(options)
347347
report = options[:report]
348+
report.initial_environment = Puppet[:environment]
348349

349350
if options[:start_time]
350351
startup_time = Time.now - options[:start_time]
@@ -384,14 +385,18 @@ def run_internal(options)
384385
configured_environment = Puppet[:environment] if Puppet.settings.set_by_config?(:environment)
385386

386387
# We only need to find out the environment to run in if we don't already have a catalog
387-
unless (cached_catalog || options[:catalog] || configured_environment)
388-
Puppet.debug(_("No environment configured, attempting to find out the last used environment"))
389-
if last_agent_environment
390-
@environment = last_agent_environment
391-
report.environment = last_agent_environment
388+
unless (cached_catalog || options[:catalog] || Puppet.settings.set_by_cli?(:environment) || Puppet[:strict_environment_mode])
389+
Puppet.debug(_("Environment not passed via CLI and no catalog was given, attempting to find out the last server-specified environment"))
390+
if last_server_specified_environment
391+
@environment = last_server_specified_environment
392+
report.environment = last_server_specified_environment
393+
else
394+
Puppet.debug(_("Could not find a usable environment in the lastrunfile. Either the file does not exist, does not have the required keys, or the values of 'initial_environment' and 'converged_environment' are identical."))
392395
end
393396
end
394397

398+
Puppet.info _("Using environment '%{env}'") % { env: @environment }
399+
395400
# This is to maintain compatibility with anyone using this class
396401
# aside from agent, apply, device.
397402
unless Puppet.lookup(:loaders) { nil }
@@ -555,21 +560,23 @@ def find_functional_server
555560
end
556561
private :find_functional_server
557562

558-
def last_agent_environment
559-
return @last_agent_environment if @last_agent_environment
563+
def last_server_specified_environment
564+
return @last_server_specified_environment if @last_server_specified_environment
560565
if Puppet::FileSystem.exist?(Puppet[:lastrunfile])
561566
summary = Puppet::Util::Yaml.safe_load_file(Puppet[:lastrunfile])
562567
return unless summary.dig('application', 'run_mode') == 'agent'
563-
@last_agent_environment = summary.dig('application', 'environment')
568+
initial_environment = summary.dig('application', 'initial_environment')
569+
converged_environment = summary.dig('application', 'converged_environment')
570+
@last_server_specified_environment = converged_environment if initial_environment != converged_environment
564571
end
565572

566-
Puppet.debug(_("Found last used environment: %{environment}") % { environment: @last_agent_environment }) if @last_agent_environment
567-
@last_agent_environment
573+
Puppet.debug(_("Found last server-specified environment: %{environment}") % { environment: @last_server_specified_environment }) if @last_server_specified_environment
574+
@last_server_specified_environment
568575
rescue => detail
569-
Puppet.debug(_("Unable to get last used environment: %{detail}") % { detail: detail })
576+
Puppet.debug(_("Could not find last server-specified environment: %{detail}") % { detail: detail })
570577
nil
571578
end
572-
private :last_agent_environment
579+
private :last_server_specified_environment
573580

574581
def send_report(report)
575582
puts report.summary if Puppet[:summarize]

lib/puppet/transaction/report.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Puppet::Transaction::Report
7575
# @return [String] the environment name
7676
attr_accessor :environment
7777

78+
# The name of the environment the agent initially started in
79+
# @return [String] the environment name
80+
attr_accessor :initial_environment
81+
7882
# Whether there are changes that we decided not to apply because of noop
7983
# @return [Boolean]
8084
#
@@ -382,7 +386,8 @@ def raw_summary
382386
},
383387
"application" => {
384388
"run_mode" => Puppet.run_mode.name.to_s,
385-
"environment" => environment
389+
"initial_environment" => initial_environment,
390+
"converged_environment" => environment
386391
}
387392
}
388393

spec/unit/configurer_spec.rb

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,7 @@ def expects_neither_new_or_cached_catalog
12161216
include PuppetSpec::Settings
12171217

12181218
describe "when the last used environment is available" do
1219-
let(:last_used_environment) { 'development' }
1220-
let(:default_environment) { 'production' }
1219+
let(:last_server_specified_environment) { 'development' }
12211220

12221221
before do
12231222
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
@@ -1226,7 +1225,8 @@ def expects_neither_new_or_cached_catalog
12261225
config: 1624882680
12271226
puppet: 6.24.0
12281227
application:
1229-
environment: development
1228+
initial_environment: #{Puppet[:environment]}
1229+
converged_environment: #{last_server_specified_environment}
12301230
run_mode: agent
12311231
SUMMARY
12321232
end
@@ -1251,64 +1251,88 @@ def expects_neither_new_or_cached_catalog
12511251
expect(configurer.environment).to eq('usethis')
12521252
end
12531253

1254-
it "uses the default environment if given a catalog" do
1254+
it "uses environment from Puppet[:environment] if given a catalog" do
12551255
configurer.run(catalog: catalog)
12561256

1257-
expect(configurer.environment).to eq(default_environment)
1257+
expect(configurer.environment).to eq(Puppet[:environment])
12581258
end
12591259

1260-
it "uses the default environment if use_cached_catalog = true" do
1260+
it "uses environment from Puppet[:environment] if use_cached_catalog = true" do
12611261
Puppet[:use_cached_catalog] = true
12621262
expects_cached_catalog_only(catalog)
12631263
configurer.run
12641264

1265-
expect(configurer.environment).to eq(default_environment)
1265+
expect(configurer.environment).to eq(Puppet[:environment])
12661266
end
12671267

1268-
describe "when the environment is not configured" do
1268+
describe "when the environment is not set via CLI" do
12691269
it "uses the environment found in lastrunfile if the key exists" do
12701270
configurer.run
12711271

1272-
expect(configurer.environment).to eq(last_used_environment)
1272+
expect(configurer.environment).to eq(last_server_specified_environment)
12731273
end
12741274

1275-
it "uses the default environment if the run mode doesn't match" do
1275+
it "uses environment from Puppet[:environment] if strict_environment_mode is set" do
1276+
Puppet[:strict_environment_mode] = true
1277+
configurer.run
1278+
1279+
expect(configurer.environment).to eq(Puppet[:environment])
1280+
end
1281+
1282+
it "uses environment from Puppet[:environment] if initial_environment is the same as converged_environment" do
1283+
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
1284+
---
1285+
version:
1286+
config: 1624882680
1287+
puppet: 6.24.0
1288+
application:
1289+
initial_environment: development
1290+
converged_environment: development
1291+
run_mode: agent
1292+
SUMMARY
1293+
configurer.run
1294+
1295+
expect(configurer.environment).to eq(Puppet[:environment])
1296+
end
1297+
1298+
it "uses environment from Puppet[:environment] if the run mode doesn't match" do
12761299
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
12771300
---
12781301
version:
12791302
config: 1624882680
12801303
puppet: 6.24.0
12811304
application:
1282-
environment: development
1305+
initial_environment: #{Puppet[:environment]}
1306+
converged_environment: #{last_server_specified_environment}
12831307
run_mode: user
12841308
SUMMARY
12851309
configurer.run
12861310

1287-
expect(configurer.environment).to eq(default_environment)
1311+
expect(configurer.environment).to eq(Puppet[:environment])
12881312
end
12891313

1290-
it "uses the default environment if lastrunfile is invalid YAML" do
1314+
it "uses environment from Puppet[:environment] if lastrunfile is invalid YAML" do
12911315
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
12921316
Key: 'this is my very very very ' +
12931317
'long string'
12941318
SUMMARY
12951319
configurer.run
12961320

1297-
expect(configurer.environment).to eq(default_environment)
1321+
expect(configurer.environment).to eq(Puppet[:environment])
12981322
end
12991323

1300-
it "uses the default environment if lastrunfile exists but is empty" do
1324+
it "uses environment from Puppet[:environment] if lastrunfile exists but is empty" do
13011325
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', '')
13021326
configurer.run
13031327

1304-
expect(configurer.environment).to eq(default_environment)
1328+
expect(configurer.environment).to eq(Puppet[:environment])
13051329
end
13061330

1307-
it "uses the default environment if the last used one cannot be found" do
1331+
it "uses environment from Puppet[:environment] if the last used one cannot be found" do
13081332
Puppet[:lastrunfile] = tmpfile('last_run_summary.yaml')
13091333
configurer.run
13101334

1311-
expect(configurer.environment).to eq(default_environment)
1335+
expect(configurer.environment).to eq(Puppet[:environment])
13121336
end
13131337
end
13141338
end

0 commit comments

Comments
 (0)