Guest User

Untitled

a guest
Dec 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. """
  2. Created on Sep 27, 2012
  3.  
  4. @author: dballest
  5. """
  6.  
  7. from WMCore.Database.CMSCouch import Database
  8. from optparse import OptionParser
  9. import sys
  10. import json
  11.  
  12. def readACDCInput(inputFile):
  13. """
  14. _readACDCInput_
  15.  
  16. This expects a text file JSON-compatible with the following structure:
  17.  
  18. [ {'owner' : <owner>, 'group' : <group>, 'collection_name' : <collection_name>,
  19. 'fileset_name' : <fileset_name>, "original_dn" : <original_dn>}, ]
  20.  
  21. It just parses it out and returns it
  22. """
  23. try:
  24. handle = open(inputFile, 'r')
  25. acdcList = json.load(handle)
  26. return acdcList
  27. finally:
  28. handle.close()
  29. return []
  30.  
  31. def readUsersMap(inputFile):
  32. """
  33. _readACDCInput_
  34.  
  35. This expects a text file JSON-compatible with the following structure:
  36.  
  37. {<dn> : [<owner>,<group>],}
  38.  
  39. It just parses it out and returns it
  40. """
  41. try:
  42. handle = open(inputFile, 'r')
  43. usersMap = json.load(handle)
  44. return usersMap
  45. finally:
  46. handle.close()
  47. return {}
  48.  
  49. def main():
  50.  
  51. parser = OptionParser()
  52. parser.add_option("-f", "--input-acdc", dest="acdcList")
  53. parser.add_option("-m", "--input-mapfile", dest="mapFile")
  54. parser.add_option("-u", "--url", dest="url")
  55. parser.add_option("-d", "--dry-run", dest="dryRun",
  56. action="store_true", default=False)
  57. parser.add_option("-l", "--log-file", dest="logFile")
  58.  
  59. (options, _) = parser.parse_args()
  60.  
  61. handle = open(options.logFile, 'w')
  62.  
  63. url = options.url
  64. database = 'wmagent_acdc'
  65. acdcDB = Database(database, url)
  66. handle.write('Opening ACDC database in %s/%s\n' % (url, database))
  67.  
  68. inputACDC = readACDCInput(options.acdcList)
  69. usersMap = readUsersMap(options.mapFile)
  70. handle.write('Have %d workflows to fix\n' % len(inputACDC))
  71. handle.write('=================================================================\n')
  72. for workflow in inputACDC:
  73. collection_name = workflow['collection_name']
  74. fileset_name = workflow['fileset_name']
  75. original_dn = workflow['original_dn']
  76. handle.write('Original workflow: %s\n' % collection_name)
  77. handle.write('Original task: %s\n' % fileset_name)
  78. handle.write('Original owner DN: %s\n' % original_dn)
  79. if original_dn in usersMap:
  80. handle.write('This DN maps to %s-%s\n' % (usersMap[original_dn][1], usersMap[original_dn][0]))
  81. else:
  82. handle.write('The original DN can not be found in the map file, skipping the workflow\n')
  83. continue
  84. params = {'reduce' : False,
  85. 'key' : [usersMap[original_dn][1], usersMap[original_dn][0], collection_name, fileset_name]}
  86. result = acdcDB.loadView('ACDC', 'owner_coll_fileset_docs', params)
  87.  
  88. rows = result['rows']
  89. docIds = map(lambda x : x['id'], rows)
  90. handle.write('Found %d documents to change\n' % len(rows))
  91. handle.write('Changing from %s-%s to %s-%s\n' % (usersMap[original_dn][1], usersMap[original_dn][0],
  92. workflow['group'], workflow['owner']))
  93.  
  94. for docId in docIds:
  95. doc = acdcDB.document(docId)
  96. doc['owner'] = {'group' : workflow['group'], 'user' : workflow['owner']}
  97. if not options.dryRun:
  98. acdcDB.queue(doc)
  99. if not options.dryRun:
  100. response = acdcDB.commit()
  101. else:
  102. response = 'This is a dry-run no changes were made'
  103.  
  104. handle.write('Response to write operation: %s\n'% str(response))
  105. handle.write('Response length: %d\n' % len(response))
  106. handle.write('=================================================================\n')
  107.  
  108. handle.write('Finished script')
  109. handle.close()
  110.  
  111. if __name__ == '__main__':
  112. sys.exit(main())
Add Comment
Please, Sign In to add comment