|
1 | 1 | require 'puppet/network/http_pool' |
2 | 2 |
|
3 | | -module Puppet |
4 | | - module Util |
5 | | - # Validator class, for testing that PuppetDB is alive |
6 | | - class PuppetdbValidator |
7 | | - attr_reader :puppetdb_server |
8 | | - attr_reader :puppetdb_port |
9 | | - attr_reader :use_ssl |
10 | | - attr_reader :test_path |
11 | | - attr_reader :test_headers |
| 3 | +# Validator class, for testing that PuppetDB is alive |
| 4 | +class Puppet::Util::PuppetdbValidator |
| 5 | + attr_reader :puppetdb_server |
| 6 | + attr_reader :puppetdb_port |
| 7 | + attr_reader :use_ssl |
| 8 | + attr_reader :test_path |
| 9 | + attr_reader :test_headers |
12 | 10 |
|
13 | | - def initialize(puppetdb_server, puppetdb_port, use_ssl=true, test_path = "/pdb/meta/v1/version") |
14 | | - @puppetdb_server = puppetdb_server |
15 | | - @puppetdb_port = puppetdb_port |
16 | | - @use_ssl = use_ssl |
17 | | - @test_path = test_path |
18 | | - @test_headers = { "Accept" => "application/json" } |
19 | | - end |
| 11 | + def initialize(puppetdb_server, puppetdb_port, use_ssl = true, test_path = '/pdb/meta/v1/version') |
| 12 | + @puppetdb_server = puppetdb_server |
| 13 | + @puppetdb_port = puppetdb_port |
| 14 | + @use_ssl = use_ssl |
| 15 | + @test_path = test_path |
| 16 | + @test_headers = { 'Accept' => 'application/json' } |
| 17 | + end |
20 | 18 |
|
21 | | - # Utility method; attempts to make an http/https connection to the puppetdb server. |
22 | | - # This is abstracted out into a method so that it can be called multiple times |
23 | | - # for retry attempts. |
24 | | - # |
25 | | - # @return true if the connection is successful, false otherwise. |
26 | | - def attempt_connection |
27 | | - # All that we care about is that we are able to connect successfully via |
28 | | - # http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL |
29 | | - # on the puppetdb server. |
30 | | - conn = Puppet::Network::HttpPool.http_instance(puppetdb_server, puppetdb_port, use_ssl) |
| 19 | + # Utility method; attempts to make an http/https connection to the puppetdb server. |
| 20 | + # This is abstracted out into a method so that it can be called multiple times |
| 21 | + # for retry attempts. |
| 22 | + # |
| 23 | + # @return true if the connection is successful, false otherwise. |
| 24 | + def attempt_connection |
| 25 | + # All that we care about is that we are able to connect successfully via |
| 26 | + # http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL |
| 27 | + # on the puppetdb server. |
| 28 | + conn = Puppet::Network::HttpPool.http_instance(puppetdb_server, puppetdb_port, use_ssl) |
31 | 29 |
|
32 | | - response = conn.get(test_path, test_headers) |
33 | | - unless response.kind_of?(Net::HTTPSuccess) |
34 | | - Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? "s" : ""}://#{puppetdb_server}:#{puppetdb_port}): [#{response.code}] #{response.msg}" |
35 | | - return false |
36 | | - end |
37 | | - return true |
38 | | - rescue Exception => e |
39 | | - Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? "s" : ""}://#{puppetdb_server}:#{puppetdb_port}): #{e.message}" |
40 | | - return false |
41 | | - end |
| 30 | + response = conn.get(test_path, test_headers) |
| 31 | + unless response.is_a?(Net::HTTPSuccess) |
| 32 | + Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? 's' : ''}://#{puppetdb_server}:#{puppetdb_port}): [#{response.code}] #{response.msg}" |
| 33 | + return false |
42 | 34 | end |
| 35 | + return true |
| 36 | + rescue StandardError => e |
| 37 | + Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? 's' : ''}://#{puppetdb_server}:#{puppetdb_port}): #{e.message}" |
| 38 | + return false |
43 | 39 | end |
44 | 40 | end |
45 | | - |
0 commit comments