#!/bin/env python # # kill_daq - kills CODA components for a specified configuration # # DL, 10-Mar-2014 # # # # This is the complment to the start_daq script. It will # try and kill processes related to a given CODA configuration. # These processes may be running on remote computers. # # Running this wih no arguments will print a list of # CODA configurations based on the current COOL_HOME # and EXPID environment variable settings. One of those # configuration names should be given on the command line # as a single argument to this script so it knows which # components to try and kill. # # contact: # davidl@jlab.org x5567 # import sys,os import subprocess import string import time import math import re COOL_HOME = os.getenv("COOL_HOME") EXPID = os.getenv("EXPID") DAQ_HOME = os.getenv("DAQ_HOME") CONFIG = "" if len(sys.argv) > 1 : CONFIG = sys.argv[1] rocs = [] # will be filled by GetComponents ts = [] # will be filled by GetComponents dcs = [] # will be filled by GetComponents pebs = [] # will be filled by GetComponents sebs = [] # will be filled by GetComponents ers = [] # will be filled by GetComponents fcss = [] # will be filled by GetComponents files = [] # will be filled by GetComponents hosts = {} # will be filled by GetHosts testmode = False # if set to True, then no commands will be executed. Just printed. #------------------ # FindExecutable #------------------ def FindExecutable(exename, args=[]): try: proc = subprocess.Popen(["which", exename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) result = proc.communicate()[0].strip() if proc.returncode == 0: for arg in args: result += " " + arg return result except: print sys.exc_info()[0] return "" #------------------ # KillROC #------------------ def KillROC(roc): global coda_roc, xwintool # By convention, we use the dns name as the roc name in jcedit host = roc roccmd = '%s' % (coda_roc) # Close window if xwintool exists if xwintool != "": cmd = '%s -kill %s' % (xwintool, roc) print cmd if not testmode: subprocess.Popen(string.split(cmd)) # Kill remote process since closing window doesn't # necessarily kill the remote client cmd = 'ssh %s pkill -f "%s"' % (host, roccmd) print cmd if not testmode: subprocess.Popen(string.split(cmd)) # OK, just obliterate everything! cmd = 'ssh %s pkill -9 -f "coda_roc"' % (host) print cmd if not testmode: subprocess.Popen(string.split(cmd)) #------------------ # KillComponent #------------------ def KillComponent(component, command): global hosts, xwintool # This will either kill the window on the local system # (if the xwintool exists) or it will ssh to the remote # system and kill the process there, leaving the window # open. # Get host name if it was specified in hosts file. If this # is the case then we will need to setup the environment # once we log in before killing the process. If we're just # closing the local window, then we need to add the host # to the window name in the same way start_daq does. host = "" # empty string means run locally envsetup = "" # don't setup environment if running locally title = component if component in hosts: host = hosts[component] envsetup = 'source ${DAQ_HOME}/../work/online_setup.cshrc && ' title += "-%s" % host # Close window if xwintool exists cmd = "" if xwintool != "": cmd = '%s -kill %s' % (xwintool, title) print cmd if not testmode: subprocess.Popen(string.split(cmd)) # Kill remote process since closing window doesn't # necessarily kill the remote client cmd = 'pkill -f "%s"' % command if host!="": cmd = 'ssh %s %s' % (host, cmd) print cmd if not testmode: subprocess.Popen(string.split(cmd)) # Kill remote process by component name (some processes # seem to linger!) cmd = 'pkill -f "%s"' % component if host!="": cmd = 'ssh %s %s' % (host, cmd) print cmd if not testmode: subprocess.Popen(string.split(cmd)) #------------------ # GetConfigurations #------------------ def GetConfigurations(): confdir = "%s/%s/config/Control" % (COOL_HOME, EXPID) if os.path.exists(confdir): return os.listdir(confdir) else: print "Directory doesn't exist: %s" % confdir print "Check your COOL_HOME and EXPID environment variables!" sys.exit(-1) #------------------ # GetComponents #------------------ def GetComponents(): global rocs, ts, pebs, ers, fcss, files configdir = "%s/%s/config/Control/%s" % (COOL_HOME, EXPID, CONFIG) fname = "%s/%s.rdf" % (configdir, CONFIG) with open(fname) as f: lines = f.readlines() for line in lines: idx_end = string.rfind(line, '.rdf"/>') if idx_end < 2: continue idx_start = string.rfind(line[0:idx_end], '/') if idx_start < 2: continue cname = line[idx_start+1:idx_end] # Read in specific component file and get type if 'hasType>ROC<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): rocs.append(cname) if 'hasType>TS<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): ts.append(cname) if 'hasType>DC<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): dcs.append(cname) if 'hasType>PEB<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): pebs.append(cname) if 'hasType>SEB<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): sebs.append(cname) if 'hasType>ER<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): ers.append(cname) if 'hasType>FCS<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): fcss.append(cname) if 'hasType>FILE<' in open('%s/Components/%s.rdf' % (configdir, cname)).read(): files.append(cname) if string.find(cname, "localhost" ) == 0: rocs.append(cname) # for running sandbox test #------------------ # GetHosts #------------------ def GetHosts(): global hosts, rocs, ts # Add all rocs and TS's for roc in rocs : hosts[roc] = roc for roc in ts : hosts[roc] = roc if DAQ_HOME==None: print 'DAQ_HOME not set. All non-ROC hosts will be run locally' return hostsfile = '%s/config/coda/hosts' % DAQ_HOME if not os.path.exists(hostsfile): print 'File %s does not exist. All non-ROC hosts will be run locally' % hostsfile return # Open hosts file and parse it with open(hostsfile) as f: lines = f.readlines() for line in lines: if string.find(line.strip(), '#') == 0 : continue strs = line.strip().split() if len(strs)<2 : continue hosts[strs[0]] = strs[1] #----------------------------------------- # Check that COOL_HOME and EXPID are set if COOL_HOME == None: print "COOL_HOME environment variable not set!" sys.exit(-1) if EXPID == None: print "EXPID environment variable not set!" sys.exit(-1) # Get configuration list configs = GetConfigurations() if not CONFIG in configs: print "\nYou must specify one of the following configurations:\n" for config in configs: sys.stdout.write("%s\n" % config) print "\n" sys.exit(0) # Get the list of ROCs, PEBs, and ERs GetComponents() # Get hosts GetHosts() print "\n--- Kill all DAQ components ---\n" # Find executables in current PATH platform = FindExecutable('platform') rcgui = FindExecutable('rcgui') coda_dc = FindExecutable('coda_emu_dc64') coda_peb = FindExecutable('coda_emu_peb64') coda_seb = FindExecutable('coda_emu_seb64') coda_er = FindExecutable('coda_emu_er64') coda_fcs = FindExecutable('coda_emu_fcs64') coda_roc = FindExecutable('coda_roc_test') coda_roc = FindExecutable('coda_roc_test') xwintool = FindExecutable('xwintool') print "CODA executable locations:" print "---------------------------" print " platform: %s" % platform print " rcgui: %s" % rcgui print " coda_dc: %s" % coda_dc print " coda_peb: %s" % coda_peb print " coda_seb: %s" % coda_seb print " coda_er: %s" % coda_er print " coda_fcs: %s" % coda_fcs print " coda_roc: %s" % coda_roc print " xwintool: %s" % xwintool print "" # Print components list print "Components:" print "---------------------------" sys.stdout.write(" TS(%2d): " % len(ts)) print ts sys.stdout.write(" ROC(%2d): " % len(rocs)) print rocs sys.stdout.write(" DC(%2d): " % len(dcs)) print dcs sys.stdout.write(" PEB(%2d): " % len(pebs)) print pebs sys.stdout.write(" SEB(%2d): " % len(sebs)) print sebs sys.stdout.write(" FCS(%2d): " % len(fcss)) print fcss sys.stdout.write(" ER(%2d): " % len(ers)) print ers sys.stdout.write("FILE(%2d): " % len(files)) print files #-------------------------------------------------------- # Kill platform KillComponent('platform', platform) # Kill all DCs for dc in dcs: KillComponent(dc, coda_dc) # Kill all PEBs for peb in pebs: KillComponent(peb, coda_peb) # Kill all SEBs for seb in sebs: KillComponent(seb, coda_seb) # Kill all ERs for er in ers: KillComponent(er, coda_er) # Kill all FCSs for fcs in fcss: KillComponent(fcs, coda_fcs) # Kill rcgui window (Run Control) KillComponent('rcgui', rcgui) # Kill all ROCs for roc in rocs: KillROC(roc) if testmode: print "NOTE: This was run in test mode so no commands were actually issued!" else: time.sleep(2) # wait for background processes to print thier messages print "Done"