Skip to content

Commit ccf333c

Browse files
committed
fix: rebase CLI and add required options
1 parent e06b58f commit ccf333c

File tree

1 file changed

+93
-46
lines changed

1 file changed

+93
-46
lines changed

lib/vsc/utils/script_tools.py

Lines changed: 93 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class TimestampMixin:
220220
"""
221221
TIMESTAMP_MIXIN_OPTIONS = {
222222
"start_timestamp": ("The timestamp form which to start, otherwise use the cached value", None, "store", None),
223-
"timestamnp_file": ("Location to cache the start timestamp", None, "store", None),
223+
"timestamp_file": ("Location to cache the start timestamp", None, "store", None),
224224
}
225225

226226
def timestamp_prologue(self):
@@ -251,16 +251,69 @@ def timestamp_epilogue(self):
251251
except Exception as err:
252252
raise TimestampException("Failed to write timestamp") from err
253253

254+
class LogMixin:
255+
"""
256+
A mixin class providing methods for logging.
257+
"""
258+
LOG_MIXIN_OPTIONS = {
259+
'debug': ("Enable debug log mode", None, "store_true", False),
260+
'info': ("Enable info log mode", None, "store_true", False),
261+
'quiet': ("Enable quiet/warning log mode", None, "store_true", False),
262+
}
263+
264+
def log_prologue(self):
265+
"""
266+
Set the log level
267+
"""
268+
if self.options.quiet:
269+
logging.basicConfig(level=logging.WARNING)
270+
elif self.options.info:
271+
logging.basicConfig(level=logging.INFO)
272+
elif self.options.debug:
273+
logging.basicConfig(level=logging.DEBUG)
274+
else:
275+
logging.basicConfig(level=logging.ERROR)
276+
277+
def log_epilogue(self):
278+
"""
279+
Nothing to do here
280+
"""
254281

255282
class CLIBase:
256283

257284
CLI_OPTIONS = {}
258285
CLI_BASE_OPTIONS = {
259286
'dry-run': ('do not make any updates whatsoever', None, 'store_true', False),
287+
'configfiles': ('config file to read', 'str', 'store', None),
288+
'help': ('show this help message and exit', None, 'help', None),
289+
'ignoreconfigfiles': ('do not read any config files', None, 'store', None),
260290
}
261291

262-
def __init__(self, name):
292+
def __init__(self, name=None):
263293
self.name = name
294+
# Set all the options
295+
argparser = ArgParser()
296+
argparser = populate_config_parser(argparser, self.__class__.CLI_BASE_OPTIONS)
297+
298+
if isinstance(self, HAMixin):
299+
argparser = populate_config_parser(argparser, self.__class__.HA_MIXIN_OPTIONS)
300+
301+
if isinstance(self, TimestampMixin):
302+
argparser = populate_config_parser(argparser, self.__class__.TIMESTAMP_MIXIN_OPTIONS)
303+
304+
if isinstance(self, LockMixin):
305+
argparser = populate_config_parser(argparser, self.__class__.LOCK_MIXIN_OPTIONS)
306+
307+
if isinstance(self, LogMixin):
308+
argparser = populate_config_parser(argparser, self.__class__.LOG_MIXIN_OPTIONS)
309+
310+
if isinstance(self, NagiosStatusMixin):
311+
argparser = populate_config_parser(argparser, self.__class__.NAGIOS_MIXIN_OPTIONS)
312+
313+
argparser = populate_config_parser(argparser, self.get_options())
314+
315+
self.options = argparser.parse_args()
316+
264317

265318
def critical(self, msg):
266319
if isinstance(self, NagiosStatusMixin):
@@ -298,26 +351,6 @@ def main(self):
298351
"""
299352
#errors = []
300353

301-
# Set all the options
302-
argparser = ArgParser()
303-
argparser = populate_config_parser(argparser, self.__class__.CLI_BASE_OPTIONS)
304-
305-
if isinstance(self, HAMixin):
306-
argparser = populate_config_parser(argparser, self.__class__.HA_MIXIN_OPTIONS)
307-
308-
if isinstance(self, TimestampMixin):
309-
argparser = populate_config_parser(argparser, self.__class__.TIMESTAMP_MIXIN_OPTIONS)
310-
311-
if isinstance(self, LockMixin):
312-
argparser = populate_config_parser(argparser, self.__class__.LOCK_MIXIN_OPTIONS)
313-
314-
if isinstance(self, NagiosStatusMixin):
315-
argparser = populate_config_parser(argparser, self.__class__.NAGIOS_MIXIN_OPTIONS)
316-
317-
argparser = populate_config_parser(argparser, self.get_options())
318-
319-
self.options = argparser.parse_args()
320-
321354
msg = self.name
322355
if self.options.dry_run:
323356
msg += " (dry-run)"
@@ -332,6 +365,9 @@ def main(self):
332365
self.critical(str(err))
333366

334367
try:
368+
if isinstance(self, LogMixin):
369+
self.log_prologue()
370+
335371
if isinstance(self, LockMixin):
336372
self.lock_prologue()
337373

@@ -368,6 +404,11 @@ def main(self):
368404
self.nagios_epilogue()
369405

370406

407+
class FullCLIBase(HAMixin, LockMixin, TimestampMixin, LogMixin, NagiosStatusMixin, CLIBase):
408+
"""
409+
A class for command line scripts with all mixins, i.e., what you usually want.
410+
"""
411+
371412

372413
def _merge_options(options):
373414
"""Merge the given set of options with the default options, updating default values where needed.
@@ -514,8 +555,13 @@ def critical_exception_handler(self, tp, value, traceback):
514555
self.critical(message)
515556

516557

558+
class CLI(FullCLIBase):
559+
560+
def __init__(self, name=None, default_options=None): # pylint: disable=unused-argument
561+
super().__init__(name)
562+
517563
@deprecated_class("Base your scripts on the CLIBase class instead")
518-
class CLI:
564+
class OldCLI:
519565
"""
520566
Base class to implement cli tools that require timestamps, nagios checks, etc.
521567
"""
@@ -701,26 +747,27 @@ class NrpeCLI(CLI):
701747
def __init__(self, name=None, default_options=None):
702748
super().__init__(name=name, default_options=default_options)
703749

704-
def ok(self, msg):
705-
"""
706-
Convenience method that exists with nagios OK exitcode
707-
"""
708-
exit_from_errorcode(0, msg)
709-
710-
def warning(self, msg):
711-
"""
712-
Convenience method exists with nagios warning exitcode
713-
"""
714-
exit_from_errorcode(1, msg)
715-
716-
def critical(self, msg):
717-
"""
718-
Convenience method that exists with nagios critical exitcode
719-
"""
720-
exit_from_errorcode(2, msg)
721-
722-
def unknown(self, msg):
723-
"""
724-
Convenience method that exists with nagios unknown exitcode
725-
"""
726-
exit_from_errorcode(3, msg)
750+
# def ok(self, msg):
751+
# """
752+
# Convenience method that exists with nagios OK exitcode
753+
# """
754+
# exit_from_errorcode(0, msg)
755+
#
756+
# def warning(self, msg):
757+
# """
758+
# Convenience method exists with nagios warning exitcode
759+
# """
760+
# exit_from_errorcode(1, msg)
761+
#
762+
# def critical(self, msg):
763+
# """
764+
# Convenience method that exists with nagios critical exitcode
765+
# """
766+
# exit_from_errorcode(2, msg)
767+
#
768+
# def unknown(self, msg):
769+
# """
770+
# Convenience method that exists with nagios unknown exitcode
771+
# """
772+
# exit_from_errorcode(3, msg)
773+
#

0 commit comments

Comments
 (0)