Class: ConsulBridge::JoinConsul

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#callObject



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

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 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