Commit c0456d22 by David Baumgold

Merge pull request #1537 from edx/import-later

Ignore import errors until we care about them
parents 99c0fe5e 43a98000
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(
......
...@@ -22,11 +22,12 @@ import time ...@@ -22,11 +22,12 @@ import time
import json import json
import socket import socket
try: try:
import boto
except ImportError:
boto = None
else:
import boto.sqs import boto.sqs
from boto.exception import NoAuthHandlerFound from boto.exception import NoAuthHandlerFound
except ImportError:
print "Boto is required for the sqs_notify callback plugin"
raise
class CallbackModule(object): class CallbackModule(object):
...@@ -47,36 +48,42 @@ class CallbackModule(object): ...@@ -47,36 +48,42 @@ class CallbackModule(object):
- START events - START events
""" """
def __init__(self): def __init__(self):
self.enable_sqs = 'ANSIBLE_ENABLE_SQS' in os.environ
if not self.enable_sqs:
return
# make sure we got our imports
if not boto:
raise ImportError(
"The sqs callback module requires the boto Python module, "
"which is not installed or was not found."
)
self.start_time = time.time() self.start_time = time.time()
if 'ANSIBLE_ENABLE_SQS' in os.environ: if not 'SQS_REGION' in os.environ:
self.enable_sqs = True print 'ANSIBLE_ENABLE_SQS enabled but SQS_REGION ' \
if not 'SQS_REGION' in os.environ: 'not defined in environment'
print 'ANSIBLE_ENABLE_SQS enabled but SQS_REGION ' \ sys.exit(1)
'not defined in environment' self.region = os.environ['SQS_REGION']
sys.exit(1) try:
self.region = os.environ['SQS_REGION'] self.sqs = boto.sqs.connect_to_region(self.region)
try: except NoAuthHandlerFound:
self.sqs = boto.sqs.connect_to_region(self.region) print 'ANSIBLE_ENABLE_SQS enabled but cannot connect ' \
except NoAuthHandlerFound: 'to AWS due invalid credentials'
print 'ANSIBLE_ENABLE_SQS enabled but cannot connect ' \ sys.exit(1)
'to AWS due invalid credentials' if not 'SQS_NAME' in os.environ:
sys.exit(1) print 'ANSIBLE_ENABLE_SQS enabled but SQS_NAME not ' \
if not 'SQS_NAME' in os.environ: 'defined in environment'
print 'ANSIBLE_ENABLE_SQS enabled but SQS_NAME not ' \ sys.exit(1)
'defined in environment' self.name = os.environ['SQS_NAME']
sys.exit(1) self.queue = self.sqs.create_queue(self.name)
self.name = os.environ['SQS_NAME'] if 'SQS_MSG_PREFIX' in os.environ:
self.queue = self.sqs.create_queue(self.name) self.prefix = os.environ['SQS_MSG_PREFIX']
if 'SQS_MSG_PREFIX' in os.environ:
self.prefix = os.environ['SQS_MSG_PREFIX']
else:
self.prefix = ''
self.last_seen_ts = {}
else: else:
self.enable_sqs = False self.prefix = ''
self.last_seen_ts = {}
def runner_on_failed(self, host, res, ignore_errors=False): def runner_on_failed(self, host, res, ignore_errors=False):
if self.enable_sqs: if self.enable_sqs:
......
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