Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 1 | import pkgutil |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 2 | import sys |
| 3 | import traceback |
| 4 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 5 | from cfg_checker.common import config, logger, logger_cli |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 6 | from cfg_checker.common.exception import CheckerException |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 7 | from cfg_checker.helpers.args_utils import MyParser |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 8 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 9 | main_pkg_name = __name__.split('.')[0] |
| 10 | mods_package_name = "modules" |
| 11 | mods_import_path = main_pkg_name + '.' + mods_package_name |
| 12 | mods_prefix = mods_import_path + '.' |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 13 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 14 | commands = {} |
| 15 | parsers = {} |
| 16 | helps = {} |
| 17 | # Pure dynamic magic, loading all 'do_*' methods from available modules |
| 18 | _m = __import__(mods_import_path, fromlist=[main_pkg_name]) |
| 19 | for _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') |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def execute_command(args, command): |
| 34 | # Validate the commands |
| 35 | # check command |
| 36 | if command not in commands: |
| 37 | logger_cli.info("\n# Please, type a command listed above") |
| 38 | return 0 |
| 39 | elif args.type not in commands[command]: |
| 40 | # check type |
| 41 | logger_cli.info( |
| 42 | "\n# Please, select '{}' command type listed above".format( |
| 43 | command |
| 44 | ) |
| 45 | ) |
| 46 | return 0 |
| 47 | else: |
| 48 | # form function name to call |
| 49 | _method_name = "do_" + args.type |
| 50 | _target_module = __import__( |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 51 | mods_prefix + command, |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 52 | fromlist=[""] |
| 53 | ) |
| 54 | _method = getattr(_target_module, _method_name) |
| 55 | |
| 56 | # Execute the command |
| 57 | try: |
| 58 | _method(args) |
| 59 | return 0 |
| 60 | except CheckerException as e: |
| 61 | logger_cli.error("\nERROR: {}".format( |
| 62 | e.message |
| 63 | )) |
| 64 | |
| 65 | exc_type, exc_value, exc_traceback = sys.exc_info() |
| 66 | logger_cli.debug("\n{}".format( |
| 67 | "".join(traceback.format_exception( |
| 68 | exc_type, |
| 69 | exc_value, |
| 70 | exc_traceback |
| 71 | )) |
| 72 | )) |
| 73 | return 1 |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def cli_command(_title, _name): |
| 77 | my_parser = MyParser(_title) |
| 78 | parsers[_name](my_parser) |
| 79 | |
| 80 | # parse arguments |
| 81 | try: |
| 82 | args = my_parser.parse_args() |
| 83 | except TypeError: |
| 84 | logger_cli.info("\n# Please, check arguments") |
| 85 | sys.exit(0) |
| 86 | |
| 87 | # force use of sudo |
| 88 | config.ssh_uses_sudo = True |
| 89 | |
| 90 | # Execute the command |
| 91 | result = execute_command(args, _name) |
| 92 | logger.debug(result) |
| 93 | sys.exit(result) |