Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running a sysmoBTS |
| 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 |
| 8 | # it under the terms of the GNU Affero General Public License as |
| 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 |
| 15 | # GNU Affero General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Affero General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 20 | import os |
| 21 | from . import log, config, util, template, process |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 22 | |
| 23 | class SysmoBts(log.Origin): |
| 24 | suite_run = None |
| 25 | nitb = None |
| 26 | run_dir = None |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 27 | inst = None |
| 28 | remote_addr = None |
| 29 | remote_inst = None |
| 30 | remote_env = None |
| 31 | remote_dir = None |
| 32 | |
| 33 | REMOTE_DIR = '/osmo-gsm-tester' |
| 34 | BTS_SYSMO_BIN = 'osmo-bts-sysmo' |
| 35 | BTS_SYSMO_CFG = 'osmo-bts-sysmo.cfg' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 36 | |
| 37 | def __init__(self, suite_run, conf): |
| 38 | self.suite_run = suite_run |
| 39 | self.conf = conf |
| 40 | self.set_name('osmo-bts-sysmo') |
| 41 | self.set_log_category(log.C_RUN) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 42 | self.remote_env = {} |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 43 | |
| 44 | def start(self): |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 45 | with self: |
| 46 | if self.nitb is None: |
| 47 | raise RuntimeError('BTS needs to be added to a NITB before it can be started') |
| 48 | self.log('Starting sysmoBTS to connect to', self.nitb) |
| 49 | self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name())) |
| 50 | self.configure() |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 51 | |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 52 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(SysmoBts.BTS_SYSMO_BIN))) |
| 53 | lib = self.inst.child('lib') |
| 54 | if not os.path.isdir(lib): |
| 55 | self.raise_exn('No lib/ in', self.inst) |
| 56 | if not self.inst.isfile('bin', SysmoBts.BTS_SYSMO_BIN): |
| 57 | self.raise_exn('No osmo-bts-sysmo binary in', self.inst) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 58 | |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 59 | self.remote_dir = util.Dir(SysmoBts.REMOTE_DIR) |
| 60 | self.remote_inst = util.Dir(self.remote_dir.child(os.path.basename(str(self.inst)))) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 61 | |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 62 | self.run_remote('rm-remote-dir', ('test', '!', '-d', SysmoBts.REMOTE_DIR, '||', 'rm', '-rf', SysmoBts.REMOTE_DIR)) |
| 63 | self.run_remote('mk-remote-dir', ('mkdir', '-p', SysmoBts.REMOTE_DIR)) |
| 64 | self.run_local('scp-inst-to-sysmobts', |
| 65 | ('scp', '-r', str(self.inst), '%s:%s' % (self.remote_addr, str(self.remote_inst)))) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 66 | |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 67 | remote_run_dir = self.remote_dir.child(SysmoBts.BTS_SYSMO_BIN) |
| 68 | self.run_remote('mk-remote-run-dir', ('mkdir', '-p', remote_run_dir)) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 69 | |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 70 | remote_config_file = self.remote_dir.child(SysmoBts.BTS_SYSMO_CFG) |
| 71 | self.run_local('scp-cfg-to-sysmobts', |
| 72 | ('scp', '-r', self.config_file, '%s:%s' % (self.remote_addr, remote_config_file))) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 73 | |
Neels Hofmeyr | b818b2f | 2017-04-10 05:15:38 +0200 | [diff] [blame] | 74 | remote_lib = self.remote_inst.child('lib') |
| 75 | remote_binary = self.remote_inst.child('bin', 'osmo-bts-sysmo') |
| 76 | self.launch_remote('osmo-bts-sysmo', |
| 77 | ('LD_LIBRARY_PATH=%s' % remote_lib, |
| 78 | remote_binary, '-c', remote_config_file, '-r', '1'), |
| 79 | remote_cwd=remote_run_dir) |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 80 | |
| 81 | def _process_remote(self, name, popen_args, remote_cwd=None): |
| 82 | run_dir = self.run_dir.new_dir(name) |
| 83 | return process.RemoteProcess(name, run_dir, self.remote_addr, remote_cwd, |
| 84 | popen_args) |
| 85 | |
| 86 | def run_remote(self, name, popen_args, remote_cwd=None): |
| 87 | proc = self._process_remote(name, popen_args, remote_cwd) |
| 88 | proc.launch() |
| 89 | proc.wait() |
| 90 | if proc.result != 0: |
| 91 | proc.raise_exn('Exited in error') |
| 92 | |
| 93 | def launch_remote(self, name, popen_args, remote_cwd=None): |
| 94 | proc = self._process_remote(name, popen_args, remote_cwd) |
| 95 | self.suite_run.remember_to_stop(proc) |
| 96 | proc.launch() |
| 97 | |
| 98 | def run_local(self, name, popen_args): |
| 99 | run_dir = self.run_dir.new_dir(name) |
| 100 | proc = process.Process(name, run_dir, popen_args) |
| 101 | proc.launch() |
| 102 | proc.wait() |
| 103 | if proc.result != 0: |
| 104 | proc.raise_exn('Exited in error') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 105 | |
| 106 | def configure(self): |
| 107 | if self.nitb is None: |
| 108 | raise RuntimeError('BTS needs to be added to a NITB before it can be configured') |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 109 | |
| 110 | self.remote_addr = self.conf.get('addr') |
| 111 | |
| 112 | self.config_file = self.run_dir.new_file(SysmoBts.BTS_SYSMO_CFG) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 113 | self.dbg(config_file=self.config_file) |
| 114 | |
| 115 | values = { 'osmo_bts_sysmo': config.get_defaults('osmo_bts_sysmo') } |
| 116 | config.overlay(values, self.suite_run.config()) |
| 117 | config.overlay(values, { 'osmo_bts_sysmo': { 'oml_remote_ip': self.nitb.addr() } }) |
| 118 | config.overlay(values, { 'osmo_bts_sysmo': self.conf }) |
| 119 | self.dbg(conf=values) |
| 120 | |
| 121 | with open(self.config_file, 'w') as f: |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 122 | r = template.render(SysmoBts.BTS_SYSMO_CFG, values) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 123 | self.dbg(r) |
| 124 | f.write(r) |
| 125 | |
| 126 | def conf_for_nitb(self): |
| 127 | values = config.get_defaults('nitb_bts') |
| 128 | config.overlay(values, config.get_defaults('osmo_bts_sysmo')) |
| 129 | config.overlay(values, self.conf) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 130 | self.dbg(conf=values) |
| 131 | return values |
| 132 | |
| 133 | def set_nitb(self, nitb): |
| 134 | self.nitb = nitb |
| 135 | |
| 136 | # vim: expandtab tabstop=4 shiftwidth=4 |