blob: dc1f391c66f5dbc51f6c52c5564ae066090bcd75 [file] [log] [blame]
Alex0989ecf2022-03-29 13:43:21 -05001# Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com)
2# Copyright 2019-2022 Mirantis, Inc.
Alex265f45e2019-04-23 18:51:23 -05003import argparse
Alex Savatieievc9055712019-03-01 14:43:56 -06004import os
Alex265f45e2019-04-23 18:51:23 -05005import sys
Alex Savatieievc9055712019-03-01 14:43:56 -06006
Alex9a4ad212020-10-01 18:04:25 -05007from cfg_checker.common.exception import ConfigException, \
8 CommandTypeNotSupportedException, CheckerException
Alex Savatieievc9055712019-03-01 14:43:56 -06009
10
Alex265f45e2019-04-23 18:51:23 -050011class MyParser(argparse.ArgumentParser):
12 def error(self, message):
13 sys.stderr.write('Error: {0}\n\n'.format(message))
14 self.print_help()
15
16
Alexe9908f72020-05-19 16:04:53 -050017def get_skip_args(args):
18 if hasattr(args, "skip_nodes"):
19 _skip = getattr(args, "skip_nodes")
20 if _skip:
21 _skip = _skip.split(',')
22 else:
23 _skip = None
24 if hasattr(args, "skip_nodes_file"):
25 _skip_file = getattr(args, "skip_nodes_file")
26 else:
27 _skip_file = None
28 return _skip, _skip_file
29
30
Alex5cace3b2021-11-10 16:40:37 -060031def get_arg(args, str_arg, nofail=False):
Alexbfa947c2021-11-11 18:14:28 -060032 if hasattr(args, str_arg):
33 return getattr(args, str_arg)
Alex Savatieievc9055712019-03-01 14:43:56 -060034 else:
Alex5cace3b2021-11-10 16:40:37 -060035 if nofail:
36 return None
Alex41485522019-04-12 17:26:18 -050037 _c = args.command if hasattr(args, 'command') else ''
38 _t = args.type if hasattr(args, 'type') else ''
39 raise ConfigException(
Alexe0c5b9e2019-04-23 18:51:23 -050040 "Argument '{}' not found executing: mcp-check {} {}".format(
Alex41485522019-04-12 17:26:18 -050041 str_arg,
42 _c,
43 _t
44 )
45 )
Alex Savatieievc9055712019-03-01 14:43:56 -060046
47
48def get_path_arg(path):
49 if os.path.exists(path):
50 return path
51 else:
52 raise ConfigException("'{}' not exists".format(path))
Alex41485522019-04-12 17:26:18 -050053
54
Alexbab1efe2019-04-23 18:51:23 -050055def get_network_map_type_and_filename(args):
56 if args.html or args.text:
57 if args.html and args.text:
58 raise ConfigException("Multuple report types not supported")
59 if args.html is not None:
60 return 'html', args.html
61 if args.text is not None:
62 return 'text', args.text
63 else:
64 return 'console', None
65
66
67def get_package_report_type_and_filename(args):
Alex41485522019-04-12 17:26:18 -050068 if args.html or args.csv:
69 if args.html and args.csv:
70 raise ConfigException("Multuple report types not supported")
71 if args.html is not None:
72 return 'html', args.html
73 if args.csv is not None:
74 return 'csv', args.csv
75 else:
76 raise ConfigException("Report type and filename not set")
Alex9a4ad212020-10-01 18:04:25 -050077
78
79def check_supported_env(target_env, args, config):
80 def _raise_type_not_supported():
81 raise CommandTypeNotSupportedException(
82 "'{}' -> '{}' is not supported on any of "
83 "the currently detected environments: "
84 "{}".format(
85 args.command,
86 args.type,
87 ",".join(config.detected_envs))
88 )
89
90 def _check_target_vs_used(_tenv):
Alexccb72e02021-01-20 16:38:03 -060091 if not hasattr(args, 'force_env_type'):
Alex9a4ad212020-10-01 18:04:25 -050092 return _tenv
Alexccb72e02021-01-20 16:38:03 -060093 elif args.force_env_type == _tenv or not args.force_env_type:
Alex9a4ad212020-10-01 18:04:25 -050094 return _tenv
95 else:
96 raise CommandTypeNotSupportedException(
97 "'{}' -> '{}' is not supported "
98 "for selected env of '{}'".format(
99 args.command,
100 args.type,
Alexccb72e02021-01-20 16:38:03 -0600101 args.force_env_type
Alex9a4ad212020-10-01 18:04:25 -0500102 )
103 )
104
105 if isinstance(target_env, list):
106 _set = set(config.detected_envs).intersection(set(target_env))
107 if len(_set) < 1:
108 _raise_type_not_supported()
109 if len(_set) > 1:
Alexccb72e02021-01-20 16:38:03 -0600110 if not hasattr(args, 'force_env_type'):
Alex9a4ad212020-10-01 18:04:25 -0500111 raise CheckerException(
112 "Multiple envs detected: {}, use --use-env option".format(
113 ",".join(list(_set))
114 )
115 )
Alexccb72e02021-01-20 16:38:03 -0600116 elif args.force_env_type and args.force_env_type in _set:
117 return args.force_env_type
Alex9a4ad212020-10-01 18:04:25 -0500118 else:
119 _raise_type_not_supported()
120 else:
121 return _check_target_vs_used(list(_set)[0])
122 elif isinstance(target_env, str):
123 if target_env not in config.detected_envs:
124 _raise_type_not_supported()
125 else:
126 return _check_target_vs_used(target_env)
127 else:
128 raise CheckerException(
129 "Unexpected target env type '{}' in '{}' -> '{}'".format(
130 target_env,
131 args.command,
132 args.type,
133 )
134 )