Mercurial > repos > yufei-luo > s_mart
comparison commons/tools/CleanClusterNodesAfterRepet.py @ 18:94ab73e8a190
Uploaded
| author | m-zytnicki |
|---|---|
| date | Mon, 29 Apr 2013 03:20:15 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 17:b0e8584489e6 | 18:94ab73e8a190 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 ## @file | |
| 4 # Clean the cluster nodes after REPET was used. | |
| 5 # | |
| 6 # usage: CleanClusterNodesAfterRepet.py [ options ] | |
| 7 # options: | |
| 8 # -h: this help | |
| 9 # -n: node to clean (otherwise all nodes will be cleaned) | |
| 10 # -t: temporary directory (e.g. '/tmp') | |
| 11 # -p: pattern (e.g. 'DmelChr4*') | |
| 12 # -v: verbosity level (default=0/1/2) | |
| 13 | |
| 14 import os | |
| 15 import sys | |
| 16 import getopt | |
| 17 | |
| 18 class CleanClusterNodesAfterRepet( object ): | |
| 19 """ | |
| 20 Clean the cluster nodes after REPET was used. | |
| 21 """ | |
| 22 | |
| 23 def __init__( self ): | |
| 24 """ | |
| 25 Constructor. | |
| 26 """ | |
| 27 self._lNodes = [] | |
| 28 self._tmpDir = "" | |
| 29 self._pattern = "" | |
| 30 self._verbose = 0 | |
| 31 self._lNodesToExcept = ["compute-2-46", "compute-2-47"] | |
| 32 | |
| 33 def help( self ): | |
| 34 """ | |
| 35 Display the help on stdout. | |
| 36 """ | |
| 37 print | |
| 38 print "usage: %s.py [ options ]" % ( type(self).__name__ ) | |
| 39 print "options:" | |
| 40 print " -h: this help" | |
| 41 print " -n: node to clean (otherwise all nodes will be cleaned)" | |
| 42 print " -t: temporary directory (e.g. '/tmp')" | |
| 43 print " -p: pattern (e.g. 'DmelChr4*')" | |
| 44 print " -v: verbosity level (default=0/1/2)" | |
| 45 print | |
| 46 | |
| 47 def setAttributesFromCmdLine( self ): | |
| 48 """ | |
| 49 Set the attributes from the command-line. | |
| 50 """ | |
| 51 try: | |
| 52 opts, args = getopt.getopt(sys.argv[1:],"hi:n:t:p:v:") | |
| 53 except getopt.GetoptError, err: | |
| 54 print str(err); self.help(); sys.exit(1) | |
| 55 for o,a in opts: | |
| 56 if o == "-h": | |
| 57 self.help(); sys.exit(0) | |
| 58 elif o == "-n": | |
| 59 self.setLNodes( a.split(" ") ) | |
| 60 elif o == "-t": | |
| 61 self.setTempDirectory( a ) | |
| 62 elif o == "-p": | |
| 63 self.setPattern( a ) | |
| 64 elif o == "-v": | |
| 65 self.setVerbosityLevel( a ) | |
| 66 | |
| 67 def setLNodes( self, a ): | |
| 68 self._lNodes = a | |
| 69 | |
| 70 def setTempDirectory( self, a ): | |
| 71 if a[-1] == "/": | |
| 72 self._tmpDir = a[:-1] | |
| 73 else: | |
| 74 self._tmpDir = a | |
| 75 | |
| 76 def setPattern( self, a ): | |
| 77 self._pattern = a | |
| 78 | |
| 79 def setVerbosityLevel( self, verbose ): | |
| 80 self._verbose = int(verbose) | |
| 81 | |
| 82 def checkAttributes( self ): | |
| 83 """ | |
| 84 Before running, check the required attributes are properly filled. | |
| 85 """ | |
| 86 if self._tmpDir == "": | |
| 87 print "ERROR: need a valid temporary directory" | |
| 88 self.help(); sys.exit(1) | |
| 89 | |
| 90 def getAllNodesList( self ): | |
| 91 """ | |
| 92 Return the list of the names of each node. | |
| 93 """ | |
| 94 lNodes = [] | |
| 95 log = os.system( "qhost > qhost.txt" ) | |
| 96 if log != 0: print "ERROR with qhost"; sys.exit(1) | |
| 97 inF = open( "qhost.txt", "r" ) | |
| 98 line = inF.readline() | |
| 99 line = inF.readline() | |
| 100 line = inF.readline() | |
| 101 while True: | |
| 102 if line == "": | |
| 103 break | |
| 104 tokens = line.split() | |
| 105 if tokens[3] == "-": | |
| 106 line = inF.readline() | |
| 107 continue | |
| 108 lNodes.append( tokens[0] ) | |
| 109 line = inF.readline() | |
| 110 inF.close() | |
| 111 #Remove nodes to avoid from the nodes list | |
| 112 for node in self._lNodesToExcept: | |
| 113 if node in lNodes: | |
| 114 lNodes.remove(node) | |
| 115 return lNodes | |
| 116 | |
| 117 def showNodeList( self, lNodes ): | |
| 118 print "nb of nodes: %i" % ( len(lNodes) ) | |
| 119 for i in range(0,len(lNodes)): | |
| 120 print " %i: %s" % ( i+1, lNodes[i] ) | |
| 121 | |
| 122 def cleanNodes( self): | |
| 123 """ | |
| 124 Connect to each job and clean the temporary directory. | |
| 125 """ | |
| 126 nbNodes = len(self._lNodes) | |
| 127 nodeCount = 0 | |
| 128 for node in self._lNodes: | |
| 129 nodeCount += 1 | |
| 130 if self._verbose > 0: | |
| 131 print "connect to node '%s' (%i/%i)..." % ( node, nodeCount, nbNodes ) | |
| 132 sys.stdout.flush() | |
| 133 cmd = "ssh" | |
| 134 cmd += " -q %s " % ( node ) | |
| 135 cmd += "'find %s" % ( self._tmpDir ) | |
| 136 cmd += " -user %s" % ( os.environ["USER"] ) | |
| 137 if self._pattern != "": | |
| 138 cmd += " -name '%s'" % ( self._pattern ) | |
| 139 cmd += " 2> /dev/null -exec rm -rf {} \; ; exit'" | |
| 140 if self._verbose > 0: print cmd; sys.stdout.flush() | |
| 141 os.system( cmd ) # warning, even if everything goes right, ssh returns an error code, i.e. different than 0 | |
| 142 | |
| 143 def clean( self ): | |
| 144 if os.path.exists( "qhost.txt" ): | |
| 145 os.remove( "qhost.txt" ) | |
| 146 | |
| 147 def start( self ): | |
| 148 """ | |
| 149 Useful commands before running the program. | |
| 150 """ | |
| 151 if self._verbose > 0: | |
| 152 print "START %s" % ( type(self).__name__ ); sys.stdout.flush() | |
| 153 self.checkAttributes() | |
| 154 | |
| 155 def end( self ): | |
| 156 """ | |
| 157 Useful commands before ending the program. | |
| 158 """ | |
| 159 self.clean() | |
| 160 if self._verbose > 0: | |
| 161 print "END %s" % ( type(self).__name__ ); sys.stdout.flush() | |
| 162 | |
| 163 def run( self ): | |
| 164 """ | |
| 165 Run the program. | |
| 166 """ | |
| 167 self.start() | |
| 168 if self._lNodes == []: | |
| 169 self._lNodes = self.getAllNodesList() | |
| 170 if self._verbose > 0: self.showNodeList( self._lNodes ) | |
| 171 self.cleanNodes() | |
| 172 self.end() | |
| 173 | |
| 174 if __name__ == "__main__": | |
| 175 i = CleanClusterNodesAfterRepet() | |
| 176 i.setAttributesFromCmdLine() | |
| 177 i.run() |
