Skip to contentSkip to conversation

update.py: add option -s for send mail

PATCH New [email protected] 1 participant 1 message Oct 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 update.py: add option -s for send mail

From: Changqing Li <[email protected]>

Add option -s to support send mail to admin when update_layer.py return non-zero value. So that admin can find issues ASAP.

---
 layerindex/update.py                   | 27 ++++++++++++++++++++++++++
 templates/layerindex/updatefailure.txt | 11 +++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 templates/layerindex/updatefailure.txt
layerindex/update.py +27 -0
index 5a2642b..f66a747 100755
@@ -35,6 +35,28 @@ except ImportError:
     logger.error("Please install PythonGit 0.3.1 or later in order to use this script")
     sys.exit(1)
 
+def send_email(cmd, log):
+    from django.core.mail import EmailMessage
+    from django.template.loader import get_template
+    import settings
+
+    subject = 'Notification: Layer Index Update ERRORS/WARNINGS'
+    from_email = settings.DEFAULT_FROM_EMAIL
+
+    to_email_list = []
+    for a in settings.ADMINS:
+        to_email_list.append(a[1])
+
+    d = {
+        'cmd': cmd,
+        'log': log,
+    }
+
+    plaintext = get_template('layerindex/updatefailure.txt')
+    text_content = plaintext.render(d)
+
+    msg = EmailMessage(subject, text_content, from_email, to_email_list)
+    msg.send()
 
 def prepare_update_layer_command(options, branch, layer, initial=False):
     """Prepare the update_layer.py command line"""
@@ -178,6 +200,9 @@ def main():
     parser.add_option("", "--keep-temp",
             help = "Preserve temporary directory at the end instead of deleting it",
             action="store_true")
+    parser.add_option("-s", "--send-mail",
+            help = "Send mail to admin when update layer have errors",
+            action="store_true", dest="sendmail", default=False)
 
     options, args = parser.parse_args(sys.argv)
     if len(args) > 1:
@@ -443,6 +468,8 @@ def main():
                         layerupdate.retcode = ret
                         if not options.dryrun:
                             layerupdate.save()
+                        if options.sendmail:
+                            send_email(cmd, output)
                         continue
 
                     col = extract_value('BBFILE_COLLECTIONS', output)
templates/layerindex/updatefailure.txt new file +11 -0
new file mode 100644
index 0000000..ddcb6b9
@@ -0,0 +1,11 @@
+Hi, Admins
+
+There are ERRORS/WARNINGS during layer index update, please check it.
+
+Layer update command:
+{{cmd}}
+
+Details:
+{{log | safe}}
+
+Have a nice day!
Signature · 1 line
2.17.1