From 334e49953eb8ac9c2d1e597f276bb7006ef785bd Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 26 Jul 2024 23:46:53 +0200 Subject: [PATCH] Add status gameservers + refactor --- osiris_listener.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/osiris_listener.py b/osiris_listener.py index 3f0324c..1ac8419 100644 --- a/osiris_listener.py +++ b/osiris_listener.py @@ -12,18 +12,19 @@ app = Flask(__name__) # Global list to store process handles processes = [] +processes_info = [] # Define the scripts and their parameters SCRIPTS = { "start_gameservers_minecraft": [ # Vanilla (Port 25565) - {"path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\Vanilla_1-21\\start_server.py", "params": ["--java_path", "'C:\Program Files\Java\jdk-22\bin'", 'minecraft_server.1.21']}, + {"name": "Minecraft Vanilla 1.21", "path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\Vanilla_1-21\\start_server.py", "params": ["--java_path", "'C:\\Program Files\\Java\\jdk-22\\bin'", 'minecraft_server.1.21']}, # The 1.12.2 Pack (SanderPack) (Port 25566) - {"path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\1-12-2-Pack\\start_server.py", "params": ["--java_path", "'C:\Program Files\OpenLogic\jdk-8.0.412.08-hotspot\bin'", 'forge-1.12.2-14.23.5.2860']}, + {"name": "Minecraft The 1.12.2 Pack (SanderPack)", "path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\1-12-2-Pack\\start_server.py", "params": ["--java_path", "'C:\\Program Files\\OpenLogic\\jdk-8.0.412.08-hotspot\\bin'", 'forge-1.12.2-14.23.5.2860']}, # GT New Horizon (SanderHorizon) (Port 25567) - {"path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\gt_new_horizons_2.6.0\\start_server.py", "params": ["--java_path", "'C:\Program Files\OpenLogic\jdk-8.0.412.08-hotspot\bin'", 'forge-1.7.10-10.13.4.1614-1.7.10-universal']}, + {"name": "Minecraft GT New Horizon 2.6.0 (SanderHorizon)", "path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\gt_new_horizons_2.6.0\\start_server.py", "params": ["--java_path", "'C:\\Program Files\\OpenLogic\\jdk-8.0.412.08-hotspot\\bin'", 'forge-1.7.10-10.13.4.1614-1.7.10-universal']}, # rlcraft (SanderHardcore) (Port 25568) - {"path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\rlcraft\\start_server.py", "params": ["--java_path", "'C:\Program Files\OpenLogic\jdk-8.0.412.08-hotspot\bin'", 'forge-1.12.2-14.23.5.2860']}, + {"name": "Minecraft rlcraft 2.9.3 (SanderHardcore)", "path": "C:\\Users\\4lexK\\Desktop\\GameServers\\Minecraft\\rlcraft\\start_server.py", "params": ["--java_path", "'C:\\Program Files\\OpenLogic\\jdk-8.0.412.08-hotspot\\\bin'", 'forge-1.12.2-14.23.5.2860']}, # Add more scripts and their parameters as needed ] @@ -32,6 +33,8 @@ SCRIPTS = { @app.route('/osiris_execute', methods=['POST']) def execute_script(): logging.info("Commando zur Ausführung erhalten.") + global processes # Declare the global variable + global processes_info data = request.get_json() command = data.get('command') @@ -46,7 +49,7 @@ def execute_script(): return jsonify({'message': 'Test Command abgesetzt und Notification auf bei Host erstellt.'}) except Exception as exc: logging.error(f"Error executing test command: {exc}") - return jsonify({'error': str(exc)}), 500 + return jsonify({'message': f'Error: {str(exc)}'}), 500 case 'shutdown': logging.info("Received request to shutdown.") @@ -57,21 +60,23 @@ def execute_script(): except Exception as e: logging.error(f"Error executing shutdown command: {exc}") return jsonify({'error': str(e)}), 500 + case 'start_gameservers_minecraft': logging.info("Received request to start minecraft servers.") #toaster.show_toast("start_gameservers_minecraft", "Starte Minecraft-Server", duration=10) - results = [] - for script_info in SCRIPTS['start_servers']: + results = [] + for script_info in SCRIPTS['start_gameservers_minecraft']: path = script_info["path"] params = script_info["params"] + script_name = script_info["name"] try: - process = subprocess.popen(['python', path] + params, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + process = subprocess.Popen(['python', path] + params, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) processes.append(process) - logging.info(f"Executed {path} with params {params}. Output: {result.stdout}") + processes_info.append({"process": process, "name": script_name}) results.append({ "script": path, "params": params, - "pid":process.pid + "pid": process.pid #"returncode": result.returncode }) except Exception as e: @@ -85,8 +90,7 @@ def execute_script(): #return jsonify({'message': 'Minecraft-Server werden gestartet.'}) case 'stop_gameservers_minecraft': - logging.info("Received request to stop minecraft servers.") - global processes + logging.info("Received request to stop minecraft servers.") for process in processes: try: #toaster.show_toast("stop_gameservers_minecraft", "Stoppe Minecraft-Server", duration=10) @@ -95,6 +99,7 @@ def execute_script(): except Exception as exc: logging.error(f"Error sending DIGINT to process with PID {process.pid}") processes = [] # clear process list + processes_info = [] return jsonify({'message': "Sent SIGINT to all running minecraft server scripts"}) case 'start_gameservers_dontstarve': try: @@ -159,6 +164,17 @@ def status(): return jsonify({'status': 'online'}) +@app.route('/status_servers', methods=['GET']) +def status_gameservers(): + logging.info("Check Status der Server") + global processes_info + running_servers = [] + for process in processes_info: + running_servers.append(process['name']) + server_list = "\n - ".join(running_servers) + return jsonify({"message": f"Aktuell laufende Server: {server_list}" }) + + if __name__ == '__main__': #toaster.show_toast("Starte Osiris", "Osiris Listener wurde gestartet. Warte auf Befehle...", duration=30) logging.info("Starte Server.")