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."""
11 CODEMUXCONF="/etc/codemux.conf"
13 def start(options, config):
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
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()
31 # Check to see if sliver is running. If not, continue
32 if vserver.VServer(sliver['name']).is_running():
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}
39 # Check old slivers for changes
41 sliverinconf = slicesinconf[sliver['name']]
42 if (sliverinconf['host'] != host) or \
43 (sliverinconf['port'] != port):
44 logger.log("codemux: Updating slice %s" % sliver['name'])
46 codemuxslices[sliver['name']] = {'host': host, 'port': port}
48 logger.log("codemux: sliver %s not running yet. Deferring."\
52 if _writeconf: writeConf(codemuxslices)
54 def writeConf(slivers, conf = CODEMUXCONF):
55 '''Write conf with default entry up top. Restart service.'''
57 f.write("* root 1080")
58 for (host, slice, port) in slivers.iteritems():
59 f.write("%s %s %s" % [host, slice, port])
62 logger.log("codemux: restarting codemux service")
63 os.system("/etc/init.d/codemux restart")
66 def parseConf(conf = CODEMUXCONF):
67 '''Parse the CODEMUXCONF and return dict of slices in conf. {slice: (host,port)}'''
71 for line in f.readlines():
72 if line.startswith("#") or (len(line.split()) != 3):
74 (host, slice, port) = line.split()[:3]
75 slicesinconf[slice] = {"host": host, "port": port}
77 except: logger.log_exc()