Class: ConsulBridge::JoinConsul

Inherits:
Base
  • Object
show all
Defined in:
lib/consul_bridge/join_consul.rb

Constant Summary collapse

JOIN_URL =
'http://127.0.0.1:8500/v1/agent/join'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

call

Constructor Details

#initialize(master_ips:, join_all: false) ⇒ JoinConsul

Returns a new instance of JoinConsul.



11
12
13
14
# File 'lib/consul_bridge/join_consul.rb', line 11

def initialize(master_ips:, join_all: false)
  self.master_ips = master_ips
  self.join_all = join_all
end

Instance Attribute Details

#join_allObject

Returns the value of attribute join_all.



7
8
9
# File 'lib/consul_bridge/join_consul.rb', line 7

def join_all
  @join_all
end

#master_ipsObject

Returns the value of attribute master_ips.



7
8
9
# File 'lib/consul_bridge/join_consul.rb', line 7

def master_ips
  @master_ips
end

Instance Method Details

#callObject



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
# File 'lib/consul_bridge/join_consul.rb', line 16

def call
  private_ip = GetPrivateIP.call.private_ip
  puts "Detected private ip: #{private_ip}"
  puts "Starting join with [#{master_ips.join(', ')}]"

  joined = 0
  master_ips.each do |ip|
    next if ip == private_ip
    begin
      puts "Trying to join #{ip}"
      Excon.get(
        JOIN_URL + "/#{ip}",
        expects: [200],
        connect_timeout: 5,
        read_timeout: 11,
        write_timeout: 5,
        tcp_nodelay: true
      )
      puts "Joined #{ip}"
      joined += 1
      break unless self.join_all
    rescue Excon::Errors::HTTPStatusError, Excon::Errors::SocketError
      next
    end
  end

  if join_all && joined < 2
    raise 'Unable to join at least 2 masters with join_all'
  elsif !join_all && joined < 1
    raise 'Unable to join any master'
  end
end