2020-08-17 02:47:53 +00:00
|
|
|
import os
|
2021-11-29 21:22:46 +00:00
|
|
|
import pathlib
|
2022-03-08 02:36:23 +00:00
|
|
|
from pathlib import Path
|
2022-03-01 23:31:38 +00:00
|
|
|
import shutil
|
2020-08-17 02:47:53 +00:00
|
|
|
import time
|
|
|
|
import logging
|
2021-08-05 02:06:38 +00:00
|
|
|
import tempfile
|
2022-01-26 01:45:30 +00:00
|
|
|
from typing import Union
|
2022-04-11 05:23:55 +00:00
|
|
|
from peewee import DoesNotExist
|
2022-04-11 10:08:36 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
# TZLocal is set as a hidden import on win pipeline
|
|
|
|
from tzlocal import get_localzone
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.controllers.crafty_perms_controller import Crafty_Perms_Controller
|
|
|
|
from app.classes.controllers.management_controller import Management_Controller
|
|
|
|
from app.classes.controllers.users_controller import Users_Controller
|
|
|
|
from app.classes.controllers.roles_controller import Roles_Controller
|
|
|
|
from app.classes.controllers.server_perms_controller import Server_Perms_Controller
|
|
|
|
from app.classes.controllers.servers_controller import Servers_Controller
|
|
|
|
from app.classes.models.server_permissions import Enum_Permissions_Server
|
|
|
|
from app.classes.models.users import helper_users
|
2022-04-12 21:58:59 +00:00
|
|
|
from app.classes.models.roles import helper_roles
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.models.management import helpers_management
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.models.servers import helper_servers
|
|
|
|
from app.classes.shared.authentication import Authentication
|
2022-04-12 01:34:46 +00:00
|
|
|
from app.classes.shared.console import Console
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
2022-01-15 00:23:50 +00:00
|
|
|
from app.classes.shared.server import Server
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.file_helpers import FileHelpers
|
2022-01-15 00:23:50 +00:00
|
|
|
from app.classes.minecraft.server_props import ServerProps
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.minecraft.serverjars import ServerJars
|
2022-01-15 00:23:50 +00:00
|
|
|
from app.classes.minecraft.stats import Stats
|
2022-03-08 04:40:44 +00:00
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-04-11 10:08:36 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class Controller:
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, database, helper):
|
|
|
|
self.helper = helper
|
|
|
|
self.server_jars = ServerJars(helper)
|
|
|
|
self.users_helper = helper_users(database, self.helper)
|
2022-04-12 21:58:59 +00:00
|
|
|
self.roles_helper = helper_roles(database)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.servers_helper = helper_servers(database)
|
|
|
|
self.management_helper = helpers_management(database, self.helper)
|
|
|
|
self.authentication = Authentication(self.helper)
|
2020-08-17 02:47:53 +00:00
|
|
|
self.servers_list = []
|
2022-04-11 05:23:55 +00:00
|
|
|
self.stats = Stats(self.helper, self)
|
2021-09-06 17:27:01 +00:00
|
|
|
self.crafty_perms = Crafty_Perms_Controller()
|
2022-04-11 05:23:55 +00:00
|
|
|
self.management = Management_Controller(self.management_helper)
|
2022-04-12 21:58:59 +00:00
|
|
|
self.roles = Roles_Controller(self.users_helper, self.roles_helper)
|
2021-09-08 22:01:10 +00:00
|
|
|
self.server_perms = Server_Perms_Controller()
|
2022-04-11 05:23:55 +00:00
|
|
|
self.servers = Servers_Controller(self.servers_helper)
|
2022-04-11 10:08:36 +00:00
|
|
|
self.users = Users_Controller(
|
|
|
|
self.helper, self.users_helper, self.authentication
|
|
|
|
)
|
2022-03-13 12:29:26 +00:00
|
|
|
tz = get_localzone()
|
|
|
|
self.support_scheduler = BackgroundScheduler(timezone=str(tz))
|
|
|
|
self.support_scheduler.start()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-09-14 18:05:17 +00:00
|
|
|
def check_server_loaded(self, server_id_to_check: int):
|
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Checking to see if we already registered {server_id_to_check}")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-09-14 18:05:17 +00:00
|
|
|
for s in self.servers_list:
|
2022-03-23 02:50:12 +00:00
|
|
|
known_server = s.get("server_id")
|
2020-09-14 18:05:17 +00:00
|
|
|
if known_server is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if known_server == server_id_to_check:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"skipping initialization of server {server_id_to_check} "
|
|
|
|
f"because it is already loaded"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-09-14 18:05:17 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def init_all_servers(self):
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
servers = self.servers.get_all_defined_servers()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
for s in servers:
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = s.get("server_id")
|
2020-09-14 18:05:17 +00:00
|
|
|
|
|
|
|
# if we have already initialized this server, let's skip it.
|
|
|
|
if self.check_server_loaded(server_id):
|
|
|
|
continue
|
2020-08-24 17:08:17 +00:00
|
|
|
|
|
|
|
# if this server path no longer exists - let's warn and bomb out
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.check_path_exists(
|
|
|
|
Helpers.get_os_understandable_path(s["path"])
|
2022-03-23 02:50:12 +00:00
|
|
|
):
|
|
|
|
logger.warning(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Unable to find server {s['server_name']} at path {s['path']}. "
|
|
|
|
f"Skipping this server"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.warning(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Unable to find server {s['server_name']} at path {s['path']}. "
|
|
|
|
f"Skipping this server"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-08-24 17:08:17 +00:00
|
|
|
continue
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
settings_file = os.path.join(
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.get_os_understandable_path(s["path"]), "server.properties"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-08-24 17:08:17 +00:00
|
|
|
|
|
|
|
# if the properties file isn't there, let's warn
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.check_file_exists(settings_file):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Unable to find {settings_file}. Skipping this server.")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.error(f"Unable to find {settings_file}. Skipping this server.")
|
2020-08-24 17:08:17 +00:00
|
|
|
continue
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
settings = ServerProps(settings_file)
|
|
|
|
|
|
|
|
temp_server_dict = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_id": s.get("server_id"),
|
|
|
|
"server_data_obj": s,
|
2022-04-11 05:23:55 +00:00
|
|
|
"server_obj": Server(self.helper, self.management_helper, self.stats),
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_settings": settings.props,
|
2020-08-17 02:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# setup the server, do the auto start and all that jazz
|
2022-03-23 02:50:12 +00:00
|
|
|
temp_server_dict["server_obj"].do_server_setup(s)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
# add this temp object to the list of init servers
|
|
|
|
self.servers_list.append(temp_server_dict)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if s["auto_start"]:
|
|
|
|
self.servers.set_waiting_start(s["server_id"], True)
|
2021-09-20 22:30:17 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.refresh_server_settings(s["server_id"])
|
2021-09-20 18:46:31 +00:00
|
|
|
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 02:50:12 +00:00
|
|
|
f"Loaded Server: ID {s['server_id']}"
|
|
|
|
+ f" | Name: {s['server_name']}"
|
|
|
|
+ f" | Autostart: {s['auto_start']}"
|
|
|
|
+ f" | Delay: {s['auto_start_delay']} "
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-10-07 13:19:47 +00:00
|
|
|
def refresh_server_settings(self, server_id: int):
|
|
|
|
server_obj = self.get_server_obj(server_id)
|
|
|
|
server_obj.reload_server_settings()
|
|
|
|
|
2021-12-21 01:17:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def check_system_user():
|
|
|
|
if helper_users.get_user_id_by_name("system") is not None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2022-01-14 04:01:18 +00:00
|
|
|
def set_project_root(self, root_dir):
|
|
|
|
self.project_root = root_dir
|
|
|
|
|
2022-01-14 01:42:53 +00:00
|
|
|
def package_support_logs(self, exec_user):
|
2022-03-23 02:50:12 +00:00
|
|
|
if exec_user["preparing"]:
|
2022-03-13 12:29:26 +00:00
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
self.users.set_prepare(exec_user["user_id"])
|
|
|
|
# pausing so on screen notifications can run for user
|
2022-01-18 18:16:09 +00:00
|
|
|
time.sleep(7)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.websocket_helper.broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
exec_user["user_id"], "notification", "Preparing your support logs"
|
|
|
|
)
|
2022-01-14 01:42:53 +00:00
|
|
|
tempDir = tempfile.mkdtemp()
|
|
|
|
tempZipStorage = tempfile.mkdtemp()
|
2022-03-23 02:50:12 +00:00
|
|
|
full_temp = os.path.join(tempDir, "support_logs")
|
2022-01-14 01:42:53 +00:00
|
|
|
os.mkdir(full_temp)
|
|
|
|
tempZipStorage = os.path.join(tempZipStorage, "support_logs")
|
|
|
|
crafty_path = os.path.join(full_temp, "crafty")
|
|
|
|
os.mkdir(crafty_path)
|
|
|
|
server_path = os.path.join(full_temp, "server")
|
|
|
|
os.mkdir(server_path)
|
2022-03-23 02:50:12 +00:00
|
|
|
if exec_user["superuser"]:
|
2022-01-14 01:42:53 +00:00
|
|
|
auth_servers = self.servers.get_all_defined_servers()
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
user_servers = self.servers.get_authorized_servers(
|
|
|
|
int(exec_user["user_id"])
|
|
|
|
)
|
2022-01-14 01:42:53 +00:00
|
|
|
auth_servers = []
|
|
|
|
for server in user_servers:
|
2022-03-23 02:50:12 +00:00
|
|
|
if (
|
|
|
|
Enum_Permissions_Server.Logs
|
|
|
|
in self.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server["server_id"]
|
|
|
|
)
|
|
|
|
):
|
2022-01-14 01:42:53 +00:00
|
|
|
auth_servers.append(server)
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Logs permission not available for server "
|
|
|
|
f"{server['server_name']}. Skipping."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
# we'll iterate through our list of log paths from auth servers.
|
2022-01-14 01:42:53 +00:00
|
|
|
for server in auth_servers:
|
2022-03-23 02:50:12 +00:00
|
|
|
final_path = os.path.join(server_path, str(server["server_name"]))
|
2022-03-14 21:39:19 +00:00
|
|
|
try:
|
|
|
|
os.mkdir(final_path)
|
|
|
|
except FileExistsError:
|
2022-03-23 02:50:12 +00:00
|
|
|
final_path += "_" + server["server_uuid"]
|
2022-03-14 21:39:19 +00:00
|
|
|
os.mkdir(final_path)
|
2022-01-19 22:23:04 +00:00
|
|
|
try:
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.copy_file(server["log_path"], final_path)
|
2022-01-19 22:23:04 +00:00
|
|
|
except Exception as e:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.warning(f"Failed to copy file with error: {e}")
|
2022-03-23 02:50:12 +00:00
|
|
|
# Copy crafty logs to archive dir
|
|
|
|
full_log_name = os.path.join(crafty_path, "logs")
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.copy_dir(os.path.join(self.project_root, "logs"), full_log_name)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.support_scheduler.add_job(
|
|
|
|
self.log_status,
|
|
|
|
"interval",
|
|
|
|
seconds=1,
|
|
|
|
id="logs_" + str(exec_user["user_id"]),
|
|
|
|
args=[full_temp, tempZipStorage + ".zip", exec_user],
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.make_archive(tempZipStorage, tempDir)
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if len(self.helper.websocket_helper.clients) > 0:
|
|
|
|
self.helper.websocket_helper.broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
exec_user["user_id"],
|
|
|
|
"support_status_update",
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.calc_percent(full_temp, tempZipStorage + ".zip"),
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-14 01:42:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
tempZipStorage += ".zip"
|
2022-04-11 10:08:36 +00:00
|
|
|
self.helper.websocket_helper.broadcast_user(
|
|
|
|
exec_user["user_id"], "send_logs_bootbox", {}
|
|
|
|
)
|
2022-01-19 20:33:24 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.users.set_support_path(exec_user["user_id"], tempZipStorage)
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.users.stop_prepare(exec_user["user_id"])
|
|
|
|
self.support_scheduler.remove_job("logs_" + str(exec_user["user_id"]))
|
2022-01-14 01:42:53 +00:00
|
|
|
|
2021-12-21 01:17:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_system_user():
|
2022-03-23 02:50:12 +00:00
|
|
|
helper_users.add_user(
|
|
|
|
"system",
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.random_string_generator(64),
|
2022-03-23 02:50:12 +00:00
|
|
|
"default@example.com",
|
|
|
|
False,
|
|
|
|
False,
|
|
|
|
)
|
2021-12-21 01:17:28 +00:00
|
|
|
|
2021-08-28 22:48:30 +00:00
|
|
|
def get_server_settings(self, server_id):
|
|
|
|
for s in self.servers_list:
|
2022-03-23 02:50:12 +00:00
|
|
|
if int(s["server_id"]) == int(server_id):
|
|
|
|
return s["server_settings"]
|
2021-08-28 22:48:30 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.warning(f"Unable to find server object for server id {server_id}")
|
2021-08-28 22:48:30 +00:00
|
|
|
return False
|
|
|
|
|
2022-01-31 02:04:08 +00:00
|
|
|
def crash_detection(self, server_obj):
|
|
|
|
svr = self.get_server_obj(server_obj.server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
# start or stop crash detection depending upon user preference
|
2022-03-23 06:06:13 +00:00
|
|
|
# The below functions check to see if the server is running.
|
|
|
|
# They only execute if it's running.
|
2022-01-31 02:04:08 +00:00
|
|
|
if server_obj.crash_detection == 1:
|
|
|
|
svr.start_crash_detection()
|
|
|
|
else:
|
|
|
|
svr.stop_crash_detection()
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-13 12:29:26 +00:00
|
|
|
def log_status(self, source_path, dest_path, exec_user):
|
2022-04-11 05:23:55 +00:00
|
|
|
results = Helpers.calc_percent(source_path, dest_path)
|
2022-03-13 12:29:26 +00:00
|
|
|
self.log_stats = results
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if len(self.helper.websocket_helper.clients) > 0:
|
|
|
|
self.helper.websocket_helper.broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
exec_user["user_id"], "support_status_update", results
|
|
|
|
)
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-13 12:29:26 +00:00
|
|
|
def send_log_status(self):
|
|
|
|
try:
|
|
|
|
return self.log_stats
|
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
return {"percent": 0, "total_files": 0}
|
2022-01-31 02:04:08 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
def get_server_obj(self, server_id: Union[str, int]) -> Union[bool, Server]:
|
2020-08-17 02:47:53 +00:00
|
|
|
for s in self.servers_list:
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(s["server_id"]) == str(server_id):
|
|
|
|
return s["server_obj"]
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.warning(f"Unable to find server object for server id {server_id}")
|
2022-01-15 00:23:50 +00:00
|
|
|
return False # TODO: Change to None
|
2021-11-19 20:59:23 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
def get_server_data(self, server_id: str):
|
2021-08-28 22:48:30 +00:00
|
|
|
for s in self.servers_list:
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(s["server_id"]) == str(server_id):
|
|
|
|
return s["server_data_obj"]
|
2021-08-28 22:48:30 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.warning(f"Unable to find server object for server id {server_id}")
|
2021-08-28 22:48:30 +00:00
|
|
|
return False
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def list_defined_servers():
|
2022-04-11 05:23:55 +00:00
|
|
|
servers = helper_servers.get_all_defined_servers()
|
2020-08-19 01:04:43 +00:00
|
|
|
return servers
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
def list_running_servers(self):
|
|
|
|
running_servers = []
|
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
# for each server
|
|
|
|
for s in self.servers_list:
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
# is the server running?
|
2022-03-23 02:50:12 +00:00
|
|
|
srv_obj = s["server_obj"]
|
2020-08-24 23:11:17 +00:00
|
|
|
running = srv_obj.check_running()
|
|
|
|
# if so, let's add a dictionary to the list of running servers
|
|
|
|
if running:
|
2022-03-23 02:50:12 +00:00
|
|
|
running_servers.append({"id": srv_obj.server_id, "name": srv_obj.name})
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
return running_servers
|
|
|
|
|
|
|
|
def stop_all_servers(self):
|
|
|
|
servers = self.list_running_servers()
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Found {len(servers)} running server(s)")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Found {len(servers)} running server(s)")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
logger.info("Stopping All Servers")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Stopping All Servers")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
for s in servers:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Stopping Server ID {s['id']} - {s['name']}")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Stopping Server ID {s['id']} - {s['name']}")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.stop_server(s["id"])
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-09-14 18:05:17 +00:00
|
|
|
# let's wait 2 seconds to let everything flush out
|
|
|
|
time.sleep(2)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-09-14 18:05:17 +00:00
|
|
|
logger.info("All Servers Stopped")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("All Servers Stopped")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-09-14 18:05:17 +00:00
|
|
|
def stop_server(self, server_id):
|
|
|
|
# issue the stop command
|
2021-11-28 01:09:13 +00:00
|
|
|
svr_obj = self.get_server_obj(server_id)
|
2020-09-14 18:05:17 +00:00
|
|
|
svr_obj.stop_threaded_server()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def create_jar_server(
|
|
|
|
self,
|
|
|
|
server: str,
|
|
|
|
version: str,
|
|
|
|
name: str,
|
|
|
|
min_mem: int,
|
|
|
|
max_mem: int,
|
|
|
|
port: int,
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
server_id = Helpers.create_uuid()
|
|
|
|
server_dir = os.path.join(self.helper.servers_dir, server_id)
|
|
|
|
backup_path = os.path.join(self.helper.backup_path, server_id)
|
|
|
|
if Helpers.is_os_windows():
|
|
|
|
server_dir = Helpers.wtol_path(server_dir)
|
|
|
|
backup_path = Helpers.wtol_path(backup_path)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_dir.replace(" ", "^ ")
|
|
|
|
backup_path.replace(" ", "^ ")
|
2020-09-10 15:01:57 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
server_file = f"{server}-{version}.jar"
|
2020-09-10 15:01:57 +00:00
|
|
|
full_jar_path = os.path.join(server_dir, server_file)
|
|
|
|
|
|
|
|
# make the dir - perhaps a UUID?
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.ensure_dir_exists(server_dir)
|
|
|
|
Helpers.ensure_dir_exists(backup_path)
|
2020-09-10 15:01:57 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# do a eula.txt
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(os.path.join(server_dir, "eula.txt"), "w", encoding="utf-8") as f:
|
2021-11-19 20:59:23 +00:00
|
|
|
f.write("eula=false")
|
2020-09-10 15:01:57 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
# setup server.properties with the port
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(
|
|
|
|
os.path.join(server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as f:
|
2022-01-26 01:45:30 +00:00
|
|
|
f.write(f"server-port={port}")
|
2020-09-10 15:01:57 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Unable to create required server files due to :{e}")
|
2020-09-10 15:01:57 +00:00
|
|
|
return False
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2022-03-23 06:06:13 +00:00
|
|
|
server_command = (
|
2022-04-11 05:23:55 +00:00
|
|
|
f"java -Xms{Helpers.float_to_string(min_mem)}M "
|
|
|
|
f"-Xmx{Helpers.float_to_string(max_mem)}M "
|
2022-03-23 06:06:13 +00:00
|
|
|
f'-jar "{full_jar_path}" nogui'
|
|
|
|
)
|
2022-01-19 22:08:29 +00:00
|
|
|
else:
|
2022-03-23 06:06:13 +00:00
|
|
|
server_command = (
|
2022-04-11 05:23:55 +00:00
|
|
|
f"java -Xms{Helpers.float_to_string(min_mem)}M "
|
|
|
|
f"-Xmx{Helpers.float_to_string(max_mem)}M "
|
2022-03-23 06:06:13 +00:00
|
|
|
f"-jar {full_jar_path} nogui"
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
server_log_file = f"{server_dir}/logs/latest.log"
|
2020-09-10 15:01:57 +00:00
|
|
|
server_stop = "stop"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
new_id = self.register_server(
|
|
|
|
name,
|
|
|
|
server_id,
|
|
|
|
server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_file,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
|
|
|
port,
|
|
|
|
server_type="minecraft-java",
|
|
|
|
)
|
2022-03-04 00:36:36 +00:00
|
|
|
|
|
|
|
# download the jar
|
2022-04-11 05:23:55 +00:00
|
|
|
self.server_jars.download_jar(server, version, full_jar_path, new_id)
|
2022-03-04 00:36:36 +00:00
|
|
|
|
2020-09-10 16:03:18 +00:00
|
|
|
return new_id
|
2020-09-10 15:01:57 +00:00
|
|
|
|
2020-09-14 20:01:38 +00:00
|
|
|
@staticmethod
|
2022-03-23 02:50:12 +00:00
|
|
|
def verify_jar_server(server_path: str, server_jar: str):
|
2022-04-11 05:23:55 +00:00
|
|
|
server_path = Helpers.get_os_understandable_path(server_path)
|
|
|
|
path_check = Helpers.check_path_exists(server_path)
|
|
|
|
jar_check = Helpers.check_file_exists(os.path.join(server_path, server_jar))
|
2020-09-14 20:01:38 +00:00
|
|
|
if not path_check or not jar_check:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-12-12 17:54:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def verify_zip_server(zip_path: str):
|
2022-04-11 05:23:55 +00:00
|
|
|
zip_path = Helpers.get_os_understandable_path(zip_path)
|
|
|
|
zip_check = Helpers.check_file_exists(zip_path)
|
2020-12-12 17:54:10 +00:00
|
|
|
if not zip_check:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def import_jar_server(
|
|
|
|
self,
|
|
|
|
server_name: str,
|
|
|
|
server_path: str,
|
|
|
|
server_jar: str,
|
|
|
|
min_mem: int,
|
|
|
|
max_mem: int,
|
|
|
|
port: int,
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
server_id = Helpers.create_uuid()
|
|
|
|
new_server_dir = os.path.join(Helpers.servers_dir, server_id)
|
|
|
|
backup_path = os.path.join(Helpers.backup_path, server_id)
|
|
|
|
if Helpers.is_os_windows():
|
|
|
|
new_server_dir = Helpers.wtol_path(new_server_dir)
|
|
|
|
backup_path = Helpers.wtol_path(backup_path)
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server_dir.replace(" ", "^ ")
|
|
|
|
backup_path.replace(" ", "^ ")
|
2020-09-14 20:01:38 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.ensure_dir_exists(new_server_dir)
|
|
|
|
Helpers.ensure_dir_exists(backup_path)
|
|
|
|
server_path = Helpers.get_os_understandable_path(server_path)
|
2022-03-01 23:31:38 +00:00
|
|
|
try:
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.copy_dir(server_path, new_server_dir, True)
|
2022-03-01 23:31:38 +00:00
|
|
|
except shutil.Error as ex:
|
|
|
|
logger.error(f"Server import failed with error: {ex}")
|
2020-09-14 20:01:38 +00:00
|
|
|
|
2021-11-23 03:26:06 +00:00
|
|
|
has_properties = False
|
|
|
|
for item in os.listdir(new_server_dir):
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(item) == "server.properties":
|
2021-11-23 03:26:06 +00:00
|
|
|
has_properties = True
|
|
|
|
if not has_properties:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as f:
|
2022-01-26 01:45:30 +00:00
|
|
|
f.write(f"server-port={port}")
|
2021-11-23 03:26:06 +00:00
|
|
|
f.close()
|
|
|
|
|
2020-09-14 20:01:38 +00:00
|
|
|
full_jar_path = os.path.join(new_server_dir, server_jar)
|
2022-01-15 06:21:04 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2022-03-23 06:06:13 +00:00
|
|
|
server_command = (
|
2022-04-11 05:23:55 +00:00
|
|
|
f"java -Xms{Helpers.float_to_string(min_mem)}M "
|
|
|
|
f"-Xmx{Helpers.float_to_string(max_mem)}M "
|
2022-03-23 06:06:13 +00:00
|
|
|
f'-jar "{full_jar_path}" nogui'
|
|
|
|
)
|
2022-01-19 22:08:29 +00:00
|
|
|
else:
|
2022-03-23 06:06:13 +00:00
|
|
|
server_command = (
|
2022-04-11 05:23:55 +00:00
|
|
|
f"java -Xms{Helpers.float_to_string(min_mem)}M "
|
|
|
|
f"-Xmx{Helpers.float_to_string(max_mem)}M "
|
2022-03-23 06:06:13 +00:00
|
|
|
f"-jar {full_jar_path} nogui"
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
server_log_file = f"{new_server_dir}/logs/latest.log"
|
2020-09-14 20:01:38 +00:00
|
|
|
server_stop = "stop"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
new_id = self.register_server(
|
|
|
|
server_name,
|
|
|
|
server_id,
|
|
|
|
new_server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_jar,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
|
|
|
port,
|
|
|
|
server_type="minecraft-java",
|
|
|
|
)
|
2020-09-14 20:01:38 +00:00
|
|
|
return new_id
|
2020-09-10 15:01:57 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def import_zip_server(
|
|
|
|
self,
|
|
|
|
server_name: str,
|
|
|
|
zip_path: str,
|
|
|
|
server_jar: str,
|
|
|
|
min_mem: int,
|
|
|
|
max_mem: int,
|
|
|
|
port: int,
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
server_id = Helpers.create_uuid()
|
|
|
|
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
|
|
|
|
backup_path = os.path.join(self.helper.backup_path, server_id)
|
|
|
|
if Helpers.is_os_windows():
|
|
|
|
new_server_dir = Helpers.wtol_path(new_server_dir)
|
|
|
|
backup_path = Helpers.wtol_path(backup_path)
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server_dir.replace(" ", "^ ")
|
|
|
|
backup_path.replace(" ", "^ ")
|
2022-01-15 06:30:33 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
tempDir = Helpers.get_os_understandable_path(zip_path)
|
|
|
|
Helpers.ensure_dir_exists(new_server_dir)
|
|
|
|
Helpers.ensure_dir_exists(backup_path)
|
2022-01-10 03:49:03 +00:00
|
|
|
has_properties = False
|
2022-03-23 02:50:12 +00:00
|
|
|
# extracts archive to temp directory
|
2022-01-10 03:49:03 +00:00
|
|
|
for item in os.listdir(tempDir):
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(item) == "server.properties":
|
2022-01-10 03:49:03 +00:00
|
|
|
has_properties = True
|
|
|
|
try:
|
2022-03-01 21:54:06 +00:00
|
|
|
if not os.path.isdir(os.path.join(tempDir, item)):
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.move_file(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(tempDir, item), os.path.join(new_server_dir, item)
|
|
|
|
)
|
2022-03-01 21:54:06 +00:00
|
|
|
else:
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.move_dir(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(tempDir, item), os.path.join(new_server_dir, item)
|
|
|
|
)
|
2022-01-10 03:49:03 +00:00
|
|
|
except Exception as ex:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
2022-01-10 03:49:03 +00:00
|
|
|
if not has_properties:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as f:
|
2022-01-26 01:45:30 +00:00
|
|
|
f.write(f"server-port={port}")
|
2022-01-10 03:49:03 +00:00
|
|
|
f.close()
|
2020-12-12 17:54:10 +00:00
|
|
|
|
|
|
|
full_jar_path = os.path.join(new_server_dir, server_jar)
|
2022-01-15 05:58:17 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2022-03-23 06:06:13 +00:00
|
|
|
server_command = (
|
2022-04-11 05:23:55 +00:00
|
|
|
f"java -Xms{Helpers.float_to_string(min_mem)}M "
|
|
|
|
f"-Xmx{Helpers.float_to_string(max_mem)}M "
|
2022-03-23 06:06:13 +00:00
|
|
|
f'-jar "{full_jar_path}" nogui'
|
|
|
|
)
|
2022-01-19 22:08:29 +00:00
|
|
|
else:
|
2022-03-23 06:06:13 +00:00
|
|
|
server_command = (
|
2022-04-11 05:23:55 +00:00
|
|
|
f"java -Xms{Helpers.float_to_string(min_mem)}M "
|
|
|
|
f"-Xmx{Helpers.float_to_string(max_mem)}M "
|
2022-03-23 06:06:13 +00:00
|
|
|
f"-jar {full_jar_path} nogui"
|
|
|
|
)
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug("command: " + server_command)
|
2022-01-26 01:45:30 +00:00
|
|
|
server_log_file = f"{new_server_dir}/logs/latest.log"
|
2020-12-12 17:54:10 +00:00
|
|
|
server_stop = "stop"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
new_id = self.register_server(
|
|
|
|
server_name,
|
|
|
|
server_id,
|
|
|
|
new_server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_jar,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
|
|
|
port,
|
|
|
|
server_type="minecraft-java",
|
|
|
|
)
|
2020-12-12 17:54:10 +00:00
|
|
|
return new_id
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-02-10 23:20:36 +00:00
|
|
|
# BEDROCK IMPORTS
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-02-10 20:27:40 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def import_bedrock_server(
|
|
|
|
self, server_name: str, server_path: str, server_exe: str, port: int
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
server_id = Helpers.create_uuid()
|
|
|
|
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
|
|
|
|
backup_path = os.path.join(self.helper.backup_path, server_id)
|
|
|
|
if Helpers.is_os_windows():
|
|
|
|
new_server_dir = Helpers.wtol_path(new_server_dir)
|
|
|
|
backup_path = Helpers.wtol_path(backup_path)
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server_dir.replace(" ", "^ ")
|
|
|
|
backup_path.replace(" ", "^ ")
|
2022-02-10 20:27:40 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.ensure_dir_exists(new_server_dir)
|
|
|
|
Helpers.ensure_dir_exists(backup_path)
|
|
|
|
server_path = Helpers.get_os_understandable_path(server_path)
|
2022-03-01 23:31:38 +00:00
|
|
|
try:
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.copy_dir(server_path, new_server_dir, True)
|
2022-03-01 23:31:38 +00:00
|
|
|
except shutil.Error as ex:
|
|
|
|
logger.error(f"Server import failed with error: {ex}")
|
2022-02-10 20:27:40 +00:00
|
|
|
|
|
|
|
has_properties = False
|
|
|
|
for item in os.listdir(new_server_dir):
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(item) == "server.properties":
|
2022-02-10 20:27:40 +00:00
|
|
|
has_properties = True
|
|
|
|
if not has_properties:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as f:
|
2022-02-10 20:27:40 +00:00
|
|
|
f.write(f"server-port={port}")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
full_jar_path = os.path.join(new_server_dir, server_exe)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2022-02-10 20:27:40 +00:00
|
|
|
server_command = f'"{full_jar_path}"'
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
server_command = f"./{server_exe}"
|
|
|
|
logger.debug("command: " + server_command)
|
2022-02-10 20:27:40 +00:00
|
|
|
server_log_file = "N/A"
|
|
|
|
server_stop = "stop"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
new_id = self.register_server(
|
|
|
|
server_name,
|
|
|
|
server_id,
|
|
|
|
new_server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_exe,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
|
|
|
port,
|
|
|
|
server_type="minecraft-bedrock",
|
|
|
|
)
|
2022-03-01 23:31:38 +00:00
|
|
|
if os.name != "nt":
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.check_file_exists(full_jar_path):
|
2022-03-02 16:58:48 +00:00
|
|
|
os.chmod(full_jar_path, 0o2775)
|
2022-02-10 20:27:40 +00:00
|
|
|
return new_id
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def import_bedrock_zip_server(
|
|
|
|
self, server_name: str, zip_path: str, server_exe: str, port: int
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
server_id = Helpers.create_uuid()
|
|
|
|
new_server_dir = os.path.join(self.helper.servers_dir, server_id)
|
|
|
|
backup_path = os.path.join(self.helper.backup_path, server_id)
|
|
|
|
if Helpers.is_os_windows():
|
|
|
|
new_server_dir = Helpers.wtol_path(new_server_dir)
|
|
|
|
backup_path = Helpers.wtol_path(backup_path)
|
2022-03-23 02:50:12 +00:00
|
|
|
new_server_dir.replace(" ", "^ ")
|
|
|
|
backup_path.replace(" ", "^ ")
|
2022-02-10 20:27:40 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
tempDir = Helpers.get_os_understandable_path(zip_path)
|
|
|
|
Helpers.ensure_dir_exists(new_server_dir)
|
|
|
|
Helpers.ensure_dir_exists(backup_path)
|
2022-02-10 20:27:40 +00:00
|
|
|
has_properties = False
|
2022-03-23 02:50:12 +00:00
|
|
|
# extracts archive to temp directory
|
2022-02-10 20:27:40 +00:00
|
|
|
for item in os.listdir(tempDir):
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(item) == "server.properties":
|
2022-02-10 20:27:40 +00:00
|
|
|
has_properties = True
|
|
|
|
try:
|
2022-03-01 21:54:06 +00:00
|
|
|
if not os.path.isdir(os.path.join(tempDir, item)):
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.move_file(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(tempDir, item), os.path.join(new_server_dir, item)
|
|
|
|
)
|
2022-03-01 21:54:06 +00:00
|
|
|
else:
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.move_dir(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(tempDir, item), os.path.join(new_server_dir, item)
|
|
|
|
)
|
2022-02-10 20:27:40 +00:00
|
|
|
except Exception as ex:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
2022-02-10 20:27:40 +00:00
|
|
|
if not has_properties:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"No server.properties found on zip file import. "
|
|
|
|
f"Creating one with port selection of {str(port)}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
with open(
|
|
|
|
os.path.join(new_server_dir, "server.properties"), "w", encoding="utf-8"
|
|
|
|
) as f:
|
2022-02-10 20:27:40 +00:00
|
|
|
f.write(f"server-port={port}")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
full_jar_path = os.path.join(new_server_dir, server_exe)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2022-02-10 20:27:40 +00:00
|
|
|
server_command = f'"{full_jar_path}"'
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
server_command = f"./{server_exe}"
|
|
|
|
logger.debug("command: " + server_command)
|
2022-02-10 20:27:40 +00:00
|
|
|
server_log_file = "N/A"
|
|
|
|
server_stop = "stop"
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
new_id = self.register_server(
|
|
|
|
server_name,
|
|
|
|
server_id,
|
|
|
|
new_server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_exe,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
|
|
|
port,
|
|
|
|
server_type="minecraft-bedrock",
|
|
|
|
)
|
2022-03-01 23:31:38 +00:00
|
|
|
if os.name != "nt":
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.check_file_exists(full_jar_path):
|
2022-03-02 16:58:48 +00:00
|
|
|
os.chmod(full_jar_path, 0o2775)
|
2022-03-01 23:31:38 +00:00
|
|
|
|
2022-02-10 20:27:40 +00:00
|
|
|
return new_id
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-02-10 23:20:36 +00:00
|
|
|
# BEDROCK IMPORTS END
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-02-10 20:27:40 +00:00
|
|
|
|
2021-11-29 21:22:46 +00:00
|
|
|
def rename_backup_dir(self, old_server_id, new_server_id, new_uuid):
|
|
|
|
server_data = self.servers.get_server_data_by_id(old_server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
old_bu_path = server_data["backup_path"]
|
2021-11-29 21:22:46 +00:00
|
|
|
Server_Perms_Controller.backup_role_swap(old_server_id, new_server_id)
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.is_os_windows():
|
2022-04-11 10:08:36 +00:00
|
|
|
backup_path = Helpers.validate_traversal(
|
|
|
|
self.helper.backup_path, old_bu_path
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
|
|
|
backup_path = Helpers.validate_traversal(
|
2022-04-11 10:08:36 +00:00
|
|
|
Helpers.wtol_path(self.helper.backup_path),
|
|
|
|
Helpers.wtol_path(old_bu_path),
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
backup_path = Helpers.wtol_path(str(backup_path))
|
2022-03-23 02:50:12 +00:00
|
|
|
backup_path.replace(" ", "^ ")
|
2022-03-08 02:36:23 +00:00
|
|
|
backup_path = Path(backup_path)
|
2021-11-29 21:22:46 +00:00
|
|
|
backup_path_components = list(backup_path.parts)
|
|
|
|
backup_path_components[-1] = new_uuid
|
|
|
|
new_bu_path = pathlib.PurePath(os.path.join(*backup_path_components))
|
2021-11-30 00:09:43 +00:00
|
|
|
if os.path.isdir(new_bu_path):
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.validate_traversal(self.helper.backup_path, new_bu_path):
|
2021-11-30 00:13:21 +00:00
|
|
|
os.rmdir(new_bu_path)
|
2021-11-29 21:22:46 +00:00
|
|
|
backup_path.rename(new_bu_path)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def register_server(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
server_uuid: str,
|
|
|
|
server_dir: str,
|
|
|
|
backup_path: str,
|
|
|
|
server_command: str,
|
|
|
|
server_file: str,
|
|
|
|
server_log_file: str,
|
|
|
|
server_stop: str,
|
|
|
|
server_port: int,
|
|
|
|
server_type: str,
|
|
|
|
):
|
2020-09-10 15:01:57 +00:00
|
|
|
# put data in the db
|
2022-01-15 06:47:31 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
new_id = self.servers.create_server(
|
2022-03-23 02:50:12 +00:00
|
|
|
name,
|
|
|
|
server_uuid,
|
|
|
|
server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_file,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
|
|
|
server_type,
|
|
|
|
server_port,
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-04-11 10:08:36 +00:00
|
|
|
if not Helpers.check_file_exists(
|
|
|
|
os.path.join(server_dir, "crafty_managed.txt")
|
|
|
|
):
|
2021-11-29 21:22:46 +00:00
|
|
|
try:
|
|
|
|
# place a file in the dir saying it's owned by crafty
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(
|
|
|
|
os.path.join(server_dir, "crafty_managed.txt"),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as f:
|
2021-11-29 21:22:46 +00:00
|
|
|
f.write(
|
2022-03-23 06:06:13 +00:00
|
|
|
"The server is managed by Crafty Controller.\n "
|
|
|
|
"Leave this directory/files alone please"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-11-29 21:22:46 +00:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
except Exception as e:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Unable to create required server files due to :{e}")
|
2021-11-29 21:22:46 +00:00
|
|
|
return False
|
2020-09-10 15:01:57 +00:00
|
|
|
|
|
|
|
# let's re-init all servers
|
|
|
|
self.init_all_servers()
|
|
|
|
|
|
|
|
return new_id
|
|
|
|
|
2021-08-10 00:48:22 +00:00
|
|
|
def remove_server(self, server_id, files):
|
2020-09-14 18:05:17 +00:00
|
|
|
counter = 0
|
|
|
|
for s in self.servers_list:
|
|
|
|
|
|
|
|
# if this is the droid... im mean server we are looking for...
|
2022-03-23 02:50:12 +00:00
|
|
|
if str(s["server_id"]) == str(server_id):
|
2020-09-14 18:05:17 +00:00
|
|
|
server_data = self.get_server_data(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_name = server_data["server_name"]
|
2020-09-14 18:05:17 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Deleting Server: ID {server_id} | Name: {server_name} ")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Deleting Server: ID {server_id} | Name: {server_name} ")
|
2020-09-14 18:05:17 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
srv_obj = s["server_obj"]
|
2020-09-14 18:05:17 +00:00
|
|
|
running = srv_obj.check_running()
|
|
|
|
|
|
|
|
if running:
|
|
|
|
self.stop_server(server_id)
|
2021-08-10 00:48:22 +00:00
|
|
|
if files:
|
2021-11-29 21:22:46 +00:00
|
|
|
try:
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.del_dirs(
|
|
|
|
Helpers.get_os_understandable_path(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
)
|
|
|
|
)
|
2021-11-29 21:22:46 +00:00
|
|
|
except Exception as e:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Unable to delete server files for server with ID: "
|
|
|
|
f"{server_id} with error logged: {e}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.check_path_exists(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.servers.get_server_data_by_id(server_id)["backup_path"]
|
|
|
|
):
|
2022-04-11 05:23:55 +00:00
|
|
|
FileHelpers.del_dirs(
|
|
|
|
Helpers.get_os_understandable_path(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.servers.get_server_data_by_id(server_id)[
|
|
|
|
"backup_path"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Cleanup scheduled tasks
|
2021-11-29 21:22:46 +00:00
|
|
|
try:
|
|
|
|
helpers_management.delete_scheduled_task_by_server(server_id)
|
|
|
|
except DoesNotExist:
|
|
|
|
logger.info("No scheduled jobs exist. Continuing.")
|
2020-09-14 18:05:17 +00:00
|
|
|
# remove the server from the DB
|
2021-09-08 22:01:10 +00:00
|
|
|
self.servers.remove_server(server_id)
|
2020-09-14 18:05:17 +00:00
|
|
|
|
|
|
|
# remove the server from servers list
|
|
|
|
self.servers_list.pop(counter)
|
|
|
|
|
|
|
|
counter += 1
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-03-02 17:55:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def clear_unexecuted_commands():
|
2022-03-02 18:07:00 +00:00
|
|
|
helpers_management.clear_unexecuted_commands()
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-03-13 12:29:26 +00:00
|
|
|
@staticmethod
|
|
|
|
def clear_support_status():
|
|
|
|
helper_users.clear_support_status()
|