zz_failure_summary.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. '''
  2. Ansible callback plugin.
  3. '''
  4. # Reason: In several locations below we disable pylint protected-access
  5. # for Ansible objects that do not give us any public way
  6. # to access the full details we need to report check failures.
  7. # Status: disabled permanently or until Ansible object has a public API.
  8. # This does leave the code more likely to be broken by future Ansible changes.
  9. from pprint import pformat
  10. from ansible.plugins.callback import CallbackBase
  11. from ansible import constants as C
  12. from ansible.utils.color import stringc
  13. class CallbackModule(CallbackBase):
  14. '''
  15. This callback plugin stores task results and summarizes failures.
  16. The file name is prefixed with `zz_` to make this plugin be loaded last by
  17. Ansible, thus making its output the last thing that users see.
  18. '''
  19. CALLBACK_VERSION = 2.0
  20. CALLBACK_TYPE = 'aggregate'
  21. CALLBACK_NAME = 'failure_summary'
  22. CALLBACK_NEEDS_WHITELIST = False
  23. _playbook_file = None
  24. def __init__(self):
  25. super(CallbackModule, self).__init__()
  26. self.__failures = []
  27. def v2_playbook_on_start(self, playbook):
  28. super(CallbackModule, self).v2_playbook_on_start(playbook)
  29. # re: playbook attrs see top comment # pylint: disable=protected-access
  30. self._playbook_file = playbook._file_name
  31. def v2_runner_on_failed(self, result, ignore_errors=False):
  32. super(CallbackModule, self).v2_runner_on_failed(result, ignore_errors)
  33. self.__failures.append(dict(result=result, ignore_errors=ignore_errors))
  34. def v2_playbook_on_stats(self, stats):
  35. super(CallbackModule, self).v2_playbook_on_stats(stats)
  36. if self.__failures:
  37. self._print_failure_details(self.__failures)
  38. def _print_failure_details(self, failures):
  39. '''Print a summary of failed tasks or checks.'''
  40. self._display.display(u'\nFailure summary:\n')
  41. width = len(str(len(failures)))
  42. initial_indent_format = u' {{:>{width}}}. '.format(width=width)
  43. initial_indent_len = len(initial_indent_format.format(0))
  44. subsequent_indent = u' ' * initial_indent_len
  45. subsequent_extra_indent = u' ' * (initial_indent_len + 10)
  46. for i, failure in enumerate(failures, 1):
  47. entries = _format_failure(failure)
  48. self._display.display(u'\n{}{}'.format(initial_indent_format.format(i), entries[0]))
  49. for entry in entries[1:]:
  50. entry = entry.replace(u'\n', u'\n' + subsequent_extra_indent)
  51. indented = u'{}{}'.format(subsequent_indent, entry)
  52. self._display.display(indented)
  53. failed_checks = set()
  54. playbook_context = None
  55. # re: result attrs see top comment # pylint: disable=protected-access
  56. for failure in failures:
  57. # get context from check task result since callback plugins cannot access task vars
  58. playbook_context = playbook_context or failure['result']._result.get('playbook_context')
  59. failed_checks.update(
  60. name
  61. for name, result in failure['result']._result.get('checks', {}).items()
  62. if result.get('failed')
  63. )
  64. if failed_checks:
  65. self._print_check_failure_summary(failed_checks, playbook_context)
  66. def _print_check_failure_summary(self, failed_checks, context):
  67. checks = ','.join(sorted(failed_checks))
  68. # NOTE: context is not set if all failures occurred prior to checks task
  69. summary = (
  70. '\n'
  71. 'The execution of "{playbook}"\n'
  72. 'includes checks designed to fail early if the requirements\n'
  73. 'of the playbook are not met. One or more of these checks\n'
  74. 'failed. To disregard these results, you may choose to\n'
  75. 'disable failing checks by setting an Ansible variable:\n\n'
  76. ' openshift_disable_check={checks}\n\n'
  77. 'Failing check names are shown in the failure details above.\n'
  78. 'Some checks may be configurable by variables if your requirements\n'
  79. 'are different from the defaults; consult check documentation.\n'
  80. 'Variables can be set in the inventory or passed on the\n'
  81. 'command line using the -e flag to ansible-playbook.\n'
  82. ).format(playbook=self._playbook_file, checks=checks)
  83. if context in ['pre-install', 'health']:
  84. summary = (
  85. '\n'
  86. 'You may choose to configure or disable failing checks by\n'
  87. 'setting Ansible variables. To disable those above:\n\n'
  88. ' openshift_disable_check={checks}\n\n'
  89. 'Consult check documentation for configurable variables.\n'
  90. 'Variables can be set in the inventory or passed on the\n'
  91. 'command line using the -e flag to ansible-playbook.\n'
  92. ).format(checks=checks)
  93. # other expected contexts: install, upgrade
  94. self._display.display(summary)
  95. # re: result attrs see top comment # pylint: disable=protected-access
  96. def _format_failure(failure):
  97. '''Return a list of pretty-formatted text entries describing a failure, including
  98. relevant information about it. Expect that the list of text entries will be joined
  99. by a newline separator when output to the user.'''
  100. result = failure['result']
  101. host = result._host.get_name()
  102. play = _get_play(result._task)
  103. if play:
  104. play = play.get_name()
  105. task = result._task.get_name()
  106. msg = result._result.get('msg', u'???')
  107. fields = (
  108. (u'Host', host),
  109. (u'Play', play),
  110. (u'Task', task),
  111. (u'Message', stringc(msg, C.COLOR_ERROR)),
  112. )
  113. if 'checks' in result._result:
  114. fields += ((u'Details', _format_failed_checks(result._result['checks'])),)
  115. row_format = '{:10}{}'
  116. return [row_format.format(header + u':', body) for header, body in fields]
  117. def _format_failed_checks(checks):
  118. '''Return pretty-formatted text describing checks that failed.'''
  119. failed_check_msgs = []
  120. for check, body in checks.items():
  121. if body.get('failed', False): # only show the failed checks
  122. msg = body.get('msg', u"Failed without returning a message")
  123. failed_check_msgs.append('check "%s":\n%s' % (check, msg))
  124. if failed_check_msgs:
  125. return stringc("\n\n".join(failed_check_msgs), C.COLOR_ERROR)
  126. else: # something failed but no checks will admit to it, so dump everything
  127. return stringc(pformat(checks), C.COLOR_ERROR)
  128. # This is inspired by ansible.playbook.base.Base.dump_me.
  129. # re: play/task/block attrs see top comment # pylint: disable=protected-access
  130. def _get_play(obj):
  131. '''Given a task or block, recursively tries to find its parent play.'''
  132. if hasattr(obj, '_play'):
  133. return obj._play
  134. if getattr(obj, '_parent'):
  135. return _get_play(obj._parent)