Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-bts-trx |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
Pau Espin Pedrol | 302c756 | 2018-10-02 13:08:02 +0200 | [diff] [blame] | 21 | import stat |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 22 | import pprint |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 23 | from abc import ABCMeta, abstractmethod |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 24 | from . import log, config, util, template, process, bts_osmo |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 25 | from . import powersupply |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 26 | from .event_loop import MainLoop |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 27 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 28 | class OsmoBtsTrx(bts_osmo.OsmoBtsMainUnit): |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 29 | ############## |
| 30 | # PROTECTED |
| 31 | ############## |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 32 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 33 | BIN_BTS_TRX = 'osmo-bts-trx' |
| 34 | BIN_PCU = 'osmo-pcu' |
| 35 | |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 36 | CONF_BTS_TRX = 'osmo-bts-trx.cfg' |
| 37 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 38 | def __init__(self, suite_run, conf): |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 39 | super().__init__(suite_run, conf, OsmoBtsTrx.BIN_BTS_TRX, 'osmo_bts_trx') |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 40 | self.run_dir = None |
| 41 | self.inst = None |
| 42 | self.trx = None |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 43 | self.pwsup_list = [] |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 44 | self.env = {} |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 45 | self.gen_conf = {} |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 46 | |
| 47 | def trx_remote_ip(self): |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 48 | conf_ip = self.conf.get('osmo_trx', {}).get('trx_ip', None) |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 49 | if conf_ip is not None: |
| 50 | return conf_ip |
| 51 | # if 'trx_remote_ip' is not configured, use same IP as BTS |
| 52 | return self.remote_addr() |
| 53 | |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 54 | def launch_process(self, keepalive, binary_name, *args): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 55 | binary = os.path.abspath(self.inst.child('bin', binary_name)) |
| 56 | run_dir = self.run_dir.new_dir(binary_name) |
| 57 | if not os.path.isfile(binary): |
| 58 | raise RuntimeError('Binary missing: %r' % binary) |
| 59 | proc = process.Process(binary_name, run_dir, |
| 60 | (binary,) + args, |
| 61 | env=self.env) |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 62 | self.suite_run.remember_to_stop(proc, keepalive) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 63 | proc.launch() |
Neels Hofmeyr | fd39f3e | 2017-05-07 02:02:35 +0200 | [diff] [blame] | 64 | return proc |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 65 | |
| 66 | def configure(self): |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 67 | if self.bsc is None: |
| 68 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured') |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 69 | self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 70 | self.dbg(config_file=self.config_file) |
| 71 | |
| 72 | values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx')) |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 73 | config.overlay(values, dict(osmo_bts_trx=dict(osmo_trx=config.get_defaults('osmo_trx')))) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 74 | config.overlay(values, self.suite_run.config()) |
Pau Espin Pedrol | 329b6f4 | 2017-08-08 13:55:24 +0200 | [diff] [blame] | 75 | config.overlay(values, { |
| 76 | 'osmo_bts_trx': { |
| 77 | 'oml_remote_ip': self.bsc.addr(), |
Pau Espin Pedrol | 15aae98 | 2017-09-08 13:55:54 +0200 | [diff] [blame] | 78 | 'pcu_socket_path': self.pcu_socket_path(), |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 79 | 'osmo_trx': { |
| 80 | 'bts_ip': self.remote_addr(), |
| 81 | 'trx_ip': self.trx_remote_ip(), |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 82 | 'egprs': 'enable' if self.conf_for_bsc()['gprs_mode'] == 'egprs' else 'disable', |
Pau Espin Pedrol | 44b7ea1 | 2018-09-17 20:24:13 +0200 | [diff] [blame] | 83 | 'channels': [{} for trx_i in range(self.num_trx())] |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 84 | } |
Pau Espin Pedrol | 329b6f4 | 2017-08-08 13:55:24 +0200 | [diff] [blame] | 85 | } |
| 86 | }) |
| 87 | config.overlay(values, { 'osmo_bts_trx': self.conf }) |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 88 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 89 | self.gen_conf = values |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 90 | self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 91 | |
| 92 | with open(self.config_file, 'w') as f: |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 93 | r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 94 | self.dbg(r) |
| 95 | f.write(r) |
| 96 | |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 97 | self.pwsup_list = [None] * self.num_trx() |
| 98 | # Construct trx_list appending with empty dicts if needed: |
| 99 | conf_trx_list = self.conf.get('trx_list', []) |
| 100 | conf_trx_list = conf_trx_list + [{}] * (self.num_trx() - len(conf_trx_list)) |
| 101 | for trx_i in range(self.num_trx()): |
| 102 | pwsup_opt = conf_trx_list[trx_i].get('power_supply', {}) |
| 103 | if not pwsup_opt: |
| 104 | self.dbg('no power_supply configured for TRX %d' % trx_i) |
| 105 | continue |
| 106 | pwsup_type = pwsup_opt.get('type') |
| 107 | if not pwsup_type: |
| 108 | raise log.Error('No type attribute provided in power_supply conf for TRX %d!' % trx_i) |
| 109 | self.pwsup_list[trx_i] = powersupply.get_instance_by_type(pwsup_type, pwsup_opt) |
| 110 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 111 | def launch_trx_enabled(self): |
| 112 | return util.str2bool(self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}).get('launch_trx')) |
| 113 | |
| 114 | def get_osmo_trx_type(self): |
| 115 | return self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}).get('type') |
| 116 | |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 117 | ######################## |
| 118 | # PUBLIC - INTERNAL API |
| 119 | ######################## |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 120 | def conf_for_bsc(self): |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 121 | values = self.conf_for_bsc_prepare() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 122 | self.dbg(conf=values) |
| 123 | return values |
| 124 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 125 | def conf_for_osmotrx(self): |
| 126 | return dict(osmo_trx=self.gen_conf['osmo_bts_trx'].get('osmo_trx', {})) |
| 127 | |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 128 | def cleanup(self): |
| 129 | i = 0 |
| 130 | for pwsup in self.pwsup_list: |
| 131 | if pwsup: |
| 132 | self.dbg('Powering off TRX %d' % i) |
| 133 | pwsup.power_set(False) |
| 134 | i = i + 1 |
| 135 | self.pwsup_list = [] |
| 136 | |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 137 | ################### |
| 138 | # PUBLIC (test API included) |
| 139 | ################### |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 140 | def start(self, keepalive=False): |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 141 | if self.bsc is None: |
| 142 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started') |
| 143 | self.suite_run.poll() |
| 144 | |
| 145 | self.log('Starting to connect to', self.bsc) |
| 146 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 147 | self.configure() |
| 148 | |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 149 | # Power cycle all TRX if needed (right now only TRX0 for SC5): |
| 150 | i = 0 |
| 151 | for pwsup in self.pwsup_list: |
| 152 | if pwsup: |
| 153 | self.dbg('Powering cycling TRX %d' % i) |
| 154 | pwsup.power_cycle(1.0) |
| 155 | i = i + 1 |
| 156 | |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 157 | if self.launch_trx_enabled(): |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 158 | self.trx = OsmoTrx.get_instance_by_type(self.get_osmo_trx_type(), self.suite_run, self.conf_for_osmotrx()) |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 159 | self.trx.start(keepalive) |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 160 | self.log('Waiting for %s to start up...' % self.trx.name()) |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 161 | MainLoop.wait(self, self.trx.trx_ready) |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 162 | |
| 163 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts'))) |
| 164 | lib = self.inst.child('lib') |
| 165 | if not os.path.isdir(lib): |
| 166 | raise RuntimeError('No lib/ in %r' % self.inst) |
| 167 | self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 168 | |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 169 | self.proc_bts = self.launch_process(keepalive, OsmoBtsTrx.BIN_BTS_TRX, '-r', '1', |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 170 | '-c', os.path.abspath(self.config_file), |
Pau Espin Pedrol | c93707f | 2019-08-05 18:19:45 +0200 | [diff] [blame] | 171 | '-i', self.bsc.addr()) |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 172 | self.suite_run.poll() |
| 173 | |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 174 | |
| 175 | ################################################################################ |
| 176 | # TRX |
| 177 | ################################################################################ |
| 178 | |
| 179 | class Trx(log.Origin, metaclass=ABCMeta): |
| 180 | ############## |
| 181 | # PROTECTED |
| 182 | ############## |
| 183 | def __init__(self, suite_run, conf, name): |
| 184 | super().__init__(log.C_RUN, name) |
| 185 | self.suite_run = suite_run |
| 186 | self.conf = conf |
| 187 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 188 | self.listen_ip = conf.get('osmo_trx', {}).get('trx_ip') |
| 189 | self.remote_user = conf.get('osmo_trx', {}).get('remote_user', None) |
| 190 | |
| 191 | @classmethod |
| 192 | def get_instance_by_type(cls, type, suite_run, conf): |
| 193 | KNOWN_OSMOTRX_TYPES = { |
| 194 | 'uhd': OsmoTrxUHD, |
| 195 | 'lms': OsmoTrxLMS, |
| 196 | 'sc5': TrxSC5 |
| 197 | } |
| 198 | osmo_trx_class = KNOWN_OSMOTRX_TYPES.get(type) |
| 199 | return osmo_trx_class(suite_run, conf) |
| 200 | |
| 201 | ############## |
| 202 | # PUBLIC (test API included) |
| 203 | ############## |
| 204 | @abstractmethod |
| 205 | def start(self, keepalive=False): |
| 206 | pass |
| 207 | |
| 208 | @abstractmethod |
| 209 | def trx_ready(self): |
| 210 | pass |
| 211 | |
| 212 | class OsmoTrx(Trx, metaclass=ABCMeta): |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 213 | |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 214 | CONF_OSMO_TRX = 'osmo-trx.cfg' |
Pau Espin Pedrol | a9006df | 2018-10-01 12:26:39 +0200 | [diff] [blame] | 215 | REMOTE_DIR = '/osmo-gsm-tester-trx/last_run' |
Pau Espin Pedrol | 302c756 | 2018-10-02 13:08:02 +0200 | [diff] [blame] | 216 | WRAPPER_SCRIPT = 'ssh_sigkiller.sh' |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 217 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 218 | ############## |
| 219 | # PROTECTED |
| 220 | ############## |
| 221 | def __init__(self, suite_run, conf): |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 222 | super().__init__(suite_run, conf, self.binary_name()) |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 223 | self.env = {} |
Pau Espin Pedrol | a9006df | 2018-10-01 12:26:39 +0200 | [diff] [blame] | 224 | self.log("OSMOTRX CONF: %r" % conf) |
Pau Espin Pedrol | a9006df | 2018-10-01 12:26:39 +0200 | [diff] [blame] | 225 | self.bts_ip = conf.get('osmo_trx', {}).get('bts_ip') |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 226 | self.inst = None |
| 227 | self.proc_trx = None |
| 228 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 229 | @abstractmethod |
| 230 | def binary_name(self): |
| 231 | 'Used by base class. Subclass can create different OsmoTRX implementations.' |
| 232 | pass |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 233 | |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 234 | def configure(self): |
| 235 | self.config_file = self.run_dir.new_file(OsmoTrx.CONF_OSMO_TRX) |
| 236 | self.dbg(config_file=self.config_file) |
| 237 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 238 | values = self.conf |
Pau Espin Pedrol | 1ff0376 | 2018-10-02 15:50:48 +0200 | [diff] [blame] | 239 | |
| 240 | # we don't need to enable multi-arfcn for single channel |
| 241 | if len(values.get('osmo_trx', {}).get('channels', [])) > 1: |
| 242 | multi_arfcn_bool = util.str2bool(values.get('osmo_trx', {}).get('multi_arfcn', False)) |
| 243 | else: |
| 244 | multi_arfcn_bool = False |
Pau Espin Pedrol | 94eab26 | 2018-09-17 20:25:55 +0200 | [diff] [blame] | 245 | config.overlay(values, { 'osmo_trx': { 'multi_arfcn': multi_arfcn_bool } }) |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 246 | |
| 247 | self.dbg('OSMO-TRX CONFIG:\n' + pprint.pformat(values)) |
| 248 | |
| 249 | with open(self.config_file, 'w') as f: |
| 250 | r = template.render(OsmoTrx.CONF_OSMO_TRX, values) |
| 251 | self.dbg(r) |
| 252 | f.write(r) |
| 253 | |
Pau Espin Pedrol | a9006df | 2018-10-01 12:26:39 +0200 | [diff] [blame] | 254 | def launch_process_local(self, keepalive, binary_name, *args): |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 255 | binary = os.path.abspath(self.inst.child('bin', binary_name)) |
| 256 | run_dir = self.run_dir.new_dir(binary_name) |
| 257 | if not os.path.isfile(binary): |
| 258 | raise RuntimeError('Binary missing: %r' % binary) |
| 259 | proc = process.Process(binary_name, run_dir, |
| 260 | (binary,) + args, |
| 261 | env=self.env) |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 262 | self.suite_run.remember_to_stop(proc, keepalive) |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 263 | proc.launch() |
| 264 | return proc |
| 265 | |
Pau Espin Pedrol | a9006df | 2018-10-01 12:26:39 +0200 | [diff] [blame] | 266 | def launch_process_remote(self, name, popen_args, remote_cwd=None, keepalive=False): |
| 267 | run_dir = self.run_dir.new_dir(name) |
| 268 | proc = process.RemoteProcess(name, run_dir, self.remote_user, self.listen_ip, remote_cwd, |
| 269 | popen_args) |
| 270 | self.suite_run.remember_to_stop(proc, keepalive) |
| 271 | proc.launch() |
| 272 | return proc |
| 273 | |
Pau Espin Pedrol | 302c756 | 2018-10-02 13:08:02 +0200 | [diff] [blame] | 274 | def generate_wrapper_script(self): |
| 275 | wrapper_script = self.run_dir.new_file(OsmoTrx.WRAPPER_SCRIPT) |
| 276 | with open(wrapper_script, 'w') as f: |
| 277 | r = """#!/bin/bash |
| 278 | mypid=0 |
| 279 | sign_handler() { |
| 280 | sig=$1 |
| 281 | echo "received signal handler $sig, killing $mypid" |
| 282 | kill $mypid |
| 283 | } |
Pau Espin Pedrol | f11c4ee | 2019-08-14 12:30:15 +0200 | [diff] [blame] | 284 | trap 'sign_handler SIGTERM' SIGTERM |
Pau Espin Pedrol | 302c756 | 2018-10-02 13:08:02 +0200 | [diff] [blame] | 285 | trap 'sign_handler SIGINT' SIGINT |
| 286 | trap 'sign_handler SIGHUP' SIGHUP |
| 287 | "$@" & |
| 288 | mypid=$! |
| 289 | echo "waiting for $mypid" |
| 290 | wait $mypid |
Pau Espin Pedrol | f11c4ee | 2019-08-14 12:30:15 +0200 | [diff] [blame] | 291 | echo "process $mypid finished" |
Pau Espin Pedrol | 302c756 | 2018-10-02 13:08:02 +0200 | [diff] [blame] | 292 | """ |
| 293 | f.write(r) |
| 294 | st = os.stat(wrapper_script) |
| 295 | os.chmod(wrapper_script, st.st_mode | stat.S_IEXEC) |
| 296 | return wrapper_script |
| 297 | |
Pau Espin Pedrol | dd71022 | 2019-11-26 14:33:09 +0100 | [diff] [blame] | 298 | def inst_compatible_for_remote(self): |
| 299 | proc = process.run_remote_sync(self.run_dir, self.remote_user, self.listen_ip, 'uname-m', ('uname', '-m')) |
| 300 | if "x86_64" in (proc.get_stdout() or ''): |
| 301 | return True |
| 302 | return False |
| 303 | |
| 304 | def start_remotely(self, keepalive): |
| 305 | # Run remotely through ssh. We need to run osmo-trx under a wrapper |
| 306 | # script since osmo-trx ignores SIGHUP and will keep running after |
| 307 | # we close local ssh session. The wrapper script catches SIGHUP and |
| 308 | # sends SIGINT to it. |
| 309 | remote_run_dir = util.Dir(OsmoTrx.REMOTE_DIR) |
| 310 | remote_config_file = remote_run_dir.child(OsmoTrx.CONF_OSMO_TRX) |
| 311 | |
| 312 | have_inst = self.inst_compatible_for_remote() |
| 313 | if have_inst: |
| 314 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx'))) |
| 315 | |
| 316 | # if self.inst is None, we still want to copy config file, create remote run dir, etc. |
| 317 | self.remote_inst = process.copy_inst_ssh(self.run_dir, self.inst, remote_run_dir, self.remote_user, |
| 318 | self.listen_ip, self.binary_name(), self.config_file) |
| 319 | |
| 320 | wrapper_script = self.generate_wrapper_script() |
| 321 | remote_wrapper_script = remote_run_dir.child(OsmoTrx.WRAPPER_SCRIPT) |
| 322 | process.scp(self.run_dir, self.remote_user, self.listen_ip, 'scp-wrapper-to-remote', wrapper_script, remote_wrapper_script) |
| 323 | |
| 324 | if have_inst: |
| 325 | remote_lib = self.remote_inst.child('lib') |
| 326 | remote_binary = self.remote_inst.child('bin', self.binary_name()) |
| 327 | args = ('LD_LIBRARY_PATH=%s' % remote_lib, remote_wrapper_script, remote_binary, '-C', remote_config_file) |
| 328 | else: # Use whatever is available i nremote system PATH: |
| 329 | args = (remote_wrapper_script, self.binary_name(), '-C', remote_config_file) |
| 330 | |
| 331 | self.proc_trx = self.launch_process_remote(self.binary_name(), args, remote_cwd=remote_run_dir, keepalive=keepalive) |
| 332 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 333 | ############## |
| 334 | # PUBLIC (test API included) |
| 335 | ############## |
| 336 | def start(self, keepalive=False): |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 337 | self.configure() |
Pau Espin Pedrol | dd71022 | 2019-11-26 14:33:09 +0100 | [diff] [blame] | 338 | if self.remote_user: |
| 339 | self.start_remotely(keepalive) |
| 340 | return |
| 341 | # Run locally if ssh user is not set |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 342 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx'))) |
Pau Espin Pedrol | dd71022 | 2019-11-26 14:33:09 +0100 | [diff] [blame] | 343 | lib = self.inst.child('lib') |
| 344 | self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 345 | self.proc_trx = self.launch_process_local(keepalive, self.binary_name(), |
| 346 | '-C', os.path.abspath(self.config_file)) |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 347 | |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 348 | def trx_ready(self): |
| 349 | if not self.proc_trx or not self.proc_trx.is_running: |
| 350 | return False |
| 351 | return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '') |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 352 | |
| 353 | class OsmoTrxUHD(OsmoTrx): |
| 354 | BIN_TRX = 'osmo-trx-uhd' |
| 355 | |
| 356 | def __init__(self, suite_run, conf): |
| 357 | super().__init__(suite_run, conf) |
| 358 | |
| 359 | def binary_name(self): |
| 360 | return OsmoTrxUHD.BIN_TRX |
| 361 | |
Pau Espin Pedrol | e789f0c | 2018-08-27 18:45:01 +0200 | [diff] [blame] | 362 | class OsmoTrxLMS(OsmoTrx): |
| 363 | BIN_TRX = 'osmo-trx-lms' |
| 364 | |
| 365 | def __init__(self, suite_run, conf): |
| 366 | super().__init__(suite_run, conf) |
Pau Espin Pedrol | e789f0c | 2018-08-27 18:45:01 +0200 | [diff] [blame] | 367 | |
| 368 | def binary_name(self): |
| 369 | return OsmoTrxLMS.BIN_TRX |
| 370 | |
Pau Espin Pedrol | 55cee16 | 2019-09-10 13:47:04 +0200 | [diff] [blame] | 371 | class TrxSC5(Trx): |
| 372 | |
| 373 | def __init__(self, suite_run, conf): |
| 374 | super().__init__(suite_run, conf, "sc5-trx") |
| 375 | self.ready = False |
| 376 | |
| 377 | def start(self, keepalive=False): |
| 378 | name = "ssh_sc5_ccli" |
| 379 | run_dir = self.run_dir.new_dir(name) |
| 380 | popen_args = ('/cx/bin/ccli', '-c', 'gsm.unlock') |
| 381 | proc = process.RemoteProcess(name, run_dir, self.remote_user, self.listen_ip, None, |
| 382 | popen_args) |
| 383 | keep_trying = 10 |
| 384 | while keep_trying > 0: |
| 385 | if proc.respawn_sync(raise_nonsuccess=False) == 0 and 'OK' in (proc.get_stdout() or ''): |
| 386 | break |
| 387 | keep_trying = keep_trying - 1 |
| 388 | self.log('Configuring SC5 TRX failed, retrying %d more times' % keep_trying) |
| 389 | MainLoop.sleep(self, 5) |
| 390 | if keep_trying == 0: |
| 391 | raise log.Error('Failed configuring SC5!') |
| 392 | self.ready = True |
| 393 | |
| 394 | def trx_ready(self): |
| 395 | return self.ready |
| 396 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 397 | # vim: expandtab tabstop=4 shiftwidth=4 |