This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservermanager.py
executable file
·384 lines (362 loc) · 14.7 KB
/
servermanager.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python3
# SchoolConnect Server-Manager
# © 2019 - 2021 Johannes Kreutz.
# Include dependencies
import sys
import os
import json
from flask import Flask, request
# Include config
import config
# Update steps
if not os.path.exists(config.configpath + "repo.txt"):
with open(config.configpath + "repo.txt", "w") as f:
f.write(json.dumps({"url":"https://philleconnect.org/assets/repository/repository.json", "name":"production"}, sort_keys=True, indent=4))
# Include modules
import modules.repository as repository
import modules.service as service
import modules.network as network
import modules.managerupdate as update
import modules.essentials as ess
from modules.envstore import envman
# Manager objects
repo = repository.repository()
env = envman()
api = Flask(__name__)
# Variables
services = []
globalNetwork = None
# First setup
if len(sys.argv) > 1 and sys.argv[1] == "firstsetup":
if os.path.exists(config.configpath + ".ServerManagerSetupDone"):
print("SchoolConnect Server-Manager seems to be installed already. If you think this is an error, delete the file '.ServerManagerSetupDone' and run this again.")
sys.exit()
# Store fixed environment variables
env.storeValue("MYSQL_DATABASE", "schoolconnect", "Name der Hauptdatenbank.", False)
env.storeValue("MYSQL_USER", "pc_admin_mysql_user", "Nutzername für die Hauptdatenbank.", False)
env.storeValue("MYSQL_PASSWORD", ess.essentials.randomString(128), "Interne Zugangskennung für die Hauptdatenbank.", False)
#env.storeValue("POSTGRES_DB", "hydra", "Name der Authentifizierungsdatenbank.", False)
#env.storeValue("POSTGRES_USER", "hydra_user", "Nutzername für die Authentifizierungsdatenbank.", False)
#env.storeValue("POSTGRES_PASSWORD", ess.essentials.randomString(128), "Interne Zugangskennung für die Authentifizierungsdatenbank.", False)
#env.storeValue("SECRETS_SYSTEM", ess.essentials.randomString(128), "Sicherheitsschlüssel für Ory Hydra.", False)
#env.storeValue("OIDC_SUBJECT_TYPE_PAIRWISE_SALT", ess.essentials.randomString(128), "OpenID Connect Sicherheits-Salt 'pairwise'.", False)
env.storeValue("MANAGEMENT_APIS_SHARED_SECRET", ess.essentials.randomString(256), "Shared Secret für Management-APIs.", False)
# Create main network
globalNetwork = network.network(False, None, "schoolconnect", False)
mainconfig = open(config.servicepath + "config.json", "w")
mainconfig.write(json.dumps({"globalNetwork":globalNetwork.getId()}, sort_keys=True, indent=4))
mainconfig.close()
# Create api token for pc_admin
apitokenfile = open(config.configpath + config.apitokenfile, "w")
apitokenfile.write(ess.essentials.randomString(512))
apitokenfile.close()
# Create essential services and containers
essentials = repo.getAvailable("essential")
for essential in essentials:
latestVersion = repo.getLatestAvailable(essential["name"])
newService = service.service(essential["name"], True)
newService.prepareBuild(essential["name"], latestVersion["url"], latestVersion["version"])
newService.continueInstallation()
services.append(newService)
sys.exit()
# Normal startup
# Get main network reference
mainconfig = open(config.servicepath + "config.json", "r")
globalNetwork = network.network(True, json.loads(mainconfig.read())["globalNetwork"], None, False)
mainconfig.close()
# Check for installed services - create objects
for filename in os.listdir(config.servicepath):
if os.path.isdir(config.servicepath + filename) and "buildcache" not in filename:
newService = service.service(filename, False)
services.append(newService)
# Helper functions
# Return the service with the given Name
def getServiceByName(name):
for service in services:
if service.getName() == name:
return service
return False
# Returns if a service is installed
def isInstalled(name):
for service in services:
if service.getName() == name:
return True
return False
# Returns api key
def getApiKey():
apitokenfile = open(config.configpath + config.apitokenfile, "r")
token = apitokenfile.read()
apitokenfile.close()
return token
# Servermanager update
def installManagerUpdate(version):
return True
# API REQUESTS
# Server testing
@api.route("/", methods=["POST"])
def index():
return "Hey there! I'm up and running!"
# STATUS CHECKS
# Get available containers and their status
@api.route("/status", methods=["POST"])
def getServices():
data = request.form
if data.get("apikey") == getApiKey():
status = []
for service in services:
previous = service.hasPrevious()
if previous != "":
if not repo.isRevertPossible(service.getName(), service.getInstalledVersion()):
previous = ""
status.append({
"name": service.getName(),
"version": service.getInstalledVersion(),
"wanted": service.shouldRun(),
"status": service.getStatus(),
"running": service.isRunning(),
"previous": previous,
"type": repo.getType(service.getName())
})
return json.dumps(status)
else:
return json.dumps({"error":"ERR_AUTH"})
# DATA LOADING
# Get all available containers
@api.route("/repo", methods=["POST"])
def getAvailable():
data = request.form
if data.get("apikey") == getApiKey():
availableServices = repo.getAvailable("plugin")
installableServices = []
for availableService in availableServices:
availableService["installed"] = isInstalled(availableService["name"])
installableServices.append(availableService)
return json.dumps(installableServices)
else:
return json.dumps({"error":"ERR_AUTH"})
# Check for Updates
@api.route("/checkforupdates", methods=["POST"])
def checkForRepoUpdate():
data = request.form
if data.get("apikey") == getApiKey():
repo.update()
return json.dumps({"result":"DONE"})
else:
return json.dumps({"error":"ERR_AUTH"})
# SERVICE CONTROL
# Start or stop a service
@api.route("/control", methods=["POST"])
def controlService():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service == False:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
if data.get("action") == "start":
service.start()
else:
service.stop()
return json.dumps({"result":service.getStatus()})
else:
return json.dumps({"error":"ERR_AUTH"})
# SERVICE INSTALLATION
# Install new service
@api.route("/install", methods=["POST"])
def installService():
data = request.form
if data.get("apikey") == getApiKey():
latestVersion = repo.getLatestAvailable(data.get("service"))
if latestVersion != None:
newService = service.service(None, True)
services.append(newService)
return json.dumps({"result":newService.prepareBuild(data.get("service"), latestVersion["url"], latestVersion["version"])})
else:
return json.dumps({"error":"ERR_SERVICE_NOT_AVAILABLE"})
else:
return json.dumps({"error":"ERR_AUTH"})
# Delete service
@api.route("/delete", methods=["POST"])
def deleteService():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service != False:
return json.dumps({"result":service.asyncDelete()})
else:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
return json.dumps({"error":"ERR_AUTH"})
# SERVICE UPDATING
# Execute a service update
@api.route("/executeupdate", methods=["POST"])
def executeUpdate():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service != False:
return json.dumps({"result":service.prepareUpdate(repo.getUrl(data.get("service"), data.get("version")), data.get("version"))})
else:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
return json.dumps({"error":"ERR_AUTH"})
# Check the status of a running update
@api.route("/actionstatus", methods=["POST"])
def checkActionStatus():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service != False:
status = service.getStatus()
if status == "updatePending":
service.asyncUpdate()
return json.dumps({"result":"updating"})
elif status == "installPending":
service.asyncContinueInstallation()
return json.dumps({"result":"installing"})
elif status == "installed":
service.start()
return json.dumps({"result":"installing"})
elif status == "deleted":
services.remove(service)
return json.dumps({"result":"deleted"})
else:
return json.dumps({"result":status})
else:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
return json.dumps({"error":"ERR_AUTH"})
# Revert to previous version
@api.route("/executerevert", methods=["POST"])
def executeRevert():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service != False:
if repo.isRevertPossible(data.get("service"), service.getInstalledVersion()):
return json.dumps({"result":service.asyncRevert()})
else:
return json.dumps({"error":"ERR_REVERT_NOT_ALLOWED"})
else:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
return json.dumps({"error":"ERR_AUTH"})
# Check if an update is available
@api.route("/updatecheck", methods=["POST"])
def checkForUpdate():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service == False:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
update = {"actualVersion":service.getInstalledVersion(),"latestPossible":repo.getLatestCompatible(data.get("service"), service.getInstalledVersion())}
return json.dumps(update)
else:
return json.dumps({"error":"ERR_AUTH"})
# SERVICE REBUILD
# Start rebuilding a service
@api.route("/rebuild", methods=["POST"])
def rebuildService():
data = request.form
if data.get("apikey") == getApiKey():
service = getServiceByName(data.get("service"))
if service != False:
return json.dumps({"result":service.prepareRebuild()})
else:
return json.dumps({"error":"ERR_SERVICE_NOT_FOUND"})
else:
return json.dumps({"error":"ERR_AUTH"})
# SERVERMANAGER CONTROL
# Servermanager version and available updates
@api.route("/manager", methods=["POST"])
def checkManagerVersion():
data = request.form
if data.get("apikey") == getApiKey():
response = {"actual":config.servermanagerversion}
if repo.getLatestAvailable("servermanager")["version"] != config.servermanagerversion:
response["available"] = repo.getLatestAvailable("servermanager")["version"]
return json.dumps(response)
else:
return json.dumps({"error":"ERR_AUTH"})
# Execute a manager update
@api.route("/executemanagerupdate", methods=["POST"])
def executeManagerUpdate():
data = request.form
if data.get("apikey") == getApiKey():
update.managerupdate.installUpdate(data.get("version"))
return json.dumps({"result":"running"})
else:
return json.dumps({"error":"ERR_AUTH"})
# Check manager update status
@api.route("/managerupdatecheck", methods=["POST"])
def managerUpdateCheck():
data = request.form
if data.get("apikey") == getApiKey():
return json.dumps({"result":config.servermanagerversion})
else:
return json.dumps({"error":"ERR_AUTH"})
# Get license key
@api.route("/licensekey", methods=["POST"])
def licenseKey():
data = request.form
if data.get("apikey") == getApiKey():
return json.dumps({"result":license.getLicenseKey()})
else:
return json.dumps({"error":"ERR_AUTH"})
# Get actual repository
@api.route("/branch", methods=["POST"])
def getBranch():
data = request.form
if data.get("apikey") == getApiKey():
with open(config.configpath + "repo.txt", "r") as f:
return json.dumps({"result":json.loads(f.read())})
else:
return json.dumps({"error":"ERR_AUTH"})
# Switch branch
@api.route("/setbranch", methods=["POST"])
def setBranch():
data = request.form
if data.get("apikey") == getApiKey():
with open(config.configpath + "repo.txt", "w") as f:
if data.get("branch") == "production":
f.write(json.dumps({"url":"https://philleconnect.org/assets/repository/repository.json", "name":"production"}, sort_keys=True, indent=4))
elif data.get("branch") == "beta":
f.write(json.dumps({"url":"https://philleconnect.org/assets/repository/repository-beta.json", "name":"beta"}, sort_keys=True, indent=4))
return json.dumps({"result":"done"})
else:
return json.dumps({"error":"ERR_AUTH"})
# ENVIRONMENT VARIABLES
# Get a list of all existing environment variables
@api.route("/listenv", methods=["POST"])
def listEnv():
data = request.form
if data.get("apikey") == getApiKey():
main = json.loads(env.getJson())
for service in services:
localEnv = envman(service.getName())
data = json.loads(localEnv.getJson())
for id, content in data.items():
main["[" + service.getName() + "]" + id] = content
return json.dumps(main)
else:
return json.dumps({"error":"ERR_AUTH"})
# Store a new environment variable
@api.route("/storeenv", methods=["POST"])
def storeEnv():
data = request.form
if data.get("apikey") == getApiKey():
for key, entry in json.loads(data.get("data")).items():
if key.startswith("["):
parts = key.split("]")
serviceName = parts[0][1:]
localEnv = envman(serviceName)
localEnv.storeValue(parts[1], entry["value"], entry["description"], entry["mutable"])
else:
env.storeValue(key, entry["value"], entry["description"], entry["mutable"])
return json.dumps({"result":"SUCCESS"})
else:
return json.dumps({"error":"ERR_AUTH"})
# Create server
if __name__ == "__main__":
api.run(debug=True, host="192.168.255.255", port=49100, threaded=True)