-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.py
77 lines (62 loc) · 2.5 KB
/
endpoint.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging as log
from config import ConfigKeys
from ping import ping_endpoint
from suspend import suspend_endpoint
from poweroff import poweroff_endpoint
from wake import wake_endpoint
class Endpoint:
def __init__(self, settings: dict) -> None:
self.name = None
self.ssh_port = 22
self.ssh_user = "root"
self.hostname = None
self.eth_address = None
self.enabled = True
self.configure(settings)
def pretty_print(self):
print("Endpoint information")
print(f"\tName: {self.name}")
print(f"\tSSH Port: {self.ssh_port}")
print(f"\tHostname: {self.ssh_user}")
print(f"\tETH Address: {self.eth_address}")
print(f"\tEnabled: {self.enabled}")
def configure(self, settings: dict):
for item in settings:
if item == ConfigKeys.NAME.value:
self.name = settings[item]
if item == ConfigKeys.ETH_ADDRESS.value:
self.eth_address = settings[item]
elif item == ConfigKeys.HOSTNAME.value:
self.hostname = settings[item]
elif item == ConfigKeys.SSH_USER.value:
self.ssh_user = settings[item]
elif item == ConfigKeys.SSH_PORT.value:
self.ssh_port = settings[item]
elif item == ConfigKeys.ENABLED.value:
self.enabled = bool(settings[item])
if not self.enabled:
log.warning(f"'{self.name}' is configured as disabled.")
def ping(self):
if not self.enabled:
return None
log.info(
f"Trying to ping '{self.name}', '{self.ssh_user}@{self.hostname}:{self.ssh_port}'")
return ping_endpoint(self.hostname)
def suspend(self):
if not self.enabled:
return None
log.info(
f"Trying to suspend '{self.name}', '{self.ssh_user}@{self.hostname}:{self.ssh_port}'")
return suspend_endpoint(hostname=self.hostname, username=self.ssh_user, port=self.ssh_port)
def poweroff(self):
if not self.enabled:
return None
log.info(
f"Trying to poweroff '{self.name}', '{self.ssh_user}@{self.hostname}:{self.ssh_port}'")
return poweroff_endpoint(hostname=self.hostname, username=self.ssh_user, port=self.ssh_port)
def wake(self):
if not self.enabled:
return None
log.info(
f"Trying to wake '{self.name}', ethaddr: {self.eth_address}")
return wake_endpoint(self.eth_address)