2021-09-08 22:01:10 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2022-05-26 12:50:20 +00:00
|
|
|
import time
|
2021-09-25 20:58:01 +00:00
|
|
|
import json
|
2022-04-14 12:33:53 +00:00
|
|
|
import typing as t
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.controllers.roles_controller import RolesController
|
2022-05-26 12:50:20 +00:00
|
|
|
|
2022-05-26 21:19:59 +00:00
|
|
|
from app.classes.shared.singleton import Singleton
|
2022-05-30 20:40:34 +00:00
|
|
|
from app.classes.shared.server import ServerInstance
|
2022-05-26 12:50:20 +00:00
|
|
|
from app.classes.shared.console import Console
|
|
|
|
from app.classes.shared.helpers import Helpers
|
|
|
|
from app.classes.shared.main_models import DatabaseShortcuts
|
|
|
|
|
|
|
|
from app.classes.minecraft.server_props import ServerProps
|
|
|
|
from app.classes.minecraft.stats import Stats
|
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.servers import HelperServers
|
|
|
|
from app.classes.models.users import HelperUsers, ApiKeys
|
2022-03-23 02:50:12 +00:00
|
|
|
from app.classes.models.server_permissions import (
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers,
|
|
|
|
EnumPermissionsServer,
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-05-26 12:50:20 +00:00
|
|
|
class ServersController(metaclass=Singleton):
|
2022-05-30 20:40:34 +00:00
|
|
|
servers_list: ServerInstance
|
2022-05-26 12:50:20 +00:00
|
|
|
|
|
|
|
def __init__(self, helper, servers_helper, management_helper):
|
2022-05-26 13:02:47 +00:00
|
|
|
self.helper: Helpers = helper
|
2022-05-26 12:50:20 +00:00
|
|
|
self.servers_helper: HelperServers = servers_helper
|
|
|
|
self.management_helper = management_helper
|
|
|
|
self.servers_list = []
|
|
|
|
self.stats = Stats(self.helper, self)
|
2021-09-29 18:57:04 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Generic Servers Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-01-26 01:45:30 +00:00
|
|
|
def create_server(
|
2022-04-11 05:23:55 +00:00
|
|
|
self,
|
2022-01-26 01:45:30 +00:00
|
|
|
name: str,
|
|
|
|
server_uuid: str,
|
|
|
|
server_dir: str,
|
|
|
|
backup_path: str,
|
|
|
|
server_command: str,
|
|
|
|
server_file: str,
|
|
|
|
server_log_file: str,
|
|
|
|
server_stop: str,
|
2022-02-10 20:27:40 +00:00
|
|
|
server_type: str,
|
2022-05-05 00:32:09 +00:00
|
|
|
server_port: int = 25565,
|
2022-05-05 11:02:23 +00:00
|
|
|
server_host: str = "127.0.0.1",
|
2022-05-05 00:32:09 +00:00
|
|
|
) -> int:
|
|
|
|
"""Create a server in the database
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The name of the server
|
|
|
|
server_uuid: This is the UUID of the server
|
|
|
|
server_dir: The directory where the server is located
|
|
|
|
backup_path: The path to the backup folder
|
|
|
|
server_command: The command to start the server
|
|
|
|
server_file: The name of the server file
|
|
|
|
server_log_file: The path to the server log file
|
|
|
|
server_stop: This is the command to stop the server
|
|
|
|
server_type: This is the type of server you're creating.
|
2022-05-05 11:02:23 +00:00
|
|
|
server_port: The port the server will be monitored on, defaults to 25565
|
|
|
|
server_host: The host the server will be monitored on, defaults to 127.0.0.1
|
2022-05-05 00:32:09 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: The new server's id
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
PeeweeException: If the server already exists
|
|
|
|
"""
|
|
|
|
return HelperServers.create_server(
|
2022-01-26 01:45:30 +00:00
|
|
|
name,
|
|
|
|
server_uuid,
|
|
|
|
server_dir,
|
|
|
|
backup_path,
|
|
|
|
server_command,
|
|
|
|
server_file,
|
|
|
|
server_log_file,
|
|
|
|
server_stop,
|
2022-02-10 20:27:40 +00:00
|
|
|
server_type,
|
2022-03-23 02:50:12 +00:00
|
|
|
server_port,
|
2022-05-05 11:02:23 +00:00
|
|
|
server_host,
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2021-11-21 10:52:29 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_obj(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperServers.get_server_obj(server_id)
|
2021-11-21 10:52:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def update_server(server_obj):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperServers.update_server(server_obj)
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-03-04 00:36:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_download(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 13:20:21 +00:00
|
|
|
return srv.stats_helper.set_download()
|
2022-03-04 00:36:36 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def finish_download(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 13:20:21 +00:00
|
|
|
return srv.stats_helper.finish_download()
|
2022-03-04 00:36:36 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_download_status(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
server = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return server.stats_helper.get_download_status()
|
2022-03-04 00:36:36 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def remove_server(self, server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
roles_list = PermissionsServers.get_roles_from_server(server_id)
|
2021-09-29 18:57:04 +00:00
|
|
|
for role in roles_list:
|
|
|
|
role_id = role.role_id
|
2022-04-14 02:10:25 +00:00
|
|
|
role_data = RolesController.get_role_with_servers(role_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
role_data["servers"] = {server_id}
|
2022-04-14 02:10:25 +00:00
|
|
|
PermissionsServers.delete_roles_permissions(role_id, role_data["servers"])
|
|
|
|
PermissionsServers.remove_roles_of_server(server_id)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.servers_helper.remove_server(server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_data_by_id(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperServers.get_server_data_by_id(server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-05-26 12:50:20 +00:00
|
|
|
|
2022-05-30 20:40:34 +00:00
|
|
|
def get_server_instance_by_id(self, server_id: t.Union[str, int]) -> ServerInstance:
|
2022-05-26 12:50:20 +00:00
|
|
|
for server in self.servers_list:
|
2022-05-30 17:28:39 +00:00
|
|
|
if int(server["server_id"]) == int(server_id):
|
2022-05-26 12:50:20 +00:00
|
|
|
return server["server_obj"]
|
2022-05-30 17:28:39 +00:00
|
|
|
|
|
|
|
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}")
|
2022-05-26 12:50:20 +00:00
|
|
|
|
|
|
|
def init_all_servers(self):
|
|
|
|
|
|
|
|
servers = self.get_all_defined_servers()
|
|
|
|
|
|
|
|
for server in servers:
|
|
|
|
server_id = server.get("server_id")
|
|
|
|
|
|
|
|
# if we have already initialized this server, let's skip it.
|
|
|
|
if self.check_server_loaded(server_id):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# if this server path no longer exists - let's warn and bomb out
|
|
|
|
if not Helpers.check_path_exists(
|
|
|
|
Helpers.get_os_understandable_path(server["path"])
|
|
|
|
):
|
|
|
|
logger.warning(
|
|
|
|
f"Unable to find server "
|
|
|
|
f"{server['server_name']} at path {server['path']}. "
|
|
|
|
f"Skipping this server"
|
|
|
|
)
|
|
|
|
|
|
|
|
Console.warning(
|
|
|
|
f"Unable to find server "
|
|
|
|
f"{server['server_name']} at path {server['path']}. "
|
|
|
|
f"Skipping this server"
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
|
|
|
settings_file = os.path.join(
|
|
|
|
Helpers.get_os_understandable_path(server["path"]), "server.properties"
|
|
|
|
)
|
|
|
|
|
|
|
|
# if the properties file isn't there, let's warn
|
|
|
|
if not Helpers.check_file_exists(settings_file):
|
|
|
|
logger.error(f"Unable to find {settings_file}. Skipping this server.")
|
|
|
|
Console.error(f"Unable to find {settings_file}. Skipping this server.")
|
|
|
|
continue
|
|
|
|
|
|
|
|
settings = ServerProps(settings_file)
|
|
|
|
|
|
|
|
temp_server_dict = {
|
|
|
|
"server_id": server.get("server_id"),
|
|
|
|
"server_data_obj": server,
|
2022-05-30 20:40:34 +00:00
|
|
|
"server_obj": ServerInstance(
|
2022-05-26 12:50:20 +00:00
|
|
|
server.get("server_id"),
|
|
|
|
self.helper,
|
|
|
|
self.management_helper,
|
|
|
|
self.stats,
|
|
|
|
),
|
|
|
|
"server_settings": settings.props,
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup the server, do the auto start and all that jazz
|
|
|
|
temp_server_dict["server_obj"].do_server_setup(server)
|
|
|
|
|
|
|
|
# add this temp object to the list of init servers
|
|
|
|
self.servers_list.append(temp_server_dict)
|
|
|
|
|
|
|
|
if server["auto_start"]:
|
2022-05-30 05:36:25 +00:00
|
|
|
self.set_waiting_start(server["server_id"], True)
|
2022-05-26 12:50:20 +00:00
|
|
|
|
|
|
|
self.refresh_server_settings(server["server_id"])
|
|
|
|
|
|
|
|
Console.info(
|
|
|
|
f"Loaded Server: ID {server['server_id']}"
|
|
|
|
f" | Name: {server['server_name']}"
|
|
|
|
f" | Autostart: {server['auto_start']}"
|
|
|
|
f" | Delay: {server['auto_start_delay']}"
|
|
|
|
)
|
|
|
|
|
|
|
|
def check_server_loaded(self, server_id_to_check: int):
|
|
|
|
|
|
|
|
logger.info(f"Checking to see if we already registered {server_id_to_check}")
|
|
|
|
|
|
|
|
for server in self.servers_list:
|
|
|
|
known_server = server.get("server_id")
|
|
|
|
if known_server is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if known_server == server_id_to_check:
|
|
|
|
logger.info(
|
|
|
|
f"skipping initialization of server {server_id_to_check} "
|
|
|
|
f"because it is already loaded"
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def refresh_server_settings(self, server_id: int):
|
2022-05-30 20:05:36 +00:00
|
|
|
server_obj = self.get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
server_obj.reload_server_settings()
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_defined_servers():
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperServers.get_all_defined_servers()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_authorized_servers(user_id):
|
2022-04-14 12:33:53 +00:00
|
|
|
server_data: t.List[t.Dict[str, t.Any]] = []
|
2022-04-14 02:10:25 +00:00
|
|
|
user_roles = HelperUsers.user_role_query(user_id)
|
|
|
|
for user in user_roles:
|
|
|
|
role_servers = PermissionsServers.get_role_servers_from_role_id(
|
|
|
|
user.role_id
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
for role in role_servers:
|
2022-05-26 21:19:59 +00:00
|
|
|
server_data.append(
|
|
|
|
ServersController().get_server_instance_by_id(
|
|
|
|
role.server_id.server_id
|
|
|
|
)
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
return server_data
|
|
|
|
|
2022-04-14 12:33:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_authorized_users(server_id: str):
|
|
|
|
user_ids: t.Set[int] = set()
|
2022-05-05 00:32:09 +00:00
|
|
|
roles_list = PermissionsServers.get_roles_from_server(server_id)
|
2022-04-14 12:33:53 +00:00
|
|
|
for role in roles_list:
|
2022-05-05 00:32:09 +00:00
|
|
|
role_users = HelperUsers.get_users_from_role(role.role_id)
|
2022-04-14 12:33:53 +00:00
|
|
|
for user_role in role_users:
|
|
|
|
user_ids.add(user_role.user_id)
|
|
|
|
|
2022-05-05 00:32:09 +00:00
|
|
|
for user_id in HelperUsers.get_super_user_list():
|
2022-04-14 12:33:53 +00:00
|
|
|
user_ids.add(user_id)
|
|
|
|
|
|
|
|
return user_ids
|
|
|
|
|
2022-05-26 12:50:20 +00:00
|
|
|
def get_all_servers_stats(self):
|
|
|
|
server_data = []
|
|
|
|
try:
|
|
|
|
for server in self.servers_list:
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(
|
2022-05-26 12:50:20 +00:00
|
|
|
server.get("server_id")
|
|
|
|
)
|
|
|
|
latest = srv.stats_helper.get_latest_server_stats()
|
|
|
|
server_data.append(
|
|
|
|
{
|
2022-06-11 21:34:31 +00:00
|
|
|
"server_data": ServersController.get_server_data_by_id(
|
|
|
|
server.get("server_id")
|
|
|
|
),
|
2022-05-26 12:50:20 +00:00
|
|
|
"stats": latest,
|
|
|
|
"user_command_permission": True,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
except IndexError as ex:
|
|
|
|
logger.error(
|
|
|
|
f"Stats collection failed with error: {ex}. Was a server just created?"
|
|
|
|
)
|
|
|
|
return server_data
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_authorized_servers_stats_api_key(api_key: ApiKeys):
|
|
|
|
server_data = []
|
2022-05-30 20:05:36 +00:00
|
|
|
authorized_servers = ServersController().get_authorized_servers(
|
2022-05-20 15:07:12 +00:00
|
|
|
api_key.user_id # TODO: API key authorized servers?
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for server in authorized_servers:
|
2022-05-30 20:40:34 +00:00
|
|
|
srv: ServerInstance = server
|
2022-05-26 12:50:20 +00:00
|
|
|
latest = srv.stats_helper.get_latest_server_stats()
|
2022-04-14 02:10:25 +00:00
|
|
|
key_permissions = PermissionsServers.get_api_key_permissions_list(
|
2022-05-26 21:19:59 +00:00
|
|
|
api_key, server.server_id
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
if EnumPermissionsServer.COMMANDS in key_permissions:
|
2022-01-15 00:23:50 +00:00
|
|
|
user_command_permission = True
|
|
|
|
else:
|
|
|
|
user_command_permission = False
|
2022-03-23 02:50:12 +00:00
|
|
|
server_data.append(
|
|
|
|
{
|
2022-05-26 21:19:59 +00:00
|
|
|
"server_data": DatabaseShortcuts.get_data_obj(server.server_object),
|
2022-05-26 13:43:24 +00:00
|
|
|
"stats": latest,
|
2022-03-23 02:50:12 +00:00
|
|
|
"user_command_permission": user_command_permission,
|
|
|
|
}
|
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
return server_data
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_authorized_servers_stats(user_id):
|
|
|
|
server_data = []
|
2022-04-14 02:10:25 +00:00
|
|
|
authorized_servers = ServersController.get_authorized_servers(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
for server in authorized_servers:
|
2022-05-30 20:40:34 +00:00
|
|
|
srv: ServerInstance = server
|
2022-05-26 12:50:20 +00:00
|
|
|
latest = srv.stats_helper.get_latest_server_stats()
|
2022-01-15 00:23:50 +00:00
|
|
|
# TODO
|
2022-04-14 02:10:25 +00:00
|
|
|
user_permissions = PermissionsServers.get_user_id_permissions_list(
|
2022-05-26 21:19:59 +00:00
|
|
|
user_id, server.server_id
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
if EnumPermissionsServer.COMMANDS in user_permissions:
|
2021-09-08 22:01:10 +00:00
|
|
|
user_command_permission = True
|
|
|
|
else:
|
|
|
|
user_command_permission = False
|
2022-03-23 02:50:12 +00:00
|
|
|
server_data.append(
|
|
|
|
{
|
2022-06-11 21:34:31 +00:00
|
|
|
"server_data": ServersController.get_server_data_by_id(
|
|
|
|
server.server_id
|
|
|
|
),
|
2022-05-26 10:49:51 +00:00
|
|
|
"stats": latest,
|
2022-03-23 02:50:12 +00:00
|
|
|
"user_command_permission": user_command_permission,
|
|
|
|
}
|
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
return server_data
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_friendly_name(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperServers.get_server_friendly_name(server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-05-26 12:50:20 +00:00
|
|
|
def get_server_settings(self, server_id):
|
|
|
|
for server in self.servers_list:
|
|
|
|
if int(server["server_id"]) == int(server_id):
|
|
|
|
return server["server_settings"]
|
|
|
|
|
|
|
|
logger.warning(f"Unable to find server object for server id {server_id}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
def crash_detection(self, server_obj):
|
2022-06-01 23:26:57 +00:00
|
|
|
svr = self.get_server_instance_by_id(server_obj.server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
# start or stop crash detection depending upon user preference
|
|
|
|
# The below functions check to see if the server is running.
|
|
|
|
# They only execute if it's running.
|
|
|
|
if server_obj.crash_detection == 1:
|
|
|
|
svr.start_crash_detection()
|
|
|
|
else:
|
|
|
|
svr.stop_crash_detection()
|
|
|
|
|
|
|
|
def get_server_obj_optional(
|
|
|
|
self, server_id: t.Union[str, int]
|
2022-05-30 20:40:34 +00:00
|
|
|
) -> t.Optional[ServerInstance]:
|
2022-05-26 12:50:20 +00:00
|
|
|
for server in self.servers_list:
|
|
|
|
if str(server["server_id"]) == str(server_id):
|
|
|
|
return server["server_obj"]
|
|
|
|
|
|
|
|
logger.warning(f"Unable to find server object for server id {server_id}")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_server_data(self, server_id: str):
|
|
|
|
for server in self.servers_list:
|
|
|
|
if str(server["server_id"]) == str(server_id):
|
|
|
|
return server["server_data_obj"]
|
|
|
|
|
|
|
|
logger.warning(f"Unable to find server object for server id {server_id}")
|
|
|
|
return False
|
|
|
|
|
2022-05-30 20:05:36 +00:00
|
|
|
def list_defined_servers(self):
|
|
|
|
defined_servers = []
|
|
|
|
for server in self.servers_list:
|
|
|
|
defined_servers.append(
|
|
|
|
self.get_server_instance_by_id(server.get("server_id"))
|
|
|
|
)
|
|
|
|
return defined_servers
|
2022-05-26 12:50:20 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_all_server_ids() -> t.List[int]:
|
|
|
|
return HelperServers.get_all_server_ids()
|
|
|
|
|
|
|
|
def list_running_servers(self):
|
|
|
|
running_servers = []
|
|
|
|
|
|
|
|
# for each server
|
2022-05-30 17:28:39 +00:00
|
|
|
for server in self.servers_list:
|
2022-05-26 12:50:20 +00:00
|
|
|
# is the server running?
|
2022-05-30 20:40:34 +00:00
|
|
|
srv_obj: ServerInstance = server["server_obj"]
|
2022-05-26 12:50:20 +00:00
|
|
|
running = srv_obj.check_running()
|
|
|
|
# if so, let's add a dictionary to the list of running servers
|
|
|
|
if running:
|
|
|
|
running_servers.append({"id": srv_obj.server_id, "name": srv_obj.name})
|
|
|
|
|
|
|
|
return running_servers
|
|
|
|
|
|
|
|
def stop_all_servers(self):
|
|
|
|
servers = self.list_running_servers()
|
|
|
|
logger.info(f"Found {len(servers)} running server(s)")
|
|
|
|
Console.info(f"Found {len(servers)} running server(s)")
|
|
|
|
|
|
|
|
logger.info("Stopping All Servers")
|
|
|
|
Console.info("Stopping All Servers")
|
|
|
|
|
|
|
|
for server in servers:
|
|
|
|
logger.info(f"Stopping Server ID {server['id']} - {server['name']}")
|
|
|
|
Console.info(f"Stopping Server ID {server['id']} - {server['name']}")
|
|
|
|
|
|
|
|
self.stop_server(server["id"])
|
|
|
|
|
|
|
|
# let's wait 2 seconds to let everything flush out
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
logger.info("All Servers Stopped")
|
|
|
|
Console.info("All Servers Stopped")
|
|
|
|
|
|
|
|
def stop_server(self, server_id):
|
|
|
|
# issue the stop command
|
2022-05-30 17:28:39 +00:00
|
|
|
svr_obj = self.get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
svr_obj.stop_threaded_server()
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers_Stats Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_stats_by_id(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.get_latest_server_stats()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def server_id_exists(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.server_id_exists()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-02-26 17:29:07 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_type_by_id(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelperServers.get_server_type_by_id(server_id)
|
2022-02-26 17:29:07 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-01-15 00:23:50 +00:00
|
|
|
def server_id_authorized(server_id_a, user_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
user_roles = HelperUsers.user_role_query(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
for role in user_roles:
|
2022-04-14 02:10:25 +00:00
|
|
|
for server_id_b in PermissionsServers.get_role_servers_from_role_id(
|
2022-03-23 02:50:12 +00:00
|
|
|
role.role_id
|
|
|
|
):
|
2022-01-19 21:22:22 +00:00
|
|
|
if str(server_id_a) == str(server_id_b.server_id):
|
2022-01-15 00:23:50 +00:00
|
|
|
return True
|
|
|
|
return False
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-28 01:43:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_crashed(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.is_crashed()
|
2022-01-28 01:43:23 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool:
|
|
|
|
# TODO
|
2022-04-14 02:10:25 +00:00
|
|
|
return ServersController.server_id_authorized(server_id, api_key.user.user_id)
|
2022-01-15 00:23:50 +00:00
|
|
|
# There is no view server permission
|
|
|
|
# permission_helper.both_have_perm(api_key)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-27 19:07:34 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_update(server_id, value):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.set_update(value)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-04-14 02:10:25 +00:00
|
|
|
def get_ttl_without_player(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.get_ttl_without_player()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def can_stop_no_players(server_id, time_limit):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.can_stop_no_players(time_limit)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-27 19:07:34 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_waiting_start(server_id, value):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
srv.stats_helper.set_waiting_start(value)
|
2021-09-21 19:13:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_waiting_start(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.get_waiting_start()
|
2021-09-29 18:57:04 +00:00
|
|
|
|
2022-01-10 19:58:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_update_status(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
return srv.stats_helper.get_update_status()
|
2022-01-10 19:58:38 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers Helpers Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_banned_players(server_id):
|
2022-05-30 20:40:34 +00:00
|
|
|
srv = ServersController().get_server_instance_by_id(server_id)
|
2022-05-26 12:50:20 +00:00
|
|
|
stats = srv.stats_helper.get_server_stats()
|
2022-03-23 02:50:12 +00:00
|
|
|
server_path = stats["server_id"]["path"]
|
|
|
|
path = os.path.join(server_path, "banned-players.json")
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.get_os_understandable_path(path), encoding="utf-8"
|
2022-03-23 02:50:12 +00:00
|
|
|
) as file:
|
2021-09-08 22:01:10 +00:00
|
|
|
content = file.read()
|
|
|
|
file.close()
|
|
|
|
except Exception as ex:
|
2022-03-23 02:50:12 +00:00
|
|
|
print(ex)
|
2021-09-08 22:01:10 +00:00
|
|
|
return None
|
2021-09-29 18:57:04 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
return json.loads(content)
|
2021-09-29 18:57:04 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
def check_for_old_logs(self):
|
2022-04-14 02:10:25 +00:00
|
|
|
servers = HelperServers.get_all_defined_servers()
|
2021-09-08 22:01:10 +00:00
|
|
|
for server in servers:
|
2022-03-23 02:50:12 +00:00
|
|
|
logs_path = os.path.split(server["log_path"])[0]
|
|
|
|
latest_log_file = os.path.split(server["log_path"])[1]
|
|
|
|
logs_delete_after = int(server["logs_delete_after"])
|
2021-09-08 22:01:10 +00:00
|
|
|
if logs_delete_after == 0:
|
|
|
|
continue
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
log_files = list(
|
|
|
|
filter(lambda val: val != latest_log_file, os.listdir(logs_path))
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
for log_file in log_files:
|
|
|
|
log_file_path = os.path.join(logs_path, log_file)
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.check_file_exists(
|
2022-03-23 02:50:12 +00:00
|
|
|
log_file_path
|
2022-04-11 05:23:55 +00:00
|
|
|
) and Helpers.is_file_older_than_x_days(
|
2022-03-23 02:50:12 +00:00
|
|
|
log_file_path, logs_delete_after
|
|
|
|
):
|
2021-09-08 22:01:10 +00:00
|
|
|
os.remove(log_file_path)
|