Skip to content

Commit 867691b

Browse files
committed
(PUP-11328) Get the environment's name without loading the environment
The previous commit added back the node request as a fallback, but it was calling `Node#environment`, which will try to load the environment from disk. Since the server-assigned environment likely doesn't exist on the agent, the run printed the following warning and continued using "production" Warning: Could not find a directory environment named 'xxx' anywhere in the path ... Does the directory exist? To complicate matters, node termini set the environment on the node differently. The :rest terminus returns a node with an environment_name, but not an instance. The :plain terminus returns a node with an environment instance, but not an environment_name. To avoid breaking things, retrieve the environment name the same way we did before[1]. [1] https://github.com/puppetlabs/puppet/blob/6.24.0/lib/puppet/configurer.rb#L315-L319
1 parent 9d3e630 commit 867691b

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

lib/puppet/configurer.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,17 @@ def current_server_specified_environment(current_environment, configured_environ
513513
:transaction_uuid => @transaction_uuid,
514514
:fail_on_404 => true)
515515

516-
@server_specified_environment = node.environment.name.to_s
516+
# The :rest node terminus returns a node with an environment_name, but not an
517+
# environment instance. Attempting to get the environment instance will load
518+
# it from disk, which will likely fail. So create a remote environment.
519+
#
520+
# The :plain node terminus returns a node with an environment, but not an
521+
# environment_name.
522+
if !node.has_environment_instance? && node.environment_name
523+
node.environment = Puppet::Node::Environment.remote(node.environment_name)
524+
end
525+
526+
@server_specified_environment = node.environment.to_s
517527

518528
if @server_specified_environment != @environment
519529
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: @server_specified_environment }

spec/integration/application/agent_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,14 @@ def with_another_agent_running(&block)
643643

644644
context "environment convergence" do
645645
it "falls back to making a node request if the last server-specified environment cannot be loaded" do
646-
server.start_server do |port|
646+
mounts = {}
647+
mounts[:node] = -> (req, res) {
648+
node = Puppet::Node.new('test', environment: Puppet::Node::Environment.remote('doesnotexistonagent'))
649+
res.body = formatter.render(node)
650+
res['Content-Type'] = formatter.mime
651+
}
652+
653+
server.start_server(mounts: mounts) do |port|
647654
Puppet[:serverport] = port
648655
Puppet[:log_level] = 'debug'
649656

spec/unit/configurer_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require 'puppet/configurer'
33

44
describe Puppet::Configurer do
5+
include PuppetSpec::Files
6+
57
before do
68
Puppet[:server] = "puppetmaster"
79
Puppet[:report] = true
@@ -10,6 +12,17 @@
1012
allow_any_instance_of(described_class).to(
1113
receive(:valid_server_environment?).and_return(true)
1214
)
15+
16+
Puppet[:lastrunfile] = file_containing('last_run_summary.yaml', <<~SUMMARY)
17+
---
18+
version:
19+
config: 1624882680
20+
puppet: #{Puppet.version}
21+
application:
22+
initial_environment: #{Puppet[:environment]}
23+
converged_environment: #{Puppet[:environment]}
24+
run_mode: agent
25+
SUMMARY
1326
end
1427

1528
let(:node_name) { Puppet[:node_name_value] }
@@ -1099,7 +1112,6 @@ def expects_neither_new_or_cached_catalog
10991112
end
11001113

11011114
describe "when selecting an environment" do
1102-
include PuppetSpec::Files
11031115
include PuppetSpec::Settings
11041116

11051117
describe "when the last used environment is available" do
@@ -1205,10 +1217,12 @@ def expects_neither_new_or_cached_catalog
12051217
describe "when the last used environment is not available" do
12061218
describe "when the node request succeeds" do
12071219
let(:node_environment) { Puppet::Node::Environment.remote(:salam) }
1208-
let(:node) { double(Puppet::Node, :environment => node_environment) }
1220+
let(:node) { Puppet::Node.new(Puppet[:node_name_value]) }
12091221
let(:last_server_specified_environment) { 'development' }
12101222

12111223
before do
1224+
node.environment = node_environment
1225+
12121226
allow(Puppet::Node.indirection).to receive(:find)
12131227
allow(Puppet::Node.indirection).to receive(:find)
12141228
.with(anything, hash_including(:ignore_cache => true, :fail_on_404 => true))

0 commit comments

Comments
 (0)