Merge from trunk.
[nodemanager.git] / plugins / codemux.py
index 78ae100..25b866f 100644 (file)
@@ -11,15 +11,20 @@ from config import Config
 
 CODEMUXCONF="/etc/codemux/codemux.conf"
 
 
 CODEMUXCONF="/etc/codemux/codemux.conf"
 
-def start(options, config):
-    pass
+def start(options, conf):
+    logger.log("codemux plugin starting up...")
 
 
-
-def GetSlivers(data):
+def GetSlivers(data, config, plc = None):
     """
     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.
     """
+    if 'OVERRIDES' in dir(config):
+        if config.OVERRIDES.get('codemux') == '-1':
+            logger.log("codemux:  Disabled", 2)
+            stopService()
+            return
+
     logger.log("codemux:  Starting.", 2)
     # slices already in conf
     slicesinconf = parseConf()
     logger.log("codemux:  Starting.", 2)
     # slices already in conf
     slicesinconf = parseConf()
@@ -35,8 +40,11 @@ def GetSlivers(data):
         for attribute in sliver['attributes']:
             if attribute['tagname'] == 'codemux':
                 # add to conf.  Attribute is [host, port]
         for attribute in sliver['attributes']:
             if attribute['tagname'] == 'codemux':
                 # add to conf.  Attribute is [host, port]
-                params = {'host': attribute['value'].split(",")[0], 
-                          'port': attribute['value'].split(",")[1]}
+                parts = attribute['value'].split(",")
+                if len(parts)<2:
+                    logger.log("codemux: attribute value (%s) for codemux not separated by comma. Skipping."%attribute['value'])
+                    continue
+                params = {'host': parts[0], 'port': parts[1]}
                 try:
                     # Check to see if sliver is running.  If not, continue
                     if vserver.VServer(sliver['name']).is_running():
                 try:
                     # Check to see if sliver is running.  If not, continue
                     if vserver.VServer(sliver['name']).is_running():
@@ -63,7 +71,10 @@ def GetSlivers(data):
             logger.log("codemux:  Removing %s" % deadslice)
             _writeconf = True 
 
             logger.log("codemux:  Removing %s" % deadslice)
             _writeconf = True 
 
-    if _writeconf:  writeConf(sortDomains(codemuxslices))
+    if _writeconf:  writeConf(sortDomains(codemuxslices))    
+    # ensure the service is running
+    startService()
+
 
 def writeConf(slivers, conf = CODEMUXCONF):
     '''Write conf with default entry up top. Elements in [] should have lower order domain names first. Restart service.'''
 
 def writeConf(slivers, conf = CODEMUXCONF):
     '''Write conf with default entry up top. Elements in [] should have lower order domain names first. Restart service.'''
@@ -84,6 +95,7 @@ def writeConf(slivers, conf = CODEMUXCONF):
     try:  restartService()
     except:  logger.log_exc()
 
     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}
 def sortDomains(slivers):
     '''Given a dict of {slice: {domainname, port}}, return array of slivers with lower order domains first'''
     dnames = {} # {host: slice}
@@ -100,6 +112,7 @@ def sortDomains(slivers):
     for host in hosts: sortedslices.append({host: dnames[host]})
     
     return 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)}'''
         
 def parseConf(conf = CODEMUXCONF):
     '''Parse the CODEMUXCONF and return dict of slices in conf. {slice: (host,port)}'''
@@ -119,6 +132,31 @@ def parseConf(conf = CODEMUXCONF):
     except IOError: logger.log_exc()
     return slicesinconf
 
     except IOError: logger.log_exc()
     return slicesinconf
 
+
+def isRunning():
+    if len(os.popen("pidof codemux").readline().rstrip("\n")) > 0:
+        return True
+    else:
+        return False
+
+
 def restartService():
     logger.log("codemux:  Restarting codemux service")
 def restartService():
     logger.log("codemux:  Restarting codemux service")
-    os.system("/etc/init.d/codemux condrestart")
+    if isRunning():
+        logger.log_call("/etc/init.d/codemux","condrestart")
+    else:
+        logger.log_call("/etc/init.d/codemux","restart")
+
+
+def startService():
+    if not isRunning():
+        logger.log("codemux:  Starting codemux service")
+        logger.log_call("/etc/init.d/codemux", "start")
+    logger.log_call("/sbin/chkconfig", "codemux", "on")
+
+
+def stopService():
+    if isRunning():
+        logger.log("codemux:  Stopping codemux service")
+        logger.log_call("/etc/init.d/codemux", "stop")
+    logger.log_call("/sbin/chkconfig", "codemux", "off")