summaryrefslogtreecommitdiff
path: root/test/legacy26/buildscripts/cleanbb.py
blob: fee7efdc0c12bca90db2c07ef27ad9cf4b2859cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python

import re
import sys
import os, os.path
import utils
import time
from optparse import OptionParser

def shouldKill( c, root=None ):

    if "smoke.py" in c:
        return False

    if "emr.py" in c:
        return False

    if "java" in c:
        return False

    # 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 ):
        return True

    if ( c.find( "buildbot" ) >= 0 or c.find( "slave" ) >= 0 ) and c.find( "/mongo/" ) >= 0:
        return True

    if c.find( "xml-data/build-dir" ) >= 0: # for bamboo
        return True

    return False

def killprocs( signal="", root=None ):
    killed = 0

    if sys.platform == 'win32':
        return killed

    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:
        x = x.lstrip()
        if not shouldKill( x, root=root ):
            continue

        pid = x.split( " " )[0]
        print( "killing: " + x )
        utils.execsys( "/bin/kill " + signal + " " +  pid )
        killed = killed + 1

    return killed


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


def cleanup( root , nokill ):
    if nokill:
        print "nokill requested, not killing anybody"
    else:
        if killprocs( root=root ) > 0:
            time.sleep(3)
            killprocs( "-9", root=root )

    # 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 ):
        for x in filenames:
            foo = dirpath + "/" + x
            if os.path.exists(foo):
                if not tryToRemove(foo):
                    raise Exception("Couldn't remove file '%s' after 60 seconds" % foo)

if __name__ == "__main__":
    parser = OptionParser(usage="read the script")
    parser.add_option("--nokill", dest='nokill', default=False, action='store_true')
    (options, args) = parser.parse_args()

    root = "/data/db/"
    if len(args) > 0:
        root = args[0]

    cleanup( root , options.nokill )