Class: Puppet::HTTP::Resolver::ServerList

Inherits:
Puppet::HTTP::Resolver show all
Defined in:
lib/puppet/http/resolver/server_list.rb

Overview

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.

Instance Method Summary collapse

Methods inherited from Puppet::HTTP::Resolver

#check_connection?

Constructor Details

#initialize(client, server_list_setting:, default_port:, services:) ⇒ ServerList

Create a server list resolver.

Parameters:

  • client (Puppet::HTTP::Client)
  • server_list_setting (Array<String>)

    array of servers set via the configuration or the command line

  • default_port (Integer)

    if a port is not set for a server in server_list, use this port

  • services (Array<Symbol>)

    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.



19
20
21
22
23
24
# File 'lib/puppet/http/resolver/server_list.rb', line 19

def initialize(client, server_list_setting:, default_port:, services:)
  @client = client
  @server_list_setting = server_list_setting
  @default_port = default_port
  @services = services
end

Instance Method Details

#resolve(session, name, ssl_context: nil, canceled_handler: nil) ⇒ nil, Puppet::HTTP::Service

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

Parameters:

  • session (Puppet::HTTP::Session)
  • name (Symbol)

    the name of the service being resolved

  • ssl_context (Puppet::SSL::SSLContext) (defaults to: nil)
  • canceled_handler (Proc) (defaults to: nil)

    optional callback allowing a resolver to cancel resolution.

Returns:

  • (nil)

    return nil if the service to be resolved does not support server_list

  • (Puppet::HTTP::Service)

    a validated service to use for future HTTP requests

Raises:

  • (Puppet::Error)

    raise if none of the servers defined in server_list are available



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/http/resolver/server_list.rb', line 43

def resolve(session, name, ssl_context: nil, canceled_handler: nil)
  # If we're configured to use an explicit service host, e.g. report_server
  # then don't use server_list to resolve the `:report` service.
  return nil unless @services.include?(name)

  # If we resolved the URL already, use its host & port for the service
  if @resolved_url
    return Puppet::HTTP::Service.create_service(@client, session, name, @resolved_url.host, @resolved_url.port)
  end

  # Return the first simple service status endpoint we can connect to
  @server_list_setting.value.each_with_index do |server, index|
    host = server[0]
    port = server[1] || @default_port

    service = Puppet::HTTP::Service.create_service(@client, session, :puppetserver, host, port)
    begin
      service.get_simple_status(ssl_context: ssl_context)
      @resolved_url = service.url
      return Puppet::HTTP::Service.create_service(@client, session, name, @resolved_url.host, @resolved_url.port)
    rescue Puppet::HTTP::ResponseError => detail
      if index < @server_list_setting.value.length - 1
        Puppet.warning(_("Puppet server %{host}:%{port} is unavailable: %{code} %{reason}") %
                            { host: service.url.host, port: service.url.port, code: detail.response.code, reason: detail.response.reason } +
                            ' ' + _("Trying with next server from server_list."))
      else
        Puppet.log_exception(detail, _("Puppet server %{host}:%{port} is unavailable: %{code} %{reason}") %
                             { host: service.url.host, port: service.url.port, code: detail.response.code, reason: detail.response.reason })
      end
    rescue Puppet::HTTP::HTTPError => detail
      if index < @server_list_setting.value.length - 1
        Puppet.warning(_("Unable to connect to server from server_list setting: %{detail}") % { detail: detail } +
                           ' ' + _("Trying with next server from server_list."))
      else
        Puppet.log_exception(detail, _("Unable to connect to server from server_list setting: %{detail}") % { detail: detail })
      end
    end
  end

  # don't fallback to other resolvers
  canceled_handler.call(true) if canceled_handler

  # not found
  nil
end