dispatch.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import logging
  2. from borgmatic.hooks import (
  3. cronhub,
  4. cronitor,
  5. healthchecks,
  6. mongodb,
  7. mysql,
  8. ntfy,
  9. pagerduty,
  10. postgresql,
  11. sqlite,
  12. )
  13. logger = logging.getLogger(__name__)
  14. HOOK_NAME_TO_MODULE = {
  15. 'cronhub': cronhub,
  16. 'cronitor': cronitor,
  17. 'healthchecks': healthchecks,
  18. 'mongodb_databases': mongodb,
  19. 'mysql_databases': mysql,
  20. 'ntfy': ntfy,
  21. 'pagerduty': pagerduty,
  22. 'postgresql_databases': postgresql,
  23. 'sqlite_databases': sqlite,
  24. }
  25. def call_hook(function_name, hooks, log_prefix, hook_name, *args, **kwargs):
  26. '''
  27. Given the hooks configuration dict and a prefix to use in log entries, call the requested
  28. function of the Python module corresponding to the given hook name. Supply that call with the
  29. configuration for this hook (if any), the log prefix, and any given args and kwargs. Return any
  30. return value.
  31. Raise ValueError if the hook name is unknown.
  32. Raise AttributeError if the function name is not found in the module.
  33. Raise anything else that the called function raises.
  34. '''
  35. config = hooks.get(hook_name, {})
  36. try:
  37. module = HOOK_NAME_TO_MODULE[hook_name]
  38. except KeyError:
  39. raise ValueError('Unknown hook name: {}'.format(hook_name))
  40. logger.debug('{}: Calling {} hook function {}'.format(log_prefix, hook_name, function_name))
  41. return getattr(module, function_name)(config, log_prefix, *args, **kwargs)
  42. def call_hooks(function_name, hooks, log_prefix, hook_names, *args, **kwargs):
  43. '''
  44. Given the hooks configuration dict and a prefix to use in log entries, call the requested
  45. function of the Python module corresponding to each given hook name. Supply each call with the
  46. configuration for that hook, the log prefix, and any given args and kwargs. Collect any return
  47. values into a dict from hook name to return value.
  48. If the hook name is not present in the hooks configuration, then don't call the function for it
  49. and omit it from the return values.
  50. Raise ValueError if the hook name is unknown.
  51. Raise AttributeError if the function name is not found in the module.
  52. Raise anything else that a called function raises. An error stops calls to subsequent functions.
  53. '''
  54. return {
  55. hook_name: call_hook(function_name, hooks, log_prefix, hook_name, *args, **kwargs)
  56. for hook_name in hook_names
  57. if hooks.get(hook_name)
  58. }
  59. def call_hooks_even_if_unconfigured(function_name, hooks, log_prefix, hook_names, *args, **kwargs):
  60. '''
  61. Given the hooks configuration dict and a prefix to use in log entries, call the requested
  62. function of the Python module corresponding to each given hook name. Supply each call with the
  63. configuration for that hook, the log prefix, and any given args and kwargs. Collect any return
  64. values into a dict from hook name to return value.
  65. Raise AttributeError if the function name is not found in the module.
  66. Raise anything else that a called function raises. An error stops calls to subsequent functions.
  67. '''
  68. return {
  69. hook_name: call_hook(function_name, hooks, log_prefix, hook_name, *args, **kwargs)
  70. for hook_name in hook_names
  71. }