CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions
uploadConditions.ConditionsUploader Class Reference
Inheritance diagram for uploadConditions.ConditionsUploader:

Public Member Functions

def __init__ (self, hostname=defaultHostname, urlTemplate=defaultUrlTemplate)
 
def setHost (self, hostname)
 
def signIn (self, username, password)
 
def signOut (self)
 
def uploadFile (self, filename, backend=defaultBackend, temporaryFile=defaultTemporaryFile)
 

Public Attributes

 hostname
 
 http
 
 password
 
 token
 
 urlTemplate
 
 userName
 

Private Member Functions

def _checkForUpdates (self)
 

Detailed Description

Upload conditions to the CMS conditions uploader service.

Definition at line 435 of file uploadConditions.py.

Constructor & Destructor Documentation

◆ __init__()

def uploadConditions.ConditionsUploader.__init__ (   self,
  hostname = defaultHostname,
  urlTemplate = defaultUrlTemplate 
)

Definition at line 439 of file uploadConditions.py.

439  def __init__(self, hostname = defaultHostname, urlTemplate = defaultUrlTemplate):
440  self.hostname = hostname
441  self.urlTemplate = urlTemplate
442  self.userName = None
443  self.http = None
444  self.password = None
445  self.token = None
446 

Member Function Documentation

◆ _checkForUpdates()

def uploadConditions.ConditionsUploader._checkForUpdates (   self)
private
Updates this script, if a new version is found.

Definition at line 494 of file uploadConditions.py.

494  def _checkForUpdates(self):
495  '''Updates this script, if a new version is found.
496  '''
497 
498  logging.debug('%s: Checking if a newer version of this script is available ...', self.hostname)
499  version = int(self.http.query('getUploadScriptVersion'))
500 
501  if version <= __version__:
502  logging.debug('%s: Script is up-to-date.', self.hostname)
503  return
504 
505  logging.info('%s: Updating to a newer version (%s) than the current one (%s): downloading ...', self.hostname, version, __version__)
506 
507  uploadScript = self.http.query('getUploadScript')
508 
509  self.signOut()
510 
511  logging.info('%s: ... saving the new version ...', self.hostname)
512  with open(sys.argv[0], 'wb') as f:
513  f.write(uploadScript)
514 
515  logging.info('%s: ... executing the new version...', self.hostname)
516  os.execl(sys.executable, *([sys.executable] + sys.argv))
517 
518 

References upload_popcon.ConditionsUploader.hostname, uploadConditions.ConditionsUploader.hostname, upload_popcon.ConditionsUploader.http, uploadConditions.ConditionsUploader.http, createfilelist.int, upload_popcon.ConditionsUploader.signOut(), and uploadConditions.ConditionsUploader.signOut().

◆ setHost()

def uploadConditions.ConditionsUploader.setHost (   self,
  hostname 
)

Definition at line 447 of file uploadConditions.py.

447  def setHost( self, hostname ):
448  if not hostname==self.hostname:
449  self.token = None
450  self.hostname = hostname
451 

References upload_popcon.ConditionsUploader.hostname, uploadConditions.ConditionsUploader.hostname, upload_popcon.HTTP.token, uploadConditions.HTTP.token, upload_popcon.ConditionsUploader.token, and uploadConditions.ConditionsUploader.token.

◆ signIn()

def uploadConditions.ConditionsUploader.signIn (   self,
  username,
  password 
)

Definition at line 452 of file uploadConditions.py.

452  def signIn(self, username, password ):
453  if self.token is None:
454  logging.debug("Initializing connection with server %s",self.hostname)
455  ''' init the server.
456  '''
457  self.http = HTTP()
458  if socket.getfqdn().strip().endswith('.cms'):
459  self.http.setProxy('https://cmsproxy.cms:3128/')
460  self.http.setBaseUrl(self.urlTemplate % self.hostname)
461  '''Signs in the server.
462  '''
463  logging.info('%s: Signing in user %s ...', self.hostname, username)
464  try:
465  self.token = self.http.getToken(username, password)
466  except Exception as e:
467  ret = -1
468  # optionally, we may want to have a different return for network related errors:
469  #code = self.http.curl.getinfo(pycurl.RESPONSE_CODE)
470  #if code in ( 502,503,504 ):
471  # ret = -10
472  logging.error("Caught exception when trying to connect to %s: %s" % (self.hostname, str(e)) )
473  return ret
474 
475  if not self.token:
476  logging.error("could not get token for user %s from %s" % (username, self.hostname) )
477  return -2
478  logging.debug( "got: '%s'", str(self.token) )
479  self.userName = username
480  self.password = password
481  else:
482  logging.debug("User %s has been already authenticated." %username)
483  return 0
484 

References EcalCondTools.getToken(), upload_popcon.ConditionsUploader.hostname, uploadConditions.ConditionsUploader.hostname, upload_popcon.ConditionsUploader.http, uploadConditions.ConditionsUploader.http, cond::auth::ServiceCredentials.password, cond::CredentialData.password, upload_popcon.ConditionsUploader.password, uploadConditions.ConditionsUploader.password, str, digitizers_cfi.strip, upload_popcon.HTTP.token, uploadConditions.HTTP.token, upload_popcon.ConditionsUploader.token, uploadConditions.ConditionsUploader.token, upload_popcon.ConditionsUploader.urlTemplate, uploadConditions.ConditionsUploader.urlTemplate, cond::auth::ServiceCredentials.userName, cond::CredentialData.userName, upload_popcon.ConditionsUploader.userName, and uploadConditions.ConditionsUploader.userName.

◆ signOut()

def uploadConditions.ConditionsUploader.signOut (   self)
Signs out the server.

Definition at line 485 of file uploadConditions.py.

485  def signOut(self):
486  '''Signs out the server.
487  '''
488 
489  logging.info('%s: Signing out...', self.hostname)
490  # self.http.query('logout')
491  self.token = None
492 
493 

References upload_popcon.ConditionsUploader.hostname, uploadConditions.ConditionsUploader.hostname, upload_popcon.HTTP.token, uploadConditions.HTTP.token, upload_popcon.ConditionsUploader.token, and uploadConditions.ConditionsUploader.token.

Referenced by uploadConditions.ConditionsUploader._checkForUpdates().

◆ uploadFile()

def uploadConditions.ConditionsUploader.uploadFile (   self,
  filename,
  backend = defaultBackend,
  temporaryFile = defaultTemporaryFile 
)
Uploads a file to the dropBox.

The filename can be without extension, with .db or with .txt extension.
It will be stripped and then both .db and .txt files are used.

Definition at line 519 of file uploadConditions.py.

519  def uploadFile(self, filename, backend = defaultBackend, temporaryFile = defaultTemporaryFile):
520  '''Uploads a file to the dropBox.
521 
522  The filename can be without extension, with .db or with .txt extension.
523  It will be stripped and then both .db and .txt files are used.
524  '''
525 
526  basepath = filename.rsplit('.db', 1)[0].rsplit('.txt', 1)[0]
527  basename = os.path.basename(basepath)
528 
529  logging.debug('%s: %s: Creating tar file for upload ...', self.hostname, basename)
530 
531  try:
532  tarFile = tarfile.open(temporaryFile, 'w:bz2')
533 
534  with open('%s.db' % basepath, 'rb') as data:
535  addToTarFile(tarFile, data, 'data.db')
536  except Exception as e:
537  msg = 'Error when creating tar file. \n'
538  msg += 'Please check that you have write access to the directory you are running,\n'
539  msg += 'and that you have enough space on this disk (df -h .)\n'
540  logging.error(msg)
541  raise Exception(msg)
542 
543  with tempfile.NamedTemporaryFile(mode='rb+') as metadata:
544  with open('%s.txt' % basepath, 'r') as originalMetadata:
545  metadata.write(json.dumps(json.load(originalMetadata), sort_keys = True, indent = 4).encode())
546 
547  metadata.seek(0)
548  addToTarFile(tarFile, metadata, 'metadata.txt')
549 
550  tarFile.close()
551 
552  logging.debug('%s: %s: Calculating hash...', self.hostname, basename)
553 
554  fileHash = hashlib.sha1()
555  with open(temporaryFile, 'rb') as f:
556  while True:
557  data = f.read(4 * 1024 * 1024)
558  if not data:
559  break
560  fileHash.update(data)
561 
562  fileHash = fileHash.hexdigest()
563  fileInfo = os.stat(temporaryFile)
564  fileSize = fileInfo.st_size
565 
566  logging.debug('%s: %s: Hash: %s', self.hostname, basename, fileHash)
567 
568  logging.info('%s: %s: Uploading file (%s, size %s) to the %s backend...', self.hostname, basename, fileHash, fileSize, backend)
569  os.rename(temporaryFile, fileHash)
570  try:
571  ret = self.http.query('uploadFile',
572  {
573  'backend': backend,
574  'fileName': basename,
575  'userName': self.userName,
576  },
577  files = {
578  'uploadedFile': fileHash,
579  }
580  )
581  except Exception as e:
582  logging.error('Error from uploading: %s' % str(e))
583  ret = json.dumps( { "status": -1, "upload" : { 'itemStatus' : { basename : {'status':'failed', 'info':str(e)}}}, "error" : str(e)} )
584 
585  os.unlink(fileHash)
586 
587  statusInfo = json.loads(ret)['upload']
588  logging.debug( 'upload returned: %s', statusInfo )
589 
590  okTags = []
591  skippedTags = []
592  failedTags = []
593  for tag, info in statusInfo['itemStatus'].items():
594  logging.debug('checking tag %s, info %s', tag, str(json.dumps(info, indent=4,sort_keys=True)) )
595  if 'ok' in info['status'].lower() :
596  okTags.append( tag )
597  logging.info('tag %s successfully uploaded', tag)
598  if 'skip' in info['status'].lower() :
599  skippedTags.append( tag )
600  logging.warning('found tag %s to be skipped. reason: \n ... \t%s ', tag, info['info'])
601  if 'fail' in info['status'].lower() :
602  failedTags.append( tag )
603  logging.error('found tag %s failed to upload. reason: \n ... \t%s ', tag, info['info'])
604 
605  if len(okTags) > 0: logging.info ("tags sucessfully uploaded: %s ", str(okTags) )
606  if len(skippedTags) > 0: logging.warning("tags SKIPped to upload : %s ", str(skippedTags) )
607  if len(failedTags) > 0: logging.error ("tags FAILed to upload : %s ", str(failedTags) )
608 
609  fileLogURL = 'https://cms-conddb.cern.ch/cmsDbBrowser/logs/show_cond_uploader_log/%s/%s'
610  backend = 'Prod'
611  if self.hostname=='cms-conddb-dev.cern.ch':
612  backend = 'Prep'
613  logging.info('file log at: %s', fileLogURL % (backend,fileHash))
614 
615  return len(okTags)>0
616 

References uploadConditions.addToTarFile(), alcaDQMUpload.encode(), upload_popcon.ConditionsUploader.hostname, uploadConditions.ConditionsUploader.hostname, upload_popcon.ConditionsUploader.http, uploadConditions.ConditionsUploader.http, mps_monitormerge.items, str, cond::auth::ServiceCredentials.userName, cond::CredentialData.userName, upload_popcon.ConditionsUploader.userName, and uploadConditions.ConditionsUploader.userName.

Member Data Documentation

◆ hostname

uploadConditions.ConditionsUploader.hostname

◆ http

uploadConditions.ConditionsUploader.http

◆ password

uploadConditions.ConditionsUploader.password

Definition at line 444 of file uploadConditions.py.

Referenced by uploadConditions.ConditionsUploader.signIn().

◆ token

uploadConditions.ConditionsUploader.token

◆ urlTemplate

uploadConditions.ConditionsUploader.urlTemplate

Definition at line 441 of file uploadConditions.py.

Referenced by uploadConditions.ConditionsUploader.signIn().

◆ userName

uploadConditions.ConditionsUploader.userName
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
mps_monitormerge.items
list items
Definition: mps_monitormerge.py:29
conditionUploadTest.uploadFile
def uploadFile(fileName, logFileName)
Definition: conditionUploadTest.py:88
query
Definition: query.py:1
str
#define str(s)
Definition: TestProcessor.cc:53
uploadConditions.addToTarFile
def addToTarFile(tarFile, fileobj, arcname)
Definition: uploadConditions.py:428
Exception
createfilelist.int
int
Definition: createfilelist.py:10
alcaDQMUpload.encode
def encode(args, files)
Definition: alcaDQMUpload.py:32
HTTP
Definition: HTTP.py:1
EcalCondTools.getToken
def getToken(db, tag, since)
Definition: EcalCondTools.py:334