Class: IpHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ip-helper.rb

Instance Method Summary collapse

Instance Method Details

#get_current_machine_ipv4sObject



26
27
28
29
30
31
32
33
# File 'lib/ip-helper.rb', line 26

def get_current_machine_ipv4s
  loopback_regex = /^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/

  potential_ips = Socket.ip_address_list.map{|info| info.ip_address}
                        .select {|info| not loopback_regex.match(info)}

  potential_ips.select { |info| is_valid_v4_ip info}
end

#get_event_store_ips_from_dns(dns_name) ⇒ Object



44
45
46
47
48
49
# File 'lib/ip-helper.rb', line 44

def get_event_store_ips_from_dns(dns_name)
  Resolv::DNS.open { |dns|
    resources = dns.getresources dns_name, Resolv::DNS::Resource::IN::A
    resources.map { |res| res.address.to_s }
  }
end

#get_ips_in_cluster(cluster_dns) ⇒ Object



16
17
18
19
20
# File 'lib/ip-helper.rb', line 16

def get_ips_in_cluster(cluster_dns)
  event_store_ips = get_event_store_ips_from_dns cluster_dns
  return event_store_ips.count unless event_store_ips == nil
  return 0
end

#get_local_ip_that_also_on_cluster(cluster_dns) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/ip-helper.rb', line 7

def get_local_ip_that_also_on_cluster(cluster_dns)
  current_machine_ips = get_current_machine_ipv4s
  event_store_ips = get_event_store_ips_from_dns cluster_dns

  return no_event_store_ips_error cluster_dns unless event_store_ips.any?

  get_matching_ips current_machine_ips, event_store_ips
end

#get_matching_ips(machine_ips, event_store_ips) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ip-helper.rb', line 35

def get_matching_ips(machine_ips, event_store_ips)
  matched_ips = machine_ips.select do |ip_to_look_for|
    event_store_ips.find { |ip_to_match| ip_to_look_for == ip_to_match }
  end
  return no_matching_ip_error machine_ips, event_store_ips unless matched_ips.one?

  matched_ips[0]
end

#is_valid_v4_ip(potential_ip) ⇒ Object



22
23
24
# File 'lib/ip-helper.rb', line 22

def is_valid_v4_ip(potential_ip)
  /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/.match potential_ip
end

#no_event_store_ips_error(dns_name) ⇒ Object



55
56
57
# File 'lib/ip-helper.rb', line 55

def no_event_store_ips_error(dns_name)
  "could not find any ips at dns name #{dns_name} so cannot check gossips"
end

#no_matching_ip_error(machine_ips, event_store_ips) ⇒ Object



51
52
53
# File 'lib/ip-helper.rb', line 51

def no_matching_ip_error(machine_ips, event_store_ips)
  "this machine has ips of #{machine_ips}, event store (according to dns lookup) has ips of #{event_store_ips}. There should be exactly one match, but wasn't. "
end