]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Evaluating version
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 11:59:50 +0000 (13:59 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 11:59:50 +0000 (13:59 +0200)
lib/webhooks/__init__.py
lib/webhooks/base_app.py
lib/webhooks/get_module_changes.py

index 303ae19eee7559848d596c729c7ec403d41f85ac..f2dca250685938e47f52b147a0f17b3016a21b0e 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.12.3'
+__version__ = '0.12.4'
 
 # vim: ts=4 et list
index 3473336e0d80d3fde495eeaadc4cac65b9557c71..5c26d234d938f8633b8ddd0155dddb949b0bcb83 100644 (file)
@@ -853,7 +853,7 @@ class BaseHookApp(BaseObject):
         return True
 
     # -------------------------------------------------------------------------
-    def send_error_msgs(self, project='undefined'):
+    def send_error_msgs(self, project=None):
 
         if not self.error_data:
             return
@@ -864,15 +864,22 @@ class BaseHookApp(BaseObject):
         if len(self.error_data) > 1:
             s = 's'
 
-        body = 'Error{s} while processing {p!r} project:\n\n'.format(
-            s=s, p=project)
+        if project:
+            body = 'Error{s} while processing {p!r} project:\n\n'.format(
+                s=s, p=project)
+            subject = 'Puppetmaster deploy error{s} for project {p!r}'.format(
+                s=s, p=project)
+        else:
+            body = 'Error{s} while processing {a!r}:\n\n'.format(
+                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\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['Subject'] = subject
         msg['From'] = self.sender_address
         to_addresses = ''
         if self.mail_to_addresses:
@@ -916,11 +923,19 @@ class BaseHookApp(BaseObject):
             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)))
+            if project:
+                msg = (
+                    "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))
+            else:
+                msg = (
+                    "Errors on sending error message for {a!r} "
+                    "to {r!r} via {s}:{p}:\n{e}").format(
+                    a=self.appname, r=to_addresses, s=self.smtp_server,
+                    p=self.smtp_port, e=pp(result))
+            LOG.error(msg)
 
         return
 
index 57d972cde0a62b507151ef8adbdce774eee5231f..8deca6918c7de3fede1db4e35ac2c327747ed240 100644 (file)
@@ -24,6 +24,8 @@ import re
 
 from operator import itemgetter
 
+from distutils.version import LooseVersion
+
 # Third party modules
 import six
 import yaml
@@ -51,6 +53,28 @@ class GetModuleChangesError(BaseHookError):
 
     pass
 
+# =============================================================================
+class InvalidEnvironmentError(GetModuleChangesError):
+
+    # -------------------------------------------------------------------------
+    def __init__(self, env_name):
+        """
+        Constructor.
+
+        @param env_name: the name of the invalid environment
+        @type function_name: str
+        """
+
+        self.env_name = env_name
+        if not env_name:
+            self.env_name = '__unkown_environment__'
+
+    # -------------------------------------------------------------------------
+    def __str__(self):
+        """Typecasting into a string for error output."""
+
+        return "Puppet environment {!r} does not exists.".format(self.env_name)
+
 
 # =============================================================================
 class GetModuleChangesApp(BaseHookApp):
@@ -152,14 +176,69 @@ class GetModuleChangesApp(BaseHookApp):
             LOG.error(str(e))
             self.exit(7)
 
-        mod_list = self.get_newer_modules(module_infos)
+        version_infos = self.get_newer_modules(module_infos)
 
     # -------------------------------------------------------------------------
     def get_newer_modules(self, module_infos):
 
         env_found = False
 
-
+        LOG.info("Checking verions of modules ...")
+
+        version_infos = []
+
+        for module_info in module_infos.values():
+
+            if self.verbose > 1:
+                LOG.debug("Checking module {!r} ...".format(module_info.full_name))
+
+            if self.environment not in module_info.local_versions:
+                LOG.debug("Module {m!r} not used in environment {e!r}.".format(
+                    m=module_info.full_name, e=self.environment))
+                continue
+            env_found = True
+            local_version = LooseVersion(module_info.local_versions[self.environment])
+            if self.verbose > 1:
+                LOG.debug("Module {m!r} has local version {v!r}.".format(
+                    m=module_info.full_name, v=str(local_version)))
+
+            if not module_info.forge_avail or not module_info.forge_version:
+                LOG.debug("Module {m!r} not available on Puppet forge.".format(
+                    m=module_info.full_name))
+                continue
+
+            forge_version = LooseVersion(module_info.forge_version)
+            if self.verbose > 1:
+                LOG.debug("Version of module {m!r} on Puppet forge: {v!r}.".format(
+                    m=module_info.full_name, v=str(forge_version)))
+
+            if local_version < forge_version:
+                LOG.info((
+                    "Version of module {m!r} on Puppet forge {fv!r} is newer than "
+                    "the local version {lv!r}.").format(
+                    m=module_info.full_name, lv=str(local_version), fv=str(forge_version)))
+            else:
+                LOG.debug((
+                    "Version of module {m!r} on Puppet forge {fv!r} is equal or older than "
+                    "the local version {lv!r}.").format(
+                    m=module_info.full_name, lv=str(local_version), fv=str(forge_version)))
+                continue
+
+            version_info = {
+                'name': module_info.name,
+                'full_name': module_info.full_name,
+                'local_version': local_version,
+                'forge_version': forge_version
+            }
+            version_infos.append(version_info)
+
+        if not env_found:
+            raise InvalidEnvironmentError(self.environment)
+
+        if self.verbose > 1:
+            LOG.debug("Found newer modules in Puppet forge:\n{}".format(pp(version_infos)))
+
+        return version_infos
 
 # =============================================================================