blob: f98e6086479daca2bf21af40b2687083f01bb5ae [file] [log] [blame]
Alexbab1efe2019-04-23 18:51:23 -05001import pkgutil
Alex265f45e2019-04-23 18:51:23 -05002import sys
3import traceback
4
Alexbab1efe2019-04-23 18:51:23 -05005from cfg_checker.common import config, logger, logger_cli
Alex265f45e2019-04-23 18:51:23 -05006from cfg_checker.common.exception import CheckerException
Alexbab1efe2019-04-23 18:51:23 -05007from cfg_checker.helpers.args_utils import MyParser
Alex265f45e2019-04-23 18:51:23 -05008
Alexbab1efe2019-04-23 18:51:23 -05009main_pkg_name = __name__.split('.')[0]
10mods_package_name = "modules"
11mods_import_path = main_pkg_name + '.' + mods_package_name
12mods_prefix = mods_import_path + '.'
Alex265f45e2019-04-23 18:51:23 -050013
Alexbab1efe2019-04-23 18:51:23 -050014commands = {}
Alexac2a2732020-09-11 11:00:26 -050015parsers_inits = {}
Alexbab1efe2019-04-23 18:51:23 -050016parsers = {}
17helps = {}
18# Pure dynamic magic, loading all 'do_*' methods from available modules
19_m = __import__(mods_import_path, fromlist=[main_pkg_name])
20for _imp, modName, isMod in pkgutil.iter_modules(_m.__path__, mods_prefix):
21 # iterate all packages, add to dict
22 if isMod:
23 # load module
24 _p = _imp.find_module(modName).load_module(modName)
25 # create a shortname
26 mod_name = modName.split('.')[-1]
27 # A package! Create it and add commands
28 commands[mod_name] = \
29 [_n[3:] for _n in dir(_p) if _n.startswith("do_")]
Alexac2a2732020-09-11 11:00:26 -050030 parsers_inits[mod_name] = getattr(_p, 'init_parser')
31 parsers[mod_name] = {}
Alexbab1efe2019-04-23 18:51:23 -050032 helps[mod_name] = getattr(_p, 'command_help')
Alex265f45e2019-04-23 18:51:23 -050033
34
35def execute_command(args, command):
36 # Validate the commands
Alexcf91b182019-05-31 11:57:07 -050037 # check commands
Alexac2a2732020-09-11 11:00:26 -050038 if command not in commands:
39
40 logger_cli.info("\n# Please, type a command listed above")
41 return 1
Alexcf91b182019-05-31 11:57:07 -050042 if not hasattr(args, 'type') or not args.type:
Alexac2a2732020-09-11 11:00:26 -050043 parsers[command].print_help()
Alexcf91b182019-05-31 11:57:07 -050044 logger_cli.info("\n# Please, type a command listed above")
Alex3bc95f62020-03-05 17:00:04 -060045 return 1
Alexd0391d42019-05-21 18:48:55 -050046 _type = args.type.replace("-", "_") if "-" in args.type else args.type
Alexac2a2732020-09-11 11:00:26 -050047 if _type not in commands[command]:
Alex265f45e2019-04-23 18:51:23 -050048 # check type
49 logger_cli.info(
50 "\n# Please, select '{}' command type listed above".format(
51 command
52 )
53 )
Alex3bc95f62020-03-05 17:00:04 -060054 return 1
Alex265f45e2019-04-23 18:51:23 -050055 else:
56 # form function name to call
Alexd0391d42019-05-21 18:48:55 -050057 _method_name = "do_" + _type
Alex265f45e2019-04-23 18:51:23 -050058 _target_module = __import__(
Alexbab1efe2019-04-23 18:51:23 -050059 mods_prefix + command,
Alex265f45e2019-04-23 18:51:23 -050060 fromlist=[""]
61 )
62 _method = getattr(_target_module, _method_name)
63
64 # Execute the command
65 try:
66 _method(args)
67 return 0
68 except CheckerException as e:
69 logger_cli.error("\nERROR: {}".format(
70 e.message
71 ))
72
73 exc_type, exc_value, exc_traceback = sys.exc_info()
74 logger_cli.debug("\n{}".format(
75 "".join(traceback.format_exception(
76 exc_type,
77 exc_value,
78 exc_traceback
79 ))
80 ))
81 return 1
Alexbab1efe2019-04-23 18:51:23 -050082
83
84def cli_command(_title, _name):
85 my_parser = MyParser(_title)
Alexac2a2732020-09-11 11:00:26 -050086 parsers[_name] = parsers_inits[_name](my_parser)
Alexbab1efe2019-04-23 18:51:23 -050087
88 # parse arguments
89 try:
Alexd0391d42019-05-21 18:48:55 -050090 args, unknown = my_parser.parse_known_args()
Alexbab1efe2019-04-23 18:51:23 -050091 except TypeError:
92 logger_cli.info("\n# Please, check arguments")
Alex3bc95f62020-03-05 17:00:04 -060093 sys.exit(1)
Alexbab1efe2019-04-23 18:51:23 -050094
Alexd0391d42019-05-21 18:48:55 -050095 if unknown:
96 logger_cli.error(
97 "# Unexpected arguments: {}".format(
98 ", ".join(["'{}'".format(a) for a in unknown])
99 )
100 )
101 sys.exit(1)
102
Alexbab1efe2019-04-23 18:51:23 -0500103 # force use of sudo
104 config.ssh_uses_sudo = True
105
106 # Execute the command
107 result = execute_command(args, _name)
108 logger.debug(result)
109 sys.exit(result)