okozachenko | cb31608 | 2021-05-07 21:30:39 +0300 | [diff] [blame^] | 1 | # Email notification package
|
| 2 | # This should be upgraded by integrating with mail server to send batch
|
| 3 | import smtplib
|
| 4 | from email.mime.text import MIMEText
|
| 5 | from email.mime.multipart import MIMEMultipart
|
| 6 | from oslo_log import log
|
| 7 | import staffeln.conf
|
| 8 | from staffeln.common import time as xtime
|
| 9 | from staffeln.common import email
|
| 10 | from staffeln.i18n import _
|
| 11 | from staffeln.conductor import backup
|
| 12 | from staffeln.common import openstack as openstacksdk
|
| 13 |
|
| 14 | CONF = staffeln.conf.CONF
|
| 15 | LOG = log.getLogger(__name__)
|
| 16 |
|
| 17 |
|
| 18 | class BackupResult(object):
|
| 19 |
|
| 20 | def __init__(self):
|
| 21 | self.content = ""
|
| 22 | self.project_list = []
|
| 23 | self.success_backup_list = {}
|
| 24 | self.failed_backup_list = {}
|
| 25 |
|
| 26 | def add_project(self, id, name):
|
| 27 | if id in self.success_backup_list: return
|
| 28 | self.project_list.append({
|
| 29 | "name": name,
|
| 30 | "id": id
|
| 31 | })
|
| 32 | self.success_backup_list[id] = []
|
| 33 | self.failed_backup_list[id] = []
|
| 34 |
|
| 35 | def add_success_backup(self, project_id, volume_id, backup_id):
|
| 36 | if not project_id in self.success_backup_list:
|
| 37 | LOG.error(_("Not registered project is reported for backup result."))
|
| 38 | return
|
| 39 | self.success_backup_list[project_id].append({
|
| 40 | "volume_id": volume_id,
|
| 41 | "backup_id": backup_id,
|
| 42 | })
|
| 43 |
|
| 44 | def add_failed_backup(self, project_id, volume_id, reason):
|
| 45 | if not project_id in self.failed_backup_list:
|
| 46 | LOG.error(_("Not registered project is reported for backup result."))
|
| 47 | return
|
| 48 | self.failed_backup_list[project_id].append({
|
| 49 | "volume_id": volume_id,
|
| 50 | "reason": reason,
|
| 51 | })
|
| 52 |
|
| 53 | def send_result_email(self):
|
| 54 | subject = "Backup result"
|
| 55 | try:
|
| 56 | email.send(
|
| 57 | src_email=CONF.notification.sender_email,
|
| 58 | src_pwd=CONF.notification.sender_pwd,
|
| 59 | dest_email=CONF.notification.receiver,
|
| 60 | subject=subject,
|
| 61 | content=self.content,
|
| 62 | smtp_server_domain=CONF.notification.smtp_server_domain,
|
| 63 | smtp_server_port=CONF.notification.smtp_server_port,
|
| 64 | )
|
| 65 | LOG.info(_("Backup result email sent"))
|
| 66 | except Exception as e:
|
| 67 | LOG.error(_("Backup result email send failed. Please check email configuration. %s" % (str(e))))
|
| 68 |
|
| 69 | def publish(self):
|
| 70 | # 1. get quota
|
| 71 | self.content = "<h3>${TIME}</h3><br>"
|
| 72 | self.content = self.content.replace("${TIME}", xtime.get_current_strtime())
|
| 73 | html = ""
|
| 74 | for project in self.project_list:
|
| 75 | quota = backup.Backup().get_backup_quota(project["id"])
|
| 76 |
|
| 77 | html += "<h3>Project: ${PROJECT}</h3><br>" \
|
| 78 | "<h3>Quota Usage</h3><br>" \
|
| 79 | "<h4>Limit: ${QUOTA_LIMIT}, In Use: ${QUOTA_IN_USE}, Reserved: ${QUOTA_RESERVED}</h4><br>" \
|
| 80 | "<h3>Success List</h3><br>" \
|
| 81 | "<h4>${SUCCESS_VOLUME_LIST}</h4><br>" \
|
| 82 | "<h3>Failed List</h3><br>" \
|
| 83 | "<h4>${FAILED_VOLUME_LIST}</h4><br>"
|
| 84 |
|
| 85 | success_volumes = "<br>".join(
|
| 86 | ["Volume ID: %s, Backup ID: %s" % (str(e["volume_id"]), str(e["backup_id"]))
|
| 87 | for e in self.success_backup_list])
|
| 88 | failed_volumes = "<br>".join(
|
| 89 | ["Volume ID: %s, Reason: %s" % (str(e["volume_id"]), str(e["reason"]))
|
| 90 | for e in self.failed_backup_list])
|
| 91 | html = html.replace("${QUOTA_LIMIT}", str(quota["limit"]))
|
| 92 | html = html.replace("${QUOTA_IN_USE}", str(quota["in_use"]))
|
| 93 | html = html.replace("${QUOTA_RESERVED}", str(quota["reserved"]))
|
| 94 | html = html.replace("${SUCCESS_VOLUME_LIST}", success_volumes)
|
| 95 | html = html.replace("${FAILED_VOLUME_LIST}", failed_volumes)
|
| 96 | html = html.replace("${PROJECT}", project["name"])
|
| 97 | if html == "": return
|
| 98 | self.content += html
|
| 99 | self.send_result_email()
|