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