Commit 0965bde5 by David Baumgold

Hipchat plugin: ignore import errors until we care about them

parent 99c0fe5e
import os import os
import prettytable
import hipchat
import time import time
import random
from ansible import utils from ansible import utils
try:
import prettytable
except ImportError:
prettytable = None
try:
import hipchat
except ImportError:
hipchat = None
class CallbackModule(object): class CallbackModule(object):
...@@ -24,30 +29,40 @@ class CallbackModule(object): ...@@ -24,30 +29,40 @@ class CallbackModule(object):
""" """
def __init__(self): def __init__(self):
self.enabled = "HIPCHAT_TOKEN" in os.environ
if 'HIPCHAT_TOKEN' in os.environ: if not self.enabled:
self.start_time = time.time() return
self.task_report = []
self.last_task = None # make sure we got our imports
self.last_task_changed = False if not hipchat:
self.last_task_count = 0 raise ImportError(
self.last_task_delta = 0 "The hipchat plugin requires the hipchat Python module, "
self.last_task_start = time.time() "which is not installed or was not found."
self.condensed_task_report = (os.getenv('HIPCHAT_CONDENSED', True) == True) )
self.room = os.getenv('HIPCHAT_ROOM', 'ansible') if not prettytable:
self.from_name = os.getenv('HIPCHAT_FROM', 'ansible') raise ImportError(
self.allow_notify = (os.getenv('HIPCHAT_NOTIFY') != 'false') "The hipchat plugin requires the prettytable Python module, "
try: "which is not installed or was not found."
self.hipchat_conn = hipchat.HipChat(token=os.getenv('HIPCHAT_TOKEN')) )
except Exception as e: self.start_time = time.time()
utils.warning("Unable to connect to hipchat: {}".format(e)) self.task_report = []
self.hipchat_msg_prefix = os.getenv('HIPCHAT_MSG_PREFIX', '') self.last_task = None
self.hipchat_msg_color = os.getenv('HIPCHAT_MSG_COLOR', '') self.last_task_changed = False
self.printed_playbook = False self.last_task_count = 0
self.playbook_name = None self.last_task_delta = 0
self.enabled = True self.last_task_start = time.time()
else: self.condensed_task_report = (os.getenv('HIPCHAT_CONDENSED', True) == True)
self.enabled = False self.room = os.getenv('HIPCHAT_ROOM', 'ansible')
self.from_name = os.getenv('HIPCHAT_FROM', 'ansible')
self.allow_notify = (os.getenv('HIPCHAT_NOTIFY') != 'false')
try:
self.hipchat_conn = hipchat.HipChat(token=os.getenv('HIPCHAT_TOKEN'))
except Exception as e:
utils.warning("Unable to connect to hipchat: {}".format(e))
self.hipchat_msg_prefix = os.getenv('HIPCHAT_MSG_PREFIX', '')
self.hipchat_msg_color = os.getenv('HIPCHAT_MSG_COLOR', '')
self.printed_playbook = False
self.playbook_name = None
def _send_hipchat(self, message, room=None, from_name=None, color=None, message_format='text'): def _send_hipchat(self, message, room=None, from_name=None, color=None, message_format='text'):
...@@ -221,7 +236,7 @@ class CallbackModule(object): ...@@ -221,7 +236,7 @@ class CallbackModule(object):
summary_output = "<b>{}</b>: <i>{}</i> - ".format(self.hipchat_msg_prefix, host) summary_output = "<b>{}</b>: <i>{}</i> - ".format(self.hipchat_msg_prefix, host)
for summary_item in ['ok', 'changed', 'unreachable', 'failures']: for summary_item in ['ok', 'changed', 'unreachable', 'failures']:
if stats[summary_item] != 0: if stats[summary_item] != 0:
summary_output += "<b>{}</b> - {} ".format(summary_item, stats[summary_item]) summary_output += "<b>{}</b> - {} ".format(summary_item, stats[summary_item])
summary_all_host_output.append(summary_output) summary_all_host_output.append(summary_output)
self._send_hipchat("<br />".join(summary_all_host_output), message_format='html') self._send_hipchat("<br />".join(summary_all_host_output), message_format='html')
msg = "<b>{description}</b>: Finished Ansible run for <b><i>{play}</i> in {min:02} minutes, {sec:02} seconds</b><br /><br />".format( msg = "<b>{description}</b>: Finished Ansible run for <b><i>{play}</i> in {min:02} minutes, {sec:02} seconds</b><br /><br />".format(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment