did some refactoring on loading of servers, fixed bug where server object would be forgotten when new server added

This commit is contained in:
Phillip Tarrant 2020-09-14 14:05:17 -04:00
parent fbc8e7fc2a
commit f96649613f
4 changed files with 121 additions and 49 deletions

View File

@ -21,15 +21,31 @@ class Controller:
def __init__(self): def __init__(self):
self.servers_list = [] self.servers_list = []
def init_all_servers(self): def check_server_loaded(self, server_id_to_check: int):
# if we have servers defined, let's destroy it and start over logger.info("Checking to see if we already registered {}".format(server_id_to_check))
if len(self.servers_list) > 0:
self.servers_list = [] for s in self.servers_list:
known_server = s.get('server_id')
if known_server is None:
return False
if known_server == server_id_to_check:
logger.info('skipping initialization of server {} because it is already loaded'.format(server_id_to_check))
return True
return False
def init_all_servers(self):
servers = db_helper.get_all_defined_servers() servers = db_helper.get_all_defined_servers()
for s in servers: for s in servers:
server_id = s.get('server_id')
# if we have already initialized this server, let's skip it.
if self.check_server_loaded(server_id):
continue
# if this server path no longer exists - let's warn and bomb out # if this server path no longer exists - let's warn and bomb out
if not helper.check_path_exits(s['path']): if not helper.check_path_exits(s['path']):
@ -84,6 +100,14 @@ class Controller:
servers = db_helper.get_all_defined_servers() servers = db_helper.get_all_defined_servers()
return servers return servers
def get_server_data(self, server_id):
for s in self.servers_list:
if int(s['server_id']) == int(server_id):
return s['server_data_obj']
logger.warning("Unable to find server object for server id {}".format(server_id))
return False
def list_running_servers(self): def list_running_servers(self):
running_servers = [] running_servers = []
@ -104,6 +128,7 @@ class Controller:
def stop_all_servers(self): def stop_all_servers(self):
servers = self.list_running_servers() servers = self.list_running_servers()
print(servers)
logger.info("Found {} running server(s)".format(len(servers))) logger.info("Found {} running server(s)".format(len(servers)))
console.info("Found {} running server(s)".format(len(servers))) console.info("Found {} running server(s)".format(len(servers)))
@ -114,36 +139,7 @@ class Controller:
logger.info("Stopping Server ID {} - {}".format(s['id'], s['name'])) logger.info("Stopping Server ID {} - {}".format(s['id'], s['name']))
console.info("Stopping Server ID {} - {}".format(s['id'], s['name'])) console.info("Stopping Server ID {} - {}".format(s['id'], s['name']))
# get object self.stop_server(s['id'])
svr_obj = self.get_server_obj(s['id'])
running = svr_obj.check_running(True)
# issue the stop command
svr_obj.stop_threaded_server()
# while it's running, we wait
x = 0
while running:
logger.info("Server {} is still running - waiting 2s to see if it stops".format(s['name']))
console.info("Server {} is still running - waiting 2s to see if it stops".format(s['name']))
running = svr_obj.check_running()
# let's keep track of how long this is going on...
x = x + 1
# if we have been waiting more than 120 seconds. let's just kill the pid
if x >= 60:
logger.error("Server {} is taking way too long to stop. Killing this process".format(s['name']))
console.error("Server {} is taking way too long to stop. Killing this process".format(s['name']))
svr_obj.killpid(svr_obj.PID)
running = False
# if we killed the server, let's clean up the object
if not running:
svr_obj.cleanup_server_object()
time.sleep(2)
# let's wait 2 seconds to let everything flush out # let's wait 2 seconds to let everything flush out
time.sleep(2) time.sleep(2)
@ -151,6 +147,39 @@ class Controller:
logger.info("All Servers Stopped") logger.info("All Servers Stopped")
console.info("All Servers Stopped") console.info("All Servers Stopped")
def stop_server(self, server_id):
# get object
svr_obj = self.get_server_obj(server_id)
svr_data = self.get_server_data(server_id)
server_name = svr_data['server_name']
running = svr_obj.check_running(True)
# issue the stop command
svr_obj.stop_threaded_server()
# while it's running, we wait
x = 0
while running:
logger.info("Server {} is still running - waiting 2s to see if it stops".format(server_name))
console.info("Server {} is still running - waiting 2s to see if it stops".format(server_name))
running = svr_obj.check_running()
# let's keep track of how long this is going on...
x = x + 1
# if we have been waiting more than 120 seconds. let's just kill the pid
if x >= 60:
logger.error("Server {} is taking way too long to stop. Killing this process".format(server_name))
console.error("Server {} is taking way too long to stop. Killing this process".format(server_name))
svr_obj.killpid(svr_obj.PID)
running = False
# if we killed the server, let's clean up the object
if not running:
svr_obj.cleanup_server_object()
def create_jar_server(self, server: str, version: str, name: str, min_mem: int, max_mem: int, port: int): def create_jar_server(self, server: str, version: str, name: str, min_mem: int, max_mem: int, port: int):
server_id = helper.create_uuid() server_id = helper.create_uuid()
server_dir = os.path.join(helper.servers_dir, server_id) server_dir = os.path.join(helper.servers_dir, server_id)
@ -190,8 +219,7 @@ class Controller:
def import_server(self): def import_server(self):
print("todo") print("todo")
def register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str, def register_server(self, name: str, server_id: str, server_dir: str, server_command: str, server_file: str, server_log_file: str, server_stop: str):
server_log_file: str, server_stop: str):
# put data in the db # put data in the db
new_id = Servers.insert({ new_id = Servers.insert({
Servers.server_name: name, Servers.server_name: name,
@ -222,5 +250,31 @@ class Controller:
return new_id return new_id
def remove_server(self, server_id):
counter = 0
for s in self.servers_list:
# if this is the droid... im mean server we are looking for...
if int(s['server_id']) == int(server_id):
server_data = self.get_server_data(server_id)
server_name = server_data['server_name']
logger.info("Deleting Server: ID {} | Name: {} ".format(server_id, server_name))
console.info("Deleting Server: ID {} | Name: {} ".format(server_id, server_name))
srv_obj = s['server_obj']
running = srv_obj.check_running()
if running:
self.stop_server(server_id)
# remove the server from the DB
Servers.delete().where(Servers.server_id == server_id).execute()
# remove the server from servers list
self.servers_list.pop(counter)
counter += 1
controller = Controller() controller = Controller()

View File

@ -57,6 +57,20 @@ class PanelHandler(BaseHandler):
elif page == 'contribute': elif page == 'contribute':
template = "panel/contribute.html" template = "panel/contribute.html"
elif page == "remove_server":
server_id = self.get_argument('id', None)
server_data = controller.get_server_data(server_id)
server_name = server_data['server_name']
db_helper.add_to_audit_log(user_data['user_id'],
"Deleted server {} named {}".format(server_id, server_name),
server_id,
self.get_remote_ip())
controller.remove_server(server_id)
self.redirect("/panel/dashboard")
return
elif page == 'dashboard': elif page == 'dashboard':
page_data['servers'] = db_helper.get_all_servers_stats() page_data['servers'] = db_helper.get_all_servers_stats()

View File

@ -1,5 +1,5 @@
{ {
"last_refreshed": "09/10/2020, 11:57:02", "last_refreshed": "09/14/2020, 11:31:20",
"servers": { "servers": {
"nukkitx": [ "nukkitx": [
"1.14" "1.14"
@ -68,6 +68,7 @@
"1.8" "1.8"
], ],
"bukkit": [ "bukkit": [
"1.16.3",
"1.16.2", "1.16.2",
"1.16.1", "1.16.1",
"1.15.2", "1.15.2",
@ -96,6 +97,7 @@
"1.8" "1.8"
], ],
"paper": [ "paper": [
"1.16.3",
"1.16.2", "1.16.2",
"1.16.1", "1.16.1",
"1.15.2", "1.15.2",
@ -118,6 +120,7 @@
"1.8.8" "1.8.8"
], ],
"spigot": [ "spigot": [
"1.16.3",
"1.16.2", "1.16.2",
"1.16.1", "1.16.1",
"1.15.2", "1.15.2",
@ -163,30 +166,31 @@
"1.1620w20a", "1.1620w20a",
"1.1620w19a", "1.1620w19a",
"1.1620w18a", "1.1620w18a",
"1.1620w17a",
"1.1620w16a", "1.1620w16a",
"1.1620w15a", "1.1620w10a",
"1.16.2rc2",
"1.1620w11a",
"1.1620w09a",
"1.1620w08a",
"1.1620w07a",
"1.16.3rc1",
"1.16.2rc1",
"1.1620w14a", "1.1620w14a",
"1.1620w14infinite", "1.1620w14infinite",
"1.1620w13b", "1.1620w13b",
"1.1620w13a", "1.1620w13a",
"1.1620w12a", "1.1620w12a",
"1.1620w11a", "1.161.16.3rc1",
"1.1620w10a", "1.16rc1",
"1.1620w09a", "1.1620w17a",
"1.1620w08a", "1.1620w15a",
"1.1620w07a",
"1.1620w06a", "1.1620w06a",
"1.16.3", "1.16.3",
"1.16.2rc2",
"1.16.3rc1",
"1.16.2rc1",
"1.16.2pre3", "1.16.2pre3",
"1.16.2pre2", "1.16.2pre2",
"1.16.2pre1", "1.16.2pre1",
"1.16.2", "1.16.2",
"1.161.16.1", "1.161.16.1",
"1.16rc1",
"1.16.1", "1.16.1",
"1.16", "1.16",
"1.15pre7", "1.15pre7",

View File

@ -160,7 +160,7 @@
<a class="btn btn-sm btn-danger disabled">Delete Server</a><br /> <a class="btn btn-sm btn-danger disabled">Delete Server</a><br />
<small>Please stop the server before deleting it</small> <small>Please stop the server before deleting it</small>
{% else %} {% else %}
<a href="/panel/command" class="btn btn-sm btn-danger">Delete Server</a> <a href="/panel/remove_server?id={{ data['server_stats'][0]['server_id']['server_id'] }}" class="btn btn-sm btn-danger">Delete Server</a>
{% end %} {% end %}
</div> </div>