"""
Mail notification system
"""
import smtplib
import ast
import re
from email.mime.text import MIMEText
from openstack_lease_it.settings import GLOBAL_CONFIG, EMAIL_REGEXP, LOGGER_NOTIFICATION
[docs]class MailNotification(object): # pylint: disable=too-few-public-methods
"""
A class to abstract e-mail notification
"""
def __init__(self, users):
"""
Not yet implemented
"""
self.users = users
self.smtp = smtplib.SMTP_SSL(GLOBAL_CONFIG['NOTIFICATION_SMTP'])
self.smtp.login(GLOBAL_CONFIG['NOTIFICATION_USERNAME'],
GLOBAL_CONFIG['NOTIFICATION_PASSWORD'])
delete_content = open(GLOBAL_CONFIG['NOTIFICATION_DELETE_CONTENT'], 'r')
lease_content = open(GLOBAL_CONFIG['NOTIFICATION_LEASE_CONTENT'], 'r')
self.notification = {
'delete': delete_content.read(),
'notify': lease_content.read()
}
@staticmethod
[docs] def send(self, notifications):
"""
The all notifications give in parameter, each notifications type (delete or notify) are
a dictionnary of users with a list of instance to notify per user
:param notifications: dict of users and instances
:return: void
"""
for notification in notifications:
for user in notifications[notification]:
mail_text = self.format_mail(user, notification, notifications[notification][user])
mail = MIMEText(mail_text)
mail['Subject'] = GLOBAL_CONFIG['NOTIFICATION_SUBJECT']
mail['From'] = GLOBAL_CONFIG['NOTIFICATION_EMAIL_HEADER']
try:
email = self.users[user]['email']
if not re.match(EMAIL_REGEXP, email) and \
GLOBAL_CONFIG['NOTIFICATION_DOMAIN'] != "":
LOGGER_NOTIFICATION.info("email %s not match a email format"
" (name@domain.com). Add @%s",
email, GLOBAL_CONFIG['NOTIFICATION_DOMAIN'])
email = "{0}@{1}".format(email, GLOBAL_CONFIG['NOTIFICATION_DOMAIN'])
mail['To'] = email
if ast.literal_eval(GLOBAL_CONFIG['NOTIFICATION_DEBUG']):
LOGGER_NOTIFICATION.info("Only notify administrator instead of %s", email)
recipient = [GLOBAL_CONFIG['NOTIFICATION_EMAIL_HEADER']]
else:
recipient = [GLOBAL_CONFIG['NOTIFICATION_EMAIL_HEADER'],
email]
except KeyError:
LOGGER_NOTIFICATION.info("email field of %s as not be found", user)
mail['To'] = GLOBAL_CONFIG['NOTIFICATION_EMAIL_HEADER']
recipient = [GLOBAL_CONFIG['NOTIFICATION_EMAIL_HEADER']]
LOGGER_NOTIFICATION.info("Notification %s to %s", notification, ''.join(recipient))
self.smtp.sendmail(GLOBAL_CONFIG['NOTIFICATION_EMAIL_HEADER'],
recipient,
mail.as_string())