From: Frank Brehm Date: Thu, 23 Aug 2018 16:00:56 +0000 (+0200) Subject: Got upstream versions X-Git-Tag: 0.9.8^2~8 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=46afc53aab00d28904fd8f48a16006d23cffeda8;p=pixelpark%2Fpuppetmaster-webhooks.git Got upstream versions --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index a425805..fb088a9 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.9.3' +__version__ = '0.9.4' # vim: ts=4 et list diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 98821e1..7b3e9d3 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -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))