From fb836e7301968f54db2f3c2bc985e17a19a9900c Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 14 Feb 2017 17:30:49 +0100 Subject: [PATCH] Start performing json --- lib/webhooks/base_app.py | 100 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 3e73999..870cdab 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -14,6 +14,9 @@ import logging import re import textwrap import datetime +import json +import smtplib +from email.message import EmailMessage # Third party modules import yaml @@ -367,7 +370,102 @@ class BaseHookApp(object): if self.verbose > 1: LOG.debug("Base directory: {!r}".format(self.base_dir)) - self.run() + self.data = sys.stdin.read() + try: + self.json_data = json.loads(self.data) + except Exception as e: + msg = "Got a {n} reading input data as JSON: {e}".format(n=e.__class__.__name__, e=e) + msg += "\nInput data: {!r}".format(self.data) + LOG.error(msg) + self.error_data.append(msg) + else: + + if self.verbose > 1: + LOG.debug("Got JSON data:\n{}".format(pp(self.json_data))) + + self.ref = self.json_data['ref'].split('/')[-1] + self.namespace = self.json_data['project']['namespace'] + self.name = self.json_data['project']['name'] + self.full_name = self.json_data['project']['path_with_namespace'] + + try: + self.run() + except Exception as e: + msg = "Got a {n} performing the deploy: {e}".format(n=e.__class__.__name__, e=e) + msg += "\n\nTraceback:\n{}".format(traceback.format_exc()) + self.error_data.append(msg) + LOG.error(msg) + finally: + if self.full_name: + self.send_error_msgs(self.full_name) + else: + self.send_error_msgs() + LOG.info("Finished.") + sys.exit(0) + + # ------------------------------------------------------------------------- + def send_error_msgs(self, project='undefined'): + + if not self.error_data: + return + + msg = EmailMessage() + + s = '' + if len(self.error_data) > 1: + s = 's' + + body = 'Error{s} while processing {p!r} project:\n\n'.format( + s=s, p=project) + body += '\n\n'.join(self.error_data) + body += '\n\nCheers\nPuppetmaster' + msg.set_content(body) + msg.set_charset('utf-8') + + msg['Subject'] = 'Puppetmaster deploy error{s} for project {p!r}'.format( + s=s, p=project) + msg['From'] = self.sender_address + to_addresses = '' + if self.mail_to_addresses: + to_addresses = ', '.join(self.mail_to_addresses) + else: + to_addresses = self.default_email + msg['To'] = to_addresses + if self.mail_cc_addresses: + msg['CC'] = ', '.join(self.mail_cc_addresses) + + msg['X-Mailer'] = "Puppetmaster deploy script v.{}".format(__version__) + + if self.verbose: + LOG.info("Sending the following mail to {r!r} via {s}:{p}:\n{m}".format( + r=to_addresses, m=msg.as_string(unixfrom=True), + s=self.smtp_server, p=self.smtp_port)) + else: + LOG.info("Sending a mail to {r!r} via {s}:{p}:\n{e}".format( + r=to_addresses, e=pp(self.error_data), + s=self.smtp_server, p=self.smtp_port)) + + server = smtplib.SMTP(self.smtp_server, self.smtp_port) + if 'REQUEST_METHOD' not in os.environ: + if self.verbose > 2: + server.set_debuglevel(2) + elif self.verbose > 1: + server.set_debuglevel(1) + server.starttls() + result = server.send_message(msg) + server.quit() + + if not result.keys(): + LOG.debug("Susseccful sent message to {r!r} via {s}:{p}.".format( + r=to_addresses, s=self.smtp_server, p=self.smtp_port)) + else: + LOG.error(( + "Errors on sending error message for project " + "{pr!r} to {r!r} via {s}:{p}:\n{e}").format( + r=to_addresses, s=self.smtp_server, p=self.smtp_port, + pr=project, e=pp(result))) + + return # ------------------------------------------------------------------------- def run(self): -- 2.39.5