config_1.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. def archivers(self):
  2. """
  3. This method load the configuration and instantiate all the Source and
  4. Destination objects needed for each archiver
  5. """
  6. self.load()
  7. if self._archivers:
  8. return self._archivers
  9. archiver_sections = [
  10. a for a in self.sections() if str(a).startswith('archiver:')
  11. ]
  12. def args_factory(section):
  13. """
  14. Generic function that takes a section from configuration file
  15. and return arguments that are passed to source or destination
  16. factory
  17. """
  18. args_factory = {
  19. k: v if k not in BOOLEAN_OPTIONS else self.parser.getboolean(
  20. section, k)
  21. for (k, v) in self.parser.items(section)
  22. }
  23. args_factory['name'] = re.sub('^(src|dst):', '', section)
  24. args_factory['dry_run'] = self.dry_run
  25. args_factory['conf'] = self
  26. logging.debug(
  27. "'%s' factory parameters: %s", args_factory['name'], {
  28. k: v if k != 'password' else '***********'
  29. for (k, v) in args_factory.items()
  30. })
  31. return args_factory
  32. # Instanciate archivers:
  33. # One archiver is bascally a process of archiving
  34. # One archiver got one source and at least one destination
  35. # It means we have a total of source*count(destination)
  36. # processes to run per archiver
  37. for archiver in archiver_sections:
  38. # If enable: 0 in archiver config ignore it
  39. if not self.parser.getboolean(archiver, 'enable'):
  40. logging.info("Archiver %s is disabled, ignoring it", archiver)
  41. continue
  42. # src and dst sections are comma, semicolon, or carriage return
  43. # separated name
  44. src_sections = [
  45. 'src:{}'.format(i.strip())
  46. for i in re.split(r'\n|,|;', self.parser[archiver]['src'])
  47. ]
  48. # destination is not mandatory
  49. # usefull to just delete data from DB
  50. dst_sections = [
  51. 'dst:{}'.format(i.strip()) for i in re.split(
  52. r'\n|,|;', self.parser[archiver].get('dst', '')) if i
  53. ]
  54. for src_section in src_sections:
  55. src_args_factory = args_factory(src_section)
  56. src = src_factory(**src_args_factory)
  57. destinations = []
  58. for dst_section in dst_sections:
  59. dst_args_factory = args_factory(dst_section)
  60. dst_args_factory['source'] = src
  61. dst = dst_factory(**dst_args_factory)
  62. destinations.append(dst)
  63. self._archivers.append(
  64. Archiver(name=re.sub('^archiver:', '', archiver),
  65. src=src,
  66. dst=destinations,
  67. conf=self))
  68. return self._archivers