2013-10-29 10:54:23 -04:00
|
|
|
#!/usr/bin/env python
|
2010-02-04 10:21:36 -05:00
|
|
|
|
2013-10-29 10:54:23 -04:00
|
|
|
import re
|
2010-02-04 10:21:36 -05:00
|
|
|
import sys
|
2012-03-26 11:38:52 -04:00
|
|
|
import os, os.path
|
2014-11-19 16:31:17 -05:00
|
|
|
import shutil
|
2010-02-04 10:21:36 -05:00
|
|
|
import utils
|
|
|
|
|
import time
|
2010-07-23 15:52:35 -04:00
|
|
|
from optparse import OptionParser
|
2010-02-04 10:21:36 -05:00
|
|
|
|
2013-10-29 10:54:23 -04:00
|
|
|
def shouldKill( c, root=None ):
|
2010-06-02 13:21:29 -04:00
|
|
|
|
2012-05-24 17:51:08 -04:00
|
|
|
if "smoke.py" in c:
|
2012-02-17 00:06:50 -05:00
|
|
|
return False
|
|
|
|
|
|
2012-05-11 13:55:47 -04:00
|
|
|
if "emr.py" in c:
|
|
|
|
|
return False
|
|
|
|
|
|
2012-05-24 17:51:08 -04:00
|
|
|
if "java" in c:
|
|
|
|
|
return False
|
|
|
|
|
|
2013-10-29 10:54:23 -04:00
|
|
|
# if root directory is provided, see if command line matches mongod process running
|
|
|
|
|
# with the same data directory
|
|
|
|
|
|
|
|
|
|
if root and re.compile("(\W|^)mongod(.exe)?\s+.*--dbpath(\s+|=)%s(\s+|$)" % root).search( c ):
|
2010-06-02 13:21:29 -04:00
|
|
|
return True
|
|
|
|
|
|
2013-05-28 12:41:30 -04:00
|
|
|
if ( c.find( "buildbot" ) >= 0 or c.find( "slave" ) >= 0 ) and c.find( "/mongo/" ) >= 0:
|
2010-06-02 13:21:29 -04:00
|
|
|
return True
|
|
|
|
|
|
2013-01-04 18:38:46 -05:00
|
|
|
if c.find( "xml-data/build-dir" ) >= 0: # for bamboo
|
|
|
|
|
return True
|
|
|
|
|
|
2010-06-02 13:21:29 -04:00
|
|
|
return False
|
|
|
|
|
|
2013-10-29 10:54:23 -04:00
|
|
|
def killprocs( signal="", root=None ):
|
2010-02-04 10:21:36 -05:00
|
|
|
killed = 0
|
2012-04-20 14:10:07 -04:00
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
|
return killed
|
|
|
|
|
|
2010-06-02 15:03:03 -04:00
|
|
|
l = utils.getprocesslist()
|
|
|
|
|
print( "num procs:" + str( len( l ) ) )
|
|
|
|
|
if len(l) == 0:
|
|
|
|
|
print( "no procs" )
|
|
|
|
|
try:
|
|
|
|
|
print( execsys( "/sbin/ifconfig -a" ) )
|
|
|
|
|
except Exception,e:
|
|
|
|
|
print( "can't get interfaces" + str( e ) )
|
|
|
|
|
|
|
|
|
|
for x in l:
|
2010-02-04 10:21:36 -05:00
|
|
|
x = x.lstrip()
|
2013-10-29 10:54:23 -04:00
|
|
|
if not shouldKill( x, root=root ):
|
2010-02-04 10:21:36 -05:00
|
|
|
continue
|
2013-09-05 17:38:19 -04:00
|
|
|
|
2012-03-26 11:38:52 -04:00
|
|
|
pid = x.split( " " )[0]
|
2010-02-04 10:21:36 -05:00
|
|
|
print( "killing: " + x )
|
|
|
|
|
utils.execsys( "/bin/kill " + signal + " " + pid )
|
|
|
|
|
killed = killed + 1
|
|
|
|
|
|
|
|
|
|
return killed
|
|
|
|
|
|
|
|
|
|
|
2014-03-25 11:14:59 -04:00
|
|
|
def tryToRemove(path):
|
|
|
|
|
for _ in range(60):
|
|
|
|
|
try:
|
|
|
|
|
os.remove(path)
|
|
|
|
|
return True
|
|
|
|
|
except OSError, e:
|
|
|
|
|
errno = getattr(e, 'winerror', None)
|
|
|
|
|
# check for the access denied and file in use WindowsErrors
|
|
|
|
|
if errno in (5, 32):
|
|
|
|
|
print("os.remove(%s) failed, retrying in one second." % path)
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2010-07-23 15:52:35 -04:00
|
|
|
def cleanup( root , nokill ):
|
|
|
|
|
if nokill:
|
|
|
|
|
print "nokill requested, not killing anybody"
|
|
|
|
|
else:
|
2013-10-29 10:54:23 -04:00
|
|
|
if killprocs( root=root ) > 0:
|
2010-07-23 15:52:35 -04:00
|
|
|
time.sleep(3)
|
2013-10-29 10:54:23 -04:00
|
|
|
killprocs( "-9", root=root )
|
2010-06-02 13:39:01 -04:00
|
|
|
|
2010-02-04 10:21:36 -05:00
|
|
|
# delete all regular files, directories can stay
|
|
|
|
|
# NOTE: if we delete directories later, we can't delete diskfulltest
|
|
|
|
|
for ( dirpath , dirnames , filenames ) in os.walk( root , topdown=False ):
|
2013-09-05 17:38:19 -04:00
|
|
|
for x in filenames:
|
2010-06-02 15:14:51 -04:00
|
|
|
foo = dirpath + "/" + x
|
2014-03-25 11:14:59 -04:00
|
|
|
if os.path.exists(foo):
|
|
|
|
|
if not tryToRemove(foo):
|
|
|
|
|
raise Exception("Couldn't remove file '%s' after 60 seconds" % foo)
|
2010-02-04 10:21:36 -05:00
|
|
|
|
2014-11-19 16:31:17 -05:00
|
|
|
# delete all directories under root.
|
|
|
|
|
for directoryEntry in os.listdir(root):
|
|
|
|
|
if directoryEntry == 'diskfulltest':
|
|
|
|
|
continue
|
|
|
|
|
path = root + '/' + directoryEntry
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
|
shutil.rmtree(path, ignore_errors=True)
|
|
|
|
|
|
2010-02-04 10:21:36 -05:00
|
|
|
if __name__ == "__main__":
|
2010-07-23 15:52:35 -04:00
|
|
|
parser = OptionParser(usage="read the script")
|
|
|
|
|
parser.add_option("--nokill", dest='nokill', default=False, action='store_true')
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
2010-02-04 10:21:36 -05:00
|
|
|
root = "/data/db/"
|
2010-07-23 15:52:35 -04:00
|
|
|
if len(args) > 0:
|
|
|
|
|
root = args[0]
|
2013-09-05 17:38:19 -04:00
|
|
|
|
2010-07-23 15:52:35 -04:00
|
|
|
cleanup( root , options.nokill )
|