-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
82 lines (56 loc) · 2.13 KB
/
server.py
File metadata and controls
82 lines (56 loc) · 2.13 KB
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
78
79
80
81
82
import time
import paramiko
from constansts import CMD_DICT
def get_client(hostname: str, username: str, timeout: int = 5) -> paramiko.SSHClient:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username, timeout=timeout)
return ssh
def get_client_list(
hostname_list: list[str], username: str
) -> list[paramiko.SSHClient]:
client_list = []
for hostname in hostname_list:
try:
ssh = get_client(hostname, username)
client_list.append(ssh)
except:
print(f"Unable to connect to {hostname}")
continue
return client_list
def get_server_usernames(ssh: paramiko.SSHClient) -> list:
stdin, stdout, stderr = ssh.exec_command(CMD_DICT["get_users"])
return stdout.read().decode().split()
def get_server_processes_per_user(ssh: paramiko.SSHClient, username: str) -> list:
stdin, stdout, stderr = ssh.exec_command(f"ps -u {username}")
raw_psout = stdout.read().decode()
processes = []
for process in raw_psout.split("\n")[1:]:
if not process:
break
id, tty, time, cmd = process.split()
processes.append((id, time, cmd))
return processes
def is_process_running(ssh: paramiko.SSHClient, p: str):
stdin, stdout, stderr = ssh.exec_command(f"ps")
raw_psout = stdout.read().decode()
for process in raw_psout.split("\n")[1:]:
if not process:
break
id, tty, time, cmd = process.split()
if cmd == p:
return True
return False
def is_server_available(ssh: paramiko.SSHClient, current_username: str):
for username in get_server_usernames(ssh):
processes = get_server_processes_per_user(ssh, username)
if username == current_username:
if bool([p for _, _, p in processes if p == "abako"]):
return False
continue
if processes:
return False
return True
def wait_for_process_to_end(ssh: paramiko.SSHClient, p: str, wait_time: int = 120):
while is_process_running(ssh, p):
time.sleep(wait_time)