]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Retrieving list of modules, which were updated a longer time ago.
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Oct 2019 14:26:56 +0000 (16:26 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Oct 2019 14:26:56 +0000 (16:26 +0200)
lib/webhooks/get_module_changes.py

index 086c4a5788f9921ed142e3cf8c56bd49604f1f7e..732e7a2b2d047d97b868250ce28285aa3ca60513 100644 (file)
@@ -19,7 +19,7 @@ from numbers import Number
 from distutils.version import LooseVersion
 
 # Third party modules
-from babel.dates import format_datetime, LOCALTZ
+from babel.dates import format_datetime, format_timedelta, LOCALTZ
 
 # Own modules
 from fb_tools.common import pp, to_bool
@@ -288,6 +288,8 @@ class GetModuleChangesApp(BaseHookApp):
         depr_infos = self.check_deprecations(module_infos)
         self.generate_deprecation_msgs(depr_infos)
 
+        update_infos = self.check_updates(module_infos)
+
         self.error_data.append("\n" + _("Checked at: {}").format(self.check_date_str))
 
     # -------------------------------------------------------------------------
@@ -535,6 +537,67 @@ class GetModuleChangesApp(BaseHookApp):
         for info in module_infos:
             self.error_data.append(template.format(**info))
 
+    # -------------------------------------------------------------------------
+    def check_updates(self, module_infos):
+
+        env_found = False
+
+        LOG.info(_("Checking for last updets modules on Puppet forge ..."))
+
+        infos = []
+
+        warn_update_timediff = datetime.timedelta(self.warn_update_days)
+
+        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
+
+            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
+
+            if not module_info.forge_updated_at:
+                LOG.debug("Did not found last update_date of module {m!r}.".format(
+                    m=module_info.full_name))
+                continue
+
+            mod_update_timediff = self.check_date - module_info.forge_updated_at
+            tdiff = format_timedelta(
+                mod_update_timediff * -1, granularity='day', add_direction=True,
+                format='long')
+            if self.verbose > 1:
+                LOG.debug("Last update of module {m!r} was {d} days ago ({t}).".format(
+                    m=module_info.full_name, d=mod_update_timediff.days, t=tdiff))
+
+            if mod_update_timediff < warn_update_timediff:
+                continue
+
+            local_dt_str = format_datetime(
+                module_info.forge_updated_at, 'yyyy-MM-dd HH:mm:ss z', tzinfo=LOCALTZ)
+            LOG.info(_(
+                "Module {m!r} was last updated at {at}, {d} days ago ({t}).").format(
+                    m=module_info.full_name, at=local_dt_str, d=mod_update_timediff.days, t=tdiff))
+
+            info = {
+                'module': module_info.full_name,
+                'upd_date': module_info.forge_updated_at,
+                'upd_date_str': local_dt_str,
+                'timediff': mod_update_timediff,
+            }
+            infos.append(info)
+
+        if self.verbose > 1:
+            LOG.debug("Modules with an update on Forge long time ago:\n" + pp(infos))
+        return infos
+
 
 # =============================================================================
 if __name__ == "__main__":