This repository was archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Make coding more python3-ish | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
|
||
DOCUMENTATION = ''' | ||
callback: help | ||
type: notification | ||
short_description: print help message | ||
version_added: historical | ||
description: | ||
- This plugin will print help message when tasks fail. | ||
''' | ||
|
||
from ansible.plugins.callback import CallbackBase | ||
from ansible import constants as C | ||
import os | ||
import logging | ||
|
||
FAIL_LOGFILE = os.path.dirname(C.DEFAULT_LOG_PATH) + "/fail.log" | ||
|
||
class CallbackModule(CallbackBase): | ||
CALLBACK_VERSION = 2.0 | ||
CALLBACK_TYPE = 'notification' | ||
CALLBACK_NAME = 'help' | ||
CALLBACK_NEEDS_WHITELIST = True | ||
|
||
def __init__(self): | ||
|
||
super(CallbackModule, self).__init__() | ||
|
||
if not os.path.exists(os.path.dirname(C.DEFAULT_LOG_PATH)): | ||
os.makedirs(os.path.dirname(C.DEFAULT_LOG_PATH)) | ||
|
||
self.logger = logging.getLogger('fail') | ||
self.logger.setLevel(logging.DEBUG) | ||
self.handler = logging.FileHandler(FAIL_LOGFILE) | ||
self.logger.addHandler(self.handler) | ||
|
||
def print_help_message(self): | ||
self._display.display("Ask for help:", color=C.COLOR_WARN) | ||
self._display.display("Contact us: [email protected]", color=C.COLOR_HIGHLIGHT) | ||
self._display.display("It seems that you encounter some problems. You can send an email to the above email address, attached with the tidb-ansible/inventory.ini and tidb-ansible/log/ansible.log files and the error message, or new issue on https://github.com/pingcap/docs/issues. We'll try our best to help you deploy a TiDB cluster. Thanks. :-)", color=C.COLOR_WARN) | ||
|
||
def v2_runner_on_failed(self, result, ignore_errors=False): | ||
if not ignore_errors: | ||
self.print_help_message() | ||
self.logger.error('[%s]: Ansible FAILED! => playbook: %s; %s; message: %s', result._host.name, self.playbook, result._task, self._dump_results(result._result)) | ||
|
||
def v2_runner_on_unreachable(self, result): | ||
self.print_help_message() | ||
self.logger.error('[%s]: Ansible UNREACHABLE! => playbook: %s; %s; message: %s', result._host.name, self.playbook, result._task, self._dump_results(result._result)) | ||
|
||
def v2_playbook_on_start(self, playbook): | ||
self.playbook = playbook._file_name | ||
open(FAIL_LOGFILE, 'w').close() | ||
|
||
def v2_playbook_on_stats(self, stats): | ||
if os.path.isfile(FAIL_LOGFILE): | ||
count = -1 | ||
with open(FAIL_LOGFILE, 'r') as f: | ||
for count, line in enumerate(f): | ||
pass | ||
count += 1 | ||
|
||
if count > 0: | ||
self._display.banner("ERROR MESSAGE SUMMARY") | ||
with open(FAIL_LOGFILE, 'r') as f: | ||
for _, line in enumerate(f): | ||
self._display.display(line, color=C.COLOR_ERROR) | ||
self.print_help_message() | ||
else: | ||
self._display.display("Congrats! All goes well. :-)", color=C.COLOR_OK) |