2013-02-23 01:20:04 +01:00
|
|
|
require 'puppet/network/http_pool'
|
|
|
|
|
|
|
|
module Puppet
|
|
|
|
module Util
|
2014-10-07 16:06:54 +02:00
|
|
|
# Validator class, for testing that PuppetDB is alive
|
2013-02-23 01:20:04 +01:00
|
|
|
class PuppetdbValidator
|
|
|
|
attr_reader :puppetdb_server
|
|
|
|
attr_reader :puppetdb_port
|
2014-10-07 16:06:54 +02:00
|
|
|
attr_reader :use_ssl
|
|
|
|
attr_reader :test_path
|
|
|
|
attr_reader :test_headers
|
2013-02-23 01:20:04 +01:00
|
|
|
|
2014-10-07 16:06:54 +02:00
|
|
|
def initialize(puppetdb_server, puppetdb_port, use_ssl=true, test_path = "/v3/version")
|
2013-02-23 01:20:04 +01:00
|
|
|
@puppetdb_server = puppetdb_server
|
2013-06-06 20:20:57 +02:00
|
|
|
@puppetdb_port = puppetdb_port
|
|
|
|
@use_ssl = use_ssl
|
2014-10-07 16:06:54 +02:00
|
|
|
@test_path = test_path
|
|
|
|
@test_headers = { "Accept" => "application/json" }
|
2013-02-23 01:20:04 +01:00
|
|
|
end
|
|
|
|
|
2014-10-07 16:06:54 +02:00
|
|
|
# Utility method; attempts to make an http/https connection to the puppetdb server.
|
2013-02-23 01:20:04 +01:00
|
|
|
# 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
|
2014-10-07 16:06:54 +02:00
|
|
|
# http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL
|
2013-02-23 01:20:04 +01:00
|
|
|
# on the puppetdb server.
|
2014-10-07 16:06:54 +02:00
|
|
|
conn = Puppet::Network::HttpPool.http_instance(puppetdb_server, puppetdb_port, use_ssl)
|
2013-06-06 20:20:57 +02:00
|
|
|
|
2014-10-07 16:06:54 +02:00
|
|
|
response = conn.get(test_path, test_headers)
|
2013-02-23 01:20:04 +01:00
|
|
|
unless response.kind_of?(Net::HTTPSuccess)
|
2014-12-02 13:43:30 +01:00
|
|
|
Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? "s" : ""}://#{puppetdb_server}:#{puppetdb_port}): [#{response.code}] #{response.msg}"
|
2013-02-23 01:20:04 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
rescue Exception => e
|
2014-12-02 13:43:30 +01:00
|
|
|
Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? "s" : ""}://#{puppetdb_server}:#{puppetdb_port}): #{e.message}"
|
2013-02-23 01:20:04 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|