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 445 of file uploadConditions.py.

Constructor & Destructor Documentation

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

Definition at line 449 of file uploadConditions.py.

449  def __init__(self, hostname = defaultHostname, urlTemplate = defaultUrlTemplate):
450  self.hostname = hostname
451  self.urlTemplate = urlTemplate
452  self.userName = None
453  self.http = None
454  self.password = None
455  self.token = None
456 
def __init__(self, hostname=defaultHostname, urlTemplate=defaultUrlTemplate)

Member Function Documentation

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

Definition at line 506 of file uploadConditions.py.

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

506  def _checkForUpdates(self):
507  '''Updates this script, if a new version is found.
508  '''
509 
510  logging.debug('%s: Checking if a newer version of this script is available ...', self.hostname)
511  version = int(self.http.query('getUploadScriptVersion'))
512 
513  if version <= __version__:
514  logging.debug('%s: Script is up-to-date.', self.hostname)
515  return
516 
517  logging.info('%s: Updating to a newer version (%s) than the current one (%s): downloading ...', self.hostname, version, __version__)
518 
519  uploadScript = self.http.query('getUploadScript')
520 
521  self.signOut()
522 
523  logging.info('%s: ... saving the new version ...', self.hostname)
524  with open(sys.argv[0], 'wb') as f:
525  f.write(uploadScript)
526 
527  logging.info('%s: ... executing the new version...', self.hostname)
528  os.execl(sys.executable, *([sys.executable] + sys.argv))
529 
530 
def uploadConditions.ConditionsUploader.setHost (   self,
  hostname 
)
def uploadConditions.ConditionsUploader.signIn (   self,
  username,
  password 
)

Definition at line 462 of file uploadConditions.py.

References 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, cond::TagInfo_t.token, 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.

Referenced by cmstc.TagCollector.signInInteractive().

462  def signIn(self, username, password ):
463  if self.token is None:
464  logging.debug("Initializing connection with server %s",self.hostname)
465  ''' init the server.
466  '''
467  self.http = HTTP()
468  if socket.getfqdn().strip().endswith('.cms'):
469  self.http.setProxy('https://cmsproxy.cms:3128/')
470  self.http.setBaseUrl(self.urlTemplate % self.hostname)
471  '''Signs in the server.
472  '''
473 
474  logging.info('%s: Signing in user %s ...', self.hostname, username)
475  try:
476  self.token = self.http.getToken(username, password)
477  except Exception as e:
478  ret = -1
479  # optionally, we may want to have a different return for network related errors:
480  #code = self.http.curl.getinfo(pycurl.RESPONSE_CODE)
481  #if code in ( 502,503,504 ):
482  # ret = -10
483  logging.error("Caught exception when trying to connect to %s: %s" % (self.hostname, str(e)) )
484  return ret
485 
486  if not self.token:
487  logging.error("could not get token for user %s from %s" % (username, self.hostname) )
488  return -2
489 
490  logging.debug( "got: '%s'", str(self.token) )
491  self.userName = username
492  self.password = password
493  else:
494  logging.debug("User %s has been already authenticated." %username)
495  return 0
496 
def signIn(self, username, password)
#define str(s)
def uploadConditions.ConditionsUploader.signOut (   self)
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 531 of file uploadConditions.py.

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

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

Member Data Documentation

uploadConditions.ConditionsUploader.hostname
uploadConditions.ConditionsUploader.http

Definition at line 453 of file uploadConditions.py.

Referenced by uploadConditions.ConditionsUploader.signIn().

uploadConditions.ConditionsUploader.password

Definition at line 454 of file uploadConditions.py.

Referenced by uploadConditions.ConditionsUploader.signIn().

uploadConditions.ConditionsUploader.token
uploadConditions.ConditionsUploader.urlTemplate

Definition at line 451 of file uploadConditions.py.

Referenced by uploadConditions.ConditionsUploader.signIn().

uploadConditions.ConditionsUploader.userName