Initial commit.
[nodemanager.git] / codemux.py
1 # $Id$
2 # $URL$
3
4 """Codemux configurator.  Monitors slice attributes and configures CoDemux to mux port 80 based on HOST field in HTTP request.  Forwards to localhost port belonging to configured slice."""
5
6 import logger
7 import os
8 import vserver
9 from sets import Set
10
11 CODEMUXCONF="/etc/codemux.conf"
12
13 def start(options, config):
14     pass
15
16
17 def GetSlivers(data):
18     """For each sliver with the codemux attribute, parse out "host,port" and make entry in conf.  Restart service after."""
19     # slices already in conf
20     slicesinconf = parseConf()
21     # slices that need to be written to the conf
22     codemuxslices = {}
23     _writeconf = False
24     # Parse attributes and update dict of scripts
25     for sliver in data['slivers']:
26         for attribute in sliver['attributes']:
27             if attribute['name'] == 'codemux':
28                 # add to conf.  Attribute is [host, port]
29                 [host, port] = attribute['value'].split()
30                 try:
31                     # Check to see if sliver is running.  If not, continue
32                     if vserver.VServer(sliver['name']).is_running():
33                         # Check for new
34                         if sliver['name'] not in slicesinconf.keys():
35                             logger.log("codemux:  New slice %s using %s" % \
36                                 (sliver['name'], host))
37                             codemuxslices[sliver['name']] = {'host': host, 'port': port}
38                             _writeconf = True
39                         # Check old slivers for changes
40                         else:
41                             sliverinconf = slicesinconf[sliver['name']]
42                             if (sliverinconf['host'] != host) or \
43                                 (sliverinconf['port'] != port):
44                                 logger.log("codemux:  Updating slice %s" % sliver['name'])
45                                 _writeconf = True
46                                 codemuxslices[sliver['name']] = {'host': host, 'port': port}
47                 except:
48                     logger.log("codemux:  sliver %s not running yet.  Deferring."\
49                                 % sliver['name'])
50                     pass
51
52     if _writeconf:  writeConf(codemuxslices)
53
54 def writeConf(slivers, conf = CODEMUXCONF):
55     '''Write conf with default entry up top. Restart service.'''
56     f.open(conf)
57     f.write("* root 1080")
58     for (host, slice, port) in slivers.iteritems():
59         f.write("%s %s %s" % [host, slice, port])
60     f.truncate()
61     f.close()
62     logger.log("codemux: restarting codemux service")
63     os.system("/etc/init.d/codemux restart")
64
65
66 def parseConf(conf = CODEMUXCONF):
67     '''Parse the CODEMUXCONF and return dict of slices in conf. {slice: (host,port)}'''
68     slicesinconf = {} 
69     try: 
70         f = open(conf)
71         for line in f.readlines():
72             if line.startswith("#") or (len(line.split()) != 3):  
73                 continue
74             (host, slice, port) = line.split()[:3]
75             slicesinconf[slice] = {"host": host, "port": port}
76         f.close()
77     except: logger.log_exc()
78     return slicesinconf
79
80