servii-backend/server_mc_manager.py
Charles Le Maux 35689e4dd0 [++] API:V2 - Firebase support
Fully implemented authentication
Fully implemented return codes and errors
Fully implemented firestore database data
Created 'fetch_servers' and 'set_subdomain' routes for front-end use.
[+] Security patch, requests will not return anything if not properly authenticated
[~] Reworked the route system entirely
[+] TODO : firebase_manager.py error handling
[+] TODO : global error log using file_manager.log_error(error_type:str, error_message:str)

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
2024-06-25 05:55:49 +01:00

60 lines
1.8 KiB
Python

import subprocess
import shlex
class MinecraftServerManager:
def __init__(self):
self.servers: dict = {}
self.servers_count: int = 0
def start_server(self, server_directory: str, port: int,
java_executable='java', jar_file='server.jar', memory_size='2048M'):
command = f"{java_executable} -Xmx{memory_size} -Xms{memory_size} -jar {jar_file} > /dev/null"
process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE)
self.servers_count = len(self.servers) + 1
self.servers[port] = {
'process': process,
'directory': server_directory,
'port': port
}
return port
def execute_server_command(self, server_id, command) -> bool:
if server_id not in self.servers:
return False
process = self.servers[server_id]['process']
process.stdin.write(command.encode('utf-8') + b'\n')
process.stdin.flush()
return True
def stop_server(self, server_id) -> bool:
if server_id not in self.servers:
return False
process = self.servers[server_id]['process']
process.communicate(input=b"stop\n")
del self.servers[server_id]
return True
def stop_server_forcefully(self, server_id) -> bool:
if server_id not in self.servers:
return False
process = self.servers[server_id]['process']
process.terminate()
del self.servers[server_id]
return True
def get_servers(self):
return self.servers.values()
def get_server_id_by_port(self, port):
for server_id, server_info in self.servers.items():
if server_info['port'] == port:
return server_id
return None
if __name__ == "__main__":
pass