Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame^] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 3 | import argparse |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 4 | import os |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 5 | import sys |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 6 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 7 | from cfg_checker.common.exception import ConfigException, \ |
| 8 | CommandTypeNotSupportedException, CheckerException |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 9 | |
| 10 | |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 11 | class MyParser(argparse.ArgumentParser): |
| 12 | def error(self, message): |
| 13 | sys.stderr.write('Error: {0}\n\n'.format(message)) |
| 14 | self.print_help() |
| 15 | |
| 16 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 17 | def 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 | |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 31 | def get_arg(args, str_arg, nofail=False): |
Alex | bfa947c | 2021-11-11 18:14:28 -0600 | [diff] [blame] | 32 | if hasattr(args, str_arg): |
| 33 | return getattr(args, str_arg) |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 34 | else: |
Alex | 5cace3b | 2021-11-10 16:40:37 -0600 | [diff] [blame] | 35 | if nofail: |
| 36 | return None |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 37 | _c = args.command if hasattr(args, 'command') else '' |
| 38 | _t = args.type if hasattr(args, 'type') else '' |
| 39 | raise ConfigException( |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 40 | "Argument '{}' not found executing: mcp-check {} {}".format( |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 41 | str_arg, |
| 42 | _c, |
| 43 | _t |
| 44 | ) |
| 45 | ) |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def get_path_arg(path): |
| 49 | if os.path.exists(path): |
| 50 | return path |
| 51 | else: |
| 52 | raise ConfigException("'{}' not exists".format(path)) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 53 | |
| 54 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 55 | def 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 | |
| 67 | def get_package_report_type_and_filename(args): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 68 | 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") |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def 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): |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 91 | if not hasattr(args, 'force_env_type'): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 92 | return _tenv |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 93 | elif args.force_env_type == _tenv or not args.force_env_type: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 94 | return _tenv |
| 95 | else: |
| 96 | raise CommandTypeNotSupportedException( |
| 97 | "'{}' -> '{}' is not supported " |
| 98 | "for selected env of '{}'".format( |
| 99 | args.command, |
| 100 | args.type, |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 101 | args.force_env_type |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 102 | ) |
| 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: |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 110 | if not hasattr(args, 'force_env_type'): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 111 | raise CheckerException( |
| 112 | "Multiple envs detected: {}, use --use-env option".format( |
| 113 | ",".join(list(_set)) |
| 114 | ) |
| 115 | ) |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 116 | elif args.force_env_type and args.force_env_type in _set: |
| 117 | return args.force_env_type |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 118 | 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 | ) |