swift.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Use of this source code is governed by a BSD-style
  2. # license that can be found in the LICENSE file.
  3. # Copyright 2019 The OSArchiver Authors. All rights reserved.
  4. """
  5. This module implements the swift remote_log backend which handle sending of
  6. OSArchiver log files into swift backend.
  7. This module use the high level module SwiftService
  8. """
  9. import logging
  10. from os.path import basename
  11. from swiftclient.service import SwiftError, SwiftService, SwiftUploadObject
  12. from osarchiver.destination.file.remote_store import RemoteStore
  13. class Swift(RemoteStore):
  14. """
  15. Swift class used to send log remotely on openstack's swift backend
  16. """
  17. def __init__(self, name=None, date=None, store_options={}):
  18. """
  19. instance osarchiver.remote_log.swift class
  20. """
  21. RemoteStore.__init__(self, backend='swift', name=name,
  22. date=date, store_options=store_options)
  23. self.container = store_options.get('container', None)
  24. self.file_name_prefix = store_options.get('file_name_prefix', '')
  25. self.service = None
  26. def send(self, files=[]):
  27. """
  28. send method implemented which is in charge of sending local log files
  29. to a remote swift destination.
  30. """
  31. options = self.store_options
  32. with SwiftService(options=options) as swift:
  33. file_objects = [
  34. SwiftUploadObject(f,
  35. object_name='%s/%s/%s' % (
  36. self.file_name_prefix,
  37. self.date,
  38. basename(f))
  39. ) for f in files]
  40. for r in swift.upload(self.container, file_objects):
  41. if r['success']:
  42. if 'object' in r:
  43. logging.info("%s successfully uploaded" % r['object'])
  44. else:
  45. error = r['error']
  46. if r['action'] == "create_container":
  47. logging.error("Failed to create container %s: %s",
  48. self.container, error)
  49. elif r['action'] == "upload_object":
  50. logging.error("Failed to upload file %s: %s",
  51. r['object'], error)
  52. else:
  53. logging.error("Unknown error while uploading file: %s",
  54. error)
  55. def clean_exit(self):
  56. """
  57. Tasks to be executed to exit cleanly
  58. """
  59. pass
  60. if __name__ == '__main__':
  61. pass