Merge branch 'dev' into merge/pretzel-lukas-cleanup-nosquash

This commit is contained in:
luukas
2022-05-20 18:07:12 +03:00
53 changed files with 3658 additions and 268 deletions

View File

@ -5,7 +5,7 @@ import shutil
import time
import logging
import tempfile
from typing import Optional, Union
import typing as t
from peewee import DoesNotExist
# TZLocal is set as a hidden import on win pipeline
@ -276,7 +276,7 @@ class Controller:
except:
return {"percent": 0, "total_files": 0}
def get_server_obj(self, server_id: Union[str, int]) -> Server:
def get_server_obj(self, server_id: t.Union[str, int]) -> Server:
for server in self.servers_list:
if str(server["server_id"]) == str(server_id):
return server["server_obj"]
@ -284,7 +284,9 @@ class Controller:
logger.warning(f"Unable to find server object for server id {server_id}")
raise Exception(f"Unable to find server object for server id {server_id}")
def get_server_obj_optional(self, server_id: Union[str, int]) -> Optional[Server]:
def get_server_obj_optional(
self, server_id: t.Union[str, int]
) -> t.Optional[Server]:
for server in self.servers_list:
if str(server["server_id"]) == str(server_id):
return server["server_obj"]
@ -305,6 +307,10 @@ class Controller:
servers = HelperServers.get_all_defined_servers()
return servers
@staticmethod
def get_all_server_ids() -> t.List[int]:
return HelperServers.get_all_server_ids()
def list_running_servers(self):
running_servers = []
@ -345,6 +351,177 @@ class Controller:
svr_obj = self.get_server_obj(server_id)
svr_obj.stop_threaded_server()
def create_api_server(self, data: dict):
server_fs_uuid = Helpers.create_uuid()
new_server_path = os.path.join(self.helper.servers_dir, server_fs_uuid)
backup_path = os.path.join(self.helper.backup_path, server_fs_uuid)
if Helpers.is_os_windows():
new_server_path = Helpers.wtol_path(new_server_path)
backup_path = Helpers.wtol_path(backup_path)
new_server_path.replace(" ", "^ ")
backup_path.replace(" ", "^ ")
Helpers.ensure_dir_exists(new_server_path)
Helpers.ensure_dir_exists(backup_path)
def _copy_import_dir_files(existing_server_path):
existing_server_path = Helpers.get_os_understandable_path(
existing_server_path
)
try:
FileHelpers.copy_dir(existing_server_path, new_server_path, True)
except shutil.Error as ex:
logger.error(f"Server import failed with error: {ex}")
def _create_server_properties_if_needed(port, empty=False):
properties_file = os.path.join(new_server_path, "server.properties")
has_properties = os.path.exists(properties_file)
if not has_properties:
logger.info(
f"No server.properties found on import."
f"Creating one with port selection of {port}"
)
with open(
properties_file,
"w",
encoding="utf-8",
) as file:
file.write(
"# generated by Crafty Controller"
+ ("" if empty else f"\nserver-port={port}")
)
root_create_data = data[data["create_type"] + "_create_data"]
create_data = root_create_data[root_create_data["create_type"] + "_create_data"]
if data["create_type"] == "minecraft_java":
if root_create_data["create_type"] == "download_jar":
server_file = f"{create_data['type']}-{create_data['version']}.jar"
full_jar_path = os.path.join(new_server_path, server_file)
# Create an EULA file
with open(
os.path.join(new_server_path, "eula.txt"), "w", encoding="utf-8"
) as file:
file.write(
"eula=" + ("true" if create_data["agree_to_eula"] else "false")
)
elif root_create_data["create_type"] == "import_server":
_copy_import_dir_files(create_data["existing_server_path"])
full_jar_path = os.path.join(new_server_path, create_data["jarfile"])
elif root_create_data["create_type"] == "import_zip":
# TODO: Copy files from the zip file to the new server directory
full_jar_path = os.path.join(new_server_path, create_data["jarfile"])
raise Exception("Not yet implemented")
_create_server_properties_if_needed(create_data["server_properties_port"])
min_mem = create_data["mem_min"]
max_mem = create_data["mem_max"]
def _gibs_to_mibs(gibs: float) -> str:
return str(int(gibs * 1024))
def _wrap_jar_if_windows():
return (
f'"{full_jar_path}"' if Helpers.is_os_windows() else full_jar_path
)
server_command = (
f"java -Xms{_gibs_to_mibs(min_mem)}M "
f"-Xmx{_gibs_to_mibs(max_mem)}M "
f"-jar {_wrap_jar_if_windows()} nogui"
)
elif data["create_type"] == "minecraft_bedrock":
if root_create_data["create_type"] == "import_server":
existing_server_path = Helpers.get_os_understandable_path(
create_data["existing_server_path"]
)
try:
FileHelpers.copy_dir(existing_server_path, new_server_path, True)
except shutil.Error as ex:
logger.error(f"Server import failed with error: {ex}")
elif root_create_data["create_type"] == "import_zip":
# TODO: Copy files from the zip file to the new server directory
raise Exception("Not yet implemented")
_create_server_properties_if_needed(0, True)
server_command = create_data["command"]
server_file = ""
elif data["create_type"] == "custom":
# TODO: working_directory, executable_update
if root_create_data["create_type"] == "raw_exec":
pass
elif root_create_data["create_type"] == "import_server":
existing_server_path = Helpers.get_os_understandable_path(
create_data["existing_server_path"]
)
try:
FileHelpers.copy_dir(existing_server_path, new_server_path, True)
except shutil.Error as ex:
logger.error(f"Server import failed with error: {ex}")
elif root_create_data["create_type"] == "import_zip":
# TODO: Copy files from the zip file to the new server directory
raise Exception("Not yet implemented")
_create_server_properties_if_needed(0, True)
server_command = create_data["command"]
server_file = root_create_data["executable_update"].get("file", "")
stop_command = data.get("stop_command", "")
if stop_command == "":
# TODO: different default stop commands for server creation types
stop_command = "stop"
log_location = data.get("log_location", "")
if log_location == "":
# TODO: different default log locations for server creation types
log_location = "/logs/latest.log"
if data["monitoring_type"] == "minecraft_java":
monitoring_port = data["minecraft_java_monitoring_data"]["port"]
monitoring_host = data["minecraft_java_monitoring_data"]["host"]
monitoring_type = "minecraft-java"
elif data["monitoring_type"] == "minecraft_bedrock":
monitoring_port = data["minecraft_bedrock_monitoring_data"]["port"]
monitoring_host = data["minecraft_bedrock_monitoring_data"]["host"]
monitoring_type = "minecraft-bedrock"
elif data["monitoring_type"] == "none":
# TODO: this needs to be NUKED..
# There shouldn't be anything set if there are nothing to monitor
monitoring_port = 25565
monitoring_host = "127.0.0.1"
monitoring_type = "minecraft-java"
new_server_id = self.register_server(
name=data["name"],
server_uuid=server_fs_uuid,
server_dir=new_server_path,
backup_path=backup_path,
server_command=server_command,
server_file=server_file,
server_log_file=log_location,
server_stop=stop_command,
server_port=monitoring_port,
server_host=monitoring_host,
server_type=monitoring_type,
)
if (
data["create_type"] == "minecraft_java"
and root_create_data["create_type"] == "download_jar"
):
self.server_jars.download_jar(
create_data["type"],
create_data["version"],
full_jar_path,
new_server_id,
)
return new_server_id, server_fs_uuid
def create_jar_server(
self,
server: str,
@ -767,6 +944,7 @@ class Controller:
server_stop: str,
server_port: int,
server_type: str,
server_host: str = "127.0.0.1",
):
# put data in the db
new_id = self.servers.create_server(
@ -780,6 +958,7 @@ class Controller:
server_stop,
server_type,
server_port,
server_host,
)
if not Helpers.check_file_exists(
@ -796,7 +975,6 @@ class Controller:
"The server is managed by Crafty Controller.\n "
"Leave this directory/files alone please"
)
file.close()
except Exception as e:
logger.error(f"Unable to create required server files due to :{e}")