Module: IspUnity

Defined in:
lib/isp_unity.rb,
lib/isp_unity/version.rb,
lib/isp_unity/routing_table.rb

Defined Under Namespace

Classes: IspUnityException

Constant Summary collapse

PATH =
YAML.load_file( config_file )[ENV['GEM_ENV']
ConfigFilePath =
File.join(PATH['config_file_path'])
RoutingTablePath =
PATH['rt_table_path'].to_s
VERSION =
"0.0.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.isp_config_listObject (readonly)

Returns the value of attribute isp_config_list.



10
11
12
# File 'lib/isp_unity/routing_table.rb', line 10

def isp_config_list
  @isp_config_list
end

Class Method Details

.configObject



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
# File 'lib/isp_unity/routing_table.rb', line 12

def config
  # If file is exists then it reads that otherwise log an error 
  if File.exist?(ConfigFilePath)
    begin
      configurations = JSON.parse(File.read(ConfigFilePath))   
      IspUnityLog.info(I18n.t('file.read.success'))
    rescue Exception => e
      IspUnityLog.debug("#{e}")
      IspUnityLog.error(I18n.t('file.read.error'))
      raise IspUnityException.new(I18n.t('file.read.error'))
    end

    @isp_config_list = [] 
    isp_list = configurations['isp']
    $ip_cluster = configurations['public_dns']
    $skip_sticky_session = configurations['skip_sticky_session']
    $priority_for_sticky = configurations['priority_for_sticky'].split(',') if configurations['priority_for_sticky']

    if isp_list 
      isp_list.each do|data|
        if data['enabled'] == 'true'
          ip_addr = SystemCall.get_ip(data['interface'].to_s)
          if ip_addr
            data['ip_address'] = SystemCall.get_ip(data['interface'])
            @isp_config_list.push(Isp.new(data)) 
          else
            puts I18n.t('no_ip_addr') + data['name']
            IspUnityLog.error(I18n.t('no_ip_addr') + data['name'] )
          end
        end
      end
      IspUnityLog.info('Isp Object succesfully created!')
    else
      raise IspUnityException.new(I18n.t('file.enter_isp'))
      IspUnityLog.error(I18n.t('file.read.no_isp_found'))
    end

    routing_table = File.read(RoutingTablePath)
    IspUnityLog.info(I18n.t('routing_table.read.success'))
    begin
      @isp_config_list.each do|isp_list|
        unless routing_table.include?(isp_list.name)
          File.open(RoutingTablePath, 'a') {|f| f.write("#{rand(100)}  #{isp_list.name} \n")}
        end
        IspUnityLog.info(I18n.t('isp.created'))
      end
    rescue Exception => e
      IspUnityLog.debug("#{e}")
      IspUnityLog.error(I18n.t('routing_table.read.error'))
      raise IspUnityException.new(I18n.t('routing_table.read.error'))
    end
  else
    @isp_config_list = []
    puts "Please make ensure that configuration file has copied on /etc dir"
    IspUnityLog.error('Please make ensure that configuration file has copied on /etc dir')
  end
end

.monitorObject



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
# File 'lib/isp_unity.rb', line 64

def monitor
  IspUnity.config
  online_isps = []
  IspUnity.isp_config_list.each do |isp|
    online_isps << isp  if LoadBalance.is_alive(isp) 
  end

  #Failover case
  unless  online_isps == isp_config_list

    #LOAD BALANCE
    LoadBalance.build_commands(IspUnity.isp_config_list)
    IspUnityLog.info(I18n.t('load_balance.build_commands'))
    SystemCall.execute(LoadBalance.commands)
    IspUnityLog.info(I18n.t('system_call.execute.load_balance.success'))

    #Flush route
    SystemCall.execute('/sbin/ip route flush cache')

    #Change sticky session 
    offline_isps = isp_lists - online_isps
    StickySession.change_rule(offline_isps, online_isps)
  
  end
end

.setupObject



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
# File 'lib/isp_unity.rb', line 32

def setup
  IspUnity.config
  isp_lists = IspUnity.isp_config_list
  IspUnityLog.info(I18n.t('file.write'))
  Route.build_commands(isp_lists)
  route_command = Route.commands
  IspUnityLog.info(I18n.t('route.build_commands'))
  IspUnityLog.debug("Route Command: #{route_command}")
  if SystemCall.execute(route_command)
    IspUnityLog.info(I18n.t('system_call.execute.route.success'))

    #RULE COMMAND
    Rule.build_commands(isp_lists)
    rule_command = Rule.commands
    IspUnityLog.info(I18n.t('rule.build_commands'))
    IspUnityLog.debug("Rule Command: #{rule_command}")
    SystemCall.execute(rule_command)
    IspUnityLog.info(I18n.t('system_call.execute.rule.success'))

    #LOAD BALANCE
    LoadBalance.build_commands(isp_lists)
    load_balance_command = LoadBalance.commands
    IspUnityLog.info(I18n.t('load_balance.build_commands'))
    IspUnityLog.debug("Load Balance Command: #{load_balance_command}")
    SystemCall.execute(load_balance_command)
    IspUnityLog.info(I18n.t('system_call.execute.load_balance.success'))

    #Sticky Session
    StickySession.execute(isp_lists) if $skip_sticky_session
  end
end