]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Generating Mail about Puppet module versions
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 12:53:26 +0000 (14:53 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 12:53:26 +0000 (14:53 +0200)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/get_module_changes.py

index f2dca250685938e47f52b147a0f17b3016a21b0e..19b0251f4edbd92cdf4211d13b3ef58a52f12851 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.12.4'
+__version__ = '0.12.5'
 
 # vim: ts=4 et list
index 5c26d234d938f8633b8ddd0155dddb949b0bcb83..3944362d838649bd69aa0420543ce06b4e6e559c 100644 (file)
@@ -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')
index 8deca6918c7de3fede1db4e35ac2c327747ed240..24a61c09ebcb96a419674c8454c2b33bc6d8be4a 100644 (file)
@@ -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__":