koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 1 | import abc |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 2 | from typing import Any, Set, Optional, List, Dict, Callable, NamedTuple |
| 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""" |
| 12 | def __init__(self, ssh_creds: ConnCreds, roles: Set[str]) -> 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] |
| 21 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 22 | def node_id(self) -> str: |
| 23 | return "{0.host}:{0.port}".format(self.ssh_creds.addr) |
| 24 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 25 | |
| 26 | class ISSHHost(metaclass=abc.ABCMeta): |
| 27 | """Minimal interface, required to setup RPC connection""" |
| 28 | info = None # type: NodeInfo |
| 29 | |
| 30 | @abc.abstractmethod |
| 31 | def run(self, cmd: str, timeout: int = 60, nolog: bool = False) -> str: |
| 32 | pass |
| 33 | |
| 34 | @abc.abstractmethod |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 35 | def __str__(self) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 36 | pass |
| 37 | |
| 38 | @abc.abstractmethod |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 39 | def disconnect(self) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 40 | pass |
| 41 | |
| 42 | @abc.abstractmethod |
| 43 | def put_to_file(self, path: str, content: bytes) -> None: |
| 44 | pass |
| 45 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 46 | def __enter__(self) -> 'ISSHHost': |
| 47 | return self |
| 48 | |
| 49 | def __exit__(self, x, y, z) -> bool: |
| 50 | self.disconnect() |
| 51 | return False |
| 52 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 53 | |
| 54 | class IRPCNode(metaclass=abc.ABCMeta): |
| 55 | """Remote filesystem interface""" |
| 56 | info = None # type: NodeInfo |
| 57 | |
| 58 | @abc.abstractmethod |
| 59 | def run(self, cmd: str, timeout: int = 60, nolog: bool = False) -> str: |
| 60 | pass |
| 61 | |
| 62 | @abc.abstractmethod |
| 63 | def copy_file(self, local_path: str, remote_path: str = None) -> str: |
| 64 | pass |
| 65 | |
| 66 | @abc.abstractmethod |
| 67 | def get_file_content(self, path: str) -> bytes: |
| 68 | pass |
| 69 | |
| 70 | @abc.abstractmethod |
| 71 | def put_to_file(self, path:str, content: bytes) -> None: |
| 72 | pass |
| 73 | |
| 74 | @abc.abstractmethod |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 75 | def get_interface(self, ip: str) -> str: |
| 76 | pass |
| 77 | |
| 78 | @abc.abstractmethod |
| 79 | def stat_file(self, path:str) -> Any: |
| 80 | pass |
| 81 | |
| 82 | @abc.abstractmethod |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 83 | def disconnect(self) -> str: |
| 84 | pass |
| 85 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 86 | def __enter__(self) -> 'IRPCNode': |
| 87 | return self |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 88 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 89 | def __exit__(self, x, y, z) -> bool: |
| 90 | self.disconnect() |
| 91 | return False |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 92 | |