From 886a8d75fe1bb8519a5f72925c0b3c3d8e0d1f16 Mon Sep 17 00:00:00 2001 From: Matus Honek Date: Dec 03 2019 13:30:53 +0000 Subject: WIP - dsctl healthcheck --list Bug Description: Fix Description: Fixes Author: Matus Honek Review by: ??? --- diff --git a/src/lib389/cli/dsctl b/src/lib389/cli/dsctl index 8b86629..d627048 100755 --- a/src/lib389/cli/dsctl +++ b/src/lib389/cli/dsctl @@ -11,6 +11,7 @@ # PYTHON_ARGCOMPLETE_OK import argparse, argcomplete +import lazy_object_proxy import logging import sys import signal @@ -101,32 +102,39 @@ if __name__ == '__main__': # Connect inst = DirSrv(verbose=args.verbose) - result = True + def inst_factory(): + # Connect + inst = DirSrv(verbose=args.verbose) - # Allocate the instance based on name - insts = [] - if args.verbose: - insts = inst.list(serverid=args.instance) - else: - signal.signal(signal.SIGINT, signal_handler) - try: + # Allocate the instance based on name + insts = [] + if args.verbose: insts = inst.list(serverid=args.instance) - except (PermissionError, IOError) as e: + else: + signal.signal(signal.SIGINT, signal_handler) + try: + insts = inst.list(serverid=args.instance) + except (PermissionError, IOError) as e: + log.error("Unable to access instance information. Are you running as the correct user? (usually dirsrv or root)") + msg = format_error_to_dict(e) + log.error("Error: %s" % " - ".join(msg.values())) + sys.exit(1) + except Exception as e: + msg = format_error_to_dict(e) + log.error("Error: %s" % " - ".join(msg.values())) + sys.exit(1) + if len(insts) != 1: + log.error("No such instance '%s'" % args.instance) log.error("Unable to access instance information. Are you running as the correct user? (usually dirsrv or root)") - msg = format_error_to_dict(e) - log.error("Error: %s" % " - ".join(msg.values())) - sys.exit(1) - except Exception as e: - msg = format_error_to_dict(e) - log.error("Error: %s" % " - ".join(msg.values())) sys.exit(1) - if len(insts) != 1: - log.error("No such instance '%s'" % args.instance) - log.error("Unable to access instance information. Are you running as the correct user? (usually dirsrv or root)") - sys.exit(1) - inst.allocate(insts[0]) - log.debug('Instance allocated') + inst.allocate(insts[0]) + log.debug('Instance allocated') + + return inst + + result = True + inst = lazy_object_proxy.Proxy(inst_factory) try: result = args.func(inst, log, args) @@ -135,7 +143,8 @@ if __name__ == '__main__': msg = format_error_to_dict(e) log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) result = False - disconnect_instance(inst) + if not hasattr(inst, '__factory__'): + disconnect_instance(inst) # Done! if result is False: diff --git a/src/lib389/lib389/cli_ctl/health.py b/src/lib389/lib389/cli_ctl/health.py index d8f3d73..c292684 100644 --- a/src/lib389/lib389/cli_ctl/health.py +++ b/src/lib389/lib389/cli_ctl/health.py @@ -7,10 +7,9 @@ # --- END COPYRIGHT BLOCK --- import json -from getpass import getpass -from lib389.cli_base import connect_instance, disconnect_instance, format_error_to_dict +from lib389.cli_base import connect_instance, disconnect_instance from lib389.cli_base.dsrc import dsrc_to_ldap, dsrc_arg_concat -from lib389.backend import Backend, Backends +from lib389.backend import Backends from lib389.config import Encryption, Config from lib389.monitor import MonitorDiskSpace from lib389.replica import Replica, Changelog5 @@ -18,6 +17,8 @@ from lib389.nss_ssl import NssSsl from lib389.dseldif import FSChecks from lib389 import plugins from lib389._constants import DSRC_HOME +from lib389 import lint +from functools import partial as pf # These get all instances, then check them all. CHECK_MANY_OBJECTS = [ @@ -52,10 +53,22 @@ def _format_check_output(log, result, idx): log.info(result['fix']) +def _list_checks(log): + import re + for r in map(pf(getattr, lint), + filter(pf(re.match, r'^DS'), + dir(lint))): + log.info(r['dsle']) + + def health_check_run(inst, log, args): """Connect to the local server using LDAPI, and perform various health checks """ + if args.list_checks: + _list_checks(log) + return + # update the args for connect_instance() args.basedn = None args.binddn = None @@ -120,4 +133,4 @@ def create_parser(subparsers): "remote Directory Server as this tool needs access to local resources, " "otherwise the report may be inaccurate.") run_healthcheck_parser.set_defaults(func=health_check_run) - + run_healthcheck_parser.add_argument('--list-checks', action='store_true') diff --git a/src/lib389/requirements.txt b/src/lib389/requirements.txt index 760eaae..4863f7f 100644 --- a/src/lib389/requirements.txt +++ b/src/lib389/requirements.txt @@ -8,3 +8,4 @@ argparse-manpage python-ldap setuptools distro +lazy-object-proxy