koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 1 | import abc |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 2 | from typing import Any, Set, Optional, Dict, NamedTuple, Optional |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 3 | from .ssh_utils import ConnCreds |
| 4 | from .common_types import IPAddr |
| 5 | |
| 6 | |
| 7 | RPCCreds = NamedTuple("RPCCreds", [("addr", IPAddr), ("key_file", str), ("cert_file", str)]) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class NodeInfo: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 11 | """Node information object, result of discovery process or config parsing""" |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 12 | def __init__(self, ssh_creds: ConnCreds, roles: Set[str], params: Dict[str, Any] = None) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 13 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 14 | # ssh credentials |
| 15 | self.ssh_creds = ssh_creds |
| 16 | # credentials for RPC connection |
| 17 | self.rpc_creds = None # type: Optional[RPCCreds] |
| 18 | self.roles = roles |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 19 | self.os_vm_id = None # type: Optional[int] |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 20 | self.params = {} # type: Dict[str, Any] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 21 | if params is not None: |
| 22 | self.params = params |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 23 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 24 | def node_id(self) -> str: |
| 25 | return "{0.host}:{0.port}".format(self.ssh_creds.addr) |
| 26 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 27 | def __str__(self) -> str: |
| 28 | return self.node_id() |
| 29 | |
| 30 | def __repr__(self) -> str: |
| 31 | return str(self) |
| 32 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 33 | |
| 34 | class ISSHHost(metaclass=abc.ABCMeta): |
| 35 | """Minimal interface, required to setup RPC connection""" |
| 36 | info = None # type: NodeInfo |
| 37 | |
| 38 | @abc.abstractmethod |
| 39 | def run(self, cmd: str, timeout: int = 60, nolog: bool = False) -> str: |
| 40 | pass |
| 41 | |
| 42 | @abc.abstractmethod |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 43 | def __str__(self) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 44 | pass |
| 45 | |
| 46 | @abc.abstractmethod |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 47 | def disconnect(self) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 48 | pass |
| 49 | |
| 50 | @abc.abstractmethod |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 51 | def put_to_file(self, path: Optional[str], content: bytes) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 52 | pass |
| 53 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 54 | def __enter__(self) -> 'ISSHHost': |
| 55 | return self |
| 56 | |
| 57 | def __exit__(self, x, y, z) -> bool: |
| 58 | self.disconnect() |
| 59 | return False |
| 60 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 61 | |
| 62 | class IRPCNode(metaclass=abc.ABCMeta): |
| 63 | """Remote filesystem interface""" |
| 64 | info = None # type: NodeInfo |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 65 | conn = None # type: Any |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 66 | rpc_log_file = None # type: str |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 67 | |
| 68 | @abc.abstractmethod |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 69 | def __str__(self) -> str: |
| 70 | pass |
| 71 | |
| 72 | @abc.abstractmethod |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame^] | 73 | def run(self, cmd: str, timeout: int = 60, nolog: bool = False, check_timeout: float = 0.01) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 74 | pass |
| 75 | |
| 76 | @abc.abstractmethod |
| 77 | def copy_file(self, local_path: str, remote_path: str = None) -> str: |
| 78 | pass |
| 79 | |
| 80 | @abc.abstractmethod |
| 81 | def get_file_content(self, path: str) -> bytes: |
| 82 | pass |
| 83 | |
| 84 | @abc.abstractmethod |
koder aka kdanilov | e7e1a4d | 2016-12-17 20:29:52 +0200 | [diff] [blame] | 85 | def put_to_file(self, path: Optional[str], content: bytes) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 86 | pass |
| 87 | |
| 88 | @abc.abstractmethod |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 89 | def stat_file(self, path:str) -> Any: |
| 90 | pass |
| 91 | |
| 92 | @abc.abstractmethod |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame^] | 93 | def disconnect(self) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 94 | pass |
| 95 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 96 | @abc.abstractmethod |
| 97 | def upload_plugin(self, name: str, code: bytes, version: str = None) -> None: |
| 98 | pass |
| 99 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 100 | def __enter__(self) -> 'IRPCNode': |
| 101 | return self |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 102 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 103 | def __exit__(self, x, y, z) -> bool: |
| 104 | self.disconnect() |
| 105 | return False |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 106 | |