-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
46 lines (40 loc) · 1.46 KB
/
remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fabric import Connection, Config
import logging
import time
class Remote:
def __init__(self, address, port, password):
self._logger = logging.getLogger(__name__)
self.address = address
self._password = password
self._config = Config(overrides={"sudo": {"password": password}})
self.connection = Connection(
host=address,
user="torizon",
port=port,
config=self._config,
connect_timeout=15,
connect_kwargs={
"password": password,
"banner_timeout": 60,
"allow_agent": True,
"look_for_keys": True,
},
)
def test_connection(self, sleep_time=3):
for tries in range(5):
try:
res = self.connection.run("true", warn=True, hide=True)
if res.exited == 0:
self._logger.info("Remote connection test OK")
return True
else:
self._logger.error(
f"Testing remote connection failed on try {tries}. Retrying"
)
except Exception as e:
self._logger.error(
f"Exception occurred while testing remote connection on try {tries}: {str(e)}"
)
time.sleep(sleep_time * (0 + 1))
self._logger.error("Remote connection test failed")
return False