Class: Naminori::Lb::Lvs

Inherits:
Base
  • Object
show all
Defined in:
lib/naminori/lb/lvs.rb

Class Method Summary collapse

Methods inherited from Base

notifier

Class Method Details

.add?(ops) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/naminori/lb/lvs.rb', line 24

def add?(ops)
  vip_exists?(ops) && !delete?(ops)
end

.add_member(rip, service) ⇒ Object



6
7
8
# File 'lib/naminori/lb/lvs.rb', line 6

def add_member(rip, service)
  transaction("add", lvs_option(rip, service)) if service.healty?(rip)
end

.command_option(type, ops) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/naminori/lb/lvs.rb', line 52

def command_option(type, ops)
  case
  when type == "add"
    "--#{type}-server --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]} -r #{ops[:rip]}:#{ops[:port]} #{method_option(ops[:method])}"
  when type == "delete"
    "--#{type}-server --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]} -r #{ops[:rip]}:#{ops[:port]}"
  end
end

.delete?(ops) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/naminori/lb/lvs.rb', line 28

def delete?(ops)
  fetch_service(ops).each do |line|
    return true if line.match(/#{ops[:rip]}:/)
  end
  false
end

.delete_member(rip, service) ⇒ Object



10
11
12
# File 'lib/naminori/lb/lvs.rb', line 10

def delete_member(rip, service)
  transaction("delete", lvs_option(rip, service))
end

.fetch_service(ops) ⇒ Object



42
43
44
45
46
# File 'lib/naminori/lb/lvs.rb', line 42

def fetch_service(ops)
  service = `ipvsadm -Ln --#{ops[:protocol]}-service #{ops[:vip]}:#{ops[:port]}`.split("\n")
  raise "fetch errror!" unless service
  service
end

.lvs_option(rip, service) ⇒ Object



48
49
50
# File 'lib/naminori/lb/lvs.rb', line 48

def lvs_option(rip, service)
  { service: service, vip: service.config.vip, rip: rip, protocols: service.config.protocols, port: service.config.port, method: service.config.method }
end

.method_option(method) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/naminori/lb/lvs.rb', line 61

def method_option(method)
  case
  when method == "gateway"
    "-g"
  when method == "nat"
    "-m"
  when method == "ip"
    "-i"
  end
end

.transaction(type, ops) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/naminori/lb/lvs.rb', line 14

def transaction(type, ops)
  ops[:protocols].collect do |protocol|
    merge_option =  ops.merge({ protocol: protocol })
    if self.send("#{type}?", merge_option) && system("ipvsadm #{command_option(type, merge_option)}")
      notifier(type, merge_option)
      true
    end
  end.all? {|res| res }
end

.vip_exists?(ops) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/naminori/lb/lvs.rb', line 35

def vip_exists?(ops)
  fetch_service(ops).each do |line|
    return true if line.match(/#{ops[:vip]}:/)
  end
  false
end