forked from puppetlabs/puppetlabs-puppetdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuppetdb_validator.rb
More file actions
35 lines (31 loc) · 1.36 KB
/
puppetdb_validator.rb
File metadata and controls
35 lines (31 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'puppet/network/http_pool'
# Validator class, for testing that PuppetDB is alive
class Puppet::Util::PuppetdbValidator
attr_reader :test_uri
attr_reader :test_headers
def initialize(puppetdb_server, puppetdb_port, use_ssl = true, test_path = '/pdb/meta/v1/version')
@test_uri = URI("#{use_ssl ? 'https' : 'http'}://#{puppetdb_server}:#{puppetdb_port}#{test_path}")
@test_headers = { 'Accept' => 'application/json' }
end
# 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.
#
# @return true if the connection is successful, false otherwise.
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.
conn = Puppet.runtime[:http]
response = conn.get(test_uri, headers: test_headers)
if response.is_a?(Puppet::HTTP::ResponseNetHTTP) && response.success?
return true
else
Puppet.notice "Unable to connect to puppetdb server (#{test_uri}): [#{response.code}] #{response.reason}"
return false
end
rescue StandardError => e
Puppet.notice "Unable to connect to puppetdb server (#{test_uri}): #{e.message}"
return false
end
end