blob: ac832673f906cbcadd41f3c239d89a84807e17b8 [file] [log] [blame]
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +02001import abc
koder aka kdanilov73084622016-11-16 21:51:08 +02002from typing import Any, Set, Optional, List, Dict, Callable, NamedTuple
3from .ssh_utils import ConnCreds
4from .common_types import IPAddr
5
6
7RPCCreds = NamedTuple("RPCCreds", [("addr", IPAddr), ("key_file", str), ("cert_file", str)])
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +02008
9
10class NodeInfo:
koder aka kdanilov73084622016-11-16 21:51:08 +020011 """Node information object, result of discovery process or config parsing"""
12 def __init__(self, ssh_creds: ConnCreds, roles: Set[str]) -> None:
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020013
koder aka kdanilov73084622016-11-16 21:51:08 +020014 # 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 kdanilov3d2bc4f2016-11-12 18:31:18 +020019 self.os_vm_id = None # type: Optional[int]
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020020 self.params = {} # type: Dict[str, Any]
21
koder aka kdanilov73084622016-11-16 21:51:08 +020022 def node_id(self) -> str:
23 return "{0.host}:{0.port}".format(self.ssh_creds.addr)
24
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020025
26class 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 kdanilov73084622016-11-16 21:51:08 +020035 def __str__(self) -> str:
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020036 pass
37
38 @abc.abstractmethod
koder aka kdanilov73084622016-11-16 21:51:08 +020039 def disconnect(self) -> None:
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020040 pass
41
42 @abc.abstractmethod
43 def put_to_file(self, path: str, content: bytes) -> None:
44 pass
45
koder aka kdanilov73084622016-11-16 21:51:08 +020046 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 kdanilov3d2bc4f2016-11-12 18:31:18 +020053
54class IRPCNode(metaclass=abc.ABCMeta):
55 """Remote filesystem interface"""
56 info = None # type: NodeInfo
koder aka kdanilov70227062016-11-26 23:23:21 +020057 conn = None # type: Any
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020058
59 @abc.abstractmethod
60 def run(self, cmd: str, timeout: int = 60, nolog: bool = False) -> str:
61 pass
62
63 @abc.abstractmethod
64 def copy_file(self, local_path: str, remote_path: str = None) -> str:
65 pass
66
67 @abc.abstractmethod
68 def get_file_content(self, path: str) -> bytes:
69 pass
70
71 @abc.abstractmethod
72 def put_to_file(self, path:str, content: bytes) -> None:
73 pass
74
75 @abc.abstractmethod
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020076 def get_interface(self, ip: str) -> str:
77 pass
78
79 @abc.abstractmethod
80 def stat_file(self, path:str) -> Any:
81 pass
82
83 @abc.abstractmethod
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020084 def disconnect(self) -> str:
85 pass
86
koder aka kdanilov73084622016-11-16 21:51:08 +020087 def __enter__(self) -> 'IRPCNode':
88 return self
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020089
koder aka kdanilov73084622016-11-16 21:51:08 +020090 def __exit__(self, x, y, z) -> bool:
91 self.disconnect()
92 return False
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020093