-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_mgmt.py
248 lines (227 loc) · 8.13 KB
/
server_mgmt.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import re
import time
from math import ceil
from typing import List, Dict, IO
import yaml
import docker
import ipaddress
import mysql.connector
# from key import *
with open("config.yml", "r") as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
def register_server_ebot(servers: List[Dict[str, str]], db_ip: str, tls_config: any) -> None:
"""
Register a game server to the Ebot server, by adding values to its database.
:param servers: A list of servers to add
:param db_ip: The IP of Ebot's DB
:param tls_config: TLS configuration to use
"""
containers = []
csgo_containers = []
for s in servers:
client = docker.DockerClient(
base_url="unix://var/run/docker.sock", tls=tls_config
)
containers.extend(client.containers.list("all"))
csgo_containers.extend(
c for c in containers if c.attrs["Config"]["User"] == "steam"
)
cnx = mysql.connector.connect(
user="ebotv3", password="ebotv3", host=db_ip, database="ebotv3"
)
cursor = cnx.cursor()
try:
cursor.execute("delete from servers where id!=-1")
except:
print("truncate failed")
try:
add_server = (
"INSERT INTO servers"
"(ip, rcon, hostname, tv_ip, created_at, updated_at)"
"values(%s,%s,%s,%s,'2017-12-17 00:00:00','2017-12-17 00:00:00')"
)
stvp = re.compile("STV_PORT")
hostp = re.compile("HOST_PORT")
ipp = re.compile("IP")
for i in csgo_containers:
for y in i.attrs["Config"]["Env"]:
if stvp.search(y):
stvport = re.split("=", y)[1]
if hostp.search(y):
hostport = re.split("=", y)[1]
if ipp.search(y):
ip = re.split("=", y)[1]
name = i.attrs["Name"]
data_server = (
"{}:{}".format(ip, hostport),
"notbanana",
name,
"{}:{}".format(ip, stvport),
)
cursor.execute(add_server, data_server)
cnx.commit()
cursor.close()
cnx.close()
except:
print("insertion failed")
def deploy_ebotserver(ebot_ip, tls_config: any, topo: IO) -> None:
"""
Deploy ebot (ebot web and his DB) on a physical server.
:param ebot_ip: IP of the server on which ebot should be deployed
:param tls_config: TLS configuration to use
:param topo: File descriptor to the topology file
"""
client = docker.APIClient(
base_url="unix://var/run/docker.sock", tls=tls_config
)
db_container = client.create_container(
"mysql:5.7",
detach=True,
host_config=client.create_host_config(
restart_policy={"Name": "always"},
mounts=[
docker.types.Mount(
target="/var/lib/mysql", source="ebot_mysql", type="volume"
)
],
network_mode="host",
),
environment={
"MYSQL_DATABASE": "ebotv3",
"MYSQL_USER": "ebotv3",
"MYSQL_PASSWORD": "ebotv3",
"MYSQL_ROOT_PASSWORD": "nhurmanroot",
},
command="mysqld",
name="db_container",
)
topo.write("db_container;{};{}\n".format(ebot_ip, client.base_url))
ebot_container = client.create_container(
"hsfactory/ebot",
detach=True,
hostname="ebot",
host_config=client.create_host_config(
restart_policy={"Name": "always"},
extra_hosts={"mysql": ebot_ip, "ebot": ebot_ip},
mounts=[
docker.types.Mount("/ebot/logs", "ebot_logs", type="volume"),
docker.types.Mount("/ebot/demos", "ebot_demo", type="volume"),
],
network_mode="host",
),
environment={
"EXTERNAL_IP": ebot_ip,
"MYSQL_HOST": "mysql",
"MYSQL_PORT": "3306",
"MYSQL_DB": "ebotv3",
"MYSQL_USER": "ebotv3",
"MYSQL_PASS": "ebotv3",
"LO3_METHOD": "restart",
"KO3_METHOD": "restart",
"DEMO_DOWNLOAD": "true",
"REMIND_RECORD": "false",
"DAMAGE_REPORT": "true",
"DELAY_READY": "false",
"NODE_STARTUP_METHOD": "node",
"TOORNAMENT_PLUGIN_KEY": "",
},
name="ebot_container",
)
topo.write("ebot_container;{};{}\n".format(ebot_ip, client.base_url))
ebotweb_container = client.create_container(
"hsfactory/ebotweb",
detach=True,
host_config=client.create_host_config(
restart_policy={"Name": "always"},
extra_hosts={"mysql": ebot_ip, "ebot": ebot_ip},
mounts=[
docker.types.Mount(
"/opt/ebot/logs", "ebot_logs", type="volume"
),
docker.types.Mount(
"/opt/ebot/demos", "ebot_demo", type="volume"
),
],
network_mode="host",
),
environment={
"EBOT_IP": ebot_ip,
"EBOT_PORT": "12360",
"EBOT_ADMIN_USER": "insalan",
"EBOT_ADMIN_PASS": "nhurman",
"EBOT_ADMIN_MAIL": "insalade@ebot",
"MYSQL_HOST": "mysql",
"MYSQL_PORT": "3306",
"MYSQL_DB": "ebotv3",
"MYSQL_USER": "ebotv3",
"MYSQL_PASS": "ebotv3",
"DEMO_DOWNLOAD": "true",
"DEFAULT_RULES": "esl5on5",
"TOORNAMENT_ID": "",
"TOORNAMENT_SECRET": "",
"TOORNAMENT_PLUGIN_KEY": "",
"TOORNAMENT_API_KEY": "",
},
name="ebotweb_container",
)
topo.write("ebotweb_container;{};{}\n".format(ebot_ip, client.base_url))
client.start(db_container)
time.sleep(10)
client.start(ebot_container)
time.sleep(10)
client.start(ebotweb_container)
def deploy_csgoserver(
nb_csgo: int,
servers: List[Dict[str, str]],
ebot_ip: str,
image: str,
tls_config: any,
topo: IO
) -> None:
"""
Deploy csgo containers over physical servers.
:param nb_csgo: Number of container to deploy
:param servers: List of physical servers on which the containers will be deployed
:param ebot_ip: IP address of ebot (#FIXME confirm with original author)
:param image: Name of the docker image to deploy
:param tls_config: TLS configuration to use
:param topo: File descriptor to the topology file
"""
ip = ipaddress.ip_address(ebot_ip)
hostport = 27015
clientport = hostport + nb_csgo
stvport = clientport + nb_csgo
hostname = "csgoinsalan"
for y in range(0, len(servers)):
for i in range(
int(ceil(nb_csgo / len(servers)) * y),
int(ceil(nb_csgo / len(servers)) * (y + 1)),
):
ip = ipaddress.ip_address(ip + 1)
client = docker.APIClient(
base_url="unix://var/run/docker.sock", tls=tls_config
)
container = client.create_container(
image,
detach=True,
hostname=hostname,
host_config=client.create_host_config(
extra_hosts={hostname: servers[y]},
restart_policy={"Name": "always"},
network_mode="host",
),
environment={
"IP": "{}".format(servers[y]),
"CSGO_HOSTNAME": "csgo-server-{}".format(i),
"CSGO_PASSWORD": "",
"RCON_PASSWORD": "notbanana",
"STEAM_ACCOUNT_TOKEN": config["csgo"]["tokens"][i] if len(config["csgo"]["tokens"]) > i else "",
# FIXME : check before anything instead of failing midway
"HOST_PORT": str(hostport + i),
"CLIENT_PORT": str(clientport + i),
"STV_PORT": str(stvport + i),
},
name="csgo-servers-{}".format(i),
)
client.start(container)
topo.write("csgo-servers-{};{};{}\n".format(i, str(ip), client.base_url))