blob: 398e8ba99e28fb0cf007634ff4bff56976528110 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# osmo_gsm_tester: read and manage config files and global config
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02002#
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +02003# Copyright (C) 2016-2020 by sysmocom - s.f.m.c. GmbH
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02004#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +02006# Author: Pau Espin Pedrol <pespin@sysmocom.de>
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02007#
8# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02009# it under the terms of the GNU General Public License as
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020010# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte27205342017-06-03 09:51:45 +020016# GNU General Public License for more details.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020017#
Harald Welte27205342017-06-03 09:51:45 +020018# You should have received a copy of the GNU General Public License
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020019# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21# discussion for choice of config file format:
22#
23# Python syntax is insane, because it allows the config file to run arbitrary
24# python commands.
25#
26# INI file format is nice and simple, but it doesn't allow having the same
27# section numerous times (e.g. to define several modems or BTS models) and does
28# not support nesting.
29#
30# JSON has too much braces and quotes to be easy to type
31#
Neels Hofmeyr3531a192017-03-28 14:30:28 +020032# YAML formatting is lean, but:
33# - too powerful. The normal load() allows arbitrary code execution. There is
34# safe_load().
35# - allows several alternative ways of formatting, better to have just one
36# authoritative style.
37# - tries to detect types. It would be better to receive every setting as
38# simple string rather than e.g. an IMSI as an integer.
39# - e.g. an IMSI starting with a zero is interpreted as octal value, resulting
40# in super confusing error messages if the user merely forgets to quote it.
41# - does not tell me which line a config item came from, so no detailed error
42# message is possible.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020043#
Neels Hofmeyr3531a192017-03-28 14:30:28 +020044# The Python ConfigParserShootout page has numerous contestants, but many of
45# those seem to be not widely used / standardized or even tested.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020046# https://wiki.python.org/moin/ConfigParserShootout
47#
48# The optimum would be a stripped down YAML format.
49# In the lack of that, we shall go with yaml.load_safe() + a round trip
50# (feeding back to itself), converting keys to lowercase and values to string.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020051# There is no solution for octal interpretations nor config file source lines
52# unless, apparently, we implement our own config parser.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020053
54import yaml
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020055import os
Pau Espin Pedrol802dfe52017-09-12 13:43:40 +020056import copy
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020057import pprint
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020058
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020059from . import log, util, template
60from . import schema
Neels Hofmeyr3531a192017-03-28 14:30:28 +020061from .util import is_dict, is_list, Dir, get_tempdir
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020062
Neels Hofmeyrf15eaf92017-06-05 18:03:53 +020063override_conf = None
64
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020065CFG_STATE_DIR = 'state_dir'
66CFG_SUITES_DIR = 'suites_dir'
67CFG_SCENARIOS_DIR = 'scenarios_dir'
Pau Espin Pedrole972c9c2020-05-12 15:06:55 +020068CFG_TRIAL_DIR = 'trial_dir'
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020069CFG_DEFAULT_SUITES_CONF = 'default_suites_conf_path'
70CFG_DEFAULTS_CONF = 'defaults_conf_path'
71CFG_RESOURCES_CONF = 'resource_conf_path'
72MAIN_CONFIG_SCHEMA = {
73 CFG_STATE_DIR: schema.STR,
Pau Espin Pedrol66ef9452020-05-25 13:26:41 +020074 CFG_SUITES_DIR + '[]': schema.STR,
75 CFG_SCENARIOS_DIR + '[]': schema.STR,
Pau Espin Pedrole972c9c2020-05-12 15:06:55 +020076 CFG_TRIAL_DIR: schema.STR,
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020077 CFG_DEFAULT_SUITES_CONF: schema.STR,
78 CFG_DEFAULTS_CONF: schema.STR,
79 CFG_RESOURCES_CONF: schema.STR,
Neels Hofmeyr3531a192017-03-28 14:30:28 +020080 }
81
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020082DF_CFG_STATE_DIR = '/var/tmp/osmo-gsm-tester/state/'
Pau Espin Pedrol66ef9452020-05-25 13:26:41 +020083DF_CFG_SUITES_DIR = ['./suites']
84DF_CFG_SCENARIOS_DIR = ['./scenarios']
Pau Espin Pedrole972c9c2020-05-12 15:06:55 +020085DF_CFG_TRIAL_DIR = './trial'
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020086DF_CFG_DEFAULT_SUITES_CONF = './default-suites.conf'
87DF_CFG_DEFAULTS_CONF = './defaults.conf'
88DF_CFG_RESOURCES_CONF = './resources.conf'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020089
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020090DEFAULT_CONFIG_FILENAME = 'main.conf'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020091
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020092DEFAULT_CONFIG_LOCATIONS = [
93 '.',
94 os.path.join(os.getenv('HOME'), '.config', 'osmo-gsm-tester', DEFAULT_CONFIG_FILENAME),
95 os.path.join('/usr/local/etc/osmo-gsm-tester', DEFAULT_CONFIG_FILENAME),
96 os.path.join('/etc/osmo-gsm-tester', DEFAULT_CONFIG_FILENAME)
97 ]
98
99MAIN_CONFIG = None
100MAIN_CONFIG_PATH = None
101
102def _find_main_config_path():
Neels Hofmeyrf15eaf92017-06-05 18:03:53 +0200103 if override_conf:
104 locations = [ override_conf ]
Pau Espin Pedrol06c82ae2020-05-07 18:15:53 +0200105 elif os.getenv('OSMO_GSM_TESTER_CONF'):
106 ENV_CONF = os.getenv('OSMO_GSM_TESTER_CONF')
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200107 log.err('Using environment variable OSMO_GSM_TESTER_CONF=%s(/paths.conf) is deprecated. Rather use -c command line argument!' % ENV_CONF)
108 locations = [ ENV_CONF + 'paths.conf' ] # directory is expected in OSMO_GSM_TESTER_CONF, bakcward compatibility
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200109 else:
110 locations = DEFAULT_CONFIG_LOCATIONS
111
112 for l in locations:
Neels Hofmeyref9ed2d2017-05-04 16:39:29 +0200113 real_l = os.path.realpath(l)
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200114 if os.path.isfile(real_l):
115 log.dbg('Found main configuration file in ', l, 'which is', real_l, _category=log.C_CNF)
116 return real_l
117 raise RuntimeError('Main configuration file not found in %r' % ([l for l in locations]))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200118
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200119def _get_main_config_path():
120 global MAIN_CONFIG_PATH
121 if MAIN_CONFIG_PATH is None:
122 MAIN_CONFIG_PATH = _find_main_config_path()
123 return MAIN_CONFIG_PATH
124
Pau Espin Pedrol66ef9452020-05-25 13:26:41 +0200125def main_config_path_to_abspath(val):
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200126 'Relative files in main config are relative towards the config file, not towards $CWD'
Pau Espin Pedrol66ef9452020-05-25 13:26:41 +0200127 # If val is a list of paths, recurse to translate its paths.
128 if isinstance(val, list):
129 for i in range(len(val)):
130 val[i] = main_config_path_to_abspath(val[i])
131 return val
132 if not val.startswith(os.pathsep):
133 return os.path.realpath(os.path.join(os.path.dirname(_get_main_config_path()), val))
134 return val
Your Name3c6673a2017-04-08 18:52:39 +0200135
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200136def _get_main_config():
137 global MAIN_CONFIG
138 if MAIN_CONFIG is None:
139 cfg = read(_get_main_config_path(), MAIN_CONFIG_SCHEMA)
140 MAIN_CONFIG = {
141 CFG_STATE_DIR: DF_CFG_STATE_DIR,
142 CFG_SUITES_DIR: DF_CFG_SUITES_DIR,
143 CFG_SCENARIOS_DIR: DF_CFG_SCENARIOS_DIR,
Pau Espin Pedrole972c9c2020-05-12 15:06:55 +0200144 CFG_TRIAL_DIR: DF_CFG_TRIAL_DIR,
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200145 CFG_DEFAULT_SUITES_CONF: DF_CFG_DEFAULT_SUITES_CONF,
146 CFG_DEFAULTS_CONF: DF_CFG_DEFAULTS_CONF,
147 CFG_RESOURCES_CONF: DF_CFG_RESOURCES_CONF,
148 }
149 overlay(MAIN_CONFIG, cfg)
150 for key, path in sorted(MAIN_CONFIG.items()):
151 MAIN_CONFIG[key] = main_config_path_to_abspath(path)
152 log.dbg('MAIN CONFIG:\n' + pprint.pformat(MAIN_CONFIG), _category=log.C_CNF)
153 return MAIN_CONFIG
154
155def get_main_config_value(cfg_name, fail_if_missing=True):
156 cfg = _get_main_config()
157 f = cfg.get(cfg_name, None)
158 if f is None and fail_if_missing:
159 raise RuntimeError('Missing configuration %s' % (cfg_name))
160 return f
161
162def read_config_file(cfg_name, validation_schema=None, if_missing_return=False):
163 '''Read content of config file cfg_name (referring to key in main config).
164 If "if_missing_return" is different than False, then instead of failing it will return whatever it is stored in that arg
165 '''
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200166 fail_if_missing = True
167 if if_missing_return is not False:
168 fail_if_missing = False
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200169 path = get_main_config_value(cfg_name, fail_if_missing=fail_if_missing)
Your Name3c6673a2017-04-08 18:52:39 +0200170 if path is None:
171 return if_missing_return
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200172 return read(path, validation_schema=validation_schema, if_missing_return=if_missing_return)
173
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200174def get_state_dir():
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200175 return Dir(get_main_config_value(CFG_STATE_DIR))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200176
Pau Espin Pedrol66ef9452020-05-25 13:26:41 +0200177def get_suites_dirs():
178 return [Dir(d) for d in get_main_config_value(CFG_SUITES_DIR)]
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200179
Pau Espin Pedrol66ef9452020-05-25 13:26:41 +0200180def get_scenarios_dirs():
181 return [Dir(d) for d in get_main_config_value(CFG_SCENARIOS_DIR)]
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +0200182
183DEFAULTS_CONF = None
184def get_defaults(for_kind):
185 global DEFAULTS_CONF
186 if DEFAULTS_CONF is None:
187 DEFAULTS_CONF = read_config_file(CFG_DEFAULTS_CONF, if_missing_return={})
188 defaults = DEFAULTS_CONF.get(for_kind, {})
189 return copy.deepcopy(defaults)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200190
191def read(path, validation_schema=None, if_missing_return=False):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200192 log.ctx(path)
193 if not os.path.isfile(path) and if_missing_return is not False:
194 return if_missing_return
195 with open(path, 'r') as f:
196 config = yaml.safe_load(f)
197 config = _standardize(config)
198 if validation_schema:
199 schema.validate(config, validation_schema)
200 return config
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200201
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200202def write(path, config):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200203 log.ctx(path)
204 with open(path, 'w') as f:
205 f.write(tostr(config))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200206
Pau Espin Pedrol4e6b5072020-05-11 15:12:07 +0200207def fromstr(config_str, validation_schema=None):
208 config = yaml.safe_load(config_str)
209 config = _standardize(config)
210 if validation_schema is not None:
211 schema.validate(config, validation_schema)
212 return config
213
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200214def tostr(config):
215 return _tostr(_standardize(config))
216
217def _tostr(config):
218 return yaml.dump(config, default_flow_style=False)
219
220def _standardize_item(item):
Pau Espin Pedrol7691f2d2020-02-18 12:12:01 +0100221 if item is None:
222 return None
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200223 if isinstance(item, (tuple, list)):
224 return [_standardize_item(i) for i in item]
225 if isinstance(item, dict):
226 return dict([(key.lower(), _standardize_item(val)) for key,val in item.items()])
227 return str(item)
228
229def _standardize(config):
230 config = yaml.safe_load(_tostr(_standardize_item(config)))
231 return config
232
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200233def overlay(dest, src):
234 if is_dict(dest):
235 if not is_dict(src):
236 raise ValueError('cannot combine dict with a value of type: %r' % type(src))
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200237
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200238 for key, val in src.items():
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200239 log.ctx(key=key)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200240 dest_val = dest.get(key)
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200241 dest[key] = overlay(dest_val, val)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200242 return dest
243 if is_list(dest):
244 if not is_list(src):
245 raise ValueError('cannot combine list with a value of type: %r' % type(src))
Pau Espin Pedrol27532042017-09-15 15:31:52 +0200246 copy_len = min(len(src),len(dest))
247 for i in range(copy_len):
Pau Espin Pedrolebced952017-08-28 17:21:34 +0200248 log.ctx(idx=i)
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200249 dest[i] = overlay(dest[i], src[i])
Pau Espin Pedrol27532042017-09-15 15:31:52 +0200250 for i in range(copy_len, len(src)):
251 dest.append(src[i])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200252 return dest
253 return src
Pau Espin Pedrol802dfe52017-09-12 13:43:40 +0200254
255def replicate_times(d):
Pau Espin Pedrol0b302792017-09-10 16:33:10 +0200256 '''
257 replicate items that have a "times" > 1
258
259 'd' is a dict matching WANT_SCHEMA, which is the same as
260 the RESOURCES_SCHEMA, except each entity that can be reserved has a 'times'
261 field added, to indicate how many of those should be reserved.
262 '''
Pau Espin Pedrol802dfe52017-09-12 13:43:40 +0200263 d = copy.deepcopy(d)
264 for key, item_list in d.items():
Pau Espin Pedrol26050342017-09-12 15:02:25 +0200265 idx = 0
266 while idx < len(item_list):
267 item = item_list[idx]
268 times = int(item.pop('times', 1))
269 for j in range(1, times):
270 item_list.insert(idx + j, copy.deepcopy(item))
271 idx += times
Pau Espin Pedrol802dfe52017-09-12 13:43:40 +0200272 return d
273
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200274# vim: expandtab tabstop=4 shiftwidth=4