-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackmania.py
executable file
·103 lines (85 loc) · 2.92 KB
/
trackmania.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
Run ./trackmania <action> <ports>
<action> can be 'down', 'up', 'status'.
<ports> is numbers concatenated referencing the server to affect. 'status' does not required any port.
ex : './trackmania.py up 12' -> starts cup1 and cup2
"""
import subprocess
import shlex
import sys
def upServer(id):
"""
It runs a docker-compose command in a specific directory
:param id: the id of the server
"""
name = "tm_server_" + id
path = "./compose/cup" + id + "/"
if (id == "t") :
name = "tm_server_time"
path = "./compose/time"
p = subprocess.Popen(["docker", "compose", "-p", name,
"-f", "docker-compose.yaml", "up", "-d"], cwd=path)
print(p.communicate())
def downServer(id):
"""
It takes an id as a parameter, and then it runs a docker-compose command to bring down the server
with that id
:param id: the id of the server
"""
name = "tm_server_" + id
path = "./compose/cup" + id + "/"
if (id == "t") :
name = "tm_server_time"
path = "./compose/time/"
p = subprocess.Popen(["docker", "compose", "-p", name,
"-f", "docker-compose.yaml", "down", "-v"], cwd=path)
print(p.communicate())
def restartServer(id):
"""
It takes an id as a parameter, and then restarts the server with that id
:param id: the id of the server
"""
downServer(id)
upServer(id)
def status():
"""
Display the status of the Trackmania servers, showing the ID, Uptime, and Name of the running dockers.
"""
p = subprocess.Popen(
shlex.split("docker ps --format \"table {{.ID}}\t{{.Status}}\t{{.Names}}\""))
print(p.communicate())
def main(args):
"""
It starts or closes the servers in the list of servers passed as an argument
:param args: the list of arguments passed to the script
"""
if (args[1] == "status"):
status()
exit(0)
try:
listServer = args[2:]
if (args[1] == "up"):
for i in range(len(listServer)):
upServer(listServer[i])
elif (args[1] == "down"):
for i in range(len(listServer)):
downServer(listServer[i])
elif (args[1] == "restart"):
for i in range(len(listServer)):
restartServer(listServer[i])
else:
print("[ERROR] Wrong argument : '{0}' is not regonized as a valid argument.".format(
args[1]))
exit(1)
except IndexError:
sys.stderr.write("[ERROR] Wrong number of arguments : '{0}' requires a list of int separated by commas.\n".format(
args[1]))
exit(1)
except FileNotFoundError as e:
sys.stderr.write(e.args)
sys.stderr.write(
"[ERROR] Index out of bound : specified server does not exist, no directory found with that index.\n")
exit(1)
if __name__ == "__main__":
main(sys.argv)