Class: Puppet::Util::PuppetdbValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/puppetdb_validator.rb

Overview

Validator class, for testing that PuppetDB is alive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(puppetdb_server, puppetdb_port, use_ssl = true, test_path = '/pdb/meta/v1/version') ⇒ PuppetdbValidator

Returns a new instance of PuppetdbValidator.



11
12
13
14
15
16
17
# File 'lib/puppet/util/puppetdb_validator.rb', line 11

def initialize(puppetdb_server, puppetdb_port, use_ssl = true, test_path = '/pdb/meta/v1/version')
  @puppetdb_server = puppetdb_server
  @puppetdb_port   = puppetdb_port
  @use_ssl         = use_ssl
  @test_path       = test_path
  @test_headers    = { 'Accept' => 'application/json' }
end

Instance Attribute Details

#puppetdb_portObject (readonly)

Returns the value of attribute puppetdb_port.



6
7
8
# File 'lib/puppet/util/puppetdb_validator.rb', line 6

def puppetdb_port
  @puppetdb_port
end

#puppetdb_serverObject (readonly)

Returns the value of attribute puppetdb_server.



5
6
7
# File 'lib/puppet/util/puppetdb_validator.rb', line 5

def puppetdb_server
  @puppetdb_server
end

#test_headersObject (readonly)

Returns the value of attribute test_headers.



9
10
11
# File 'lib/puppet/util/puppetdb_validator.rb', line 9

def test_headers
  @test_headers
end

#test_pathObject (readonly)

Returns the value of attribute test_path.



8
9
10
# File 'lib/puppet/util/puppetdb_validator.rb', line 8

def test_path
  @test_path
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



7
8
9
# File 'lib/puppet/util/puppetdb_validator.rb', line 7

def use_ssl
  @use_ssl
end

Instance Method Details

#attempt_connectionObject

Utility method; attempts to make an http/https connection to the puppetdb server. This is abstracted out into a method so that it can be called multiple times for retry attempts.

Returns:

  • true if the connection is successful, false otherwise.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puppet/util/puppetdb_validator.rb', line 54

def attempt_connection
  # All that we care about is that we are able to connect successfully via
  # http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL
  # on the puppetdb server.

  if Gem::Version.new(Puppet.version) >= Gem::Version.new('7.0.0')
    valid_connection_new_client?
  else
    valid_connection_old_client?
  end
rescue StandardError => e
  log_error(e.message)
  false
end

#log_error(cause, code = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/puppet/util/puppetdb_validator.rb', line 19

def log_error(cause, code = nil)
  if code.nil?
    Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? 's' : ''}://#{puppetdb_server}:#{puppetdb_port}): #{cause}"
  else
    Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? 's' : ''}://#{puppetdb_server}:#{puppetdb_port}): [#{code}] #{cause}"
  end
end

#valid_connection_new_client?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet/util/puppetdb_validator.rb', line 27

def valid_connection_new_client?
  test_uri = URI("#{use_ssl ? 'https' : 'http'}://#{puppetdb_server}:#{puppetdb_port}#{test_path}")
  begin
    conn = Puppet.runtime[:http]
    _response = conn.get(test_uri, headers: test_headers)
    true
  rescue Puppet::HTTP::ResponseError => e
    log_error e.message, e.response.code
    false
  end
end

#valid_connection_old_client?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'lib/puppet/util/puppetdb_validator.rb', line 39

def valid_connection_old_client?
  conn = Puppet::Network::HttpPool.http_instance(puppetdb_server, puppetdb_port, use_ssl)
  response = conn.get(test_path, test_headers)
  unless response.is_a?(Net::HTTPSuccess)
    log_error(response.msg, response.code)
    return false
  end
  true
end