From 536b0668b9f75573755922544e4a635d6f2d8bbe Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 7 Sep 2018 14:53:26 +0200 Subject: [PATCH] Generating Mail about Puppet module versions --- lib/webhooks/__init__.py | 2 +- lib/webhooks/base_app.py | 10 ++--- lib/webhooks/get_module_changes.py | 68 +++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index f2dca25..19b0251 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.12.4' +__version__ = '0.12.5' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 5c26d23..3944362 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -102,7 +102,7 @@ class BaseHookApp(BaseObject): main_branches = ('development', 'test', 'production') # ------------------------------------------------------------------------- - def __init__(self, appname=None, base_dir=None, verbose=0, version=__version__): + def __init__(self, appname=None, base_dir=None, verbose=0, version=__version__, no_cc=False): """Constructor.""" if not base_dir: @@ -155,9 +155,9 @@ class BaseHookApp(BaseObject): self.default_parent_dir = '/var/log/webhooks' self.default_email = DEFAULT_TO_EMAIL self.mail_to_addresses = [] - self.mail_cc_addresses = [ - self.default_email, - ] + self.mail_cc_addresses = [] + if not no_cc: + self.mail_cc_addresses = [self.default_email,] self.sender_address = DEFAULT_FROM_SENDER self._log_directory = os.sep + os.path.join('var', 'log', 'webhooks') @@ -874,7 +874,7 @@ class BaseHookApp(BaseObject): s=s, a=self.appname) subject = 'Puppetmaster error{s} processing {a!r}'.format( s=s, a=self.appname) - body += '\n\n'.join(self.error_data) + body += '\n'.join(self.error_data) body += '\n\nCheers\nPuppetmaster' msg.set_content(body) msg.set_charset('utf-8') diff --git a/lib/webhooks/get_module_changes.py b/lib/webhooks/get_module_changes.py index 8deca69..24a61c0 100644 --- a/lib/webhooks/get_module_changes.py +++ b/lib/webhooks/get_module_changes.py @@ -98,7 +98,7 @@ class GetModuleChangesApp(BaseHookApp): self._environment = self.default_env super(GetModuleChangesApp, self).__init__( - appname=appname, verbose=verbose, version=version) + appname=appname, verbose=verbose, version=version, no_cc=True) # ----------------------------------------------------------- @property @@ -177,6 +177,7 @@ class GetModuleChangesApp(BaseHookApp): self.exit(7) version_infos = self.get_newer_modules(module_infos) + self.generate_version_msgs(version_infos) # ------------------------------------------------------------------------- def get_newer_modules(self, module_infos): @@ -240,6 +241,71 @@ class GetModuleChangesApp(BaseHookApp): return version_infos + # ------------------------------------------------------------------------- + def generate_version_msgs(self, version_infos): + + if not version_infos: + msg = ( + "Didn't found any modules in environment {!r} with a\n" + "newer version on Puppet Forge.\n\n:-D" + ).format(self.environment) + self.error_data.append(msg) + return + + s = '' + if len(version_infos) != 1: + s = 's' + + self.error_data.append(( + "Found {n} module{s} in environment {e!r} with a " + "newer version on Puppet Forge.\n\n:-(\n").format( + n=len(version_infos), s=s, e=self.environment)) + + label = { + 'name': 'Module', + 'full_name': 'Full Module name', + 'local_version': 'Used Version', + 'forge_version': 'Version on Puppet Forge', + } + width = {} + for key in label.keys(): + width[key] = len(label[key]) + + for version_info in version_infos: + for key in label.keys(): + if len(str(version_info[key])) > width[key]: + width[key] = len(str(version_info[key])) + + len_total = 0 + first = True + for key in width.keys(): + len_total += width[key] + if not first: + len_total += 3 + first = False + + template = "{{name:<{name}}} | " + template += "{{full_name:<{full_name}}} | " + template += "{{local_version:<{local_version}}} | " + template += "{{forge_version:<{forge_version}}}" + + template = template.format(**width) + + if self.verbose > 2: + LOG.debug("Zeilen-Template:\n{}".format(template)) + LOG.debug("Zeilen-Länge: {}".format(len_total)) + + title = template.format(**label) + self.error_data.append(title) + self.error_data.append('=' * len_total) + + for version_info in version_infos: + self.error_data.append(template.format( + name=version_info['name'], full_name=version_info['full_name'], + local_version=str(version_info['local_version']), + forge_version=str(version_info['forge_version']) + )) + # ============================================================================= if __name__ == "__main__": -- 2.39.5