Add notification module and config
diff --git a/requirements.txt b/requirements.txt
index f0b6bc7..b8a1585 100755
--- a/requirements.txt
+++ b/requirements.txt
@@ -11,4 +11,7 @@
oslo.config>=8.1.0
oslo.log>=4.4.0 # Apache-2.0
openstacksdk>0.28.0
-pymysql
\ No newline at end of file
+pymysql
+
+email
+smtplib
\ No newline at end of file
diff --git a/staffeln/common/notify.py b/staffeln/common/notify.py
new file mode 100644
index 0000000..19f8440
--- /dev/null
+++ b/staffeln/common/notify.py
@@ -0,0 +1,49 @@
+# Email notification package
+# This should be upgraded by integrating with mail server to send batch
+import smtplib
+from email.mime.text import MIMEText
+from email.mime.multipart import MIMEMultipart
+import staffeln.conf
+
+CONF = staffeln.conf.CONF
+
+
+def sendEmail(src_email, src_pwd, dest_email, subject, content, smtp_server_domain, smtp_server_port):
+ try:
+ message = MIMEMultipart("alternative")
+ message["Subject"] = subject
+ message["From"] = src_email
+ message["To"] = dest_email
+ part = MIMEText(content, "html")
+ message.attach(part)
+
+ s = smtplib.SMTP(host=smtp_server_domain, port=smtp_server_port)
+ s.ehlo()
+ s.starttls()
+ # we can comment this auth func when use the trusted ip without authentication against the smtp server
+ s.login(src_email, src_pwd)
+ s.sendmail(src_email, dest_email, message.as_string())
+ s.close()
+ return True
+ except Exception as e:
+ print(str(e))
+ return False
+
+def SendNotification(content, receiver=None):
+ subject = "Backup result"
+
+ html = "<h3>${CONTENT}</h3>"
+ html = html.replace("${CONTENT}", content)
+
+ if receiver == None:
+ return
+ if len(receiver) == 0:
+ return
+
+ res = sendEmail(src_email=CONF.notification.sender_email,
+ src_pwd=CONF.notification.sender_pwd,
+ dest_email=CONF.notification.receiver,
+ subject=subject,
+ content=html,
+ smtp_server_domain=CONF.notification.smtp_server_domain,
+ smtp_server_port=CONF.notification.smtp_server_port)
diff --git a/staffeln/conf/notify.py b/staffeln/conf/notify.py
new file mode 100644
index 0000000..85207b3
--- /dev/null
+++ b/staffeln/conf/notify.py
@@ -0,0 +1,54 @@
+from oslo_config import cfg
+from staffeln.i18n import _
+
+notify_group = cfg.OptGroup(
+ "notification",
+ title="Notification options",
+ help="Options under this group are used to define notification settings.",
+)
+
+email_opts = [
+ cfg.StrOpt(
+ "template",
+ default="<h3>${CONTENT}</h3>",
+ help=_("This html template is used to email the backup result."),
+ ),
+ cfg.ListOpt(
+ "receiver",
+ default=[],
+ help=_("The receivers of the bakcup result by email."
+ "A list of addresses to receive backup result emails to. A bare"
+ " string will be treated as a list with 1 address."),
+ ),
+ cfg.StrOpt(
+ "sender_email",
+ help=_("Log in on an SMTP server that requires authentication."
+ "The user name to authenticate with."
+ ),
+ ),
+ cfg.StrOpt(
+ "sender_pwd",
+ help=_("Log in on an SMTP server that requires authentication."
+ "The password for the authentication."
+ ),
+ ),
+ cfg.StrOpt(
+ "smtp_server_domain",
+ default="smtp.gmail.com",
+ help=_("the name of the remote host to which to connect"),
+ ),
+ cfg.StrOpt(
+ "smtp_server_port",
+ default="587",
+ help=_("the port to which to connect"),
+ ),
+]
+
+
+def register_opts(conf):
+ conf.register_group(notify_group)
+ conf.register_opts(email_opts, group=notify_group)
+
+
+def list_opts():
+ return {notify_group: email_opts}