Pagerduty and Hipchat backends plus some cleanup

This commit is contained in:
Matthew Jones 2016-02-10 15:17:11 -05:00
parent 8db2f60405
commit 34ebe0a848
5 changed files with 99 additions and 2 deletions

View File

@ -13,6 +13,8 @@ from awx.main.models.base import * # noqa
from awx.main.notifications.email_backend import CustomEmailBackend
from awx.main.notifications.slack_backend import SlackBackend
from awx.main.notifications.twilio_backend import TwilioBackend
from awx.main.notifications.pagerduty_backend import PagerDutyBackend
from awx.main.notifications.hipchat_backend import HipChatBackend
# Django-JSONField
from jsonfield import JSONField
@ -25,7 +27,9 @@ class NotificationTemplate(CommonModel):
NOTIFICATION_TYPES = [('email', _('Email'), CustomEmailBackend),
('slack', _('Slack'), SlackBackend),
('twilio', _('Twilio'), TwilioBackend)]
('twilio', _('Twilio'), TwilioBackend),
('pagerduty', _('Pagerduty'), PagerDutyBackend),
('hipchat', _('HipChat'), HipChatBackend)]
NOTIFICATION_TYPE_CHOICES = [(x[0], x[1]) for x in NOTIFICATION_TYPES]
CLASS_FOR_NOTIFICATION_TYPE = dict([(x[0], x[2]) for x in NOTIFICATION_TYPES])
@ -69,6 +73,8 @@ class NotificationTemplate(CommonModel):
def send(self, subject, body):
recipients = self.notification_configuration.pop(self.notification_class.recipient_parameter)
if not isinstance(recipients, list):
recipients = [recipients]
sender = self.notification_configuration.pop(self.notification_class.sender_parameter, None)
backend_obj = self.notification_class(**self.notification_configuration)
notification_obj = EmailMessage(subject, body, sender, recipients)

View File

@ -0,0 +1,46 @@
# Copyright (c) 2016 Ansible, Inc.
# All Rights Reserved.
import logging
import requests
from django.core.mail.backends.base import BaseEmailBackend
logger = logging.getLogger('awx.main.notifications.hipchat_backend')
class HipChatBackend(BaseEmailBackend):
init_parameters = {"token": {"label": "Token", "type": "password"},
"channels": {"label": "Destination Channels", "type": "list"},
"color": {"label": "Notification Color", "type": "string"},
"api_url": {"label": "API Url (e.g: https://mycompany.hipchat.com)", "type": "string"},
"notify": {"label": "Notify channel", "type": "bool"},
"message_from": {"label": "Label to be shown with notification", "type": "string"}}
recipient_parameter = "channels"
sender_parameter = "message_from"
def __init__(self, token, color, api_url, notify, fail_silently=False, **kwargs):
super(HipChatBackend, self).__init__(fail_silently=fail_silently)
self.token = token
self.color = color
self.api_url = api_url
self.notify = notify
def send_messages(self, messages):
sent_messages = 0
for m in messages:
for rcp in m.recipients():
r = requests.post("{}/v2/room/{}/notification".format(self.api_url, rcp),
params={"auth_token": self.token},
json={"color": self.color,
"message": m.body,
"notify": self.notify,
"from": m.from_email,
"message_format": "text"})
if r.status_code != 204 and not self.fail_silently:
logger.error("Error sending messages: {}".format(r.text))
raise Exception("Error sending message to hipchat: {}".format(r.text))
sent_messages += 1
return sent_messages

View File

@ -0,0 +1,44 @@
# Copyright (c) 2016 Ansible, Inc.
# All Rights Reserved.
import logging
import pygerduty
from django.core.mail.backends.base import BaseEmailBackend
logger = logging.getLogger('awx.main.notifications.pagerduty_backend')
class PagerDutyBackend(BaseEmailBackend):
init_parameters = {"subdomain": {"label": "Pagerduty subdomain", "type": "string"},
"token": {"label": "API Token", "type": "password"},
"service_key": {"label": "API Service/Integration Key", "type": "string"},
"client_name": {"label": "Client Identifier", "type": "string"}}
recipient_parameter = "service_key"
sender_parameter = "client_name"
def __init__(self, subdomain, token, fail_silently=False, **kwargs):
super(PagerDutyBackend, self).__init__(fail_silently=fail_silently)
self.subdomain = subdomain
self.token = token
def send_messages(self, messages):
sent_messages = 0
try:
pager = pygerduty.PagerDuty(self.subdomain, self.token)
except Exception as e:
if not self.fail_silently:
raise
logger.error("Exception connecting to PagerDuty: {}".format(e))
for m in messages:
try:
pager.trigger_incident(m.recipients()[0],
description=m.subject,
details=m.body,
client=m.from_email)
except Exception as e:
logger.error("Exception sending messages: {}".format(e))
if not self.fail_silently:
raise
return sent_messages

View File

@ -41,7 +41,7 @@ class TwilioBackend(BaseEmailBackend):
body=m.body)
sent_messages += 1
except Exception as e:
logger.error("Exception sending messages: {}".format(e))
if not self.fail_silently:
raise
logger.error("Exception sending messages: {}".format(e))
return sent_messages

View File

@ -82,6 +82,7 @@ psycopg2
pyasn1==0.1.8
pycparser==2.14
pycrypto==2.6.1
pygerduty==0.32.1
PyJWT==1.4.0
pymongo==2.8
pyOpenSSL==0.15.1