def getNextJobWaitingInLine (self, testbed): manualLockWaitingFiles = glob.glob(self.testbedDir + testbed + '*' + '.manual-lock-waiting') jobWaitingFiles = glob.glob(self.testbedDir + testbed + '*' + '.job-waiting') filePrefixList = manualLockWaitingFiles + jobWaitingFiles # Build a list of files associating with their priority and submissionId if filePrefixList != []: prioritizing = {} for testbedFile in filePrefixList: # Get prioriyy and submission ID for each file. # Lowest priority number has precedence. fileContents = infraLib.getFileContents(testbedFile) priority = fileContents[4].split('=')[1] submissionId = fileContents[5].split('=')[1] prioritizing[testbedFile] = [priority, submissionId] # This sorted for loop will get files with lowest priority # and then lowest submission ID. sortedPriorityJobList = sorted(prioritizing, key=prioritizing.get) if sortedPriorityJobList: return sortedPriorityJobList[0] else: # Returning None return
def unlockTestbed(self, testbedList): """ unlockTestbed [testbed name | all] """ allResult = 'all' in testbedList if allResult == False: if self.testbedExists(testbedList) == False: self.printErrorMsg('No such testbed %s' % testbedList) return else: testbedList = self.getAllTestbedNames() for testbed in testbedList: if self.testbedExists(testbed) == False: self.printErrorMsg('No such testbed %s' % testbed) return else: # Kill the process ID on existing jobs waiting and running allTestbedLockFiles = glob.glob(self.testbedDir + testbed + '.*') if allTestbedLockFiles != None: for file in allTestbedLockFiles: fileContents = infraLib.getFileContents(file) try: processId = fileContents[6].split('=')[1] os.system('kill -9 %s' % (processId)) except IndexError: pass if glob.glob(self.testbedDir + testbed + '*') != []: os.system('rm %s' % self.testbedDir + testbed + '*')
def testbedExists(self, testbedList): fileContents = infraLib.getFileContents(self.allTestbedNamesFile) # testbedList can be either one item or a list of items # If it is one item, then convert it into a list if type(testbedList) == str: testbedList = testbedList.split() for eachTestbed in testbedList: if eachTestbed in fileContents: pass else: return False return True
def Reprioritized(self, submissionId, newPriority): # Get a list of all the files with .manual-lock-waiting and .job-waiting. # Check the submissionID # If match, write existing contents to a temp file # and change the priority number if int(newPriority) > 7: print ('\n', 'Error: Priority number range is 0-7') print ('\t', 'You entered: %s' % (newPriority), '\n') exit # For reprioritization, we only do this for jobs that are waiting. # Otherwise, it doesn't matter waitingFileList = glob.glob(self.testbedDir + '*' + '.*waiting') if waitingFileList != []: isThereSuchSubmissionIdFlag = 0 for eachWaitingFile in waitingFileList: fileContents = infraLib.getFileContents( eachWaitingFile ) typeOfLock = fileContents[0].split('=')[1] user = fileContents[1].split('=')[1] date = fileContents[2].split('=')[1] time = fileContents[3].split('=')[1] priority = fileContents[4].split('=')[1] submissionId2 = fileContents[5].split('=')[1] if submissionId == submissionId2: isThereSuchSubmissionIdFlag = 1 # Change the priority by rewriting the waiting file with # the new priority number tempFile = eachWaitingFile + '.temp' os.system('touch ' + tempFile) infraLib.writeToFile(tempFile, 'typeOfLock=' + typeOfLock + '\n') infraLib.writeToFile(tempFile, 'user='******'\n') infraLib.writeToFile(tempFile, 'date=' + date + '\n') infraLib.writeToFile(tempFile, 'time=' + time + '\n') infraLib.writeToFile(tempFile, 'priority=' + newPriority + '\n') infraLib.writeToFile(tempFile, 'submissionId=' + submissionId + '\n') os.system('rm %s' % (eachWaitingFile)) os.system('mv %s %s' % (tempFile, eachWaitingFile)) if isThereSuchSubmissionIdFlag == 0: self.printErrorMsg('No such submissionID found: %s' % submissionId)
def getAllTestbedNames(self): testbeds = infraLib.getFileContents( self.allTestbedNamesFile ) return testbeds
def lockTestbed(self, testbedList, typeOfLock='Manual-Lock', processId=None): # typeOfLock = Manual-Lock or Job # This is to distinguish if the generated file is for # manual locking or for a job # NOTE: The testbed lock file name must include the submissionId # as part of the filename because the same user could # create lock the testbed and launch a job on the same testbed. # # Lock file needs to look like this: abc.202710827155.manually-locked # abc.321740827155.manual-lock-waiting # abc.023856739300.job-running # abc.023856739300.job-waiting # abc.023856739300.test-failed allResult = 'all' in testbedList if allResult == False: if self.testbedExists(testbedList) == False: self.printErrorMsg('No such testbed %s' % testbedList) return else: testbedList = self.getAllTestbedNames() for testbed in testbedList: # If testbed is already locked, check if it is same user trying to lock testbed again. # If not the same user, then go into waiting mode. lockedFiles = glob.glob(self.testbedDir + testbed + '.' + '*' + '.manually-locked') lockedFileWaiting = glob.glob(self.testbedDir + testbed + '.' + '*' + '.manual-lock-waiting') ignoreSameUserManualLock = 0 if typeOfLock == 'Manual-Lock': if lockedFiles != []: fileContents = infraLib.getFileContents(lockedFiles[0]) if lockedFileWaiting != []: # fileContents = ['typeOfLock=Manual-Lock', 'user=hgee', 'date=Sep-10', # 'time=18:40:12', 'priority=3', 'submissionId=184012579324'] fileContents = infraLib.getFileContents(lockedFileWaiting[0]) if lockedFiles != [] or lockedFileWaiting != []: user = fileContents[1].split('=')[1] if user == os.environ.get('USER'): ignoreSameUserManualLock = 1 # If manually locking by the same user, then ignore this user continue isTestFailed = glob.glob(self.testbedDir + testbed + '.' + '*' + '.test-failed') isTestbedRunningAJob = glob.glob(self.testbedDir + testbed + '.' + '*' + '.job-running') isManuallyLocked = glob.glob(self.testbedDir + testbed + '.' + '*' + '.manually-locked') isThereAJobWaiting = glob.glob(self.testbedDir + testbed + '.' + '*' + '.job-waiting') self.submissionId = generateSubmissionId() # Create an .ini file for each testbed writeToIniObj = ConfigParser.ConfigParser() writeToIniObj.add_section(testbed) # Go to the wait list. if self.isTestbedFree(testbed) == 'Not Free': if typeOfLock == 'Manual-Lock' and ignoreSameUserManualLock == 0: lockFileName = self.testbedDir + testbed + '.' + self.submissionId + '.manual-lock-waiting' os.system('touch ' + lockFileName) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'TypeOfLock', 'Manual-Lock') if typeOfLock == 'Job': lockFileName = self.testbedDir + testbed + '.' + self.submissionId + '.job-waiting' os.system('touch ' + lockFileName) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'TypeOfLock', 'Job') if self.isTestbedFree(testbed) == 'Free': # The testbed is free if typeOfLock == 'Manual-Lock': lockFileName = self.testbedDir + testbed + '.' + self.submissionId + '.manually-locked' os.system('touch ' + lockFileName) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'TypeOfLock', 'Manually-Locked') if typeOfLock == 'Job': # This is to notify runjob's ExecuteJobFile lockFileName = self.testbedDir + testbed + '.' + self.submissionId + '.job-waiting' os.system('touch ' + lockFileName) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'TypeOfLock', 'Job') self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'User', os.environ.get('USER')) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'Date', infraLib.getDate()) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'Time', infraLib.getTime('noMilli')) self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'Priority', '3') self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'SubmissionId', self.submissionId) if processId != None: self.writeToIniFile(writeToIniObj, lockFileName, testbed, 'ProcessId', str(processId))