-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
executable file
·291 lines (215 loc) · 9.42 KB
/
manage.py
File metadata and controls
executable file
·291 lines (215 loc) · 9.42 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
"""
Main script of the Hyclone Minetest server
"""
__author__ = "AFCM"
__version__ = "1.0"
import os
import time
import signal
import subprocess
import requests
import tarfile
import fire
import pathlib
from typing import *
from termcolor import cprint
minetest_path = pathlib.Path("./server/minetest/bin/minetestserver")
mineclone2_path = pathlib.Path("./games/MineClone2")
multiserver_path = pathlib.Path("./multiserver/mt-multiserver-proxy")
###########
# Utils #
###########
#print(os.getenv("GOBIN"))
def check_bin_exists(path: pathlib.Path):
if path.exists() and path.is_file():
return
else:
cprint(str(path) + " doesn't exist!", "red")
exit(1)
def download_tar(url: str, path: pathlib.Path):
response = requests.get(url, stream = True)
file = tarfile.open(fileobj = response.raw, mode = "r|gz")
file.extractall(path = path)
def _start_world(world: str, out: Optional[int] = None) -> subprocess.Popen:
return subprocess.Popen(["./server/minetest/bin/minetestserver", "--world", f"./worlds/{world}", "--config", f"./worlds/{world}/minetest.conf", "--logfile", f"./worlds/{world}/debug.txt"], stdout=out, stderr=out)
###############
# Functions #
###############
def start(debug: bool = False, monitoring: bool = False):
"""
Start multiserver, all minetest instances and monitoring if enabled.
Exit all process normally then receiving SIGTERM or SIGINT.
"""
# TODO: check if all required binaries are installed first
redis_process: Optional[subprocess.Popen] = None
multiserver_process: Optional[subprocess.Popen] = None
minetest_processes: Dict[str, subprocess.Popen] = {}
prometheus_process: Optional[subprocess.Popen] = None
grafana_process: Optional[subprocess.Popen] = None
out = None
if debug:
out = None
else:
out = subprocess.DEVNULL
# Signals Handling
def _on_exit(a,b):
if multiserver_process:
cprint("Exiting Multiserver....", "yellow")
multiserver_process.terminate()
for world in minetest_processes:
cprint(f"Exiting world {world}....", "yellow")
minetest_processes[world].terminate()
if prometheus_process:
cprint("Exiting Prometheus....", "yellow")
prometheus_process.terminate()
if grafana_process:
cprint("Exiting Grafana....", "yellow")
grafana_process.terminate()
if redis_process:
cprint("Exiting Redis Server....", "yellow")
subprocess.run(["./database/redis-stable/src/redis-cli", "shutdown"])
exit(0)
signal.signal(signal.SIGTERM, _on_exit)
signal.signal(signal.SIGINT, _on_exit)
# Starting Redis
cprint("Starting Redis...", "green")
redis_process = subprocess.Popen(["./redis-stable/src/redis-server", "./redis.conf"], cwd="./database", stdout=out, stderr=out)
time.sleep(2)
# Starting Multiserver
cprint("Starting Multiserver...", "green")
multiserver_process = subprocess.Popen(["./multiserver/mt-multiserver-proxy"])
# Starting individual Minetest worlds
for world in os.listdir("./worlds"):
cprint(f"Starting world {world}...", "green")
minetest_processes[world] = _start_world(world, out)
# Starting Prometheus and Grafana servers
if monitoring:
cprint("Starting Prometheus...", "green")
prometheus_process = subprocess.Popen(["./prometheus", "--config.file=../prometheus.yml"], cwd="./monitoring/prometheus-2.34.0.linux-amd64/", stdout=out, stderr=out)
cprint("Starting Grafana...", "green")
grafana_process = subprocess.Popen(["./bin/grafana-server", "--config", "../grafana.ini"], cwd="./monitoring/grafana-8.4.4/", stdout=out, stderr=out)
while True:
time.sleep(1)
if multiserver_process.poll():
cprint("Multiserver Crashed!", "red")
# TODO: stop other services
exit(1)
for world in minetest_processes:
r = minetest_processes[world].poll()
if r != None:
if r == 0:
cprint(f"Restarting world {world}...", "green")
minetest_processes[world] = _start_world(world, out)
time.sleep(5)
else:
cprint(f"World {world} crashed! Restarting...", "red")
minetest_processes[world] = _start_world(world, out)
time.sleep(5)
def setup(multiserver: bool = False, multiserver_plugins: bool = False, minetest: bool = False, monitoring: bool = False, redis: bool = False, force: bool = False):
"""
Setup the server environment
"""
if not multiserver and not multiserver_plugins and not minetest and not monitoring and not redis:
cprint("You must specify which services to setup (see ./manage.py setup --help)", "red")
exit(1)
if multiserver:
cprint("Installing Multiserver....", "green")
r = subprocess.run(["go", "install", "github.com/HimbeerserverDE/mt-multiserver-proxy/cmd/mt-multiserver-proxy@latest"])
if r.returncode != 0:
cprint("Installing multiserver failed!", "red")
exit(1)
#cprint("Linking Multiserver binary....", "green")
#try:
# print(pathlib.Path("~/go/bin/mt-multiserver-proxy").absolute())
# multiserver_path.symlink_to(pathlib.Path("~/go/bin/mt-multiserver-proxy").absolute())
#except FileExistsError:
# cprint("The link seems to already exist", "yellow")
if multiserver_plugins:
for plugin in os.listdir("./multiserver/plugins_src"):
cprint(f"Building {plugin} plugin...", "green")
r = subprocess.run(["go", "build", "-buildmode=plugin", "-o", pathlib.Path("./multiserver/plugins/").absolute()], cwd=f"./multiserver/plugins_src/{plugin}")
if r.returncode != 0:
cprint(f"Build of {plugin} failed!", "red")
if minetest:
if pathlib.Path("./server/minetest").exists():
if force:
cprint("Removing old files...", "yellow")
subprocess.run(["rm", "-rf", "./server/minetest"])
else:
cprint("Minetest 5.4.1 Server is already installed! Force the reinstallation with the --force flag if you really need it.", "red")
exit(1)
cprint("Installing Minetest dependencies....", "green")
minetest_depends = [
"git", "g++", "make", "cmake", "build-essential",
"libjpeg8-dev", "libpng-dev", "zlib1g-dev", "libopengl-dev",
"libglx-dev", "libgl1-mesa-dev", "libx11-dev", "libxxf86vm-dev",
"libvorbis-dev", "libopenal-dev", "libsqlite3-dev", "libluajit-5.1-dev",
"libjsoncpp-dev", "libgmp-dev", "libcurl4-gnutls-dev", "libfreetype6-dev", "libzstd-dev"
]
r = subprocess.run(["sudo", "apt", "install", "-y"] + minetest_depends)
if r.returncode != 0:
cprint("Installing dependencies failed!", "red")
exit(1)
cprint("Cloning Minetest....", "green")
git_minetest = "https://github.com/minetest/minetest"
r1 = subprocess.run(["git", "clone", git_minetest, "./server/minetest"])
r2 = subprocess.run(["git", "checkout", "5.4.1"], cwd="./server/minetest")
if r1.returncode != 0 or r2.returncode != 0:
cprint("Cloning Failed!", "red")
exit(1)
cprint("Building Minetest....", "green")
# TODO: enable Prometheus client
r1 = subprocess.run(["cmake", "-DRUN_IN_PLACE=TRUE", "-DBUILD_SERVER=TRUE", "-DBUILD_CLIENT=FALSE"], cwd="./server/minetest")
r2 = subprocess.run(["make", "-j$(nproc)"], cwd="./server/minetest", shell=True)
if r1.returncode != 0 or r2.returncode != 0:
cprint("Building Failed!", "red")
exit(1)
cprint("Linking MineClone2...", "green")
pathlib.Path("./server/minetest/games/MineClone2").symlink_to(mineclone2_path.absolute())
for mod in os.listdir("./mods"):
link_path = pathlib.Path(f"./server/minetest/mods/{mod}")
cprint(f"Linking {mod} mod...", "green")
link_path.symlink_to(pathlib.Path(f"./mods/{mod}").absolute())
if monitoring:
prometheus_url = "https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gz"
grafana_url = "https://dl.grafana.com/oss/release/grafana-8.4.4.linux-amd64.tar.gz"
monitoring_path = pathlib.Path("./monitoring/")
if pathlib.Path("./monitoring/prometheus-2.34.0.linux-amd64").exists():
if force:
cprint("Old Prometheus files will be overiden...", "yellow")
else:
cprint("Prometheus 3.34 is already installed! Force the reinstallation with the --force flag if you are sure this wont cause any data loss.", "red")
exit(1)
if pathlib.Path("./monitoring/grafana-8.4.4.linux-amd64").exists():
if force:
cprint("Old Grafana files will be overiden...", "yellow")
else:
cprint("Grafana 8.4.4 is already installed! Force the reinstallation with the --force flag if you are sure this wont cause any data loss.", "red")
exit(1)
cprint("Installing Prometheus 2.34....", "green")
download_tar(prometheus_url, monitoring_path)
cprint("Installing Grafana 8.4.4....", "green")
download_tar(grafana_url, monitoring_path)
if redis:
redis_url = "http://download.redis.io/redis-stable.tar.gz"
database_path = pathlib.Path("./database/")
if pathlib.Path("./database/redis-stable").exists():
if force:
cprint("Old Redis files will be overiden...", "yellow")
else:
cprint("Redis Stable is already installed! Force the reinstallation with the --force flag if you are sure this wont cause any data loss.", "red")
exit(1)
cprint("Installing Redis Stable....", "green")
download_tar(redis_url, database_path)
# We put a prefix to not conflict with the system installation
cprint("Building Redis Stable....", "green")
subprocess.run(["make"], cwd="./database/redis-stable")
# https://grafana.com/grafana/plugins/redis-datasource/
if pathlib.Path("./monitoring/grafana-8.4.4").exists():
cprint("Grafana found, installing grafana-redis", "green")
subprocess.run(["./bin/grafana-cli", "--pluginsDir", "./plugins", "plugins", "install", "redis-datasource"], cwd="./monitoring/grafana-8.4.4/")
fire.Fire({
"setup": setup,
"start": start,
})