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.



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

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

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

Instance Attribute Details

#instance_portObject (readonly)

Returns the value of attribute instance_port.



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

def instance_port
  @instance_port
end

#instance_serverObject (readonly)

Returns the value of attribute instance_server.



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

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.



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

def attempt_connection
  Timeout.timeout(@timeout) do
    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
rescue Timeout::Error
  false
end