import logging
import datetime
import re
+import sys
+
+from numbers import Number
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__
"""
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)
"""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
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):
"""
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
"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):
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):