]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Got upstream versions
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Aug 2018 16:00:56 +0000 (18:00 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Aug 2018 16:00:56 +0000 (18:00 +0200)
lib/webhooks/__init__.py
lib/webhooks/get_forge_modules.py

index a425805d0111b2acd9995291ad14664be2569291..fb088a9ff3e425d3f6c352d4554f5b46d5ae761c 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.9.3'
+__version__ = '0.9.4'
 
 # vim: ts=4 et list
index 98821e188439255d2976ac6e59aeb2538cc93e90..7b3e9d3e480b5584e94f8fcb2bab434b759ff6c4 100644 (file)
@@ -17,6 +17,8 @@ import pipes
 import json
 import re
 import glob
+import warnings
+import datetime
 
 # Third party modules
 import six
@@ -146,14 +148,20 @@ class GetForgeModulesApp(BaseHookApp):
     def run(self):
         """Main routine."""
 
-        msg = "Here I go again ..."
-        LOG.info(msg)
+        dt = datetime.datetime.now().isoformat(' ')
+        msg = "[{}]: Here I go again ...".format(dt)
         self.print_out(msg)
+        print()
 
         self.check_data_dir()
         self.init_puppet_environments()
         self.collect_local_modules()
 
+        print()
+        dt = datetime.datetime.now().isoformat(' ')
+        msg = "[{}]: Finished.".format(dt)
+        self.print_out(msg)
+
     # -------------------------------------------------------------------------
     def collect_local_modules(self):
 
@@ -163,12 +171,88 @@ class GetForgeModulesApp(BaseHookApp):
             self.read_puppetfile(env)
             self.read_metadata_files(env)
 
+        self.get_upstream_versions()
+
         if self.verbose > 2:
             LOG.debug("Found modules:\n{}".format(pp(self.modules)))
 
+    # -------------------------------------------------------------------------
+    def get_upstream_versions(self):
+
+        print()
+        msg = "Collecting module information from upstream ..."
+        self.print_out(msg)
+
+        for full_name in self.modules.keys():
+            self.get_upstream_version(full_name)
+            if not self.verbose:
+                if self.modules[full_name].version_upstream:
+                    print('.', end='', flush=True)
+                else:
+                    print('~', end='', flush=True)
+        if not self.verbose:
+            print()
+
+    # -------------------------------------------------------------------------
+    def get_upstream_version(self, full_name):
+
+        module_info = self.modules[full_name]
+        url = "{url}/{name}".format(url=self.forge_uri, name=full_name)
+
+        LOG.debug(
+            "Trying to get current version of module {} from Puppet forge.".format(full_name))
+        if self.verbose > 1:
+            LOG.debug("URL to request: {}".format(url))
+
+        session = requests.Session()
+        with warnings.catch_warnings(record=True) as w:
+            warnings.simplefilter("always")
+            response = session.request('GET', url, timeout=self.http_timeout)
+            if w:
+                warn_class = w[-1].category.__name__
+                warn_msg = '{}: {}'.format(
+                    warn_class, w[-1].message)
+                if warn_class == 'SubjectAltNameWarning':
+                    LOG.debug(warn_msg)
+                else:
+                    LOG.warn(warn_msg)
+
+        LOG.debug("Got status code: {}.".format(response.status_code))
+        if not response.ok:
+            LOG.debug("Did not found module {} on Puppet forge.".format(full_name))
+            return
+
+        if not response.text:
+            LOG.warn("No output for URL {!r}".format(url))
+            return None
+        if self.verbose > 2:
+            msg = "Output:\n{}".format(response.text)
+            LOG.debug(msg)
+
+        version = None
+
+        js_info = response.json()
+        if 'current_release' in js_info and js_info['current_release']:
+            if 'version' in js_info['current_release']:
+                version = js_info['current_release']['version']
+            else:
+                msg = "Did not found version of current_release of module {}.".format(full_name)
+                LOG.warn(msg)
+        else:
+            msg = "Did not found current_release of module {}.".format(full_name)
+            LOG.warn(msg)
+
+        LOG.debug("Current version of module {name} is {version}.".format(
+            name=full_name, version=version))
+
+        if version:
+            module_info._version_upstream = version
+
     # -------------------------------------------------------------------------
     def read_metadata_files(self, env):
 
+        LOG.info("Reading metadata files of environment {!r} ...".format(env))
+
         pattern = os.path.join(self.puppet_root_env_dir, env, 'modules', '*')
         if self.verbose > 2:
             LOG.debug("Globbing pattern for module directories: {!r}".format(pattern))