File: tcp_port.rb

package info (click to toggle)
puppet-module-voxpupuli-elasticsearch 9.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,496 kB
  • sloc: ruby: 9,906; sh: 392; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
require 'puppet/util/es_instance_validator'

# This file contains a provider for the resource type `es_instance_conn_validator`,
# which validates the Elasticsearch connection by attempting a tcp connection.

Puppet::Type.type(:es_instance_conn_validator).provide(:tcp_port) do
  desc "A provider for the resource type `es_instance_conn_validator`,
        which validates the  connection by attempting an https
        connection to Elasticsearch."

  def exists?
    start_time = Time.now
    timeout = resource[:timeout]
    sleep_interval = resource[:sleep_interval]

    success = validator.attempt_connection

    while success == false && ((Time.now - start_time) < timeout)
      # It can take several seconds for the Elasticsearch to start up;
      # especially on the first install.  Therefore, our first connection attempt
      # may fail.  Here we have somewhat arbitrarily chosen to retry every 10
      # seconds until the configurable timeout has expired.
      Puppet.debug("Failed to connect to Elasticsearch; sleeping #{sleep_interval} seconds before retry")
      sleep sleep_interval
      success = validator.attempt_connection
    end

    if success
      Puppet.debug("Connected to the Elasticsearch in #{Time.now - start_time} seconds.")
    else
      Puppet.notice("Failed to connect to the Elasticsearch within timeout window of #{timeout} seconds; giving up.")
    end

    success
  end

  def create
    # If `#create` is called, that means that `#exists?` returned false, which
    # means that the connection could not be established... so we need to
    # cause a failure here.
    raise Puppet::Error, "Unable to connect to Elasticsearch! (#{@validator.instance_server}:#{@validator.instance_port})"
  end

  private

  # @api private
  def validator
    @validator ||= Puppet::Util::EsInstanceValidator.new(resource[:server], resource[:port])
  end
end