blob: 8e715f2191ecd39c697a649c0b954039f28b9114 [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 = {}
15parsers = {}
16helps = {}
17# Pure dynamic magic, loading all 'do_*' methods from available modules
18_m = __import__(mods_import_path, fromlist=[main_pkg_name])
19for _imp, modName, isMod in pkgutil.iter_modules(_m.__path__, mods_prefix):
20 # iterate all packages, add to dict
21 if isMod:
22 # load module
23 _p = _imp.find_module(modName).load_module(modName)
24 # create a shortname
25 mod_name = modName.split('.')[-1]
26 # A package! Create it and add commands
27 commands[mod_name] = \
28 [_n[3:] for _n in dir(_p) if _n.startswith("do_")]
29 parsers[mod_name] = getattr(_p, 'init_parser')
30 helps[mod_name] = getattr(_p, 'command_help')
Alex265f45e2019-04-23 18:51:23 -050031
32
33def execute_command(args, command):
34 # Validate the commands
35 # check command
Alexd0391d42019-05-21 18:48:55 -050036 _type = args.type.replace("-", "_") if "-" in args.type else args.type
Alex265f45e2019-04-23 18:51:23 -050037 if command not in commands:
38 logger_cli.info("\n# Please, type a command listed above")
39 return 0
Alexd0391d42019-05-21 18:48:55 -050040 elif _type not in commands[command]:
Alex265f45e2019-04-23 18:51:23 -050041 # check type
42 logger_cli.info(
43 "\n# Please, select '{}' command type listed above".format(
44 command
45 )
46 )
47 return 0
48 else:
49 # form function name to call
Alexd0391d42019-05-21 18:48:55 -050050 _method_name = "do_" + _type
Alex265f45e2019-04-23 18:51:23 -050051 _target_module = __import__(
Alexbab1efe2019-04-23 18:51:23 -050052 mods_prefix + command,
Alex265f45e2019-04-23 18:51:23 -050053 fromlist=[""]
54 )
55 _method = getattr(_target_module, _method_name)
56
57 # Execute the command
58 try:
59 _method(args)
60 return 0
61 except CheckerException as e:
62 logger_cli.error("\nERROR: {}".format(
63 e.message
64 ))
65
66 exc_type, exc_value, exc_traceback = sys.exc_info()
67 logger_cli.debug("\n{}".format(
68 "".join(traceback.format_exception(
69 exc_type,
70 exc_value,
71 exc_traceback
72 ))
73 ))
74 return 1
Alexbab1efe2019-04-23 18:51:23 -050075
76
77def cli_command(_title, _name):
78 my_parser = MyParser(_title)
79 parsers[_name](my_parser)
80
81 # parse arguments
82 try:
Alexd0391d42019-05-21 18:48:55 -050083 args, unknown = my_parser.parse_known_args()
Alexbab1efe2019-04-23 18:51:23 -050084 except TypeError:
85 logger_cli.info("\n# Please, check arguments")
86 sys.exit(0)
87
Alexd0391d42019-05-21 18:48:55 -050088 if unknown:
89 logger_cli.error(
90 "# Unexpected arguments: {}".format(
91 ", ".join(["'{}'".format(a) for a in unknown])
92 )
93 )
94 sys.exit(1)
95
Alexbab1efe2019-04-23 18:51:23 -050096 # force use of sudo
97 config.ssh_uses_sudo = True
98
99 # Execute the command
100 result = execute_command(args, _name)
101 logger.debug(result)
102 sys.exit(result)