root/cherokee/trunk/admin/ModuleBalancer.py

Revision 2283, 3.3 kB (checked in by alo, 1 month ago)

--

Line 
1 from Form import *
2 from Table import *
3 from Module import *
4
5 from consts import *
6
7 NOTE_BALANCER      = 'Allow to select how the connections will be dispatched.'
8 NO_GENERAL_SOURCES = 'There are no Information Sources configured. Please proceed to configure an <a href="/source">Information Source</a>.'
9 NO_SOURCE_WARNING  = 'A load balancer must be configured to use at least one information source.'
10
11 class ModuleBalancerGeneric (Module, FormHelper):
12     def __init__ (self, cfg, prefix, submit_url, name):
13         FormHelper.__init__ (self, name, cfg)
14         Module.__init__ (self, name, cfg, prefix, submit_url)
15
16     def _op_render (self):
17         new_balancer_node = self._cfg.get_val("tmp!new_balancer_node")
18         if new_balancer_node:
19             tmp = [int(x) for x in self._cfg.keys('%s!source'%(self._prefix))]
20             tmp.sort()
21
22             if tmp:
23                 new_source = str(tmp[-1]+1)
24             else:
25                 new_source = 1
26
27             self._cfg['%s!source!%s'%(self._prefix, new_source)] = new_balancer_node
28             del (self._cfg['tmp!new_balancer_node'])
29             if not self._cfg.get_val(self._prefix):
30                 self._cfg[self._prefix] = BALANCERS[0][0]
31
32         txt = ''
33         general_sources  = self._cfg.keys('source')
34         balancer_sources = self._cfg.keys('%s!source'%(self._prefix))
35
36         # These are the sources that have not been added yet
37         general_left = general_sources[:]
38         for sb in balancer_sources:
39             sg = self._cfg.get_val('%s!source!%s'%(self._prefix, sb))
40             if sg in general_left:
41                 while True:
42                     try: general_left.remove(sg)
43                     except: break
44
45         if not balancer_sources and not general_left:
46             txt += self.Dialog (NO_GENERAL_SOURCES, type_='warning')
47             return txt
48
49         if not balancer_sources:
50             txt += self.Dialog(NO_SOURCE_WARNING, type_='warning')
51         else:
52             txt += '<h2>Information Sources</h2>'
53             table = Table(3,1, style='width="100%"')
54             table += ('Nick', 'Host', '')
55             for sb in balancer_sources:
56                 sg = self._cfg.get_val ('%s!source!%s'%(self._prefix, sb))
57
58                 nick  = self._cfg.get_val('source!%s!nick'%(sg))
59                 host  = self._cfg.get_val('source!%s!host'%(sg))
60                 link  = '<a href="/source/%s">%s</a>' % (sg, nick)
61
62                 js = "post_del_key('/ajax/update', '%s!source!%s');"%(self._prefix, sb)
63                 link_del = self.InstanceImage ("bin.png", "Delete", border="0", onClick=js)
64
65                 table += (link, host, link_del)
66             txt += str(table)
67
68         txt += '<h2>Assign Information Sources</h2>'
69         if not general_left:
70             txt += 'It is already balancing among all the configured ' + \
71                    '<a href="/source">information sources</a>.'
72         else:
73             options = [('', 'Choose..')]
74             for s in general_left:
75                 nick = self._cfg.get_val('source!%s!nick'%(s))
76                 options.append((s,nick))
77
78             table = TableProps()
79             self.AddPropOptions_Reload (table, "Application Server",
80                                         "tmp!new_balancer_node", options, "")
81             txt += str(table)
82
83         return txt
84
85     def _op_apply_changes (self, uri, post):
86         return
Note: See TracBrowser for help on using the browser.