test
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 259 of file upload_popcon.py.

Constructor & Destructor Documentation

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

Definition at line 263 of file upload_popcon.py.

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

Member Function Documentation

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

Definition at line 340 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().

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

Definition at line 270 of file upload_popcon.py.

References upload_popcon.ConditionsUploader.hostname.

Referenced by upload_popcon.ConditionsUploader.uploadFile().

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

Definition at line 273 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().

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

Definition at line 299 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().

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

Definition at line 302 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().

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

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

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

Member Data Documentation

upload_popcon.ConditionsUploader.hostname

Definition at line 264 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 267 of file upload_popcon.py.

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

upload_popcon.ConditionsUploader.password

Definition at line 268 of file upload_popcon.py.

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

upload_popcon.ConditionsUploader.token

Definition at line 285 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 265 of file upload_popcon.py.

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

upload_popcon.ConditionsUploader.userName

Definition at line 266 of file upload_popcon.py.

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