CMS 3D CMS Logo

confdbOfflineConverter.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 import sys, os
3 import re
4 import hashlib
5 import os.path
6 import tempfile
7 import requests
8 import shutil
9 import subprocess
10 import atexit
11 from collections import Counter
12 
14 
15  # the machine aliases and interfaces for the *online* database are
16  # cmsonr1-s.cms, cmsonr2-s.cms, cmsonr3-s.cms
17  # cmsonr1-v.cms, cmsonr2-v.cms, cmsonr3-v.cms
18  # but the -s and -v interfaces resolve to the same hosts.
19  # The actual machines and interfaces are
20  # CMSRAC11-S.cms, CMSRAC12-S.cms, CMSRAC21-S.cms
21  # CMSRAC11-V.cms, CMSRAC12-V.cms, CMSRAC21-V.cms
22 
23  # the possible machines and interfaces for the *offline* database are
24  # cmsr1-s.cms, cmsr2-s.cms, cmsr3-s.cms
25  # cmsr1-v.cms, cmsr2-v.cms, cmsr3-v.cms
26  # but the -s and -v interfaces resolve to the same hosts
27  # The actual machines and interfaces are
28  # itrac50011-s.cern.ch, itrac50063-s.cern.ch, itrac50078-s.cern.ch
29  # itrac50011-v.cern.ch, itrac50063-v.cern.ch, itrac50078-v.cern.ch
30 
31  databases = {}
32  databases['v1'] = {}
33  databases['v1']['offline'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch', '-d', 'cms_cond.cern.ch', '-u', 'cms_hltdev_reader', '-s', 'convertMe!' )
34  databases['v1']['hltdev'] = databases['v1']['offline'] # for backwards compatibility
35  databases['v1']['online'] = ( '-t', 'oracle', '-h', 'cmsonr1-s.cms', '-d', 'cms_rcms.cern.ch', '-u', 'cms_hlt_r', '-s', 'convertMe!' )
36  databases['v1']['adg'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch', '-d', 'cms_cond.cern.ch', '-u', 'cms_hlt_gui_r', '-s', 'convertMe!' )
37  databases['v1']['orcoff'] = databases['v1']['adg'] # for backwards compatibility
38  databases['v3'] = {}
39  databases['v3']['run2'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch,cmsr2-s.cern.ch,cmsr3-s.cern.ch', '-d', 'cms_hlt.cern.ch', '-u', 'cms_hlt_gdr_r', '-s', 'convertMe!' )
40  databases['v3']['run3'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch,cmsr2-s.cern.ch,cmsr3-s.cern.ch', '-d', 'cms_hlt.cern.ch', '-u', 'cms_hlt_v3_r', '-s', 'convertMe!' )
41  databases['v3']['dev'] = ( '-t', 'oracle', '-h', 'cmsr1-s.cern.ch,cmsr2-s.cern.ch,cmsr3-s.cern.ch', '-d', 'cms_hlt.cern.ch', '-u', 'cms_hlt_gdrdev_r', '-s', 'convertMe1!' )
42  databases['v3']['online'] = ( '-t', 'oracle', '-h', 'cmsonr1-s.cms', '-d', 'cms_rcms.cern.ch', '-u', 'cms_hlt_gdr_r', '-s', 'convertMe!' )
43  databases['v3']['adg'] = ( '-t', 'oracle', '-h', 'cmsonr1-adg1-s.cern.ch', '-d', 'cms_orcon_adg.cern.ch', '-u', 'cms_hlt_gdr_r', '-s', 'convertMe!' )
44 
45  #ip addresses, there is a bug where we cant do dns over the socks server, sigh
46  ips_for_proxy = {
47  'cmsr1-s.cern.ch' : '10.116.96.89',
48  'cmsr2-s.cern.ch' : '10.116.96.139',
49  'cmsr3-s.cern.ch' : '10.116.96.105'
50  }
51 
52  databases['v3-beta'] = dict(databases['v3'])
53  databases['v3-test'] = dict(databases['v3'])
54  databases['v2'] = dict(databases['v3'])
55  #old converter can only handle a single host so we modify the params accordingly
56  for dbkey in databases['v2']:
57  dbparams = databases['v2'][dbkey]
58  if dbparams[3]=='cmsr1-s.cern.ch,cmsr2-s.cern.ch,cmsr3-s.cern.ch':
59  databases['v2'][dbkey] = dbparams[0:3]+('cmsr1-s.cern.ch',)+dbparams[4:]
60 
61  @staticmethod
63  dir = os.path.realpath(dir)
64  if not os.path.isdir(dir):
65  try:
66  os.makedirs(dir)
67  except:
68  return None
69  return dir
70 
71 
72  def __init__(self, version = 'v3', database = 'run3', url = None, verbose = False,
73  proxy = False, proxyHost = 'localhost', proxyPort = '8080'):
74  self.verbose = verbose
75  self.version = version
76  self.baseDir = '/afs/cern.ch/user/c/confdb/www/%s/lib' % version
77  self.baseUrl = 'https://confdb.web.cern.ch/confdb/%s/lib' % version
78  self.jars = ( 'ojdbc8.jar', 'cmssw-evf-confdb-converter.jar' )
79  if version=='v2':
80  #legacy driver for run2 gui
81  self.jars = ( 'ojdbc6.jar', 'cmssw-evf-confdb-converter.jar' )
82  self.workDir = ''
83  self.proxy = proxy
84  self.proxyHost = proxyHost
85  self.proxyPort = proxyPort
86 
87  # check the schema version
88  if version not in self.databases:
89  # unsupported database version
90  sys.stderr.write( "ERROR: unsupported database version \"%s\"\n" % version)
91 
92  # check the database
93  if database in self.databases[version]:
94  # load the connection parameters for the given database
95  self.connect = self.databases[version][database]
96  else:
97  # unsupported database
98  sys.stderr.write( "ERROR: unknown database \"%s\" for version \"%s\"\n" % (database, version))
99  sys.exit(1)
100 
101  if self.proxy:
102  self.proxy_connect_args = ('--dbproxy', '--dbproxyport', self.proxyPort, '--dbproxyhost', self.proxyHost)
103  temp_connect = []
104  for entry in self.connect:
105  for key,item in self.ips_for_proxy.items():
106  entry = entry.replace(key,item)
107  temp_connect.append(entry.replace(key,item))
108  self.connect = tuple(temp_connect)
109  else:
110  self.proxy_connect_args = ()
111 
112  # check for a custom base URL
113  if url is not None:
114  self.baseUrl = url
115 
116  # try to read the .jar files from AFS, or download them
117  if os.path.isdir(self.baseDir) and all(os.path.isfile(self.baseDir + '/' + jar) for jar in self.jars):
118  # read the .jar fles from AFS
119  self.workDir = self.baseDir
120  else:
121  # try to use $CMSSW_BASE/tmp
122  self.workDir = OfflineConverter.CheckTempDirectory(os.path.join(os.environ['CMSSW_BASE'],'tmp','confdb',self.version))
123  if not self.workDir:
124  # try to use $TMP
125  self.workDir = OfflineConverter.CheckTempDirectory(os.path.join(os.environ['TMP'],'confdb',self.version))
126  if not self.workDir:
127  # create a new temporary directory, and install a cleanup callback
128  self.workDir = tempfile.mkdtemp()
129  atexit.register(shutil.rmtree, self.workDir)
130  # download the .jar files
131  version_website = requests.get(self.baseUrl+"/../confdb.version").text
132  jars_require_update = True
133  if os.path.exists(os.path.join(self.workDir,"confdb.version")):
134  with open(os.path.join(self.workDir,"confdb.version")) as f:
135  version_existing = f.read()
136  if version_existing==version_website:
137  jars_require_update = False
138 
139  if jars_require_update:
140  for jar in self.jars:
141  # download to a temporay name and use an atomic rename (in case an other istance is downloading the same file
142  handle, temp = tempfile.mkstemp(dir = self.workDir, prefix = jar + '.')
143  os.close(handle)
144  request = requests.get(self.baseUrl + '/' + jar)
145  with open(temp,'wb') as f:
146  f.write(request.content)
147  os.rename(temp, self.workDir + '/' + jar)
148  #jars updated, write their version
149  handle, temp = tempfile.mkstemp(dir = self.workDir, prefix = "confdb.version" + '.')
150  os.close(handle)
151  with open(temp,'w') as f:
152  f.write(version_website)
153  os.rename(temp,os.path.join(self.workDir,"confdb.version"))
154 
155  # setup the java command line and CLASSPATH
156  if self.verbose:
157  sys.stderr.write("workDir = %s\n" % self.workDir)
158  # use non-blocking random number source /dev/urandom (instead of /dev/random), see:
159  # http://blockdump.blogspot.fr/2012/07/connection-problems-inbound-connection.html
160  # deal with timezone region not found
161  # http://stackoverflow.com/questions/9156379/ora-01882-timezone-region-not-found
162  # increase the thread stack size from the default of 1 MB to work around java.lang.StackOverflowError errors, see
163  # man java
164  self.javaCmd = ( 'java', '-cp', ':'.join(self.workDir + '/' + jar for jar in self.jars), '-Djava.security.egd=file:///dev/urandom', '-Doracle.jdbc.timezoneAsRegion=false', '-Xss32M', 'confdb.converter.BrowserConverter' )
165 
166 
167  def query(self, *args):
168  args = self.javaCmd + self.connect + self.proxy_connect_args + args
169  if self.verbose:
170  sys.stderr.write("\n" + ' '.join(args) + "\n\n" )
171  sub = subprocess.Popen(
172  args,
173  stdin = None,
174  stdout = subprocess.PIPE,
175  stderr = subprocess.PIPE,
176  shell = False,
177  universal_newlines = True )
178  return sub.communicate()
179 
180 def help():
181  sys.stdout.write("""Usage: %s OPTIONS
182 
183  --v1|--v2|--v3|--v3-beta|--v3-test (specify the ConfDB version [default: v3])
184 
185  --run3|--run2|--dev|--online|--adg (specify the target db [default: run3], online will only work inside p5 network)
186 
187  Note that for v1
188  --orcoff is a synonim of --adg
189  --offline is a synonim of --hltdev
190 
191  --configId <id> (specify the configuration by id)
192  --configName <name> (specify the configuration by name)
193  --runNumber <run> (specify the configuration by run number)
194  [exactly one of --configId OR --configName OR --runNumber is required]
195 
196  --cff (retrieve configuration *fragment*)
197  --input <f1.root[,f2.root]> (insert PoolSource with specified fileNames)
198  --input <files.list> (read a text file which lists input ROOT files)
199  --output <out.root> (insert PoolOutputModule w/ specified fileName)
200  --nopsets (exclude all globale psets)
201  --noedsources (exclude all edsources)
202  --noes (exclude all essources *and* esmodules)
203  --noessources (exclude all essources)
204  --noesmodules (exclude all esmodules)
205  --noservices (exclude all services)
206  --nooutput (exclude all output modules)
207  --nopaths (exclude all paths [+=referenced seqs&mods])
208  --nosequences (don't define sequences [+=referenced s&m])
209  --nomodules (don't define modules)
210  --psets <pset1[,pset2]> (include only specified global psets)
211  --psets <-pset1[,-pset2]> (include all global psets but the specified)
212  --essources <ess1[,ess2]> (include only specified essources)
213  --essources <-ess1[,-ess2]> (include all essources but the specified)
214  --esmodules <esm1[,esm2]> (include only specified esmodules)
215  --esmodules <-esm1[,-esm2]> (include all esmodules but the specified)
216  --services <svc1[,svc2]> (include only specified services)
217  --services <-svc1[,-svc2]> (include all services but the specified)
218  --paths <p1[,p2]> (include only specified paths)
219  --paths <-p1[,-p2]> (include all paths but the specified)
220  --streams <s1[,s2]> (include only specified streams)
221  --datasets <d1[,d2]> (include only specified datasets)
222  --sequences <s1[,s2]> (include sequences, referenced or not!)
223  --modules <p1[,p2]> (include modules, referenced or not!)
224  --blocks <m1::p1[,p2][,m2]> (generate parameter blocks)
225 
226  --verbose (print additional details)
227 """)
228 
229 
230 def main():
231  args = sys.argv[1:]
232  version = 'v3'
233  db = 'run3'
234  verbose = False
235 
236  if not args:
237  help()
238  sys.exit(1)
239 
240  if '--help' in args or '-h' in args:
241  help()
242  sys.exit(0)
243 
244  if '--verbose' in args:
245  verbose = True
246  args.remove('--verbose')
247 
248  arg_count = Counter(args)
249  db_count = arg_count['--v1'] + arg_count['--v2'] + arg_count['--v3'] + arg_count['--v3-beta'] + arg_count['--v3-test']
250  if db_count>1:
251  sys.stderr.write( 'ERROR: conflicting database version specifications: "--v1", "--v2", "--v3", "--v3-beta", and "--v3-test" are mutually exclusive options' )
252  sys.exit(1)
253 
254  if '--v1' in args:
255  version = 'v1'
256  db = 'offline'
257  args.remove('--v1')
258 
259  if '--v2' in args:
260  version = 'v2'
261  db = 'run2'
262  args.remove('--v2')
263 
264  if '--v3' in args:
265  version = 'v3'
266  db = 'run3'
267  args.remove('--v3')
268 
269  if '--v3-beta' in args:
270  version = 'v3-beta'
271  db = 'run3'
272  args.remove('--v3-beta')
273 
274  if '--v3-test' in args:
275  version = 'v3-test'
276  db = 'dev'
277  args.remove('--v3-test')
278 
279  proxy=False
280  proxy_host = "localhost"
281  proxy_port = "8080"
282  if '--dbproxy' in args:
283  proxy = True
284  args.remove('--dbproxy')
285  if '--dbproxyhost' in args:
286  proxy_host = args.pop(args.index('--dbproxyhost')+1)
287  args.remove('--dbproxyhost')
288  if '--dbproxyport' in args:
289  proxy_port = args.pop(args.index('--dbproxyport')+1)
290  args.remove('--dbproxyport')
291 
292 
293  _dbs = {}
294  _dbs['v1'] = [ '--%s' % _db for _db in OfflineConverter.databases['v1'] ] + [ '--runNumber' ]
295  _dbs['v2'] = [ '--%s' % _db for _db in OfflineConverter.databases['v2'] ] + [ '--runNumber' ]
296  _dbs['v3'] = [ '--%s' % _db for _db in OfflineConverter.databases['v3'] ] + [ '--runNumber']
297  _dbs['v3-beta'] = [ '--%s' % _db for _db in OfflineConverter.databases['v3-beta'] ] + [ '--runNumber' ]
298  _dbs['v3-test'] = [ '--%s' % _db for _db in OfflineConverter.databases['v3-test'] ] + [ '--runNumber' ]
299  _dbargs = set(args) & set(sum(_dbs.values(), []))
300 
301  if _dbargs:
302  if len(_dbargs) > 1:
303  sys.stderr.write( "ERROR: too many database specifications: \"" + "\", \"".join( _dbargs) + "\"\n" )
304  sys.exit(1)
305 
306  _arg = _dbargs.pop()
307  db = _arg[2:]
308  if db == 'runNumber':
309  db = 'adg'
310  else:
311  args.remove(_arg)
312 
313  if not db in OfflineConverter.databases[version]:
314  sys.stderr.write( "ERROR: database version \"%s\" incompatible with specification \"%s\"\n" % (version, db) )
315  sys.exit(1)
316 
317  converter = OfflineConverter(version = version, database = db, verbose = verbose,
318  proxy = proxy, proxyHost = proxy_host, proxyPort=proxy_port)
319  out, err = converter.query( * args )
320  if 'ERROR' in err:
321  sys.stderr.write( "%s: error while retriving the HLT menu\n\n%s\n\n" % (sys.argv[0], err) )
322  sys.exit(1)
323  else:
324  sys.stdout.write( out )
325 
326 
327 if __name__ == "__main__":
328  main()
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
def __init__(self, version='v3', database='run3', url=None, verbose=False, proxy=False, proxyHost='localhost', proxyPort='8080')
std::function< unsigned int(align::ID)> Counter
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
Definition: main.py:1