From 65e36ba9d9b9fa15d431bea58e975d8ea8394238 Mon Sep 17 00:00:00 2001 From: charleslemaux <146107581+charleslemaux@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:11:54 +0200 Subject: [PATCH 1/9] [+] Parameters description --- readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/readme.md b/readme.md index 648e9c0..c867712 100644 --- a/readme.md +++ b/readme.md @@ -43,6 +43,9 @@ pip install -r requirements.txt #### Launch ! ```bash gunicorn -w 4 -b 0.0.0.0:3000 app:app + +//You may also mention parameters like this. +gunicorn -w 4 -b 0.0.0.0:3000 app:app --interval 20 ``` ## Documentation From 72b48c960be616961f3f00e8cb49d2a66006d04a Mon Sep 17 00:00:00 2001 From: charleslemaux <146107581+charleslemaux@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:13:29 +0200 Subject: [PATCH 2/9] [+] Mentioning parameters --- readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index c867712..568b15a 100644 --- a/readme.md +++ b/readme.md @@ -43,8 +43,10 @@ pip install -r requirements.txt #### Launch ! ```bash gunicorn -w 4 -b 0.0.0.0:3000 app:app +``` -//You may also mention parameters like this. +*you also may be tempted to use parameters* +```bash gunicorn -w 4 -b 0.0.0.0:3000 app:app --interval 20 ``` From 8b90dc2c3d3ea89ff8b0391fe183a70bbdc8e113 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Thu, 12 Sep 2024 14:10:14 +0200 Subject: [PATCH 3/9] [-] log_error -> synchronous --- file_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/file_manager.py b/file_manager.py index 9b0f400..fff5cef 100644 --- a/file_manager.py +++ b/file_manager.py @@ -64,7 +64,7 @@ def update_server_property(file_path, property_name, new_value): file.writelines(content) -async def log_error(error_type: str, error_message: str): +def log_error(error_type: str, error_message: str): logging.basicConfig(filename='logs.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') From dec704f3c5f1b364fc9f26c88b9a1619f68a5eaa Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Fri, 13 Sep 2024 04:00:14 +0200 Subject: [PATCH 4/9] [+] Standard server field getter --- firebase_manager.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/firebase_manager.py b/firebase_manager.py index 6db0a6d..cb73a4f 100644 --- a/firebase_manager.py +++ b/firebase_manager.py @@ -6,7 +6,7 @@ import firebase_admin import jwt from firebase_admin import auth, credentials, firestore from google.api_core.exceptions import Aborted, DataLoss, NotFound, OutOfRange, PermissionDenied, ResourceExhausted -from google.cloud.firestore_v1 import FieldFilter +from google.cloud.firestore_v1 import FieldFilter, DocumentReference import file_manager from generic_executor import mc_manager @@ -133,6 +133,21 @@ def create_server(user_id: str, server_name: str, version: str, port: int, frame "enableCommandBlock": "false"}) +def get_server_field(user_id: str, name: str, field_name: str) -> Union[str, bool, None]: + try: + server_doc: DocumentReference = firestore_database.document(f'users/{user_id}/servers/{name}') + doc = server_doc.get() + if doc.exists: + field = doc.to_dict().get(field_name) + return field + else: + return None + except (NotFound, PermissionDenied, Aborted, ResourceExhausted, + OutOfRange, DataLoss, TypeError, Exception, ValueError) as e: + log_exception_to_firestore(e, user_id, {"function": "get_server_field", "name": name, "field": field_name}) + return None + + def delete_server(user_id: str, server_name: str): user_ref = firestore_database.collection('users').document(user_id) servers_ref = user_ref.collection('servers') From 97566974206f89049c08b2c23ef7c7dea866ca05 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Fri, 13 Sep 2024 04:00:51 +0200 Subject: [PATCH 5/9] [+] Useful minecraft servers organizer :) --- unit_test.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/unit_test.py b/unit_test.py index 7c25236..8c842c0 100644 --- a/unit_test.py +++ b/unit_test.py @@ -1,7 +1,7 @@ -import asyncio import os +import shutil +from typing import Callable, Union -import file_manager import firebase_manager @@ -13,10 +13,30 @@ def ban_user(user_id: str): print("Error banning user " + user_id, "|", str(e), type(e).__name__) +def organize_minecraft_files(file_list: list[str], target_directory: str): + os.makedirs(target_directory, exist_ok=True) + + for file_name in file_list: + if file_name.endswith('.jar'): + version = file_name.split('-')[1] + version_folder = os.path.join(target_directory, version) + + os.makedirs(version_folder, exist_ok=True) + + source_path = os.path.join(target_directory, file_name) + destination_path = os.path.join(version_folder, 'server.jar') + + shutil.move(source_path, destination_path) + + if __name__ == '__main__': #ban_user("MpkbDMOO8PQddQgB5VgBQdTMWF53") #file_manager.log_action("gqZN3eCHF3V2er3Py3rlgk8u2t83", "test", "DeleteServer") #firebase_manager.set_servers_not_running() - current_dir_content: list[str] = os.listdir(".") - parsed_extension: str = '.py' - print(file_manager.filter_directory_contents(parsed_extension, current_dir_content)) + #print(firebase_manager.get_server_field("secret :)", "AMITIE" ,"version")) + ''' + organize_minecraft_files( + listdir("/home/hapso/Desktop/Personal/servii-backend/servers/paper"), + "/home/hapso/Desktop/Personal/servii-backend/servers/paper") + ''' + pass From 11fcb0f89cb433a911a4699f487c17f32473c3b8 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Fri, 13 Sep 2024 04:02:07 +0200 Subject: [PATCH 6/9] [+] Now include the server's version before launching --- generic_executor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/generic_executor.py b/generic_executor.py index 80003c3..c22905f 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -236,10 +236,12 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None mc_manager.set_cooldown(user_id=user_id) try: port: int = firebase_manager.get_server_port(user_id) - server_id = mc_manager.start_server(f"users/{user_id}/{name}", port, user_id, name) + version: str = firebase_manager.get_server_field(user_id, name, "version") + if version is None: + return HTTPStatus.NOT_FOUND, f"Server {name} not found." + server_id = mc_manager.start_server(f"users/{user_id}/{name}", port, user_id, name, version) if server_id is None: return HTTPStatus.OK, f"You cannot run multiples instances at this time." - mc_manager.servers[server_id]['port'] = int(port) try: firebase_manager.update_server_running_state(user_id, name, True) except Exception as e: @@ -324,5 +326,6 @@ def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, def scheduled_actions() -> None: mc_manager.check_servers_idle() + if __name__ == "__main__": pass From f62c5fc85665f2ec41b5fb8e3a472c42d7e87615 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Fri, 13 Sep 2024 04:14:37 +0200 Subject: [PATCH 7/9] [+] Masterclass conditional minecraft version checker - Added version parameter to 'start_server' function in MinecraftManager class - Implemented a version checker that uses a pointer of Tuples, each of them containing a lower and upper bound, as well as the associated sdk - Used currying method to concatenate 'version_range_checker' into the fastest and most efficient code This system will allow easy versioning, as well as easy future sdk implementations. It will also now be easy to fetch a server's version directly in the backend. Co-authored-by: charleslemaux --- server_mc_manager.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/server_mc_manager.py b/server_mc_manager.py index c6d5cd9..92f87fa 100644 --- a/server_mc_manager.py +++ b/server_mc_manager.py @@ -1,7 +1,7 @@ import shlex import subprocess import time -from typing import Union +from typing import Union, Callable import mcipc.query import mcipc.query.client @@ -21,7 +21,7 @@ class MinecraftServerManager: self.offline_ports: list[int] = [] def start_server(self, server_directory: str, port: int, user_id: str, server_name: str, - java_executable='java', jar_file='server.jar', memory_size='2048M') -> Union[int, None]: + version: str, jar_file='server.jar', memory_size='4G') -> Union[int, None]: if port in self.servers: return None @@ -35,7 +35,15 @@ class MinecraftServerManager: " -XX:MaxTenuringThreshold=1 -Daikars.new.flags=true" " -Dusing.aikars.flags=https://mcutils.com") - command = f"{java_executable} -Xmx{memory_size} {reg_flags} -jar {jar_file} --nogui" + + + get_sdk_version: Callable[[str], str] = version_range_checker(("1.0", "1.15.2", "11"), + ("1.16.1", "1.18.1", "17"), + default_sdk="21") + + java: str = f"/usr/lib/jvm/java-{get_sdk_version(version)}-openjdk/bin/java" + + command = f"{java} -Xmx{memory_size} {reg_flags} -jar {jar_file} --nogui" process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE) #TODO: Track process behavior and stderr, while excepting Advanced Terminal features not to be avail. @@ -46,6 +54,7 @@ class MinecraftServerManager: 'port': port, 'user_id': user_id, 'name': server_name, + 'version': version, 'time': time.time(), } return port @@ -136,5 +145,34 @@ class MinecraftServerManager: return +def version_range_checker(*ranges: tuple[str, str, str], default_sdk: str = "java") -> Callable[[str], str]: + def compare_mc_versions(version1: str): + def inner(version2: str) -> int: + v1_parts: list[int] = list(map(int, version1.split('.'))) + v2_parts: list[int] = list(map(int, version2.split('.'))) + for v1, v2 in zip(v1_parts, v2_parts): + if v1 > v2: + return 1 + elif v1 < v2: + return -1 + if len(v1_parts) > len(v2_parts): + return 1 + elif len(v1_parts) < len(v2_parts): + return -1 + return 0 + return inner + + def check_range(lower_bound: str, upper_bound: str, version: str) -> bool: + return compare_mc_versions(lower_bound)(version) <= 0 <= compare_mc_versions(upper_bound)(version) + + def check_version(version: str) -> str: + for lower_bound, upper_bound, sdk in ranges: + if check_range(lower_bound, upper_bound, version): + return sdk + return default_sdk + + return check_version + + if __name__ == "__main__": pass From 89145b15a45a7a9a7584f7a965ea97388ea909c0 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sat, 14 Sep 2024 16:34:00 +0200 Subject: [PATCH 8/9] [~] Moved get_sdk_version on main server_mc_manager scope --- server_mc_manager.py | 10 ++++------ unit_test.py | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/server_mc_manager.py b/server_mc_manager.py index 92f87fa..810433f 100644 --- a/server_mc_manager.py +++ b/server_mc_manager.py @@ -35,12 +35,6 @@ class MinecraftServerManager: " -XX:MaxTenuringThreshold=1 -Daikars.new.flags=true" " -Dusing.aikars.flags=https://mcutils.com") - - - get_sdk_version: Callable[[str], str] = version_range_checker(("1.0", "1.15.2", "11"), - ("1.16.1", "1.18.1", "17"), - default_sdk="21") - java: str = f"/usr/lib/jvm/java-{get_sdk_version(version)}-openjdk/bin/java" command = f"{java} -Xmx{memory_size} {reg_flags} -jar {jar_file} --nogui" @@ -174,5 +168,9 @@ def version_range_checker(*ranges: tuple[str, str, str], default_sdk: str = "jav return check_version +get_sdk_version: Callable[[str], str] = version_range_checker(("1.0", "1.15.2", "11"), + ("1.16.1", "1.18.1", "17"), + default_sdk="21") + if __name__ == "__main__": pass diff --git a/unit_test.py b/unit_test.py index 8c842c0..487231b 100644 --- a/unit_test.py +++ b/unit_test.py @@ -3,6 +3,8 @@ import shutil from typing import Callable, Union import firebase_manager +import server_mc_manager +from generic_executor import mc_manager def ban_user(user_id: str): @@ -38,5 +40,5 @@ if __name__ == '__main__': organize_minecraft_files( listdir("/home/hapso/Desktop/Personal/servii-backend/servers/paper"), "/home/hapso/Desktop/Personal/servii-backend/servers/paper") - ''' + '''S pass From 07696572aa9d16c2941fe34b1356c4cdd1da307c Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Sat, 14 Sep 2024 16:34:44 +0200 Subject: [PATCH 9/9] [-] :eyes: --- unit_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unit_test.py b/unit_test.py index 487231b..88cf05a 100644 --- a/unit_test.py +++ b/unit_test.py @@ -40,5 +40,5 @@ if __name__ == '__main__': organize_minecraft_files( listdir("/home/hapso/Desktop/Personal/servii-backend/servers/paper"), "/home/hapso/Desktop/Personal/servii-backend/servers/paper") - '''S + ''' pass