Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: run_node: object holding information on a target env to run stuff. |
| 2 | # |
| 3 | # Copyright (C) 2020 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU 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 General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 20 | from ..core import log |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 21 | from ..core import schema |
| 22 | |
| 23 | def on_register_schemas(): |
Pau Espin Pedrol | 1abff4e | 2020-05-26 12:32:19 +0200 | [diff] [blame] | 24 | resource_schema = RunNode.schema() |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 25 | schema.register_resource_schema('run_node', resource_schema) |
| 26 | |
Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 27 | |
| 28 | class RunNode(log.Origin): |
| 29 | |
| 30 | T_LOCAL = 'local' |
| 31 | T_REM_SSH = 'ssh' |
| 32 | |
Nils Fürste | 2af2b15 | 2020-11-23 11:57:41 +0100 | [diff] [blame] | 33 | def __init__(self, type=None, run_addr=None, ssh_user=None, ssh_addr=None, run_label=None, ssh_port=None): |
Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 34 | super().__init__(log.C_RUN, 'runnode') |
| 35 | self._type = type |
| 36 | self._run_addr = run_addr |
| 37 | self._ssh_user = ssh_user |
| 38 | self._ssh_addr = ssh_addr |
Pau Espin Pedrol | 6e0b6fb | 2020-05-25 19:49:29 +0200 | [diff] [blame] | 39 | self._run_label = run_label |
Nils Fürste | 2af2b15 | 2020-11-23 11:57:41 +0100 | [diff] [blame] | 40 | self._ssh_port = ssh_port |
Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 41 | if not self._type: |
| 42 | raise log.Error('run_type not set') |
| 43 | if not self._run_addr: |
| 44 | raise log.Error('run_addr not set') |
| 45 | if self._type == RunNode.T_LOCAL and (self._ssh_user or self._ssh_addr): |
| 46 | raise log.Error('run_type=%s but ssh info set' % RunNode.T_LOCAL) |
| 47 | if self._type == RunNode.T_REM_SSH and not (self._ssh_user and self._ssh_addr): |
| 48 | raise log.Error('run_type=%s but ssh info NOT set' % RunNode.T_LOCAL) |
| 49 | |
| 50 | if self._type == RunNode.T_LOCAL: |
| 51 | self.set_name('run-' + self._run_addr) |
| 52 | else: |
| 53 | self.set_name('run-' + self._run_addr + "(" + self._ssh_user + '@' + self._ssh_addr + ")") |
| 54 | |
| 55 | @classmethod |
| 56 | def from_conf(cls, conf): |
Pau Espin Pedrol | 6e0b6fb | 2020-05-25 19:49:29 +0200 | [diff] [blame] | 57 | return cls(conf.get('run_type', None), conf.get('run_addr', None), |
| 58 | conf.get('ssh_user', None), conf.get('ssh_addr', None), |
Nils Fürste | 2af2b15 | 2020-11-23 11:57:41 +0100 | [diff] [blame] | 59 | conf.get('run_label', None), conf.get('ssh_port', None)) |
Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 60 | |
Pau Espin Pedrol | 1abff4e | 2020-05-26 12:32:19 +0200 | [diff] [blame] | 61 | @classmethod |
| 62 | def schema(cls): |
| 63 | resource_schema = { |
| 64 | 'run_type': schema.STR, |
| 65 | 'run_addr': schema.IPV4, |
| 66 | 'ssh_user': schema.STR, |
| 67 | 'ssh_addr': schema.IPV4, |
| 68 | 'run_label': schema.STR, |
Nils Fürste | 2af2b15 | 2020-11-23 11:57:41 +0100 | [diff] [blame] | 69 | 'ssh_port': schema.STR, |
Pau Espin Pedrol | 1abff4e | 2020-05-26 12:32:19 +0200 | [diff] [blame] | 70 | } |
| 71 | return resource_schema |
| 72 | |
Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 73 | def is_local(self): |
| 74 | return self._type == RunNode.T_LOCAL |
| 75 | |
| 76 | def __str__(self): |
| 77 | return self.name() |
| 78 | |
| 79 | def run_type(self): |
| 80 | return self._type |
| 81 | |
| 82 | def run_addr(self): |
| 83 | return self._run_addr |
| 84 | |
| 85 | def ssh_user(self): |
| 86 | return self._ssh_user |
| 87 | |
| 88 | def ssh_addr(self): |
| 89 | return self._ssh_addr |
| 90 | |
Pau Espin Pedrol | 6e0b6fb | 2020-05-25 19:49:29 +0200 | [diff] [blame] | 91 | def run_label(self): |
| 92 | return self._run_label |
| 93 | |
Nils Fürste | 2af2b15 | 2020-11-23 11:57:41 +0100 | [diff] [blame] | 94 | def ssh_port(self): |
| 95 | return self._ssh_port |
| 96 | |
Pau Espin Pedrol | 116a2c4 | 2020-02-11 17:41:13 +0100 | [diff] [blame] | 97 | # vim: expandtab tabstop=4 shiftwidth=4 |