forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign.py
More file actions
executable file
·331 lines (289 loc) · 12.9 KB
/
assign.py
File metadata and controls
executable file
·331 lines (289 loc) · 12.9 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# Combined assignWorkflow.py with assignProdTaskChain.py
# Author: Allie Reinsvold Hall
# May 2016
"""
Quick request assignment, useful if you want to avoid assigning by
Web interface and reqmgr.py is too unflexible.
"""
import httplib
import re
import os
import sys
import json
import optparse
from dbs.apis.dbsClient import DbsApi
import reqMgrClient as reqMgr
from pprint import pprint
from random import choice
from utils import workflowInfo, siteInfo
dbs3_url = r'https://cmsweb.cern.ch/dbs/prod/global/DBSReader'
T1_SITES = [
"T1_DE_KIT",
"T1_ES_PIC",
"T1_FR_CCIN2P3",
"T1_IT_CNAF",
"T1_RU_JINR",
"T1_UK_RAL",
"T1_US_FNAL"
]
T2_SITES = [
"T2_CH_CERN",
"T2_DE_DESY",
"T2_DE_RWTH",
"T2_ES_CIEMAT",
"T2_FR_CCIN2P3",
"T2_FR_IPHC",
"T2_IT_Bari",
"T2_IT_Legnaro",
"T2_IT_Pisa",
"T2_IT_Rome",
"T2_UK_London_Brunel",
"T2_UK_London_IC",
"T2_US_Caltech",
"T2_US_Florida",
"T2_US_MIT",
"T2_US_Nebraska",
"T2_US_Purdue",
"T2_US_UCSD",
"T2_US_Wisconsin"
]
ALL_SITES = T1_SITES + T2_SITES
def getRandomDiskSite(site=T1_SITES):
"""
Gets a random disk site and append _Disk
"""
s = choice(site)
if s.startswith("T1"):
s += "_Disk"
return s
def assignRequest(url, workflow, team, sites, era, procversion, activity, lfn, procstring, trust_site=False, replica=False, verbose=False, taskchain=False):
"""
Sends assignment request
"""
params = {"action": "Assign",
"Team" + team: "checked",
"SiteWhitelist": sites,
"SiteBlacklist": [],
"MergedLFNBase": lfn,
"UnmergedLFNBase": "/store/unmerged",
"MinMergeSize": 2147483648,
"MaxMergeSize": 4294967296,
"MaxMergeEvents": 50000,
"MaxVSize": 4294967296,
# for task chains "MaxVSize": 20294967,
"SoftTimeout" : 159600,
"Dashboard": activity,
"ProcessingVersion": procversion,
"checkbox" + workflow: "checked",
"execute":True
}
if taskchain:
params["GracePeriod"] = 1000
params["BlockCloseMaxWaitTime"] = 64800
params["BlockCloseMaxFiles"] = 500
params["BlockCloseMaxEvents"] = 20000000
params["BlockCloseMaxSize"] = 5000000000000
params["MaxVSize"] = 20294967
else:
params["CustodialSites"] = []
# add xrootd (trustSiteList)
if trust_site:
params['TrustSitelists'] = True
params['TrustPUSitelists'] = True
params["AcquisitionEra"] = era
params["ProcessingString"] = procstring
# if replica we add NonCustodial sites
if replica:
params["NonCustodialSites"] = getRandomDiskSite(),
params["NonCustodialSubType"] = "Replica"
if taskchain:
params['AutoApproveSubscriptionSites'] = [params["NonCustodialSites"]]
if verbose:
pprint(params)
res = reqMgr.assignWorkflow(url, workflow, team, params)
if res:
print 'Assigned workflow:', workflow, 'to site:', sites, 'with processing version', procversion
else:
print 'Could not assign workflow:', workflow, 'to site:', sites, 'with processing version', procversion
if verbose:
print res
def getRequestDict(url, workflow):
conn = httplib.HTTPSConnection(url, cert_file=os.getenv(
'X509_USER_PROXY'), key_file=os.getenv('X509_USER_PROXY'))
r1 = conn.request("GET", '/reqmgr/reqMgr/request?requestName=' + workflow)
r2 = conn.getresponse()
request = json.loads(r2.read())
return request
def main():
url = 'cmsweb.cern.ch'
url_tb = 'cmsweb-testbed.cern.ch'
# Example: python assign.py -w amaltaro_RVZTT_120404_163607_6269
# -t testbed-relval -s T1_US_FNAL -e CMSSW_6_0_0_pre1_FS_TEST_WMA -p v1 -a
# relval -l /store/backfill/1
usage = "usage: %prog [options] [WORKFLOW]"
parser = optparse.OptionParser(usage=usage)
parser.add_option('-t', '--team', help='Type of Requests', dest='team')
parser.add_option('-s', '--sites', help=' "t1" for Tier-1\'s and "t2" for Tier-2\'s', dest='sites')
parser.add_option('--special', help='Use it for special workflows. You also have to change the code according to the type of WF', dest='special')
parser.add_option('-r', '--replica', action='store_true', dest='replica', default=False, help='Adds a _Disk Non-Custodial Replica parameter')
parser.add_option('-p', '--procversion', help='Processing Version, if empty it will leave the processing version that comes by default in the request', dest='procversion')
parser.add_option('-a', '--activity', help='Dashboard Activity (reprocessing, production or test), if empty will set reprocessing as default', dest='activity')
parser.add_option('-x', '--xrootd', help='Assign with trustSiteLocation=True (allows xrootd capabilities)',
action='store_true', default=False, dest='xrootd')
parser.add_option('-l', '--lfn', help='Merged LFN base', dest='lfn')
parser.add_option('-v', '--verbose', help='Verbose', action='store_true', default=False, dest='verbose')
parser.add_option('--testbed', help='Assign in testbed', action='store_true', default=False, dest='testbed')
parser.add_option('--test', action="store_true",help='Nothing is injected, only print infomation about workflow and Era', dest='test')
parser.add_option('-f', '--file', help='Text file with a list of wokflows. If this option is used, the same settings will be applied to all workflows', dest='file')
parser.add_option('-w', '--workflow', help='Workflow Name', dest='workflow')
parser.add_option('-e', '--era', help='Acquistion era', dest='era')
parser.add_option("--procstr", dest="procstring", help="Overrides Processing String with a single string")
(options, args) = parser.parse_args()
if options.testbed:
url = url_tb
# parse input workflows and files. If both -w and -f options are used, then only the -w inputs are considered.
if not options.workflow:
if args:
wfs = args
elif options.file:
wfs = [l.strip() for l in open(options.file) if l.strip()]
else:
parser.error("Input a workflow name or a file to read them")
sys.exit(0)
else:
wfs = [options.workflow]
#Default values
era = {}
procversion = 1
procstring = {}
replica = False
sites = ALL_SITES
specialStr = ''
taskchain = False
team = 'production'
trust_site = False
SI = siteInfo()
# Handling the parameters given in the command line
# parse site list
if options.sites:
if options.sites == "t1":
sites = SI.sites_T1s
elif options.sites == "t2":
sites = SI.sites_T2s
else:
sites = [site for site in options.sites.split(',')]
else:
sites = SI.sites_T1s + SI.sites_T2s
if options.team:
team = options.team
if options.xrootd:
trust_site = True
if options.replica:
replica = True
for wf in wfs:
# Getting the original dictionary
schema = getRequestDict(url, wf)
wf = reqMgr.Workflow(wf, url=url)
# WF must be in assignment-approved in order to be assigned
if (schema["RequestStatus"] != "assignment-approved"):
print("The workflow '" + wf.name + "' you are trying to assign is not in assignment-approved")
sys.exit(1)
#Check to see if the workflow is a task chain or an ACDC of a taskchain
taskchain = (schema["RequestType"] == "TaskChain") or ((schema["RequestType"] == "Resubmission") and "task" in schema["InitialTaskPath"].split("/")[1])
#Dealing with era and proc string
if taskchain:
# Setting the Era and ProcStr values per Task
for key, value in schema.items():
if type(value) is dict and key.startswith("Task"):
try:
if 'ProcessingString' in value:
procstring[value['TaskName']] = value['ProcessingString']
else:
procstring[value['TaskName']] = schema['ProcessingString']
if 'AcquisitionEra' in value:
era[value['TaskName']] = value['AcquisitionEra']
else:
procstring[value['TaskName']] = schema['AcquisitionEra']
except KeyError:
print("This taskchain request has no AcquisitionEra or ProcessingString defined into the Tasks, aborting...")
sys.exit(1)
# Adding the special string - in case it was provided in the command line
if options.special:
specialStr = '_' + str(options.special)
for key, value in procstring.items():
procstring[key] = value + specialStr
# Override if a value is given using the procstring command
if options.procstring:
procstring = options.procstring
elif not taskchain:
procstring = wf.info['ProcessingString']
if options.era:
era = options.era
elif not taskchain:
era = wf.info['AcquisitionEra']
#Set era and procstring to none for merge ACDCs inside a task chain
if schema["RequestType"] == "Resubmission" and wf.info["PrepID"].startswith("task") and "Merge" in schema["InitialTaskPath"].split("/")[-1]:
era = None
procstring = None
# Must use --lfn option, otherwise workflow won't be assigned
if options.lfn:
lfn = options.lfn
elif "MergedLFNBase" in wf.info:
lfn = wf.info['MergedLFNBase']
else:
print "Can't assign the workflow! Please include workflow lfn using --lfn option."
sys.exit(0)
# activity production by default for taskchains, reprocessing for default by workflows
if options.activity:
activity = options.activity
elif taskchain:
activity = 'production'
else:
activity = 'reprocessing'
# given or default processing version
if options.procversion:
procversion = int(options.procversion)
else:
procversion = wf.info["ProcessingVersion"]
# Check for output dataset existence, and abort if output datasets already exist!
# Don't perform this check for ACDC's
datasets = schema["OutputDatasets"]
i = 0
if not (schema["RequestType"] == "Resubmission" ):
exist = False
maxv = 1
for key, value in schema.items():
if type(value) is dict and key.startswith("Task"):
dbsapi = DbsApi(url=dbs3_url)
# list all datasets with same name but different version
# numbers
datasets = dbsapi.listDatasets(acquisition_era_name=value['AcquisitionEra'], primary_ds_name=value['PrimaryDataset'], detail=True, dataset_access_type='*')
processedName = value['AcquisitionEra'] + '-' + value['ProcessingString'] + "-v\\d+"
# see if any of the dataset names is a match
for ds in datasets:
if re.match(processedName, ds['processed_ds_name']):
print "Existing dset:", ds['dataset'], "(%s)" % ds['dataset_access_type']
maxv = max(maxv, ds['processing_version'])
exist = True
else:
pass
i += 1
# suggest max version
if exist and procversion <= maxv:
print "Some output datasets exist, its advised to assign with v ==", maxv + 1
sys.exit(0)
# If the --test argument was provided, then just print the information
# gathered so far and abort the assignment
if options.test:
print "%s \tEra: %s \tProcStr: %s \tProcVer: %s" % (wf.name, era, procstring, procversion)
print "LFN: %s \tTeam: %s \tSite: %s" % (lfn, team, sites)
print "Taskchain? " + str(taskchain)
print "Activity:" + activity
sys.exit(0)
# Really assigning the workflow now
print wf.name, '\tEra:', era, '\tProcStr:', procstring, '\tProcVer:', procversion, '\tTeam:', team, '\tSite:', sites
assignRequest(url, wf.name, team, sites, era, procversion, activity, lfn, procstring, trust_site, options.replica, options.verbose, taskchain)
sys.exit(0)
if __name__ == "__main__":
main()