Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: read and manage config files and global config |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | # discussion for choice of config file format: |
| 21 | # |
| 22 | # Python syntax is insane, because it allows the config file to run arbitrary |
| 23 | # python commands. |
| 24 | # |
| 25 | # INI file format is nice and simple, but it doesn't allow having the same |
| 26 | # section numerous times (e.g. to define several modems or BTS models) and does |
| 27 | # not support nesting. |
| 28 | # |
| 29 | # JSON has too much braces and quotes to be easy to type |
| 30 | # |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 31 | # YAML formatting is lean, but: |
| 32 | # - too powerful. The normal load() allows arbitrary code execution. There is |
| 33 | # safe_load(). |
| 34 | # - allows several alternative ways of formatting, better to have just one |
| 35 | # authoritative style. |
| 36 | # - tries to detect types. It would be better to receive every setting as |
| 37 | # simple string rather than e.g. an IMSI as an integer. |
| 38 | # - e.g. an IMSI starting with a zero is interpreted as octal value, resulting |
| 39 | # in super confusing error messages if the user merely forgets to quote it. |
| 40 | # - does not tell me which line a config item came from, so no detailed error |
| 41 | # message is possible. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 42 | # |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 43 | # The Python ConfigParserShootout page has numerous contestants, but many of |
| 44 | # those seem to be not widely used / standardized or even tested. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 45 | # https://wiki.python.org/moin/ConfigParserShootout |
| 46 | # |
| 47 | # The optimum would be a stripped down YAML format. |
| 48 | # In the lack of that, we shall go with yaml.load_safe() + a round trip |
| 49 | # (feeding back to itself), converting keys to lowercase and values to string. |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 50 | # There is no solution for octal interpretations nor config file source lines |
| 51 | # unless, apparently, we implement our own config parser. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 52 | |
| 53 | import yaml |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 54 | import os |
| 55 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 56 | from . import log, schema, util |
| 57 | from .util import is_dict, is_list, Dir, get_tempdir |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 58 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 59 | ENV_PREFIX = 'OSMO_GSM_TESTER_' |
| 60 | ENV_CONF = os.getenv(ENV_PREFIX + 'CONF') |
| 61 | |
Neels Hofmeyr | f15eaf9 | 2017-06-05 18:03:53 +0200 | [diff] [blame] | 62 | override_conf = None |
| 63 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 64 | DEFAULT_CONFIG_LOCATIONS = [ |
| 65 | '.', |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 66 | os.path.join(os.getenv('HOME'), '.config', 'osmo-gsm-tester'), |
| 67 | '/usr/local/etc/osmo-gsm-tester', |
| 68 | '/etc/osmo-gsm-tester' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 69 | ] |
| 70 | |
| 71 | PATHS_CONF = 'paths.conf' |
Neels Hofmeyr | d46ea13 | 2017-04-08 15:56:31 +0200 | [diff] [blame] | 72 | DEFAULT_SUITES_CONF = 'default-suites.conf' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 73 | PATH_STATE_DIR = 'state_dir' |
| 74 | PATH_SUITES_DIR = 'suites_dir' |
| 75 | PATH_SCENARIOS_DIR = 'scenarios_dir' |
| 76 | PATHS_SCHEMA = { |
| 77 | PATH_STATE_DIR: schema.STR, |
| 78 | PATH_SUITES_DIR: schema.STR, |
| 79 | PATH_SCENARIOS_DIR: schema.STR, |
| 80 | } |
| 81 | |
| 82 | PATHS_TEMPDIR_STR = '$TEMPDIR' |
| 83 | |
| 84 | PATHS = None |
| 85 | |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 86 | def _get_config_file(basename, fail_if_missing=True): |
Neels Hofmeyr | f15eaf9 | 2017-06-05 18:03:53 +0200 | [diff] [blame] | 87 | if override_conf: |
| 88 | locations = [ override_conf ] |
| 89 | elif ENV_CONF: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 90 | locations = [ ENV_CONF ] |
| 91 | else: |
| 92 | locations = DEFAULT_CONFIG_LOCATIONS |
| 93 | |
| 94 | for l in locations: |
Neels Hofmeyr | ef9ed2d | 2017-05-04 16:39:29 +0200 | [diff] [blame] | 95 | real_l = os.path.realpath(l) |
| 96 | p = os.path.realpath(os.path.join(real_l, basename)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 97 | if os.path.isfile(p): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 98 | log.dbg('Found config file', basename, 'as', p, 'in', l, 'which is', real_l, _category=log.C_CNF) |
Neels Hofmeyr | ef9ed2d | 2017-05-04 16:39:29 +0200 | [diff] [blame] | 99 | return (p, real_l) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 100 | if not fail_if_missing: |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 101 | return None, None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 102 | raise RuntimeError('configuration file not found: %r in %r' % (basename, |
| 103 | [os.path.abspath(p) for p in locations])) |
| 104 | |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 105 | def get_config_file(basename, fail_if_missing=True): |
| 106 | path, found_in = _get_config_file(basename, fail_if_missing) |
| 107 | return path |
| 108 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 109 | def read_config_file(basename, validation_schema=None, if_missing_return=False): |
| 110 | fail_if_missing = True |
| 111 | if if_missing_return is not False: |
| 112 | fail_if_missing = False |
| 113 | path = get_config_file(basename, fail_if_missing=fail_if_missing) |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 114 | if path is None: |
| 115 | return if_missing_return |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 116 | return read(path, validation_schema=validation_schema, if_missing_return=if_missing_return) |
| 117 | |
| 118 | def get_configured_path(label, allow_unset=False): |
| 119 | global PATHS |
| 120 | |
| 121 | env_name = ENV_PREFIX + label.upper() |
| 122 | env_path = os.getenv(env_name) |
| 123 | if env_path: |
Neels Hofmeyr | ef9ed2d | 2017-05-04 16:39:29 +0200 | [diff] [blame] | 124 | real_env_path = os.path.realpath(env_path) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 125 | log.dbg('Found path', label, 'as', env_path, 'in', '$' + env_name, 'which is', real_env_path, _category=log.C_CNF) |
Neels Hofmeyr | ef9ed2d | 2017-05-04 16:39:29 +0200 | [diff] [blame] | 126 | return real_env_path |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 127 | |
| 128 | if PATHS is None: |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 129 | paths_file, found_in = _get_config_file(PATHS_CONF) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 130 | PATHS = read(paths_file, PATHS_SCHEMA) |
Neels Hofmeyr | ef9ed2d | 2017-05-04 16:39:29 +0200 | [diff] [blame] | 131 | # sorted for deterministic regression test results |
| 132 | for key, path in sorted(PATHS.items()): |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 133 | if not path.startswith(os.pathsep): |
Neels Hofmeyr | ef9ed2d | 2017-05-04 16:39:29 +0200 | [diff] [blame] | 134 | PATHS[key] = os.path.realpath(os.path.join(found_in, path)) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 135 | log.dbg(paths_file + ': relative path', path, 'is', PATHS[key], _category=log.C_CNF) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 136 | p = PATHS.get(label) |
| 137 | if p is None and not allow_unset: |
| 138 | raise RuntimeError('missing configuration in %s: %r' % (PATHS_CONF, label)) |
| 139 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 140 | log.dbg('Found path', label, 'as', p, _category=log.C_CNF) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 141 | if p.startswith(PATHS_TEMPDIR_STR): |
| 142 | p = os.path.join(get_tempdir(), p[len(PATHS_TEMPDIR_STR):]) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 143 | log.dbg('Path', label, 'contained', PATHS_TEMPDIR_STR, 'and becomes', p, _category=log.C_CNF) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 144 | return p |
| 145 | |
| 146 | def get_state_dir(): |
| 147 | return Dir(get_configured_path(PATH_STATE_DIR)) |
| 148 | |
| 149 | def get_suites_dir(): |
| 150 | return Dir(get_configured_path(PATH_SUITES_DIR)) |
| 151 | |
| 152 | def get_scenarios_dir(): |
| 153 | return Dir(get_configured_path(PATH_SCENARIOS_DIR)) |
| 154 | |
| 155 | def read(path, validation_schema=None, if_missing_return=False): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 156 | log.ctx(path) |
| 157 | if not os.path.isfile(path) and if_missing_return is not False: |
| 158 | return if_missing_return |
| 159 | with open(path, 'r') as f: |
| 160 | config = yaml.safe_load(f) |
| 161 | config = _standardize(config) |
| 162 | if validation_schema: |
| 163 | schema.validate(config, validation_schema) |
| 164 | return config |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 165 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 166 | def write(path, config): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 167 | log.ctx(path) |
| 168 | with open(path, 'w') as f: |
| 169 | f.write(tostr(config)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 170 | |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 171 | def tostr(config): |
| 172 | return _tostr(_standardize(config)) |
| 173 | |
| 174 | def _tostr(config): |
| 175 | return yaml.dump(config, default_flow_style=False) |
| 176 | |
| 177 | def _standardize_item(item): |
| 178 | if isinstance(item, (tuple, list)): |
| 179 | return [_standardize_item(i) for i in item] |
| 180 | if isinstance(item, dict): |
| 181 | return dict([(key.lower(), _standardize_item(val)) for key,val in item.items()]) |
| 182 | return str(item) |
| 183 | |
| 184 | def _standardize(config): |
| 185 | config = yaml.safe_load(_tostr(_standardize_item(config))) |
| 186 | return config |
| 187 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 188 | def get_defaults(for_kind): |
Neels Hofmeyr | 05837ad | 2017-04-14 04:18:06 +0200 | [diff] [blame] | 189 | defaults = read_config_file('defaults.conf', if_missing_return={}) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 190 | return defaults.get(for_kind, {}) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 191 | |
Your Name | 44af341 | 2017-04-13 03:11:59 +0200 | [diff] [blame] | 192 | class Scenario(log.Origin, dict): |
| 193 | def __init__(self, name, path): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 194 | super().__init__(log.C_TST, name) |
Your Name | 44af341 | 2017-04-13 03:11:59 +0200 | [diff] [blame] | 195 | self.path = path |
| 196 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 197 | def get_scenario(name, validation_schema=None): |
| 198 | scenarios_dir = get_scenarios_dir() |
| 199 | if not name.endswith('.conf'): |
| 200 | name = name + '.conf' |
| 201 | path = scenarios_dir.child(name) |
| 202 | if not os.path.isfile(path): |
| 203 | raise RuntimeError('No such scenario file: %r' % path) |
Your Name | 44af341 | 2017-04-13 03:11:59 +0200 | [diff] [blame] | 204 | sc = Scenario(name, path) |
| 205 | sc.update(read(path, validation_schema=validation_schema)) |
| 206 | return sc |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 207 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 208 | def add(dest, src): |
| 209 | if is_dict(dest): |
| 210 | if not is_dict(src): |
| 211 | raise ValueError('cannot add to dict a value of type: %r' % type(src)) |
| 212 | |
| 213 | for key, val in src.items(): |
| 214 | dest_val = dest.get(key) |
| 215 | if dest_val is None: |
| 216 | dest[key] = val |
| 217 | else: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 218 | log.ctx(key=key) |
| 219 | add(dest_val, val) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 220 | return |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 221 | if is_list(dest): |
| 222 | if not is_list(src): |
| 223 | raise ValueError('cannot add to list a value of type: %r' % type(src)) |
| 224 | dest.extend(src) |
| 225 | return |
| 226 | if dest == src: |
| 227 | return |
| 228 | raise ValueError('cannot add dicts, conflicting items (values %r and %r)' |
| 229 | % (dest, src)) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 230 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 231 | def combine(dest, src): |
| 232 | if is_dict(dest): |
| 233 | if not is_dict(src): |
| 234 | raise ValueError('cannot combine dict with a value of type: %r' % type(src)) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 235 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 236 | for key, val in src.items(): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 237 | log.ctx(key=key) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 238 | dest_val = dest.get(key) |
| 239 | if dest_val is None: |
| 240 | dest[key] = val |
| 241 | else: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 242 | combine(dest_val, val) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 243 | return |
| 244 | if is_list(dest): |
| 245 | if not is_list(src): |
| 246 | raise ValueError('cannot combine list with a value of type: %r' % type(src)) |
| 247 | for i in range(len(src)): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 248 | log.ctx(idx=i) |
| 249 | combine(dest[i], src[i]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 250 | return |
| 251 | if dest == src: |
| 252 | return |
| 253 | raise ValueError('cannot combine dicts, conflicting items (values %r and %r)' |
| 254 | % (dest, src)) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 255 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 256 | def overlay(dest, src): |
| 257 | if is_dict(dest): |
| 258 | if not is_dict(src): |
| 259 | raise ValueError('cannot combine dict with a value of type: %r' % type(src)) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 260 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 261 | for key, val in src.items(): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 262 | log.ctx(key=key) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 263 | dest_val = dest.get(key) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 264 | dest[key] = overlay(dest_val, val) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 265 | return dest |
| 266 | if is_list(dest): |
| 267 | if not is_list(src): |
| 268 | raise ValueError('cannot combine list with a value of type: %r' % type(src)) |
| 269 | for i in range(len(src)): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 270 | log.ctx(key=key) |
| 271 | dest[i] = overlay(dest[i], src[i]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 272 | return dest |
| 273 | return src |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 274 | |
| 275 | # vim: expandtab tabstop=4 shiftwidth=4 |