From: Frank Brehm Date: Fri, 7 Sep 2018 11:59:50 +0000 (+0200) Subject: Evaluating version X-Git-Tag: 1.0.1~2^2~3 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=236e1f7a148f5803535e78db878c6df5a1669914;p=pixelpark%2Fpuppetmaster-webhooks.git Evaluating version --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 303ae19..f2dca25 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.12.3' +__version__ = '0.12.4' # vim: ts=4 et list diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 3473336..5c26d23 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -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 diff --git a/lib/webhooks/get_module_changes.py b/lib/webhooks/get_module_changes.py index 57d972c..8deca69 100644 --- a/lib/webhooks/get_module_changes.py +++ b/lib/webhooks/get_module_changes.py @@ -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 # =============================================================================