blob: 403483e8f3c16d7edaafe7384a933dd17cf63074 [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import os
2import sys
Alex3ebc5632019-04-18 16:47:18 -05003from logging import DEBUG, INFO
Alex Savatieiev06ab17d2019-02-26 18:40:48 -06004
Alex9a4ad212020-10-01 18:04:25 -05005from cfg_checker.cli.arguments import add_global_arguments
Alexac2a2732020-09-11 11:00:26 -05006from cfg_checker.cli.command import execute_command, helps, parsers, \
7 parsers_inits
Alex9a4ad212020-10-01 18:04:25 -05008from cfg_checker.common.settings import CheckerConfiguration
9from cfg_checker.common import logger, logger_cli
Alex265f45e2019-04-23 18:51:23 -050010from cfg_checker.helpers.args_utils import MyParser
Alex Savatieiev5118de02019-02-20 15:50:42 -060011
12pkg_dir = os.path.dirname(__file__)
13pkg_dir = os.path.normpath(pkg_dir)
14
Alex Savatieiev9c642112019-02-26 13:55:43 -060015
Alex Savatieiev5118de02019-02-20 15:50:42 -060016def config_check_entrypoint():
Alex Savatieiev9c642112019-02-26 13:55:43 -060017 """
Alex3ebc5632019-04-18 16:47:18 -050018 Main entry point. Uses nested parsers structure
Alex Savatieiev9c642112019-02-26 13:55:43 -060019 with a default function to execute the comand
20
21 :return: - no return value
22 """
Alex Savatieiev4c406322019-02-28 17:37:09 -060023 # Main entrypoint
24 parser = MyParser(prog="# Mirantis Cloud configuration checker")
Alex9a4ad212020-10-01 18:04:25 -050025 add_global_arguments(parser)
Alex Savatieiev5118de02019-02-20 15:50:42 -060026 subparsers = parser.add_subparsers(dest='command')
Alex265f45e2019-04-23 18:51:23 -050027
Alexbab1efe2019-04-23 18:51:23 -050028 # create parsers
29 for _command in helps.keys():
30 _parser = subparsers.add_parser(
31 _command,
32 help=helps[_command]
33 )
Alexac2a2732020-09-11 11:00:26 -050034 parsers[_command] = parsers_inits[_command](_parser)
Alex41485522019-04-12 17:26:18 -050035
Alex3ebc5632019-04-18 16:47:18 -050036 # parse arguments
Alex Savatieiev9c642112019-02-26 13:55:43 -060037 try:
Alexd0391d42019-05-21 18:48:55 -050038 args, unknown = parser.parse_known_args()
Alex3ebc5632019-04-18 16:47:18 -050039 except TypeError:
Alex Savatieievc9055712019-03-01 14:43:56 -060040 logger_cli.info("\n# Please, check arguments")
Alex3bc95f62020-03-05 17:00:04 -060041 sys.exit(1)
Alex Savatieiev5118de02019-02-20 15:50:42 -060042
Alexd0391d42019-05-21 18:48:55 -050043 if unknown:
44 logger_cli.error(
45 "# Unexpected arguments: {}".format(
46 ", ".join(["'{}'".format(a) for a in unknown])
47 )
48 )
49 sys.exit(1)
50
Alexac2a2732020-09-11 11:00:26 -050051 if not args.command:
52 parser.print_help()
53 logger_cli.info("\n# Please, type a command listed above")
54 sys.exit(1)
Alex3ebc5632019-04-18 16:47:18 -050055
Alex9a4ad212020-10-01 18:04:25 -050056 # Handle debug option before config init
Alex Savatieiev799bee32019-02-20 17:19:26 -060057 if args.debug:
58 logger_cli.setLevel(DEBUG)
59 else:
60 logger_cli.setLevel(INFO)
61
Alex9a4ad212020-10-01 18:04:25 -050062 # Init the config and pass externally configured values
63 config = CheckerConfiguration(args)
64
Alex Savatieiev5118de02019-02-20 15:50:42 -060065 # Execute the command
Alex9a4ad212020-10-01 18:04:25 -050066 result = execute_command(args, args.command, config)
Alex Savatieiev5118de02019-02-20 15:50:42 -060067 logger.debug(result)
Alex265f45e2019-04-23 18:51:23 -050068 sys.exit(result)
Alex Savatieiev4c406322019-02-28 17:37:09 -060069
Alex3ebc5632019-04-18 16:47:18 -050070
Alex Savatieiev4c406322019-02-28 17:37:09 -060071if __name__ == '__main__':
Alex265f45e2019-04-23 18:51:23 -050072 config_check_entrypoint()