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.
"""
+ 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()
if slicesinconf.has_key("root"): _writeconf = False
else: _writeconf = True
+ if 'slivers' not in data:
+ logger.log("codemux.GetSlivers: could not find the slivers keyin data (PLC connection down?) - IGNORED")
+ return
+
# Parse attributes and update dict of scripts
for sliver in data['slivers']:
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
+ if len(parts) == 3:
+ ip = parts[2]
+ else:
+ ip = ""
+ params = {'host': parts[0], 'port': parts[1], 'ip': ip}
+
try:
# Check to see if sliver is running. If not, continue
if vserver.VServer(sliver['name']).is_running():
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.'''
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.write("%s %s %s %s\n" % (host, params['slice'], params['port'], params['ip']))
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']}
+ dnames[mapping['host']] = {"slice":slice, "port": mapping['port'], "ip": mapping['ip']}
hosts = dnames.keys()
# sort by length
hosts.sort(key=str.__len__)
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)}'''
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():
+ if not os.path.exists("/etc/init.d/codemux"): return
logger.log("codemux: Restarting codemux service")
- os.system("/etc/init.d/codemux stop")
- f = os.popen("/sbin/pidof codemux")
- tmp = f.readlines()
- f.close()
- if len(tmp) > 0:
- pids = tmp[0].rstrip("\n").split()
- for pid in pids:
- logger.log("codemux: Killing stalled pid %s" % pid, 2)
- os.kill(pid, 9)
- os.system("/etc/init.d/codemux start")
+ if isRunning():
+ logger.log_call("/etc/init.d/codemux","condrestart")
+ else:
+ logger.log_call("/etc/init.d/codemux","restart")
+
+
+def startService():
+ if not os.path.exists("/etc/init.d/codemux"): return
+ 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 not os.path.exists("/etc/init.d/codemux"): return
+ if isRunning():
+ logger.log("codemux: Stopping codemux service")
+ logger.log_call("/etc/init.d/codemux", "stop")
+ logger.log_call("/sbin/chkconfig", "codemux", "off")