From: Frank Brehm Date: Fri, 4 Oct 2019 13:48:50 +0000 (+0200) Subject: Adding limits for last updates of modules in forge. X-Git-Tag: 1.7.4~2 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=b9925b6fd154e1dc8cfe6e847b332210e0661166;p=pixelpark%2Fpuppetmaster-webhooks.git Adding limits for last updates of modules in forge. --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index e94ea92..ea010b3 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '1.7.2' +__version__ = '1.7.3' # vim: ts=4 et list diff --git a/lib/webhooks/get_module_changes.py b/lib/webhooks/get_module_changes.py index 3f931e5..086c4a5 100644 --- a/lib/webhooks/get_module_changes.py +++ b/lib/webhooks/get_module_changes.py @@ -12,6 +12,9 @@ from __future__ import absolute_import import logging import datetime import re +import sys + +from numbers import Number from distutils.version import LooseVersion @@ -19,7 +22,7 @@ from distutils.version import LooseVersion from babel.dates import format_datetime, LOCALTZ # Own modules -from fb_tools.common import pp +from fb_tools.common import pp, to_bool from . import __version__ @@ -70,6 +73,8 @@ class GetModuleChangesApp(BaseHookApp): """ default_env = 'development' + default_warn_update_days = 365 + default_crit_update_days = 365 * 2 re_env_name = re.compile(r'^[a-z0-9][a-z0-9_-]*[a-z0-9]$', re.IGNORECASE) @@ -78,10 +83,15 @@ class GetModuleChangesApp(BaseHookApp): """Constructor.""" self._check_date = None + self._warn_update_days = self.default_warn_update_days + self._crit_update_days = self.default_crit_update_days + self._nagios_mode = False description = _( "Generates a list of all Puppets modules, which are newer " - "in Puppet forge than in a defined environment.") + "in Puppet forge than in a defined environment. " + "It also generates a list of deprecated module and a list of modules, " + "their last update on Forge was longer than a year ago.") self._environment = self.default_env @@ -124,6 +134,52 @@ class GetModuleChangesApp(BaseHookApp): return None return format_datetime(self.check_date, 'yyyy-MM-dd HH:mm:ss z', tzinfo=LOCALTZ) + # ----------------------------------------------------------- + @property + def warn_update_days(self): + """The warning level in days, when the module was last updated on Puppet forge.""" + return self._warn_update_days + + @warn_update_days.setter + def warn_update_days(self, value): + if not isinstance(value, Number): + msg = _("The value of {w} must be a numeric value: {v!r}").format( + w='warn_update_days', v=value) + raise TypeError(msg) + if value < 1: + msg = _("The value of {w} must be at leas one day: {v}").format( + w='warn_update_days', v=value) + raise ValueError(msg) + self._warn_update_days = value + + # ----------------------------------------------------------- + @property + def crit_update_days(self): + """The warning level in days, when the module was last updated on Puppet forge.""" + return self._crit_update_days + + @crit_update_days.setter + def crit_update_days(self, value): + if not isinstance(value, Number): + msg = _("The value of {w} must be a numeric value: {v!r}").format( + w='crit_update_days', v=value) + raise TypeError(msg) + if value < 1: + msg = _("The value of {w} must be at leas one day: {v}").format( + w='crit_update_days', v=value) + raise ValueError(msg) + self._crit_update_days = value + + # ----------------------------------------------------------- + @property + def nagios_mode(self): + """Work in Nagios mode instead of sending mails.""" + return self._nagios_mode + + @nagios_mode.setter + def nagios_mode(self, value): + self._nagios_mode = to_bool(value) + # ------------------------------------------------------------------------- def as_dict(self, short=True): """ @@ -137,8 +193,11 @@ class GetModuleChangesApp(BaseHookApp): res['check_date'] = self.check_date res['check_date_str'] = self.check_date_str + res['crit_update_days'] = self.crit_update_days res['default_env'] = self.default_env res['environment'] = self.environment + res['nagios_mode'] = self.nagios_mode + res['warn_update_days'] = self.warn_update_days return res @@ -154,6 +213,25 @@ class GetModuleChangesApp(BaseHookApp): "default: {!r}.").format(self.default_env), ) + self.arg_parser.add_argument( + '-w', '--warn', '--warning', metavar=_('DAYS'), dest='warn', type=float, + help=_( + "The warning level in days, when the module was last updated on Puppet forge " + "(default: {}).").format(self.default_warn_update_days) + ) + + self.arg_parser.add_argument( + '-c', '--crit', '--critical', metavar=_('DAYS'), dest='crit', type=float, + help=_( + "The critical level in days, when the module was last updated on Puppet forge " + "(default: {}).").format(self.default_crit_update_days) + ) + + self.arg_parser.add_argument( + '-n', '--nagios', dest='nagios', action="store_true", + help=_("Work in Nagios mode instead of sending mails.") + ) + # ------------------------------------------------------------------------- def perform_arg_parser(self): @@ -161,6 +239,23 @@ class GetModuleChangesApp(BaseHookApp): if self.args.env: self.environment = self.args.env + if self.args.warn: + self.warn_update_days = self.args.warn + if self.args.crit: + self.crit_update_days = self.args.crit + if self.args.nagios: + self.nagios_mode = True + + LOG.debug("Warning level: {w} days, critical level: {c} days.".format( + w=self.warn_update_days, c=self.crit_update_days)) + + if self.warn_update_days > self.crit_update_days: + msg = _( + "The number of warning days {w} may not be greater than the number " + " of critical days {c}.").format(w=self.warn_update_days, c=self.crit_update_days) + LOG.error(msg) + self.arg_parser.print_usage(sys.stderr) + self.exit(1) # ------------------------------------------------------------------------- def evaluate_config(self, config, yaml_file):