koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 1 | import abc |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 2 | import logging |
| 3 | from typing import Any, Set, Dict, Optional, NamedTuple |
| 4 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 5 | from .ssh_utils import ConnCreds |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 6 | from .common_types import IPAddr, IStorable |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 7 | |
| 8 | |
| 9 | RPCCreds = NamedTuple("RPCCreds", [("addr", IPAddr), ("key_file", str), ("cert_file", str)]) |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 10 | logger = logging.getLogger("wally") |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 11 | |
| 12 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 13 | class NodeInfo(IStorable): |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 14 | """Node information object, result of discovery process or config parsing""" |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 15 | 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] | 16 | # ssh credentials |
| 17 | self.ssh_creds = ssh_creds |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 18 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 19 | # credentials for RPC connection |
| 20 | self.rpc_creds = None # type: Optional[RPCCreds] |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 21 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 22 | self.roles = roles |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 23 | self.os_vm_id = None # type: Optional[int] |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 24 | self.params = {} # type: Dict[str, Any] |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 25 | if params is not None: |
| 26 | self.params = params |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 27 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 28 | @property |
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: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 33 | return self.node_id |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 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']) |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 56 | obj = cls.__new__(cls) # type: ignore |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 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 | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 88 | @property |
| 89 | def node_id(self) -> str: |
| 90 | return self.info.node_id |
| 91 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 92 | |
| 93 | class IRPCNode(metaclass=abc.ABCMeta): |
| 94 | """Remote filesystem interface""" |
| 95 | info = None # type: NodeInfo |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 96 | conn = None # type: Any |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 97 | rpc_log_file = None # type: str |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 98 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 99 | @property |
| 100 | def node_id(self) -> str: |
| 101 | return self.info.node_id |
| 102 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 103 | @abc.abstractmethod |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 104 | def __str__(self) -> str: |
| 105 | pass |
| 106 | |
| 107 | @abc.abstractmethod |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 108 | 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] | 109 | pass |
| 110 | |
| 111 | @abc.abstractmethod |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 112 | def copy_file(self, local_path: str, remote_path: str = None, |
| 113 | expanduser: bool = False, compress: bool = False) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 114 | pass |
| 115 | |
| 116 | @abc.abstractmethod |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 117 | def get_file_content(self, path: str, expanduser: bool = False, compress: bool = False) -> bytes: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 118 | pass |
| 119 | |
| 120 | @abc.abstractmethod |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 121 | def put_to_file(self, path: Optional[str], content: bytes, expanduser: bool = False, compress: bool = False) -> str: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 122 | pass |
| 123 | |
| 124 | @abc.abstractmethod |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 125 | def stat_file(self, path:str, expanduser: bool = False) -> Any: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 126 | pass |
| 127 | |
| 128 | @abc.abstractmethod |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 129 | def disconnect(self) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 130 | pass |
| 131 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 132 | @abc.abstractmethod |
| 133 | def upload_plugin(self, name: str, code: bytes, version: str = None) -> None: |
| 134 | pass |
| 135 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 136 | def __enter__(self) -> 'IRPCNode': |
| 137 | return self |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 138 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 139 | def __exit__(self, x, y, z) -> bool: |
| 140 | self.disconnect() |
| 141 | return False |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 142 | |