Class: PortAuthority::Agents::LBaaS

Inherits:
PortAuthority::Agent show all
Includes:
Mechanism
Defined in:
lib/port-authority/agents/lbaas.rb

Instance Method Summary collapse

Methods inherited from PortAuthority::Agent

#end!, #exit?, #hostname, #initialize, #sem_create, #setup, #shellcmd, #thr_create, #thr_safe, #thr_start, #thr_wait

Constructor Details

This class inherits a constructor from PortAuthority::Agent

Instance Method Details

#my_ipObject



128
129
130
# File 'lib/port-authority/agents/lbaas.rb', line 128

def my_ip
  @my_ip ||= Socket.ip_address_list.detect(&:ipv4_private?).ip_address
end

#runObject



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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/port-authority/agents/lbaas.rb', line 12

def run
  setup(daemonize: Config.daemonize, nice: -10, root: true)
  Signal.trap('HUP') { Config.load! && LoadBalancer.init! && FloatingIP.init! }
  Signal.trap('USR1') { Logger.debug! }
  Signal.trap('USR2') { @lb_update_hook = true }
  @status_swarm = false
  @etcd = PortAuthority::Etcd.cluster_connect Config.etcd

  thr_create(:swarm, Config.lbaas[:swarm_interval] || Config.lbaas[:interval]) do
    begin
      Logger.debug 'Checking Swarm state'
      status = @etcd.am_i_swarm_leader?
      thr_safe { @status_swarm = status }
      Logger.debug "I am Swarm #{status ? 'leader' : 'follower' }"
    rescue StandardError => e
      Logger.error [ e.class, e.message ].join(': ')
      e.backtrace.each {|line| Logger.debug "  #{line}"}
      thr_safe { @status_swarm = false }
      sleep(Config.lbaas[:swarm_interval] || Config.lbaas[:interval])
      retry unless exit?
    end
  end

  thr_start

  FloatingIP.init!
  LoadBalancer.init!
  LoadBalancer.container || ( LoadBalancer.pull! && LoadBalancer.create! )

  Logger.debug 'Waiting for threads to gather something...'
  sleep Config.lbaas[:interval]
  first_cycle = true
  status_time = Time.now.to_i - 60

  until exit?
    status_swarm = false if first_cycle
    if @lb_update_hook
      Logger.notice 'LoadBalancer update triggerred'
      LoadBalancer.update!
      @lb_update_hook = false
      Logger.notice 'LoadBalancer update finished'
    end
    sleep Config.lbaas[:interval]
    thr_safe(:swarm) { status_swarm = @status_swarm }
    # main logic
    if status_swarm
      # handle FloatingIP on leader
      Logger.debug 'I am the LEADER'
      if FloatingIP.up?
        Logger.debug 'Got FloatingIP, that is OK'
      else
        Logger.notice 'No FloatingIP here, checking whether it is free'
        FloatingIP.arp_del!
        if FloatingIP.reachable?
          Logger.notice 'FloatingIP is still up! (ICMP)'
        else
          Logger.info 'FloatingIP is unreachable by ICMP, checking for duplicates on L2'
          FloatingIP.arp_del!
          if FloatingIP.duplicate?
            Logger.error 'FloatingIP is still assigned! (ARP)'
          else
            Logger.notice 'FloatingIP is free :) assigning'
            FloatingIP.handle! status_swarm
            Logger.notice 'Notifying the network about change'
            FloatingIP.update_arp!
          end
        end
      end
      # handle LoadBalancer on leader
      if LoadBalancer.up?
        Logger.debug 'LoadBalancer is up, that is OK'
      else
        Logger.notice 'LoadBalancer is down, starting'
        LoadBalancer.start!
      end
    else
      # handle FloatingIP on follower
      Logger.debug 'I am a follower'
      if FloatingIP.up?
        Logger.notice 'I got FloatingIP and should not, removing'
        FloatingIP.handle! status_swarm
        FloatingIP.arp_del!
        Logger.notice 'Notifying the network about change'
        FloatingIP.update_arp!
      else
        Logger.debug 'No FloatingIP here, that is OK'
      end
      # handle LoadBalancer on follower
      if LoadBalancer.up?
        Logger.notice 'LoadBalancer is up, stopping'
        LoadBalancer.stop!
      else
        Logger.debug 'LoadBalancer is down, that is OK'
      end
    end # logic end
  end

  thr_wait

  # remove FloatingIP on shutdown
  if FloatingIP.up?
    Logger.notice 'Removing FloatingIP'
    FloatingIP.handle! false
    FloatingIP.update_arp!
  end

  # stop LB on shutdown
  if LoadBalancer.up?
    Logger.notice 'Stopping LoadBalancer'
    LoadBalancer.stop!
  end

  Logger.notice 'Exiting...'
  exit 0
end