Class: EtcdTools::VipWatchdog

Inherits:
Watchdog::Init show all
Includes:
Watchdog::Vip
Defined in:
lib/etcd-tools/etcd_watchdog_vip.rb

Instance Method Summary collapse

Methods included from Watchdog::Vip

#got_vip?, #vip_alive?, #vip_dup?, #vip_handle!, #vip_update_arp!

Methods inherited from Watchdog::Init

#initialize

Methods included from Watchdog::Threads

#thread_etcd, #thread_icmp

Methods included from Watchdog::Etcd

#etcd_connect!, #leader?

Methods included from Watchdog::Helpers

#arp, #arping, #hostname, #iproute

Methods included from Watchdog::Logger

#debug, #err, #info

Constructor Details

This class inherits a constructor from EtcdTools::Watchdog::Init

Instance Method Details

#runObject



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/etcd-tools/etcd_watchdog_vip.rb', line 13

def run
  if Process.euid != 0
    err 'Must run under root user!'
    exit! 1
  end
  @semaphore.merge!({ icmp: Mutex.new })
  @thread = { icmp: thread_icmp, etcd: thread_etcd }
  @status_etcd = false
  @status_icmp = false
  @thread.each_value(&:run)
  sleep @config[:parameters][:interval]
  first_cycle = true
  while !@exit do
    status_etcd = status_icmp = false # FIXME: introduce CVs...
    @semaphore[:icmp].synchronize { status_icmp = @status_icmp }
    @semaphore[:etcd].synchronize { status_etcd = @status_etcd }
    if status_etcd
      if got_vip?
        debug '<main> i am the leader with VIP, that is OK'
      else
        info '<main> i am the leader without VIP, checking whether it is free'
        if status_icmp
          info '<main> VIP is still up! (ICMP)'
          # FIXME: notify by sensu client socket
        else
          info '<main> VIP is unreachable by ICMP, checking for duplicates on L2'
          if vip_dup?
            info '<main> VIP is still assigned! (ARP)'
            # FIXME: notify by sensu client socket
          else
            info '<main> VIP is free, assigning'
            vip_handle! status_etcd
            info '<main> updating other hosts about change'
            vip_update_arp!
          end
        end
      end
    else
      if got_vip?
        info '<main> i got VIP and should not, removing'
        vip_handle! status_etcd
        info '<main> updating other hosts about change'
        vip_update_arp!
      else
        debug '<main> i am not a leader and i do not have the VIP, that is OK'
      end
    end
    sleep @config[:parameters][:interval]
    if first_cycle
      @semaphore[:icmp].synchronize { status_icmp = @status_icmp }
      @semaphore[:etcd].synchronize { status_etcd = @status_etcd }
      info "<main> i #{status_etcd ? 'AM' : 'am NOT'} the leader"
      info "<main> i #{got_vip? ? 'DO' : 'do NOT'} have the VIP"
      info "<main> i #{status_icmp ? 'CAN' : 'CANNOT'} see the VIP"
    end
    first_cycle = false
  end
  info '<main> terminated!'
  if got_vip?
    info '<main> removing VIP'
    vip_handle! false
    vip_update_arp!
  end
  info '<main> stopping threads...'
  @thread.each_value(&:join)
  info '<main> exiting...'
  exit 0
end