Retire stockhausen/listsearch (RT#6848)
[mirror/dsa-puppet.git] / 3rdparty / modules / elasticsearch / lib / puppet / util / es_instance_validator.rb
1 require 'socket'
2 require 'timeout'
3
4 module Puppet
5   module Util
6     class EsInstanceValidator
7       attr_reader :instance_server
8       attr_reader :instance_port
9
10       def initialize(instance_server, instance_port)
11         @instance_server = instance_server
12         @instance_port   = instance_port
13       end
14
15       # Utility method; attempts to make an https connection to the Elasticsearch instance.
16       # This is abstracted out into a method so that it can be called multiple times
17       # for retry attempts.
18       #
19       # @return true if the connection is successful, false otherwise.
20       def attempt_connection
21         Timeout::timeout(Puppet[:configtimeout]) do
22           begin
23             TCPSocket.new(@instance_server, @instance_port).close
24             true
25           rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
26             Puppet.debug "Unable to connect to Elasticsearch instance (#{@instance_server}:#{@instance_port}): #{e.message}"
27             false
28           end
29         end
30       rescue Timeout::Error
31         false
32       end
33     end
34   end
35 end
36