Class: Puppet::Util::EsInstanceValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/es_instance_validator.rb

Overview

Helper class to assist with talking to the Elasticsearch service ports.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_server, instance_port) ⇒ EsInstanceValidator

Returns a new instance of EsInstanceValidator.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/util/es_instance_validator.rb', line 12

def initialize(instance_server, instance_port)
  @instance_server = instance_server
  @instance_port   = instance_port

  # Avoid deprecation warnings in Puppet versions < 4
  if Facter.value(:puppetversion).split('.').first.to_i < 4
    @timeout = Puppet[:configtimeout]
  else
    @timeout = Puppet[:http_connect_timeout]
  end
end

Instance Attribute Details

#instance_portObject (readonly)

Returns the value of attribute instance_port.



10
11
12
# File 'lib/puppet/util/es_instance_validator.rb', line 10

def instance_port
  @instance_port
end

#instance_serverObject (readonly)

Returns the value of attribute instance_server.



9
10
11
# File 'lib/puppet/util/es_instance_validator.rb', line 9

def instance_server
  @instance_server
end

Instance Method Details

#attempt_connectionObject

Utility method; attempts to make an https connection to the Elasticsearch instance. This is abstracted out into a method so that it can be called multiple times for retry attempts.

Returns:

  • true if the connection is successful, false otherwise.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puppet/util/es_instance_validator.rb', line 29

def attempt_connection
  Timeout::timeout(@timeout) do
    begin
      TCPSocket.new(@instance_server, @instance_port).close
      true
    rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
      Puppet.debug "Unable to connect to Elasticsearch instance (#{@instance_server}:#{@instance_port}): #{e.message}"
      false
    end
  end
rescue Timeout::Error
  false
end