Guest User

Untitled

a guest
Dec 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. '''
  2. Created on Aug 31, 2012
  3.  
  4. @author: dballest
  5. '''
  6.  
  7. import sys
  8. import os
  9. import json
  10. import shlex
  11.  
  12. from optparse import OptionParser
  13.  
  14. from WMCore.Services.PhEDEx.PhEDEx import PhEDEx
  15. from WMCore.Database.CMSCouch import Database
  16. from WMCore.Algorithms.SubprocessAlgos import runCommand
  17.  
  18.  
  19. def checkForMissingFiles(options):
  20. #Initialize stuff
  21. phedexAPI = PhEDEx({'cachepath' : options.cachepath})
  22. acdcCouch = Database('wmagent_acdc', options.acdcUrl)
  23.  
  24. #Let's get the IDs of the ACDC documents for the task/request/group/user
  25. array = [options.group, options.user, options.request, options.task]
  26. result = acdcCouch.loadView('ACDC', 'owner_coll_fileset_docs', {'reduce' : False}, [array])
  27.  
  28. documentsIDs = [x['id'] for x in result['rows']]
  29.  
  30. badFiles = {}
  31.  
  32. #Go through the documents
  33. for docID in documentsIDs:
  34. doc = acdcCouch.document(docID)
  35.  
  36. #Are we going to change this doc? Better back it up
  37. if options.change:
  38. backupFile = os.open(os.path.join(options.backup, "%s.bkp" % doc["_id"]), 'w')
  39. json.dump(doc, backupFile)
  40. backupFile.close()
  41.  
  42. #Go through the files
  43. files = doc["files"]
  44. for inputFile in files:
  45.  
  46. #Use PhEDEx API to get site based on the SE
  47. se = files[inputFile]["locations"][0]
  48. siteLocation = phedexAPI.getBestNodeName(se)
  49.  
  50. #Now get the PFN
  51. pfnDict = phedexAPI.getPFN(siteLocation, inputFile)
  52. inputPfn = pfnDict[(siteLocation, inputFile)]
  53.  
  54. #Run lcg-ls commands and see what we get
  55. command = 'lcg-ls -b -D srmv2 --srm-timeout 60 %s' % inputPfn
  56.  
  57. commandList = shlex.split(command)
  58. try:
  59. (stdout, stderr, exitCode) = runCommand(commandList, False, 70)
  60. except Exception, ex:
  61. exitCode = 99999
  62. stdout = ''
  63. stderr = str(ex)
  64.  
  65. if exitCode:
  66. #Something went wrong with the command
  67. #Mark the file as bad
  68. if docID not in badFiles:
  69. badFiles[docID] = []
  70. badFiles[docID].append(inputFile)
  71. print 'File %s is thought to be bad' % inputFile
  72. print 'Command was %s' % command
  73. print 'Return code was %i' % exitCode
  74. print 'Stdout was %s' % stdout
  75. print 'Stderr was %s' % stderr
  76.  
  77.  
  78.  
  79. def swapLocations(options):
  80. #Initialize stuff
  81. phedexAPI = PhEDEx({'cachepath' : options.cachepath})
  82. acdcCouch = Database('wmagent_acdc', options.acdcUrl)
  83.  
  84. #Let's get the IDs of the ACDC documents for the task/request/group/user
  85. array = [options.group, options.user, options.request, options.task]
  86. result = acdcCouch.loadView('ACDC', 'owner_coll_fileset_docs', {'reduce' : False}, [array])
  87.  
  88. documentsIDs = [x['id'] for x in result['rows']]
  89.  
  90. #Load the map file saying what we want to change of location
  91. mapFile = open(options.map, 'r')
  92. locationMap = json.load(mapFile)
  93. mapFile.close()
  94.  
  95. #Go through the documents
  96. for docID in documentsIDs:
  97. doc = acdcCouch.document(docID)
  98.  
  99. #Are we going to change this doc? Better back it up
  100. if options.change:
  101. backupFile = os.open(os.path.join(options.backup, "%s.bkp" % doc["_id"]), 'w')
  102. json.dump(doc, backupFile)
  103. backupFile.close()
  104.  
  105. #Go through the files
  106. files = doc["files"]
  107. for inputFile in files:
  108.  
  109. #Use PhEDEx API to get site based on the SE
  110. #Then map that to the desired target
  111. se = files[inputFile]["locations"][0]
  112. siteLocation = phedexAPI.getBestNodeName(se)
  113. targetLocation = locationMap.get(siteLocation, siteLocation)
  114.  
  115. if siteLocation == targetLocation:
  116. #Nothing to do with this one, move on
  117. continue
  118.  
  119. if not options.change:
  120. #No changes, then give the commands to move the files
  121. #Get the PFN for both the current location and the target location
  122. pfnDict = phedexAPI.getPFN(siteLocation, inputFile)
  123. inputPfn = pfnDict[(siteLocation, inputFile)]
  124. pfnDict = phedexAPI.getPFN(targetLocation, inputFile)
  125. targetPfn = pfnDict[(targetLocation, inputFile)]
  126.  
  127. #Print it to stdout
  128. print "lcg-cp -D srmv2 -b %s %s" % (inputPfn, targetPfn)
  129.  
  130. else:
  131. #This is changes time, let's move the stuff
  132. targetSE = phedexAPI.getNodeSE(targetLocation)
  133. files[inputFile]["locations"][0] = targetSE
  134. print "Changing location of %s from %s to %s" % (inputFile, se, targetSE)
  135.  
  136. #If specified, commit the changes
  137. if options.change:
  138. acdcCouch.commitOne(doc)
  139.  
  140. return 0
  141.  
  142. def main():
  143. myOptParser = OptionParser()
  144. myOptParser.add_option("-r", "--request", dest = "request")
  145. myOptParser.add_option("-g", "--group", dest = "group")
  146. myOptParser.add_option("-u", "--user", dest = "user")
  147. myOptParser.add_option("-t", "--task", dest = "task")
  148. myOptParser.add_option("-a", "--acdc", dest = "acdcUrl")
  149. myOptParser.add_option("-m", "--map", dest = "map")
  150. myOptParser.add_option("-o", "--mode", dest = "mode",
  151. default = "move")
  152. myOptParser.add_option("-b", "--backup-dir", dest = "backup",
  153. default = "/tmp/backup")
  154. myOptParser.add_option("-c", "--cache-dir", dest = "cachepath",
  155. default = "/tmp/")
  156. myOptParser.add_option("--commit-changes", action = 'store_true',
  157. dest = 'change', default = False)
  158. (options, _) = myOptParser.parse_args()
  159. if options.mode == 'move':
  160. return swapLocations(options)
  161. elif options.mode == 'check':
  162. return checkForMissingFiles(options)
  163.  
  164. return 0
  165.  
  166.  
  167.  
  168. if __name__ == '__main__':
  169. sys.exit(main())
Add Comment
Please, Sign In to add comment