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  'cmsonr1-adg1-s.cern.ch' : '10.116.96.109',
51  'cmsonr1-s.cms' : '10.176.84.78'
52  }
53 
54  databases['v3-beta'] = dict(databases['v3'])
55  databases['v3-test'] = dict(databases['v3'])
56  databases['v2'] = dict(databases['v3'])
57  #old converter can only handle a single host so we modify the params accordingly
58  for dbkey in databases['v2']:
59  dbparams = databases['v2'][dbkey]
60  if dbparams[3]=='cmsr1-s.cern.ch,cmsr2-s.cern.ch,cmsr3-s.cern.ch':
61  databases['v2'][dbkey] = dbparams[0:3]+('cmsr1-s.cern.ch',)+dbparams[4:]
62 
63  @staticmethod
65  dir = os.path.realpath(dir)
66  if not os.path.isdir(dir):
67  try:
68  os.makedirs(dir)
69  except:
70  return None
71  return dir
72 
73 
74  def __init__(self, version = 'v3', database = 'run3', url = None, verbose = False,
75  proxy = False, proxyHost = 'localhost', proxyPort = '8080',
76  tunnel = False, tunnelPort = '10121'):
77  self.verbose = verbose
78  self.version = version
79  self.baseDir = '/afs/cern.ch/user/c/confdb/www/%s/lib' % version
80  self.baseUrl = 'https://confdb.web.cern.ch/confdb/%s/lib' % version
81  self.jars = ( 'ojdbc8.jar', 'cmssw-evf-confdb-converter.jar' )
82  if version=='v2':
83  #legacy driver for run2 gui
84  self.jars = ( 'ojdbc6.jar', 'cmssw-evf-confdb-converter.jar' )
85  self.workDir = ''
86  self.proxy = proxy
87  self.proxyHost = proxyHost
88  self.proxyPort = proxyPort
89  self.tunnel = tunnel
90  self.tunnelPort = tunnelPort
91 
92  if self.proxy and self.tunnel:
93  sys.stderr.write( "ERROR: proxy and tunnel options can not both be true" )
94  sys.exit(1)
95 
96  # check the schema version
97  if version not in self.databases:
98  # unsupported database version
99  sys.stderr.write( "ERROR: unsupported database version \"%s\"\n" % version)
100 
101  # check the database
102  if database in self.databases[version]:
103  # load the connection parameters for the given database
104  self.connect = self.databases[version][database]
105  else:
106  # unsupported database
107  sys.stderr.write( "ERROR: unknown database \"%s\" for version \"%s\"\n" % (database, version))
108  sys.exit(1)
109 
110  if self.proxy:
111  self.proxy_connect_args = ('--dbproxy', '--dbproxyport', self.proxyPort, '--dbproxyhost', self.proxyHost)
112  temp_connect = []
113  for entry in self.connect:
114  for key,item in self.ips_for_proxy.items():
115  entry = entry.replace(key,item)
116  temp_connect.append(entry.replace(key,item))
117  self.connect = tuple(temp_connect)
118  else:
119  self.proxy_connect_args = ()
120 
121  # this sets the host to localhost
122  if self.tunnel:
123  temp_connect = list(self.connect)
124  host_index = temp_connect.index('-h')
125  temp_connect[host_index+1] = "localhost"
126  self.connect = tuple(temp_connect)
127  self.tunnel_connect_args = ('--dbport', self.tunnelPort)
128  else:
129  self.tunnel_connect_args = ()
130 
131  # check for a custom base URL
132  if url is not None:
133  self.baseUrl = url
134 
135  # try to read the .jar files from AFS, or download them
136  if os.path.isdir(self.baseDir) and all(os.path.isfile(self.baseDir + '/' + jar) for jar in self.jars):
137  # read the .jar fles from AFS
138  self.workDir = self.baseDir
139  else:
140  # try to use $CMSSW_BASE/tmp
141  self.workDir = OfflineConverter.CheckTempDirectory(os.path.join(os.environ['CMSSW_BASE'],'tmp','confdb',self.version))
142  if not self.workDir:
143  # try to use $TMP
144  self.workDir = OfflineConverter.CheckTempDirectory(os.path.join(os.environ['TMP'],'confdb',self.version))
145  if not self.workDir:
146  # create a new temporary directory, and install a cleanup callback
147  self.workDir = tempfile.mkdtemp()
148  atexit.register(shutil.rmtree, self.workDir)
149  # download the .jar files
150  version_website = requests.get(self.baseUrl+"/../confdb.version").text
151  jars_require_update = True
152  if os.path.exists(os.path.join(self.workDir,"confdb.version")):
153  with open(os.path.join(self.workDir,"confdb.version")) as f:
154  version_existing = f.read()
155  if version_existing==version_website:
156  jars_require_update = False
157 
158  if jars_require_update:
159  for jar in self.jars:
160  # download to a temporay name and use an atomic rename (in case an other istance is downloading the same file
161  handle, temp = tempfile.mkstemp(dir = self.workDir, prefix = jar + '.')
162  os.close(handle)
163  request = requests.get(self.baseUrl + '/' + jar)
164  with open(temp,'wb') as f:
165  f.write(request.content)
166  os.rename(temp, self.workDir + '/' + jar)
167  #jars updated, write their version
168  handle, temp = tempfile.mkstemp(dir = self.workDir, prefix = "confdb.version" + '.')
169  os.close(handle)
170  with open(temp,'w') as f:
171  f.write(version_website)
172  os.rename(temp,os.path.join(self.workDir,"confdb.version"))
173 
174  # setup the java command line and CLASSPATH
175  if self.verbose:
176  sys.stderr.write("workDir = %s\n" % self.workDir)
177  # use non-blocking random number source /dev/urandom (instead of /dev/random), see:
178  # http://blockdump.blogspot.fr/2012/07/connection-problems-inbound-connection.html
179  # deal with timezone region not found
180  # http://stackoverflow.com/questions/9156379/ora-01882-timezone-region-not-found
181  # increase the thread stack size from the default of 1 MB to work around java.lang.StackOverflowError errors, see
182  # man java
183  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' )
184 
185 
186  def query(self, *args):
187  args = self.javaCmd + self.connect + self.proxy_connect_args + self.tunnel_connect_args + args
188  if self.verbose:
189  sys.stderr.write("\n" + ' '.join(args) + "\n\n" )
190  sub = subprocess.Popen(
191  args,
192  stdin = None,
193  stdout = subprocess.PIPE,
194  stderr = subprocess.PIPE,
195  shell = False,
196  universal_newlines = True )
197  return sub.communicate()
198 
199 def help():
200  sys.stdout.write("""Usage: %s OPTIONS
201 
202  --v1|--v2|--v3|--v3-beta|--v3-test (specify the ConfDB version [default: v3])
203 
204  --run3|--run2|--dev|--online|--adg (specify the target db [default: run3], online will only work inside p5 network)
205 
206  Note that for v1
207  --orcoff is a synonim of --adg
208  --offline is a synonim of --hltdev
209 
210  --configId <id> (specify the configuration by id)
211  --configName <name> (specify the configuration by name)
212  --runNumber <run> (specify the configuration by run number)
213  [exactly one of --configId OR --configName OR --runNumber is required]
214 
215  --cff (retrieve configuration *fragment*)
216  --input <f1.root[,f2.root]> (insert PoolSource with specified fileNames)
217  --input <files.list> (read a text file which lists input ROOT files)
218  --output <out.root> (insert PoolOutputModule w/ specified fileName)
219  --nopsets (exclude all globale psets)
220  --noedsources (exclude all edsources)
221  --noes (exclude all essources *and* esmodules)
222  --noessources (exclude all essources)
223  --noesmodules (exclude all esmodules)
224  --noservices (exclude all services)
225  --nooutput (exclude all output modules)
226  --nopaths (exclude all paths [+=referenced seqs&mods])
227  --nosequences (don't define sequences [+=referenced s&m])
228  --nomodules (don't define modules)
229  --psets <pset1[,pset2]> (include only specified global psets)
230  --psets <-pset1[,-pset2]> (include all global psets but the specified)
231  --essources <ess1[,ess2]> (include only specified essources)
232  --essources <-ess1[,-ess2]> (include all essources but the specified)
233  --esmodules <esm1[,esm2]> (include only specified esmodules)
234  --esmodules <-esm1[,-esm2]> (include all esmodules but the specified)
235  --services <svc1[,svc2]> (include only specified services)
236  --services <-svc1[,-svc2]> (include all services but the specified)
237  --paths <p1[,p2]> (include only specified paths)
238  --paths <-p1[,-p2]> (include all paths but the specified)
239  --streams <s1[,s2]> (include only specified streams)
240  --datasets <d1[,d2]> (include only specified datasets)
241  --sequences <s1[,s2]> (include sequences, referenced or not!)
242  --modules <p1[,p2]> (include modules, referenced or not!)
243  --blocks <m1::p1[,p2][,m2]> (generate parameter blocks)
244 
245  Options to connect to target db via SOCKS proxy, or direct tunnel:
246  [the options --dbproxy and --dbtunnel are mutually exclusive]
247  --dbproxy (use a SOCKS proxy to connect outside CERN network [default: False])
248  --dbproxyhost <hostname> (host of the SOCKS proxy [default: "localhost"])
249  --dbproxyport <port> (port of the SOCKS proxy [default: 8080])
250  --dbtunnel (use direct tunnel to connect outside CERN network [default: False])
251  --dbtunnelport <port> (port when using a direct tunnel on localhost [default: 10121])
252 
253  --verbose (print additional details)
254 """)
255 
256 
257 def main():
258  args = sys.argv[1:]
259  version = 'v3'
260  db = 'run3'
261  verbose = False
262 
263  if not args:
264  help()
265  sys.exit(1)
266 
267  if '--help' in args or '-h' in args:
268  help()
269  sys.exit(0)
270 
271  if '--verbose' in args:
272  verbose = True
273  args.remove('--verbose')
274 
275  arg_count = Counter(args)
276  db_count = arg_count['--v1'] + arg_count['--v2'] + arg_count['--v3'] + arg_count['--v3-beta'] + arg_count['--v3-test']
277  if db_count>1:
278  sys.stderr.write( 'ERROR: conflicting database version specifications: "--v1", "--v2", "--v3", "--v3-beta", and "--v3-test" are mutually exclusive options' )
279  sys.exit(1)
280 
281  if '--v1' in args:
282  version = 'v1'
283  db = 'offline'
284  args.remove('--v1')
285 
286  if '--v2' in args:
287  version = 'v2'
288  db = 'run2'
289  args.remove('--v2')
290 
291  if '--v3' in args:
292  version = 'v3'
293  db = 'run3'
294  args.remove('--v3')
295 
296  if '--v3-beta' in args:
297  version = 'v3-beta'
298  db = 'run3'
299  args.remove('--v3-beta')
300 
301  if '--v3-test' in args:
302  version = 'v3-test'
303  db = 'dev'
304  args.remove('--v3-test')
305 
306  proxy = False
307  proxy_host = "localhost"
308  proxy_port = "8080"
309  if '--dbproxy' in args:
310  proxy = True
311  args.remove('--dbproxy')
312  if '--dbproxyhost' in args:
313  proxy_host = args.pop(args.index('--dbproxyhost')+1)
314  args.remove('--dbproxyhost')
315  if '--dbproxyport' in args:
316  proxy_port = args.pop(args.index('--dbproxyport')+1)
317  args.remove('--dbproxyport')
318 
319  tunnel = False
320  tunnel_port = "10121"
321  if '--dbtunnel' in args:
322  tunnel = True
323  args.remove('--dbtunnel')
324 
325  if '--dbtunnelport' in args:
326  tunnel_port = args.pop(args.index('--dbtunnelport')+1)
327  args.remove('--dbtunnelport')
328 
329  if tunnel and proxy:
330  sys.stderr.write( 'ERROR: conflicting connection specifications, "--dbtunnel" and "--dbproxy" are mutually exclusive options\n' )
331  sys.exit(1)
332 
333  _dbs = {}
334  _dbs['v1'] = [ '--%s' % _db for _db in OfflineConverter.databases['v1'] ] + [ '--runNumber' ]
335  _dbs['v2'] = [ '--%s' % _db for _db in OfflineConverter.databases['v2'] ] + [ '--runNumber' ]
336  _dbs['v3'] = [ '--%s' % _db for _db in OfflineConverter.databases['v3'] ] + [ '--runNumber']
337  _dbs['v3-beta'] = [ '--%s' % _db for _db in OfflineConverter.databases['v3-beta'] ] + [ '--runNumber' ]
338  _dbs['v3-test'] = [ '--%s' % _db for _db in OfflineConverter.databases['v3-test'] ] + [ '--runNumber' ]
339  _dbargs = set(args) & set(sum(_dbs.values(), []))
340 
341  if _dbargs:
342  if len(_dbargs) > 1:
343  sys.stderr.write( "ERROR: too many database specifications: \"" + "\", \"".join( _dbargs) + "\"\n" )
344  sys.exit(1)
345 
346  _arg = _dbargs.pop()
347  db = _arg[2:]
348  if db == 'runNumber':
349  db = 'adg'
350  else:
351  args.remove(_arg)
352 
353  if not db in OfflineConverter.databases[version]:
354  sys.stderr.write( "ERROR: database version \"%s\" incompatible with specification \"%s\"\n" % (version, db) )
355  sys.exit(1)
356 
357  converter = OfflineConverter(version = version, database = db, verbose = verbose,
358  proxy = proxy, proxyHost = proxy_host, proxyPort = proxy_port,
359  tunnel = tunnel, tunnelPort = tunnel_port)
360  out, err = converter.query( * args )
361  if 'ERROR' in err:
362  sys.stderr.write( "%s: error while retrieving the HLT menu\n\n%s\n\n" % (sys.argv[0], err) )
363  sys.exit(1)
364  else:
365  sys.stdout.write( out )
366 
367 
368 if __name__ == "__main__":
369  main()
def __init__(self, version='v3', database='run3', url=None, verbose=False, proxy=False, proxyHost='localhost', proxyPort='8080', tunnel=False, tunnelPort='10121')
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
std::function< unsigned int(align::ID)> Counter
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
Definition: main.py:1