diff --git a/osiris_listener.py b/osiris_listener.py index b7fd15d..3f0324c 100644 --- a/osiris_listener.py +++ b/osiris_listener.py @@ -3,17 +3,38 @@ import subprocess #from win10toast import ToastNotifier import os import logging +import signal logging.basicConfig(filename='osiris_listener.log', level=logging.DEBUG, format='%(asctime)s %(message)s') app = Flask(__name__) #toaster = ToastNotifier() +# Global list to store process handles +processes = [] + +# 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']}, + # 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']}, + # 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']}, + # 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']}, + + # Add more scripts and their parameters as needed + ] +} + @app.route('/osiris_execute', methods=['POST']) def execute_script(): logging.info("Commando zur Ausführung erhalten.") data = request.get_json() command = data.get('command') + if command: logging.info(f"Commando: {command}") # Hier neue Commands zur Prüfung einpflegen @@ -28,6 +49,7 @@ def execute_script(): return jsonify({'error': str(exc)}), 500 case 'shutdown': + logging.info("Received request to shutdown.") try: #toaster.show_toast("Shutdown", "Shutdown Command erhalten. Host wird heruntergefahren.", duration=10) os.system("shutdown /s /t 1") @@ -35,22 +57,45 @@ 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': - try: - #toaster.show_toast("start_gameservers_minecraft", "Starte Minecraft-Server", duration=10) - # hier pgm aufrufen - return jsonify({'message': 'Minecraft-Server werden gestartet.'}) - except Exception as exc: - logging.error(f"Error executing start_gameservers_dontstarve command: {exc}") - return jsonify({'error': str(exc)}), 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']: + path = script_info["path"] + params = script_info["params"] + try: + 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}") + results.append({ + "script": path, + "params": params, + "pid":process.pid + #"returncode": result.returncode + }) + except Exception as e: + logging.error(f"Error executing script {path}: {e}") + results.append({ + "script": path, + "params": params, + "error": str(e) + }) + return jsonify(results) + #return jsonify({'message': 'Minecraft-Server werden gestartet.'}) + case 'stop_gameservers_minecraft': - try: - #toaster.show_toast("stop_gameservers_minecraft", "Stoppe Minecraft-Server", duration=10) - # hier pgm aufrufen - return jsonify({'message': 'Minecraft-Server wurden angehalten.'}) - except Exception as exc: - logging.error(f"Error executing stop_gameservers_minecraft command: {exc}") - return jsonify({'error': str(exc)}), 500 + logging.info("Received request to stop minecraft servers.") + global processes + for process in processes: + try: + #toaster.show_toast("stop_gameservers_minecraft", "Stoppe Minecraft-Server", duration=10) + process.send_signal(signal.SIGINT) + logging.info(f"Sent SIGINT to process with PID: {process.pid}") + except Exception as exc: + logging.error(f"Error sending DIGINT to process with PID {process.pid}") + processes = [] # clear process list + return jsonify({'message': "Sent SIGINT to all running minecraft server scripts"}) case 'start_gameservers_dontstarve': try: #toaster.show_toast("start_gameservers_dontstarve", "Starte Dont Starve Together-Server", duration=10)