Retire stockhausen/listsearch (RT#6848)
[mirror/dsa-puppet.git] / 3rdparty / modules / elasticsearch / lib / puppet / provider / es_instance_conn_validator / tcp_port.rb
1 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"..","..",".."))
2 require 'puppet/util/es_instance_validator'
3
4 # This file contains a provider for the resource type `es_instance_conn_validator`,
5 # which validates the Elasticsearch instance connection by attempting an https connection.
6
7 Puppet::Type.type(:es_instance_conn_validator).provide(:tcp_port) do
8   desc "A provider for the resource type `es_instance_conn_validator`,
9         which validates the  connection by attempting an https
10         connection to the Elasticsearch instance." 
11
12   def exists?
13     start_time = Time.now
14     timeout = resource[:timeout]
15
16     success = validator.attempt_connection
17
18     while success == false && ((Time.now - start_time) < timeout)
19       # It can take several seconds for the Elasticsearch instance to start up;
20       # especially on the first install.  Therefore, our first connection attempt
21       # may fail.  Here we have somewhat arbitrarily chosen to retry every 2
22       # seconds until the configurable timeout has expired.
23       Puppet.debug("Failed to connect to the Elasticsearch instance; sleeping 2 seconds before retry")
24       sleep 2
25       success = validator.attempt_connection
26     end
27
28     if success
29       Puppet.debug("Connected to the ES instance in #{Time.now - start_time} seconds.")
30     else
31       Puppet.notice("Failed to connect to the ES instance within timeout window of #{timeout} seconds; giving up.")
32     end
33
34     success
35   end
36
37   def create
38     # If `#create` is called, that means that `#exists?` returned false, which
39     # means that the connection could not be established... so we need to
40     # cause a failure here.
41     raise Puppet::Error, "Unable to connect to ES instance ! (#{@validator.instance_server}:#{@validator.instance_port})"
42   end
43
44   private
45
46   # @api private
47   def validator
48     @validator ||= Puppet::Util::EsInstanceValidator.new(resource[:server], resource[:port])
49   end
50
51 end