CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes | Private Member Functions
upload_popcon.ConditionsUploader Class Reference
Inheritance diagram for upload_popcon.ConditionsUploader:

Public Member Functions

def __init__
 
def setHost
 
def signIn
 
def signInAgain
 
def signOut
 
def uploadFile
 

Public Attributes

 hostname
 
 http
 
 password
 
 token
 
 urlTemplate
 
 userName
 

Private Member Functions

def _uploadFile
 

Detailed Description

Upload conditions to the CMS conditions uploader service.

Definition at line 258 of file upload_popcon.py.

Constructor & Destructor Documentation

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

Definition at line 262 of file upload_popcon.py.

263  def __init__(self, hostname = defaultHostname, urlTemplate = defaultUrlTemplate):
264  self.hostname = hostname
265  self.urlTemplate = urlTemplate
266  self.userName = None
267  self.http = None
268  self.password = None

Member Function Documentation

def upload_popcon.ConditionsUploader._uploadFile (   self,
  filename,
  backend = defaultBackend,
  temporaryFile = defaultTemporaryFile 
)
private

Definition at line 337 of file upload_popcon.py.

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

Referenced by upload_popcon.ConditionsUploader.uploadFile().

338  def _uploadFile(self, filename, backend = defaultBackend, temporaryFile = defaultTemporaryFile):
339 
340  basepath = filename.rsplit('.db', 1)[0].rsplit('.txt', 1)[0]
341  basename = os.path.basename(basepath)
342 
343  logging.debug('%s: %s: Creating tar file for upload ...', self.hostname, basename)
344 
345  try:
346  tarFile = tarfile.open(temporaryFile, 'w:bz2')
347 
348  with open('%s.db' % basepath, 'rb') as data:
349  addToTarFile(tarFile, data, 'data.db')
350  except Exception as e:
351  msg = 'Error when creating tar file. \n'
352  msg += 'Please check that you have write access to the directory you are running,\n'
353  msg += 'and that you have enough space on this disk (df -h .)\n'
354  logging.error(msg)
355  raise Exception(msg)
356 
357  with tempfile.NamedTemporaryFile() as metadata:
358  with open('%s.txt' % basepath, 'rb') as originalMetadata:
359  json.dump(json.load(originalMetadata), metadata, sort_keys = True, indent = 4)
360 
361  metadata.seek(0)
362  addToTarFile(tarFile, metadata, 'metadata.txt')
363 
364  tarFile.close()
365 
366  logging.debug('%s: %s: Calculating hash...', self.hostname, basename)
367 
368  fileHash = hashlib.sha1()
369  with open(temporaryFile, 'rb') as f:
370  while True:
371  data = f.read(4 * 1024 * 1024)
372  if not data:
373  break
374  fileHash.update(data)
375 
376  fileHash = fileHash.hexdigest()
377  fileInfo = os.stat(temporaryFile)
378  fileSize = fileInfo.st_size
379 
380  logging.debug('%s: %s: Hash: %s', self.hostname, basename, fileHash)
381 
382  logging.info('%s: %s: Uploading file (%s, size %s) to the %s backend...', self.hostname, basename, fileHash, fileSize, backend)
383  os.rename(temporaryFile, fileHash)
384  try:
385  ret = self.http.query('uploadFile',
386  {
387  'backend': backend,
388  'fileName': basename,
389  'userName': self.userName,
390  },
391  files = {
392  'uploadedFile': fileHash,
393  }
394  )
395  except Exception as e:
396  logging.error('Error from uploading: %s' % str(e))
397  ret = json.dumps( { "status": -1, "upload" : { 'itemStatus' : { basename : {'status':'failed', 'info':str(e)}}}, "error" : str(e)} )
398 
399  os.unlink(fileHash)
400 
401  statusInfo = json.loads(ret)['upload']
402  logging.debug( 'upload returned: %s', statusInfo )
403 
404  okTags = []
405  skippedTags = []
406  failedTags = []
407  for tag, info in statusInfo['itemStatus'].items():
408  logging.debug('checking tag %s, info %s', tag, str(json.dumps(info, indent=4,sort_keys=True)) )
409  if 'ok' in info['status'].lower() :
410  okTags.append( tag )
411  logging.info('tag %s successfully uploaded', tag)
412  if 'skip' in info['status'].lower() :
413  skippedTags.append( tag )
414  logging.warning('found tag %s to be skipped. reason: \n ... \t%s ', tag, info['info'])
415  if 'fail' in info['status'].lower() :
416  failedTags.append( tag )
417  logging.error('found tag %s failed to upload. reason: \n ... \t%s ', tag, info['info'])
418 
419  if len(okTags) > 0: logging.info ("tags sucessfully uploaded: %s ", str(okTags) )
420  if len(skippedTags) > 0: logging.warning("tags SKIPped to upload : %s ", str(skippedTags) )
421  if len(failedTags) > 0: logging.error ("tags FAILed to upload : %s ", str(failedTags) )
422 
423  fileLogURL = 'https://%s/logs/dropBox/getFileLog?fileHash=%s'
424  logging.info('file log at: %s', fileLogURL % (self.hostname,fileHash))
425 
426  return len(okTags)>0
def upload_popcon.ConditionsUploader.setHost (   self,
  hostname 
)

Definition at line 269 of file upload_popcon.py.

References upload_popcon.ConditionsUploader.hostname.

Referenced by upload_popcon.ConditionsUploader.uploadFile().

270  def setHost( self, hostname ):
271  self.hostname = hostname
def upload_popcon.ConditionsUploader.signIn (   self,
  username,
  password 
)
init the server.

Definition at line 272 of file upload_popcon.py.

References upload_popcon.ConditionsUploader.hostname, upload_popcon.ConditionsUploader.http, and upload_popcon.ConditionsUploader.urlTemplate.

Referenced by upload_popcon.ConditionsUploader.signInAgain(), uploadConditions.ConditionsUploader.signInAgain(), and cmstc.TagCollector.signInInteractive().

273  def signIn(self, username, password):
274  ''' init the server.
275  '''
276  self.http = HTTP()
277  self.http.setBaseUrl(self.urlTemplate % self.hostname)
278  '''Signs in the server.
279  '''
280 
281  logging.info('%s: Signing in user %s ...', self.hostname, username)
282  try:
283  self.token = self.http.getToken(username, password)
284  except Exception as e:
285  logging.error("Caught exception when trying to get token for user %s from %s: %s" % (username, self.hostname, str(e)) )
286  return False
287 
288  if not self.token:
289  logging.error("could not get token for user %s from %s" % (username, self.hostname) )
290  return False
291 
292  logging.debug( "got: '%s'", str(self.token) )
293  self.userName = username
294  self.password = password
295  return True
def upload_popcon.ConditionsUploader.signInAgain (   self)

Definition at line 296 of file upload_popcon.py.

References cond::auth::ServiceCredentials.password, cond::CredentialData.password, upload_popcon.ConditionsUploader.password, upload_popcon.ConditionsUploader.signIn(), cond::auth::ServiceCredentials.userName, cond::CredentialData.userName, and upload_popcon.ConditionsUploader.userName.

Referenced by upload_popcon.ConditionsUploader.uploadFile().

297  def signInAgain(self):
298  return self.signIn( self.userName, self.password )
def upload_popcon.ConditionsUploader.signOut (   self)
Signs out the server.

Definition at line 299 of file upload_popcon.py.

References upload_popcon.ConditionsUploader.hostname, cond::TagInfo_t.token, upload_popcon.HTTP.token, and upload_popcon.ConditionsUploader.token.

Referenced by uploadConditions.ConditionsUploader._checkForUpdates().

300  def signOut(self):
301  '''Signs out the server.
302  '''
303 
304  logging.info('%s: Signing out...', self.hostname)
305  # self.http.query('logout')
306  self.token = None
307 
def upload_popcon.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 308 of file upload_popcon.py.

References upload_popcon.ConditionsUploader._uploadFile(), upload_popcon.ConditionsUploader.setHost(), and upload_popcon.ConditionsUploader.signInAgain().

309  def uploadFile(self, filename, backend = defaultBackend, temporaryFile = defaultTemporaryFile):
310  '''Uploads a file to the dropBox.
311 
312  The filename can be without extension, with .db or with .txt extension.
313  It will be stripped and then both .db and .txt files are used.
314  '''
315 
316  basepath = filename.rsplit('.db', 1)[0].rsplit('.txt', 1)[0]
317  metadataFilename = '%s.txt' % basepath
318  with open(metadataFilename, 'rb') as metadataFile:
319  metadata = json.load( metadataFile )
320  # When dest db = prep the hostname has to be set to dev.
321  forceHost = False
322  destDb = metadata['destinationDatabase']
323  ret = False
324  if destDb.startswith('oracle://cms_orcon_prod') or destDb.startswith('oracle://cms_orcoff_prep'):
325  if destDb.startswith('oracle://cms_orcoff_prep'):
326  self.setHost( defaultDevHostname )
327  self.signInAgain()
328  forceHost = True
329  ret = self._uploadFile(filename, backend, temporaryFile)
330  if forceHost:
331  # set back the hostname to the original global setting
332  self.setHost( defaultHostname )
333  self.signInAgain()
334  else:
335  logging.error("DestinationDatabase %s is not valid. Skipping the upload." %destDb)
336  return ret

Member Data Documentation

upload_popcon.ConditionsUploader.hostname

Definition at line 263 of file upload_popcon.py.

Referenced by uploadConditions.ConditionsUploader._checkForUpdates(), upload_popcon.ConditionsUploader._uploadFile(), upload_popcon.ConditionsUploader.setHost(), uploadConditions.ConditionsUploader.setHost(), upload_popcon.ConditionsUploader.signIn(), uploadConditions.ConditionsUploader.signIn(), upload_popcon.ConditionsUploader.signOut(), uploadConditions.ConditionsUploader.signOut(), and uploadConditions.ConditionsUploader.uploadFile().

upload_popcon.ConditionsUploader.http

Definition at line 266 of file upload_popcon.py.

Referenced by upload_popcon.ConditionsUploader.signIn(), and uploadConditions.ConditionsUploader.signIn().

upload_popcon.ConditionsUploader.password

Definition at line 267 of file upload_popcon.py.

Referenced by upload_popcon.ConditionsUploader.signInAgain(), and uploadConditions.ConditionsUploader.signInAgain().

upload_popcon.ConditionsUploader.token

Definition at line 282 of file upload_popcon.py.

Referenced by uploadConditions.HTTP.getToken(), uploadConditions.HTTP.query(), upload_popcon.ConditionsUploader.signOut(), and uploadConditions.ConditionsUploader.signOut().

upload_popcon.ConditionsUploader.urlTemplate

Definition at line 264 of file upload_popcon.py.

Referenced by upload_popcon.ConditionsUploader.signIn(), and uploadConditions.ConditionsUploader.signIn().

upload_popcon.ConditionsUploader.userName

Definition at line 265 of file upload_popcon.py.

Referenced by upload_popcon.ConditionsUploader._uploadFile(), upload_popcon.ConditionsUploader.signInAgain(), uploadConditions.ConditionsUploader.signInAgain(), and uploadConditions.ConditionsUploader.uploadFile().