* Now callback in NM by default
[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/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     logger.log("codemux: Starting.", 2)
20     # slices already in conf
21     slicesinconf = parseConf()
22     # slices that need to be written to the conf
23     codemuxslices = {}
24     _writeconf = False
25     # Parse attributes and update dict of scripts
26     for sliver in data['slivers']:
27         for attribute in sliver['attributes']:
28             if attribute['name'] == 'codemux':
29                 # add to conf.  Attribute is [host, port]
30                 [host, port] = attribute['value'].split()
31                 try:
32                     # Check to see if sliver is running.  If not, continue
33                     if vserver.VServer(sliver['name']).is_running():
34                         # Check for new
35                         if sliver['name'] not in slicesinconf.keys():
36                             logger.log("codemux:  New slice %s using %s" % \
37                                 (sliver['name'], host))
38                             codemuxslices[sliver['name']] = {'host': host, 'port': port}
39                             _writeconf = True
40                         # Check old slivers for changes
41                         else:
42                             sliverinconf = slicesinconf[sliver['name']]
43                             if (sliverinconf['host'] != host) or \
44                                 (sliverinconf['port'] != port):
45                                 logger.log("codemux:  Updating slice %s" % sliver['name'])
46                                 _writeconf = True
47                                 codemuxslices[sliver['name']] = {'host': host, 'port': port}
48                 except:
49                     logger.log("codemux:  sliver %s not running yet.  Deferring."\
50                                 % sliver['name'])
51
52                     logger.log_exc(name = "codemux")
53                     pass
54
55     # Remove slices from conf that no longer have the attribute
56     for deadslice in Set(slicesinconf.keys()) - Set(codemuxslices.keys()):
57         logger.log("codemux: Removing %s" % deadslice)
58         _writeconf = True
59         
60     if _writeconf:  writeConf(codemuxslices)
61
62 def writeConf(slivers, conf = CODEMUXCONF):
63     '''Write conf with default entry up top. Restart service.'''
64     f = open(conf, "w")
65     f.write("* root 1080")
66     for (host, slice, port) in slivers.iteritems():
67         f.write("%s %s %s" % [host, slice, port])
68     f.truncate()
69     f.close()
70     logger.log("codemux: restarting codemux service")
71     os.system("/etc/init.d/codemux restart")
72
73
74 def parseConf(conf = CODEMUXCONF):
75     '''Parse the CODEMUXCONF and return dict of slices in conf. {slice: (host,port)}'''
76     slicesinconf = {} 
77     try: 
78         f = open(conf)
79         for line in f.readlines():
80             if line.startswith("#") or (len(line.split()) != 3)\
81             or line.startswith("*"):  
82                 continue
83             (host, slice, port) = line.split()[:3]
84             logger.log("codemux:  found %s in conf" % slice, 2)
85             slicesinconf[slice] = {"host": host, "port": port}
86         f.close()
87     except: logger.log_exc()
88     return slicesinconf
89
90