class Puppet::HTTP::Resolver::ServerList

Use the server_list setting to resolve a service. This resolver is only used if server_list is set either on the command line or in the configuration file.

@api public

Public Class Methods

new(client, server_list_setting:, default_port:, services: ) click to toggle source

Create a server list resolver.

@param [Puppet::HTTP::Client] client @param [Array<String>] server_list_setting array of servers set via the

configuration or the command line

@param [Integer] default_port if a port is not set for a server in

server_list, use this port

@param [Array<Symbol>] services array of services that server_list can be

used to resolve. If a service is not included in this array, this resolver
will return nil.
   # File lib/puppet/http/resolver/server_list.rb
18 def initialize(client, server_list_setting:, default_port:, services: )
19   @client = client
20   @server_list_setting = server_list_setting
21   @default_port = default_port
22   @services = services
23 end

Public Instance Methods

resolve(session, name, ssl_context: nil, canceled_handler: nil) click to toggle source

Walk the server_list to find a server and port that will connect successfully.

@param [Puppet::HTTP::Session] session @param [Symbol] name the name of the service being resolved @param [Puppet::SSL::SSLContext] ssl_context @param [Proc] canceled_handler optional callback allowing a resolver

to cancel resolution.

@return [nil] return nil if the service to be resolved does not support

server_list

@return [Puppet::HTTP::Service] a validated service to use for future HTTP

requests

@raise [Puppet::Error] raise if none of the servers defined in server_list

are available

@api public

   # File lib/puppet/http/resolver/server_list.rb
42 def resolve(session, name, ssl_context: nil, canceled_handler: nil)
43   # If we're configured to use an explicit service host, e.g. report_server
44   # then don't use server_list to resolve the `:report` service.
45   return nil unless @services.include?(name)
46 
47   # If we resolved the URL already, use its host & port for the service
48   if @resolved_url
49     return Puppet::HTTP::Service.create_service(@client, session, name, @resolved_url.host, @resolved_url.port)
50   end
51 
52   # Return the first simple service status endpoint we can connect to
53   @server_list_setting.value.each_with_index do |server, index|
54     host = server[0]
55     port = server[1] || @default_port
56 
57     service = Puppet::HTTP::Service.create_service(@client, session, :puppetserver, host, port)
58     begin
59       service.get_simple_status(ssl_context: ssl_context)
60       @resolved_url = service.url
61       return Puppet::HTTP::Service.create_service(@client, session, name, @resolved_url.host, @resolved_url.port)
62     rescue Puppet::HTTP::ResponseError => detail
63       if index < @server_list_setting.value.length - 1
64         Puppet.warning(_("Puppet server %{host}:%{port} is unavailable: %{code} %{reason}") %
65                             { host: service.url.host, port: service.url.port, code: detail.response.code, reason: detail.response.reason } +
66                             ' ' + _("Trying with next server from server_list."))
67       else
68         Puppet.log_exception(detail, _("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       end
71     rescue Puppet::HTTP::HTTPError => detail
72       if index < @server_list_setting.value.length - 1
73         Puppet.warning(_("Unable to connect to server from server_list setting: %{detail}") % {detail: detail} +
74                            ' ' + _("Trying with next server from server_list."))
75       else
76         Puppet.log_exception(detail, _("Unable to connect to server from server_list setting: %{detail}") % {detail: detail})
77       end
78     end
79   end
80 
81   # don't fallback to other resolvers
82   canceled_handler.call(true) if canceled_handler
83 
84   # not found
85   nil
86 end