Skip to content

Commit 3781319

Browse files
committed
(PUP-10844) Warnings for unavailable servers
When processing server_list and trying to find a functional server, throw warnings when it cannot connect to servers from server_list and if there is no functional server in server_list, then throw the initial error.
1 parent 9ab6c32 commit 3781319

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/puppet/http/resolver/server_list.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def resolve(session, name, ssl_context: nil, canceled_handler: nil)
5454
end
5555

5656
# Return the first simple service status endpoint we can connect to
57-
@server_list_setting.value.each do |server|
57+
@server_list_setting.value.each_with_index do |server, index|
5858
host = server[0]
5959
port = server[1] || @default_port
6060

@@ -64,10 +64,21 @@ def resolve(session, name, ssl_context: nil, canceled_handler: nil)
6464
@resolved_url = service.url
6565
return Puppet::HTTP::Service.create_service(@client, session, name, @resolved_url.host, @resolved_url.port)
6666
rescue Puppet::HTTP::ResponseError => detail
67-
Puppet.log_exception(detail, _("Puppet server %{host}:%{port} is unavailable: %{code} %{reason}") %
68-
{ host: service.url.host, port: service.url.port, code: detail.response.code, reason: detail.response.reason })
67+
if index < @server_list_setting.value.length - 1
68+
Puppet.warning(_("Puppet server %{host}:%{port} is unavailable: %{code} %{reason}") %
69+
{ host: service.url.host, port: service.url.port, code: detail.response.code, reason: detail.response.reason } +
70+
' ' + _("Trying with next server from server_list."))
71+
else
72+
Puppet.log_exception(detail, _("Puppet server %{host}:%{port} is unavailable: %{code} %{reason}") %
73+
{ host: service.url.host, port: service.url.port, code: detail.response.code, reason: detail.response.reason })
74+
end
6975
rescue Puppet::HTTP::HTTPError => detail
70-
Puppet.log_exception(detail, _("Unable to connect to server from server_list setting: %{detail}") % {detail: detail})
76+
if index < @server_list_setting.value.length - 1
77+
Puppet.warning(_("Unable to connect to server from server_list setting: %{detail}") % {detail: detail} +
78+
' ' + _("Trying with next server from server_list."))
79+
else
80+
Puppet.log_exception(detail, _("Unable to connect to server from server_list setting: %{detail}") % {detail: detail})
81+
end
7182
end
7283
end
7384

spec/unit/configurer_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,29 @@ def expects_neither_new_or_cached_catalog
10721072
}.to raise_error(Puppet::Error, /Could not select a functional puppet server from server_list: 'myserver:123,someotherservername'/)
10731073
end
10741074

1075+
it "should warn when servers in 'server_list' are unreachable" do
1076+
Puppet.settings[:server_list] = "mybadserver1:123,mybadserver2:123,mygoodserver"
1077+
Puppet[:usecacheonfailure] = false
1078+
1079+
stub_request(:get, 'https://mybadserver1:123/status/v1/simple/master').and_raise(Puppet::HTTP::HTTPError)
1080+
stub_request(:get, 'https://mybadserver2:123/status/v1/simple/master').and_raise(Puppet::HTTP::HTTPError)
1081+
stub_request(:get, 'https://mygoodserver:8140/status/v1/simple/master').to_return(status: 200)
1082+
1083+
expect(Puppet).to receive(:warning).with(/^Unable to connect to server from server_list setting:.*Trying with next server from server_list.$/).twice
1084+
configurer.run
1085+
end
1086+
1087+
it "should warn when servers in 'server_list' respond with error" do
1088+
Puppet.settings[:server_list] = "mybadserver:123,someotherservername"
1089+
Puppet[:usecacheonfailure] = false
1090+
1091+
stub_request(:get, 'https://mybadserver:123/status/v1/simple/master').to_return(status: 400)
1092+
stub_request(:get, 'https://someotherservername:8140/status/v1/simple/master').to_return(status: 200)
1093+
1094+
expect(Puppet).to receive(:warning).with(/^Puppet server mybadserver:123 is unavailable: 400 Trying with next server from server_list.$/)
1095+
configurer.run
1096+
end
1097+
10751098
it "should not error when usecacheonfailure is true and no servers in 'server_list' are reachable" do
10761099
Puppet.settings[:server_list] = "myserver:123,someotherservername"
10771100
Puppet[:usecacheonfailure] = true

0 commit comments

Comments
 (0)