X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=codemux.py;h=ce69e4a13b1865f29558a7028567fe3dec32ef47;hb=7e835300a5be01f36b18a76996b40c30fd6cabdb;hp=bad9883743beb3e202e20ba6ff4722ea01ea1612;hpb=2c238990e772aedaea07ba26c27417aac9879caf;p=nodemanager.git diff --git a/codemux.py b/codemux.py index bad9883..ce69e4a 100644 --- a/codemux.py +++ b/codemux.py @@ -7,6 +7,7 @@ import logger import os import vserver from sets import Set +from config import Config CODEMUXCONF="/etc/codemux/codemux.conf" @@ -15,7 +16,10 @@ def start(options, config): def GetSlivers(data): - """For each sliver with the codemux attribute, parse out "host,port" and make entry in conf. Restart service after.""" + """ + For each sliver with the codemux attribute, parse out "host,port" + and make entry in conf. Restart service after. + """ logger.log("codemux: Starting.", 2) # slices already in conf slicesinconf = parseConf() @@ -29,37 +33,27 @@ def GetSlivers(data): # Parse attributes and update dict of scripts for sliver in data['slivers']: for attribute in sliver['attributes']: - if attribute['name'] == 'codemux': + if attribute['tagname'] == 'codemux': # add to conf. Attribute is [host, port] - [host, port] = attribute['value'].split(",") + params = {'host': attribute['value'].split(",")[0], + 'port': attribute['value'].split(",")[1]} try: # Check to see if sliver is running. If not, continue if vserver.VServer(sliver['name']).is_running(): - # Add to dict of codemuxslices - codemuxslices[sliver['name']] = {'host': host, 'port': port} - # Check if new - if sliver['name'] not in slicesinconf.keys(): - logger.log("codemux: New slice %s using %s" % \ - (sliver['name'], host)) + # Check if new or needs updating + if (sliver['name'] not in slicesinconf.keys()) \ + or (params not in slicesinconf.get(sliver['name'], [])): + logger.log("codemux: Updaiting slice %s using %s" % \ + (sliver['name'], params['host'])) # Toggle write. _writeconf = True - # Check old slivers for changes - else: - # Get info about slice in conf - sliverinconf = slicesinconf[sliver['name']] - # Check values for changes. - if (sliverinconf['host'] != host) or \ - (sliverinconf['port'] != port): - logger.log("codemux: Updating slice %s" % sliver['name']) - # use updated values - codemuxslices[sliver['name']] = {'host': host, 'port': port} - # Toggle write. - _writeconf = True + # Add to dict of codemuxslices. Make list to support more than one + # codemuxed host per slice. + codemuxslices.setdefault(sliver['name'],[]) + codemuxslices[sliver['name']].append(params) except: logger.log("codemux: sliver %s not running yet. Deferring."\ % sliver['name']) - - logger.log_exc(name = "codemux") pass # Remove slices from conf that no longer have the attribute @@ -69,32 +63,58 @@ def GetSlivers(data): logger.log("codemux: Removing %s" % deadslice) _writeconf = True - if _writeconf: writeConf(codemuxslices) + if _writeconf: writeConf(sortDomains(codemuxslices)) def writeConf(slivers, conf = CODEMUXCONF): - '''Write conf with default entry up top. Restart service.''' + '''Write conf with default entry up top. Elements in [] should have lower order domain names first. Restart service.''' f = open(conf, "w") # This needs to be the first entry... - f.write("* root 1080\n") - for (slice, params) in slivers.iteritems(): - if slice == "root": continue - f.write("%s %s %s\n" % (params['host'], slice, params['port'])) + try: + f.write("* root 1080 %s\n" % Config().PLC_PLANETFLOW_HOST) + except AttributeError: + logger.log("codemux: Can't find PLC_CONFIG_HOST in config. Using PLC_API_HOST") + f.write("* root 1080 %s\n" % Config().PLC_API_HOST) + # Sort items for like domains + for mapping in slivers: + for (host, params) in mapping.iteritems(): + if params['slice'] == "root": continue + f.write("%s %s %s\n" % (host, params['slice'], params['port'])) f.truncate() f.close() try: restartService() except: logger.log_exc() +def sortDomains(slivers): + '''Given a dict of {slice: {domainname, port}}, return array of slivers with lower order domains first''' + dnames = {} # {host: slice} + for (slice, params) in slivers.iteritems(): + for mapping in params: + dnames[mapping['host']] = {"slice":slice, "port": mapping['port']} + hosts = dnames.keys() + # sort by length + hosts.sort(key=str.__len__) + # longer first + hosts.reverse() + # make list of slivers + sortedslices = [] + for host in hosts: sortedslices.append({host: dnames[host]}) + + return sortedslices + def parseConf(conf = CODEMUXCONF): '''Parse the CODEMUXCONF and return dict of slices in conf. {slice: (host,port)}''' slicesinconf = {} # default try: f = open(conf) for line in f.readlines(): - if line.startswith("#") or (len(line.split()) != 3): + if line.startswith("#") \ + or (len(line.split()) > 4) \ + or (len(line.split()) < 3): continue (host, slice, port) = line.split()[:3] logger.log("codemux: found %s in conf" % slice, 2) - slicesinconf[slice] = {"host": host, "port": port} + slicesinconf.setdefault(slice, []) + slicesinconf[slice].append({"host": host, "port": port}) f.close() except IOError: logger.log_exc() return slicesinconf