blob: 11190b4201691294fba22b735561ad7c7e409272 [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
Alexac2a2732020-09-11 11:00:26 -05005from cfg_checker.cli.command import execute_command, helps, parsers, \
6 parsers_inits
Alex Savatieievc9055712019-03-01 14:43:56 -06007from cfg_checker.common import config, logger, logger_cli
Alex265f45e2019-04-23 18:51:23 -05008from cfg_checker.helpers.args_utils import MyParser
Alex Savatieiev5118de02019-02-20 15:50:42 -06009
10pkg_dir = os.path.dirname(__file__)
11pkg_dir = os.path.normpath(pkg_dir)
12
Alex Savatieiev9c642112019-02-26 13:55:43 -060013
Alex Savatieiev5118de02019-02-20 15:50:42 -060014def config_check_entrypoint():
Alex Savatieiev9c642112019-02-26 13:55:43 -060015 """
Alex3ebc5632019-04-18 16:47:18 -050016 Main entry point. Uses nested parsers structure
Alex Savatieiev9c642112019-02-26 13:55:43 -060017 with a default function to execute the comand
18
19 :return: - no return value
20 """
Alex Savatieiev4c406322019-02-28 17:37:09 -060021 # Main entrypoint
22 parser = MyParser(prog="# Mirantis Cloud configuration checker")
Alex3ebc5632019-04-18 16:47:18 -050023
Alex Savatieiev799bee32019-02-20 17:19:26 -060024 parser.add_argument(
25 "-d",
26 "--debug",
27 action="store_true", default=False,
28 help="Set CLI logging level to DEBUG"
29 )
Alex Savatieiev9c642112019-02-26 13:55:43 -060030 parser.add_argument(
Alex Savatieiev63576832019-02-27 15:46:26 -060031 '-s',
32 '--sudo',
33 action='store_true', default=True,
34 help="Use sudo for getting salt creds"
35 )
Alexe9908f72020-05-19 16:04:53 -050036
37 parser.add_argument(
38 '--skip-nodes',
39 metavar='skip_string', default=None,
40 help="String with nodes to skip. Only trailing '*' supported!"
41 " Example: 'cmp*,ctl01'"
42 )
43
44 parser.add_argument(
45 '--skip-nodes-file',
46 metavar='skip_nodes_file', default=None,
47 help="Filename with nodes to skip. Note: use fqdn node names."
48 )
49
Alex Savatieiev5118de02019-02-20 15:50:42 -060050 subparsers = parser.add_subparsers(dest='command')
Alex265f45e2019-04-23 18:51:23 -050051
Alexbab1efe2019-04-23 18:51:23 -050052 # create parsers
53 for _command in helps.keys():
54 _parser = subparsers.add_parser(
55 _command,
56 help=helps[_command]
57 )
Alexac2a2732020-09-11 11:00:26 -050058 parsers[_command] = parsers_inits[_command](_parser)
Alex41485522019-04-12 17:26:18 -050059
Alex3ebc5632019-04-18 16:47:18 -050060 # parse arguments
Alex Savatieiev9c642112019-02-26 13:55:43 -060061 try:
Alexd0391d42019-05-21 18:48:55 -050062 args, unknown = parser.parse_known_args()
Alex3ebc5632019-04-18 16:47:18 -050063 except TypeError:
Alex Savatieievc9055712019-03-01 14:43:56 -060064 logger_cli.info("\n# Please, check arguments")
Alex3bc95f62020-03-05 17:00:04 -060065 sys.exit(1)
Alex Savatieiev5118de02019-02-20 15:50:42 -060066
Alexd0391d42019-05-21 18:48:55 -050067 if unknown:
68 logger_cli.error(
69 "# Unexpected arguments: {}".format(
70 ", ".join(["'{}'".format(a) for a in unknown])
71 )
72 )
73 sys.exit(1)
74
Alexac2a2732020-09-11 11:00:26 -050075 if not args.command:
76 parser.print_help()
77 logger_cli.info("\n# Please, type a command listed above")
78 sys.exit(1)
Alex Savatieiev63576832019-02-27 15:46:26 -060079 # Pass externally configured values
80 config.ssh_uses_sudo = args.sudo
Alex3ebc5632019-04-18 16:47:18 -050081
Alex Savatieiev799bee32019-02-20 17:19:26 -060082 # Handle options
83 if args.debug:
84 logger_cli.setLevel(DEBUG)
85 else:
86 logger_cli.setLevel(INFO)
87
Alex Savatieiev5118de02019-02-20 15:50:42 -060088 # Execute the command
Alex265f45e2019-04-23 18:51:23 -050089 result = execute_command(args, args.command)
Alex Savatieiev5118de02019-02-20 15:50:42 -060090 logger.debug(result)
Alex265f45e2019-04-23 18:51:23 -050091 sys.exit(result)
Alex Savatieiev4c406322019-02-28 17:37:09 -060092
Alex3ebc5632019-04-18 16:47:18 -050093
Alex Savatieiev4c406322019-02-28 17:37:09 -060094if __name__ == '__main__':
Alex265f45e2019-04-23 18:51:23 -050095 config_check_entrypoint()