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