Class: Puppet::HTTP::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/http/session.rb

Overview

The session is the mechanism by which services may be connected to and accessed.

Constant Summary collapse

CAP_LOCALES =

capabilities for a site

'locales'
CAP_JSON =
'json'
SUPPORTED_LOCALES_MOUNT_AGENT_VERSION =

puppet version where locales mount was added

Gem::Version.new("5.3.4")
SUPPORTED_JSON_DEFAULT =

puppet version where JSON was enabled by default

Gem::Version.new("5.0.0")

Instance Method Summary collapse

Constructor Details

#initialize(client, resolvers) ⇒ Session

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new HTTP session. The session is the mechanism by which services may be connected to and accessed. Sessions should be created using ‘Puppet::HTTP::Client#create_session`.

Parameters:



26
27
28
29
30
31
# File 'lib/puppet/http/session.rb', line 26

def initialize(client, resolvers)
  @client = client
  @resolvers = resolvers
  @resolved_services = {}
  @server_versions = {}
end

Instance Method Details

#process_response(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Collect per-site server versions. This will allow us to modify future requests based on the version of puppetserver we are talking to.

Parameters:



88
89
90
91
92
93
94
# File 'lib/puppet/http/session.rb', line 88

def process_response(response)
  version = response[Puppet::HTTP::HEADER_PUPPET_VERSION]
  if version
    site = Puppet::HTTP::Site.from_uri(response.url)
    @server_versions[site] = version
  end
end

#route_to(name, url: nil, ssl_context: nil) ⇒ Puppet::HTTP::Service

If an explicit server and port are specified on the command line or configuration file, this method always returns a Service with that host and port. Otherwise, we walk the list of resolvers in priority order:

- DNS SRV
- Server List
- Puppet server/port settings

If a given resolver fails to connect, it tries the next available resolver until a successful connection is found and returned. The successful service is cached and returned if ‘route_to` is called again.

Parameters:

  • name (Symbol)

    the service to resolve

  • url (URI) (defaults to: nil)

    optional explicit url to use, if it is already known

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

    ssl context to be used for connections

Returns:

Raises:

  • (ArgumentError)


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
# File 'lib/puppet/http/session.rb', line 51

def route_to(name, url: nil, ssl_context: nil)
  raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)

  # short circuit if explicit URL host & port given
  if url && !url.host.nil? && !url.host.empty?
    service = Puppet::HTTP::Service.create_service(@client, self, name, url.host, url.port)
    service.connect(ssl_context: ssl_context)
    return service
  end

  cached = @resolved_services[name]
  return cached if cached

  canceled = false
  canceled_handler = ->(cancel) { canceled = cancel }

  @resolvers.each do |resolver|
    Puppet.debug("Resolving service '#{name}' using #{resolver.class}")
    service = resolver.resolve(self, name, ssl_context: ssl_context, canceled_handler: canceled_handler)
    if service
      @resolved_services[name] = service
      Puppet.debug("Resolved service '#{name}' to #{service.url}")
      return service
    elsif canceled
      break
    end
  end

  raise Puppet::HTTP::RouteError, "No more routes to #{name}"
end

#supports?(name, capability) ⇒ Boolean

Determine if a session supports a capability. Depending on the server version we are talking to, we know certain features are available or not. These specifications are defined here so we can modify our requests appropriately.

Parameters:

  • name (Symbol)

    name of the service to check

  • capability (String)

    the capability, ie ‘locales` or `json`

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/puppet/http/session.rb', line 106

def supports?(name, capability)
  raise ArgumentError, "Unknown service #{name}" unless Puppet::HTTP::Service.valid_name?(name)

  service = @resolved_services[name]
  return false unless service

  site = Puppet::HTTP::Site.from_uri(service.url)
  server_version = @server_versions[site]

  case capability
  when CAP_LOCALES
    !server_version.nil? && Gem::Version.new(server_version) >= SUPPORTED_LOCALES_MOUNT_AGENT_VERSION
  when CAP_JSON
    server_version.nil? || Gem::Version.new(server_version) >= SUPPORTED_JSON_DEFAULT
  else
    false
  end
end