Merge branch 'dev-StatsDBImprovement' into 'dev'

Dev stats db improvement

See merge request crafty-controller/crafty-4!281
This commit is contained in:
Andrew 2022-06-01 22:29:34 +00:00
commit 74597a406b
21 changed files with 562 additions and 468 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
from app.classes.controllers.servers_controller import ServersController
from app.classes.models.server_permissions import ( from app.classes.models.server_permissions import (
PermissionsServers, PermissionsServers,
@ -7,7 +8,6 @@ from app.classes.models.server_permissions import (
from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.users import HelperUsers, ApiKeys
from app.classes.models.roles import HelperRoles from app.classes.models.roles import HelperRoles
from app.classes.models.servers import HelperServers from app.classes.models.servers import HelperServers
from app.classes.models.server_stats import HelperServerStats
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -106,7 +106,8 @@ class ServerPermsController:
) )
for server in authorized_servers: for server in authorized_servers:
latest = HelperServerStats.get_server_stats_by_id(server.get("server_id")) srv = ServersController().get_server_instance_by_id(server.get("server_id"))
latest = srv.stats_helper.get_latest_server_stats()
server_data.append( server_data.append(
{ {
"server_data": server, "server_data": server,

View File

@ -1,24 +1,39 @@
import os import os
import logging import logging
import time
import json import json
import typing as t import typing as t
from app.classes.controllers.roles_controller import RolesController from app.classes.controllers.roles_controller import RolesController
from app.classes.shared.singleton import Singleton
from app.classes.shared.server import ServerInstance
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
from app.classes.models.servers import HelperServers from app.classes.models.servers import HelperServers
from app.classes.models.server_stats import HelperServerStats
from app.classes.models.users import HelperUsers, ApiKeys from app.classes.models.users import HelperUsers, ApiKeys
from app.classes.models.server_permissions import ( from app.classes.models.server_permissions import (
PermissionsServers, PermissionsServers,
EnumPermissionsServer, EnumPermissionsServer,
) )
from app.classes.shared.helpers import Helpers
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class ServersController: class ServersController(metaclass=Singleton):
def __init__(self, servers_helper): servers_list: ServerInstance
self.servers_helper = servers_helper
def __init__(self, helper, servers_helper, management_helper):
self.helper: Helpers = helper
self.servers_helper: HelperServers = servers_helper
self.management_helper = management_helper
self.servers_list = []
self.stats = Stats(self.helper, self)
# ********************************************************************************** # **********************************************************************************
# Generic Servers Methods # Generic Servers Methods
@ -82,15 +97,18 @@ class ServersController:
@staticmethod @staticmethod
def set_download(server_id): def set_download(server_id):
return HelperServerStats.set_download(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.set_download()
@staticmethod @staticmethod
def finish_download(server_id): def finish_download(server_id):
return HelperServerStats.finish_download(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.finish_download()
@staticmethod @staticmethod
def get_download_status(server_id): def get_download_status(server_id):
return HelperServerStats.get_download_status(server_id) server = ServersController().get_server_instance_by_id(server_id)
return server.stats_helper.get_download_status()
def remove_server(self, server_id): def remove_server(self, server_id):
roles_list = PermissionsServers.get_roles_from_server(server_id) roles_list = PermissionsServers.get_roles_from_server(server_id)
@ -109,6 +127,107 @@ class ServersController:
# ********************************************************************************** # **********************************************************************************
# Servers Methods # Servers Methods
# ********************************************************************************** # **********************************************************************************
def get_server_instance_by_id(self, server_id: t.Union[str, int]) -> ServerInstance:
for server in self.servers_list:
if int(server["server_id"]) == int(server_id):
return server["server_obj"]
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 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,
"server_obj": ServerInstance(
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"]:
self.set_waiting_start(server["server_id"], True)
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):
server_obj = self.get_server_instance_by_id(server_id)
server_obj.reload_server_settings()
@staticmethod @staticmethod
def get_all_defined_servers(): def get_all_defined_servers():
return HelperServers.get_all_defined_servers() return HelperServers.get_all_defined_servers()
@ -122,7 +241,11 @@ class ServersController:
user.role_id user.role_id
) )
for role in role_servers: for role in role_servers:
server_data.append(HelperServers.get_server_data_by_id(role.server_id)) server_data.append(
ServersController().get_server_instance_by_id(
role.server_id.server_id
)
)
return server_data return server_data
@ -140,21 +263,39 @@ class ServersController:
return user_ids return user_ids
@staticmethod def get_all_servers_stats(self):
def get_all_servers_stats(): server_data = []
return HelperServerStats.get_all_servers_stats() try:
for server in self.servers_list:
srv = ServersController().get_server_instance_by_id(
server.get("server_id")
)
latest = srv.stats_helper.get_latest_server_stats()
server_data.append(
{
"server_data": server["server_data_obj"],
"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
@staticmethod @staticmethod
def get_authorized_servers_stats_api_key(api_key: ApiKeys): def get_authorized_servers_stats_api_key(api_key: ApiKeys):
server_data = [] server_data = []
authorized_servers = ServersController.get_authorized_servers( authorized_servers = ServersController().get_authorized_servers(
api_key.user_id # TODO: API key authorized servers? api_key.user_id # TODO: API key authorized servers?
) )
for server in authorized_servers: for server in authorized_servers:
latest = HelperServerStats.get_server_stats_by_id(server.get("server_id")) srv: ServerInstance = server
latest = srv.stats_helper.get_latest_server_stats()
key_permissions = PermissionsServers.get_api_key_permissions_list( key_permissions = PermissionsServers.get_api_key_permissions_list(
api_key, server.get("server_id") api_key, server.server_id
) )
if EnumPermissionsServer.COMMANDS in key_permissions: if EnumPermissionsServer.COMMANDS in key_permissions:
user_command_permission = True user_command_permission = True
@ -162,7 +303,7 @@ class ServersController:
user_command_permission = False user_command_permission = False
server_data.append( server_data.append(
{ {
"server_data": server, "server_data": DatabaseShortcuts.get_data_obj(server.server_object),
"stats": latest, "stats": latest,
"user_command_permission": user_command_permission, "user_command_permission": user_command_permission,
} }
@ -175,11 +316,11 @@ class ServersController:
authorized_servers = ServersController.get_authorized_servers(user_id) authorized_servers = ServersController.get_authorized_servers(user_id)
for server in authorized_servers: for server in authorized_servers:
latest = HelperServerStats.get_server_stats_by_id(server.get("server_id")) srv: ServerInstance = server
latest = srv.stats_helper.get_latest_server_stats()
# TODO # TODO
user_permissions = PermissionsServers.get_user_id_permissions_list( user_permissions = PermissionsServers.get_user_id_permissions_list(
user_id, server.get("server_id") user_id, server.server_id
) )
if EnumPermissionsServer.COMMANDS in user_permissions: if EnumPermissionsServer.COMMANDS in user_permissions:
user_command_permission = True user_command_permission = True
@ -187,7 +328,7 @@ class ServersController:
user_command_permission = False user_command_permission = False
server_data.append( server_data.append(
{ {
"server_data": server, "server_data": DatabaseShortcuts.get_data_obj(server.server_object),
"stats": latest, "stats": latest,
"user_command_permission": user_command_permission, "user_command_permission": user_command_permission,
} }
@ -199,16 +340,105 @@ class ServersController:
def get_server_friendly_name(server_id): def get_server_friendly_name(server_id):
return HelperServers.get_server_friendly_name(server_id) return HelperServers.get_server_friendly_name(server_id)
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):
svr = self.get_server_obj(server_obj.server_id)
# 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]
) -> t.Optional[ServerInstance]:
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
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
@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
for server in self.servers_list:
# is the server running?
srv_obj: ServerInstance = server["server_obj"]
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
svr_obj = self.get_server_instance_by_id(server_id)
svr_obj.stop_threaded_server()
# ********************************************************************************** # **********************************************************************************
# Servers_Stats Methods # Servers_Stats Methods
# ********************************************************************************** # **********************************************************************************
@staticmethod @staticmethod
def get_server_stats_by_id(server_id): def get_server_stats_by_id(server_id):
return HelperServerStats.get_server_stats_by_id(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.get_latest_server_stats()
@staticmethod @staticmethod
def server_id_exists(server_id): def server_id_exists(server_id):
return HelperServerStats.server_id_exists(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.server_id_exists()
@staticmethod @staticmethod
def get_server_type_by_id(server_id): def get_server_type_by_id(server_id):
@ -227,7 +457,8 @@ class ServersController:
@staticmethod @staticmethod
def is_crashed(server_id): def is_crashed(server_id):
return HelperServerStats.is_crashed(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.is_crashed()
@staticmethod @staticmethod
def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool: def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool:
@ -238,34 +469,41 @@ class ServersController:
@staticmethod @staticmethod
def set_update(server_id, value): def set_update(server_id, value):
return HelperServerStats.set_update(server_id, value) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.set_update(value)
@staticmethod @staticmethod
def get_ttl_without_player(server_id): def get_ttl_without_player(server_id):
return HelperServerStats.get_ttl_without_player(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.get_ttl_without_player()
@staticmethod @staticmethod
def can_stop_no_players(server_id, time_limit): def can_stop_no_players(server_id, time_limit):
return HelperServerStats.can_stop_no_players(server_id, time_limit) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.can_stop_no_players(time_limit)
@staticmethod @staticmethod
def set_waiting_start(server_id, value): def set_waiting_start(server_id, value):
HelperServerStats.set_waiting_start(server_id, value) srv = ServersController().get_server_instance_by_id(server_id)
srv.stats_helper.set_waiting_start(value)
@staticmethod @staticmethod
def get_waiting_start(server_id): def get_waiting_start(server_id):
return HelperServerStats.get_waiting_start(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.get_waiting_start()
@staticmethod @staticmethod
def get_update_status(server_id): def get_update_status(server_id):
return HelperServerStats.get_update_status(server_id) srv = ServersController().get_server_instance_by_id(server_id)
return srv.stats_helper.get_update_status()
# ********************************************************************************** # **********************************************************************************
# Servers Helpers Methods # Servers Helpers Methods
# ********************************************************************************** # **********************************************************************************
@staticmethod @staticmethod
def get_banned_players(server_id): def get_banned_players(server_id):
stats = HelperServerStats.get_server_stats_by_id(server_id) srv = ServersController().get_server_instance_by_id(server_id)
stats = srv.stats_helper.get_server_stats()
server_path = stats["server_id"]["path"] server_path = stats["server_id"]["path"]
path = os.path.join(server_path, "banned-players.json") path = os.path.join(server_path, "banned-players.json")

View File

@ -1,12 +1,10 @@
import os import os
import logging import logging
import datetime import datetime
import typing as t
from playhouse.shortcuts import model_to_dict
from app.classes.models.servers import Servers, HelperServers from app.classes.models.servers import Servers, HelperServers
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
from app.classes.shared.main_models import DatabaseShortcuts
from app.classes.shared.migration import MigrationManager from app.classes.shared.migration import MigrationManager
try: try:
@ -30,7 +28,6 @@ logger = logging.getLogger(__name__)
peewee_logger = logging.getLogger("peewee") peewee_logger = logging.getLogger("peewee")
peewee_logger.setLevel(logging.INFO) peewee_logger.setLevel(logging.INFO)
# ********************************************************************************** # **********************************************************************************
# Servers Stats Class # Servers Stats Class
# ********************************************************************************** # **********************************************************************************
@ -66,11 +63,14 @@ class ServerStats(Model):
# Servers_Stats Methods # Servers_Stats Methods
# ********************************************************************************** # **********************************************************************************
class HelperServerStats: class HelperServerStats:
def __init__(self, database): server_id: int
self.database = database database = None
@staticmethod def __init__(self, server_id):
def init_database(server_id) -> t.Optional[SqliteDatabase]: self.server_id = int(server_id)
self.init_database(self.server_id)
def init_database(self, server_id):
try: try:
server = HelperServers.get_server_data_by_id(server_id) server = HelperServers.get_server_data_by_id(server_id)
db_folder = os.path.join(f"{server['path']}", "db_stats") db_folder = os.path.join(f"{server['path']}", "db_stats")
@ -78,7 +78,7 @@ class HelperServerStats:
db_folder, db_folder,
"crafty_server_stats.sqlite", "crafty_server_stats.sqlite",
) )
database = SqliteDatabase( self.database = SqliteDatabase(
db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
) )
if not os.path.exists(db_file): if not os.path.exists(db_file):
@ -93,46 +93,41 @@ class HelperServerStats:
f"{helper_stats.migration_dir}", "stats" f"{helper_stats.migration_dir}", "stats"
) )
helper_stats.db_path = db_file helper_stats.db_path = db_file
migration_manager = MigrationManager(database, helper_stats) migration_manager = MigrationManager(self.database, helper_stats)
migration_manager.up() # Automatically runs migrations migration_manager.up() # Automatically runs migrations
return database
except Exception as ex: except Exception as ex:
logger.warning( logger.warning(
f"Error try to look for the db_stats files for server : {ex}" f"Error try to look for the db_stats files for server : {ex}"
) )
return None return None
@staticmethod def select_database(self):
def select_database(server_id) -> t.Optional[SqliteDatabase]:
try: try:
server = HelperServers.get_server_data_by_id(server_id) server = HelperServers.get_server_data_by_id(self.server_id)
db_file = os.path.join( db_file = os.path.join(
f"{server['path']}", f"{server['path']}",
"db_stats", "db_stats",
"crafty_server_stats.sqlite", "crafty_server_stats.sqlite",
) )
database = SqliteDatabase( self.database = SqliteDatabase(
db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} db_file, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
) )
return database
except Exception as ex: except Exception as ex:
logger.warning( logger.warning(
f"Error try to look for the db_stats files for server : {ex}" f"Error try to look for the db_stats files for server : {ex}"
) )
return None return None
@staticmethod def get_all_servers_stats(self):
def get_all_servers_stats():
servers = HelperServers.get_all_defined_servers() servers = HelperServers.get_all_defined_servers()
server_data = [] server_data = []
try: try:
for server in servers: for server in servers:
stats = HelperServerStats.get_server_stats_by_id(server["server_id"]) latest = self.get_latest_server_stats()
server_data.append( server_data.append(
{ {
"server_data": server, "server_data": server,
"stats": stats, "stats": latest,
"user_command_permission": True, "user_command_permission": True,
} }
) )
@ -142,10 +137,8 @@ class HelperServerStats:
) )
return server_data return server_data
@staticmethod def insert_server_stats(self, server_stats):
def insert_server_stats(server_stats):
server_id = server_stats.get("id", 0) server_id = server_stats.get("id", 0)
database = HelperServerStats.select_database(server_id)
if server_id == 0: if server_id == 0:
logger.warning("Stats saving failed with error: Server unknown (id = 0)") logger.warning("Stats saving failed with error: Server unknown (id = 0)")
@ -171,186 +164,177 @@ class HelperServerStats:
ServerStats.desc: server_stats.get("desc", False), ServerStats.desc: server_stats.get("desc", False),
ServerStats.version: server_stats.get("version", False), ServerStats.version: server_stats.get("version", False),
} }
).execute(database) ).execute(self.database)
@staticmethod def remove_old_stats(self, last_week):
def remove_old_stats(server_id, minimum_to_exist): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id) ServerStats.delete().where(ServerStats.created < last_week).execute(
ServerStats.delete().where(ServerStats.created < minimum_to_exist).execute( self.database
database
) )
@staticmethod def get_latest_server_stats(self):
def get_latest_server_stats(server_id): latest = (
database = HelperServerStats.select_database(server_id)
return (
ServerStats.select() ServerStats.select()
.where(ServerStats.server_id == server_id) .where(ServerStats.server_id == self.server_id)
.order_by(ServerStats.created.desc()) .order_by(ServerStats.created.desc())
.limit(1) .limit(1)
.execute(database) .get(self.database)
) )
try:
return DatabaseShortcuts.get_data_obj(latest)
except IndexError:
return {}
@staticmethod def get_server_stats(self):
def get_server_stats_by_id(server_id):
database = HelperServerStats.select_database(server_id)
stats = ( stats = (
ServerStats.select() ServerStats.select()
.where(ServerStats.server_id == server_id) .where(ServerStats.server_id == self.server_id)
.order_by(ServerStats.created.desc()) .order_by(ServerStats.created.desc())
.limit(1) .limit(1)
.first(database) .first(self.database)
) )
return model_to_dict(stats) return DatabaseShortcuts.get_data_obj(stats)
@staticmethod def server_id_exists(self):
def server_id_exists(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id) if not HelperServers.get_server_data_by_id(self.server_id):
# We can't use .exists because it doesn't seem to use the database parameter return False
return ( return True
ServerStats.select()
.where(ServerStats.server_id == server_id)
.count(database)
!= 0
)
@staticmethod def sever_crashed(self):
def sever_crashed(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
ServerStats.update(crashed=True).where( ServerStats.update(crashed=True).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def set_download(self):
def set_download(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
ServerStats.update(downloading=True).where( ServerStats.update(downloading=True).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def finish_download(self):
def finish_download(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
ServerStats.update(downloading=False).where( ServerStats.update(downloading=False).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def get_download_status(self):
def get_download_status(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
download_status = ( download_status = (
ServerStats.select().where(ServerStats.server_id == server_id).get(database) ServerStats.select()
.where(ServerStats.server_id == self.server_id)
.get(self.database)
) )
return download_status.downloading return download_status.downloading
@staticmethod def server_crash_reset(self):
def server_crash_reset(server_id): if self.server_id is None:
if server_id is None:
return return
database = HelperServerStats.select_database(server_id) # self.select_database(self.server_id)
ServerStats.update(crashed=False).where( ServerStats.update(crashed=False).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def is_crashed(self):
def is_crashed(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id) svr: ServerStats = (
svr = ( ServerStats.select()
ServerStats.select().where(ServerStats.server_id == server_id).get(database) .where(ServerStats.server_id == self.server_id)
.get(self.database)
) )
return svr.crashed return svr.crashed
@staticmethod def set_update(self, value):
def set_update(server_id, value): if self.server_id is None:
if server_id is None:
return return
database = HelperServerStats.select_database(server_id) # self.select_database(self.server_id)
try: try:
# Checks if server even exists # Checks if server even exists
ServerStats.select().where(ServerStats.server_id == server_id).execute( ServerStats.select().where(ServerStats.server_id == self.server_id).execute(
database self.database
) )
except DoesNotExist as ex: except DoesNotExist as ex:
logger.error(f"Database entry not found! {ex}") logger.error(f"Database entry not found! {ex}")
return return
ServerStats.update(updating=value).where( ServerStats.update(updating=value).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def get_update_status(self):
def get_update_status(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
update_status = ( update_status = (
ServerStats.select().where(ServerStats.server_id == server_id).get(database) ServerStats.select()
.where(ServerStats.server_id == self.server_id)
.get(self.database)
) )
return update_status.updating return update_status.updating
@staticmethod def set_first_run(self):
def set_first_run(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
# Sets first run to false # Sets first run to false
try: try:
# Checks if server even exists # Checks if server even exists
ServerStats.select().where(ServerStats.server_id == server_id) ServerStats.select().where(ServerStats.server_id == self.server_id).execute(
except DoesNotExist as ex: self.database
)
except Exception as ex:
logger.error(f"Database entry not found! {ex}") logger.error(f"Database entry not found! {ex}")
return return
ServerStats.update(first_run=False).where( ServerStats.update(first_run=False).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def get_first_run(self):
def get_first_run(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
first_run = ( first_run = (
ServerStats.select().where(ServerStats.server_id == server_id).get(database) ServerStats.select()
.where(ServerStats.server_id == self.server_id)
.get(self.database)
) )
return first_run.first_run return first_run.first_run
@staticmethod def get_ttl_without_player(self):
def get_ttl_without_player(server_id): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
last_stat = ( last_stat = (
ServerStats.select() ServerStats.select()
.where(ServerStats.server_id == server_id) .where(ServerStats.server_id == self.server_id)
.order_by(ServerStats.created.desc()) .order_by(ServerStats.created.desc())
.first(database) .first(self.database)
) )
last_stat_with_player = ( last_stat_with_player = (
ServerStats.select() ServerStats.select()
.where(ServerStats.server_id == server_id) .where(ServerStats.server_id == self.server_id)
.where(ServerStats.online > 0) .where(ServerStats.online > 0)
.order_by(ServerStats.created.desc()) .order_by(ServerStats.created.desc())
.first(database) .first(self.database)
) )
return last_stat.created - last_stat_with_player.created return last_stat.created - last_stat_with_player.created
@staticmethod def can_stop_no_players(self, time_limit):
def can_stop_no_players(server_id, time_limit): ttl_no_players = self.get_ttl_without_player()
ttl_no_players = HelperServerStats.get_ttl_without_player(server_id)
return (time_limit == -1) or (ttl_no_players > time_limit) return (time_limit == -1) or (ttl_no_players > time_limit)
@staticmethod def set_waiting_start(self, value):
def set_waiting_start(server_id, value): # self.select_database(self.server_id)
database = HelperServerStats.select_database(server_id)
try: try:
# Checks if server even exists # Checks if server even exists
ServerStats.select().where(ServerStats.server_id == server_id).execute( ServerStats.select().where(ServerStats.server_id == self.server_id).execute(
database self.database
) )
except DoesNotExist as ex: except DoesNotExist as ex:
logger.error(f"Database entry not found! {ex}") logger.error(f"Database entry not found! {ex}")
return return
ServerStats.update(waiting_start=value).where( ServerStats.update(waiting_start=value).where(
ServerStats.server_id == server_id ServerStats.server_id == self.server_id
).execute(database) ).execute(self.database)
@staticmethod def get_waiting_start(self):
def get_waiting_start(server_id):
database = HelperServerStats.select_database(server_id)
waiting_start = ( waiting_start = (
ServerStats.select().where(ServerStats.server_id == server_id).get(database) ServerStats.select()
.where(ServerStats.server_id == self.server_id)
.get(self.database)
) )
return waiting_start.waiting_start return waiting_start.waiting_start

View File

@ -16,7 +16,7 @@ from app.classes.models.base_model import BaseModel
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# ********************************************************************************** # **********************************************************************************
# Servers Class # Servers Model
# ********************************************************************************** # **********************************************************************************
class Servers(BaseModel): class Servers(BaseModel):
server_id = AutoField() server_id = AutoField()

View File

@ -5,19 +5,23 @@ import threading
import logging import logging
import getpass import getpass
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.import3 import Import3 from app.classes.shared.import3 import Import3
from app.classes.shared.helpers import Helpers
from app.classes.shared.tasks import TasksManager
from app.classes.shared.migration import MigrationManager
from app.classes.shared.main_controller import Controller
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class MainPrompt(cmd.Cmd): class MainPrompt(cmd.Cmd):
def __init__(self, helper, tasks_manager, migration_manager, main_controller): def __init__(self, helper, tasks_manager, migration_manager, main_controller):
super().__init__() super().__init__()
self.helper = helper self.helper: Helpers = helper
self.tasks_manager = tasks_manager self.tasks_manager: TasksManager = tasks_manager
self.migration_manager = migration_manager self.migration_manager: MigrationManager = migration_manager
self.controller = main_controller self.controller: Controller = main_controller
# overrides the default Prompt # overrides the default Prompt
self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > " self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > "

View File

@ -5,7 +5,6 @@ import shutil
import time import time
import logging import logging
import tempfile import tempfile
import typing as t
from peewee import DoesNotExist from peewee import DoesNotExist
# TZLocal is set as a hidden import on win pipeline # TZLocal is set as a hidden import on win pipeline
@ -26,125 +25,43 @@ from app.classes.models.servers import HelperServers
from app.classes.shared.authentication import Authentication from app.classes.shared.authentication import Authentication
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
from app.classes.shared.server import Server
from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.file_helpers import FileHelpers
from app.classes.minecraft.server_props import ServerProps
from app.classes.minecraft.serverjars import ServerJars from app.classes.minecraft.serverjars import ServerJars
from app.classes.minecraft.stats import Stats
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class Controller: class Controller:
def __init__(self, database, helper): def __init__(self, database, helper):
self.helper = helper self.helper: Helpers = helper
self.server_jars = ServerJars(helper) self.server_jars: ServerJars = ServerJars(helper)
self.users_helper = HelperUsers(database, self.helper) self.users_helper: HelperUsers = HelperUsers(database, self.helper)
self.roles_helper = HelperRoles(database) self.roles_helper: HelperRoles = HelperRoles(database)
self.servers_helper = HelperServers(database) self.servers_helper: HelperServers = HelperServers(database)
self.management_helper = HelpersManagement(database, self.helper) self.management_helper: HelpersManagement = HelpersManagement(
self.authentication = Authentication(self.helper) database, self.helper
self.servers_list = [] )
self.stats = Stats(self.helper, self) self.authentication: Authentication = Authentication(self.helper)
self.crafty_perms = CraftyPermsController() self.crafty_perms: CraftyPermsController = CraftyPermsController()
self.management = ManagementController(self.management_helper) self.management: ManagementController = ManagementController(
self.roles = RolesController(self.users_helper, self.roles_helper) self.management_helper
self.server_perms = ServerPermsController() )
self.servers = ServersController(self.servers_helper) self.roles: RolesController = RolesController(
self.users = UsersController( self.users_helper, self.roles_helper
)
self.server_perms: ServerPermsController = ServerPermsController()
self.servers: ServersController = ServersController(
self.helper, self.servers_helper, self.management_helper
)
self.users: UsersController = UsersController(
self.helper, self.users_helper, self.authentication self.helper, self.users_helper, self.authentication
) )
tz = get_localzone() tz = get_localzone()
self.support_scheduler = BackgroundScheduler(timezone=str(tz)) self.support_scheduler: BackgroundScheduler = BackgroundScheduler(
timezone=str(tz)
)
self.support_scheduler.start() self.support_scheduler.start()
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 init_all_servers(self):
servers = self.servers.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,
"server_obj": Server(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"]:
self.servers.set_waiting_start(server["server_id"], True)
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 refresh_server_settings(self, server_id: int):
server_obj = self.get_server_obj(server_id)
server_obj.reload_server_settings()
@staticmethod @staticmethod
def check_system_user(): def check_system_user():
return HelperUsers.get_user_id_by_name("system") is not None return HelperUsers.get_user_id_by_name("system") is not None
@ -240,24 +157,6 @@ class Controller:
False, False,
) )
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):
svr = self.get_server_obj(server_obj.server_id)
# 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 log_status(self, source_path, dest_path, exec_user): def log_status(self, source_path, dest_path, exec_user):
results = Helpers.calc_percent(source_path, dest_path) results = Helpers.calc_percent(source_path, dest_path)
self.log_stats = results self.log_stats = results
@ -273,81 +172,6 @@ class Controller:
except: except:
return {"percent": 0, "total_files": 0} return {"percent": 0, "total_files": 0}
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"]
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: 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"]
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
@staticmethod
def list_defined_servers():
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 = []
# for each server
for servers in self.servers_list:
# is the server running?
srv_obj = servers["server_obj"]
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
svr_obj = self.get_server_obj(server_id)
svr_obj.stop_threaded_server()
def create_api_server(self, data: dict): def create_api_server(self, data: dict):
server_fs_uuid = Helpers.create_uuid() server_fs_uuid = Helpers.create_uuid()
new_server_path = os.path.join(self.helper.servers_dir, server_fs_uuid) new_server_path = os.path.join(self.helper.servers_dir, server_fs_uuid)
@ -986,17 +810,17 @@ class Controller:
return False return False
# let's re-init all servers # let's re-init all servers
self.init_all_servers() self.servers.init_all_servers()
return new_id return new_id
def remove_server(self, server_id, files): def remove_server(self, server_id, files):
counter = 0 counter = 0
for server in self.servers_list: for server in self.servers.servers_list:
# if this is the droid... im mean server we are looking for... # if this is the droid... im mean server we are looking for...
if str(server["server_id"]) == str(server_id): if str(server["server_id"]) == str(server_id):
server_data = self.get_server_data(server_id) server_data = self.servers.get_server_data(server_id)
server_name = server_data["server_name"] server_name = server_data["server_name"]
logger.info(f"Deleting Server: ID {server_id} | Name: {server_name} ") logger.info(f"Deleting Server: ID {server_id} | Name: {server_name} ")
@ -1007,7 +831,7 @@ class Controller:
running = srv_obj.check_running() running = srv_obj.check_running()
if running: if running:
self.stop_server(server_id) self.servers.stop_server(server_id)
if files: if files:
try: try:
FileHelpers.del_dirs( FileHelpers.del_dirs(
@ -1040,7 +864,7 @@ class Controller:
self.servers.remove_server(server_id) self.servers.remove_server(server_id)
# remove the server from servers list # remove the server from servers list
self.servers_list.pop(counter) self.servers.servers_list.pop(counter)
counter += 1 counter += 1

View File

@ -58,3 +58,7 @@ class DatabaseShortcuts:
def return_db_rows(model): def return_db_rows(model):
data = [model_to_dict(row) for row in model] data = [model_to_dict(row) for row in model]
return data return data
@staticmethod
def get_data_obj(obj):
return model_to_dict(obj)

View File

@ -16,7 +16,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
from app.classes.minecraft.stats import Stats from app.classes.minecraft.stats import Stats
from app.classes.minecraft.mc_ping import ping, ping_bedrock from app.classes.minecraft.mc_ping import ping, ping_bedrock
from app.classes.models.servers import HelperServers from app.classes.models.servers import HelperServers, Servers
from app.classes.models.server_stats import HelperServerStats from app.classes.models.server_stats import HelperServerStats
from app.classes.models.management import HelpersManagement from app.classes.models.management import HelpersManagement
from app.classes.models.users import HelperUsers from app.classes.models.users import HelperUsers
@ -96,8 +96,14 @@ class ServerOutBuf:
# ********************************************************************************** # **********************************************************************************
# Minecraft Server Class # Minecraft Server Class
# ********************************************************************************** # **********************************************************************************
class Server: class ServerInstance:
def __init__(self, helper, management_helper, stats): server_object: Servers
helper: Helpers
management_helper: HelpersManagement
stats: Stats
stats_helper: HelperServerStats
def __init__(self, server_id, helper, management_helper, stats):
self.helper = helper self.helper = helper
self.management_helper = management_helper self.management_helper = management_helper
# holders for our process # holders for our process
@ -109,12 +115,14 @@ class Server:
self.server_thread = None self.server_thread = None
self.settings = None self.settings = None
self.updating = False self.updating = False
self.server_id = None self.server_id = server_id
self.jar_update_url = None self.jar_update_url = None
self.name = None self.name = None
self.is_crashed = False self.is_crashed = False
self.restart_count = 0 self.restart_count = 0
self.stats = stats self.stats = stats
self.server_object = HelperServers.get_server_obj(self.server_id)
self.stats_helper = HelperServerStats(self.server_id)
tz = get_localzone() tz = get_localzone()
self.server_scheduler = BackgroundScheduler(timezone=str(tz)) self.server_scheduler = BackgroundScheduler(timezone=str(tz))
self.server_scheduler.start() self.server_scheduler.start()
@ -123,8 +131,8 @@ class Server:
) )
self.is_backingup = False self.is_backingup = False
# Reset crash and update at initialization # Reset crash and update at initialization
HelperServerStats.server_crash_reset(self.server_id) self.stats_helper.server_crash_reset()
HelperServerStats.set_update(self.server_id, False) self.stats_helper.set_update(False)
# ********************************************************************************** # **********************************************************************************
# Minecraft Server Management # Minecraft Server Management
@ -147,7 +155,7 @@ class Server:
self.name = server_name self.name = server_name
self.settings = server_data_obj self.settings = server_data_obj
HelperServerStats.init_database(server_id) self.stats_helper.init_database(server_id)
self.record_server_stats() self.record_server_stats()
# build our server run command # build our server run command
@ -169,7 +177,7 @@ class Server:
Console.info(f"Starting server ID: {self.server_id} - {self.name}") Console.info(f"Starting server ID: {self.server_id} - {self.name}")
logger.info(f"Starting server ID: {self.server_id} - {self.name}") logger.info(f"Starting server ID: {self.server_id} - {self.name}")
# Sets waiting start to false since we're attempting to start the server. # Sets waiting start to false since we're attempting to start the server.
HelperServerStats.set_waiting_start(self.server_id, False) self.stats_helper.set_waiting_start(False)
self.run_threaded_server(None) self.run_threaded_server(None)
# remove the scheduled job since it's ran # remove the scheduled job since it's ran
@ -236,7 +244,7 @@ class Server:
else: else:
user_lang = HelperUsers.get_user_lang_by_id(user_id) user_lang = HelperUsers.get_user_lang_by_id(user_id)
if HelperServerStats.get_download_status(self.server_id): if self.stats_helper.get_download_status():
if user_id: if user_id:
self.helper.websocket_helper.broadcast_user( self.helper.websocket_helper.broadcast_user(
user_id, user_id,
@ -409,7 +417,7 @@ class Server:
).start() ).start()
self.is_crashed = False self.is_crashed = False
HelperServerStats.server_crash_reset(self.server_id) self.stats_helper.server_crash_reset()
self.start_time = str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")) self.start_time = str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
@ -417,7 +425,7 @@ class Server:
logger.info(f"Server {self.name} running with PID {self.process.pid}") logger.info(f"Server {self.name} running with PID {self.process.pid}")
Console.info(f"Server {self.name} running with PID {self.process.pid}") Console.info(f"Server {self.name} running with PID {self.process.pid}")
self.is_crashed = False self.is_crashed = False
HelperServerStats.server_crash_reset(self.server_id) self.stats_helper.server_crash_reset()
self.record_server_stats() self.record_server_stats()
check_internet_thread = threading.Thread( check_internet_thread = threading.Thread(
target=self.check_internet_thread, target=self.check_internet_thread,
@ -430,11 +438,9 @@ class Server:
) )
check_internet_thread.start() check_internet_thread.start()
# Checks if this is the servers first run. # Checks if this is the servers first run.
if HelperServerStats.get_first_run(self.server_id): if self.stats_helper.get_first_run():
HelperServerStats.set_first_run(self.server_id) self.stats_helper.set_first_run()
loc_server_port = HelperServerStats.get_server_stats_by_id( loc_server_port = self.stats_helper.get_server_stats()["server_port"]
self.server_id
)["server_port"]
# Sends port reminder message. # Sends port reminder message.
self.helper.websocket_helper.broadcast_user( self.helper.websocket_helper.broadcast_user(
user_id, user_id,
@ -716,7 +722,7 @@ class Server:
self.server_scheduler.remove_job("stats_" + str(self.server_id)) self.server_scheduler.remove_job("stats_" + str(self.server_id))
return return
HelperServerStats.sever_crashed(self.server_id) self.stats_helper.sever_crashed()
# if we haven't tried to restart more 3 or more times # if we haven't tried to restart more 3 or more times
if self.restart_count <= 3: if self.restart_count <= 3:
@ -740,7 +746,7 @@ class Server:
self.restart_count = 0 self.restart_count = 0
self.is_crashed = True self.is_crashed = True
HelperServerStats.sever_crashed(self.server_id) self.stats_helper.sever_crashed()
# cancel the watcher task # cancel the watcher task
self.server_scheduler.remove_job("c_" + str(self.server_id)) self.server_scheduler.remove_job("c_" + str(self.server_id))
@ -969,7 +975,7 @@ class Server:
return [] return []
def jar_update(self): def jar_update(self):
HelperServerStats.set_update(self.server_id, True) self.stats_helper.set_update(True)
update_thread = threading.Thread( update_thread = threading.Thread(
target=self.a_jar_update, daemon=True, name=f"exe_update_{self.name}" target=self.a_jar_update, daemon=True, name=f"exe_update_{self.name}"
) )
@ -977,7 +983,7 @@ class Server:
def check_update(self): def check_update(self):
if HelperServerStats.get_server_stats_by_id(self.server_id)["updating"]: if self.stats_helper.get_server_stats()["updating"]:
return True return True
else: else:
return False return False
@ -1050,11 +1056,11 @@ class Server:
self.settings["executable_update_url"], current_executable self.settings["executable_update_url"], current_executable
) )
while HelperServerStats.get_server_stats_by_id(self.server_id)["updating"]: while self.stats_helper.get_server_stats()["updating"]:
if downloaded and not self.is_backingup: if downloaded and not self.is_backingup:
logger.info("Executable updated successfully. Starting Server") logger.info("Executable updated successfully. Starting Server")
HelperServerStats.set_update(self.server_id, False) self.stats_helper.set_update(False)
if len(self.helper.websocket_helper.clients) > 0: if len(self.helper.websocket_helper.clients) > 0:
# There are clients # There are clients
self.check_update() self.check_update()
@ -1452,11 +1458,11 @@ class Server:
def record_server_stats(self): def record_server_stats(self):
server_stats = self.get_servers_stats() server_stats = self.get_servers_stats()
HelperServerStats.insert_server_stats(server_stats) self.stats_helper.insert_server_stats(server_stats)
# delete old data # delete old data
max_age = self.helper.get_setting("history_max_age") max_age = self.helper.get_setting("history_max_age")
now = datetime.datetime.now() now = datetime.datetime.now()
minimum_to_exist = now - datetime.timedelta(days=max_age) minimum_to_exist = now - datetime.timedelta(days=max_age)
HelperServerStats.remove_old_stats(server_stats.get("id", 0), minimum_to_exist) self.stats_helper.remove_old_stats(minimum_to_exist)

View File

@ -0,0 +1,7 @@
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]

View File

@ -8,10 +8,12 @@ from tzlocal import get_localzone
from apscheduler.events import EVENT_JOB_EXECUTED from apscheduler.events import EVENT_JOB_EXECUTED
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger from apscheduler.triggers.cron import CronTrigger
from app.classes.controllers.users_controller import UsersController
from app.classes.models.management import HelpersManagement from app.classes.models.management import HelpersManagement
from app.classes.models.users import HelperUsers from app.classes.models.users import HelperUsers
from app.classes.shared.console import Console from app.classes.shared.console import Console
from app.classes.shared.helpers import Helpers
from app.classes.shared.main_controller import Controller from app.classes.shared.main_controller import Controller
from app.classes.web.tornado_handler import Webserver from app.classes.web.tornado_handler import Webserver
@ -36,14 +38,14 @@ class TasksManager:
controller: Controller controller: Controller
def __init__(self, helper, controller): def __init__(self, helper, controller):
self.helper = helper self.helper: Helpers = helper
self.controller = controller self.controller: Controller = controller
self.tornado = Webserver(helper, controller, self) self.tornado: Webserver = Webserver(helper, controller, self)
self.tz = get_localzone() self.tz = get_localzone()
self.scheduler = BackgroundScheduler(timezone=str(self.tz)) self.scheduler = BackgroundScheduler(timezone=str(self.tz))
self.users_controller = self.controller.users self.users_controller: UsersController = self.controller.users
self.webserver_thread = threading.Thread( self.webserver_thread = threading.Thread(
target=self.tornado.run_tornado, daemon=True, name="tornado_thread" target=self.tornado.run_tornado, daemon=True, name="tornado_thread"
@ -84,7 +86,9 @@ class TasksManager:
commands = HelpersManagement.get_unactioned_commands() commands = HelpersManagement.get_unactioned_commands()
for cmd in commands: for cmd in commands:
try: try:
svr = self.controller.get_server_obj(cmd.server_id) svr = self.controller.servers.get_server_instance_by_id(
cmd.server_id.server_id
)
except: except:
logger.error( logger.error(
"Server value requested does not exist! " "Server value requested does not exist! "
@ -131,7 +135,7 @@ class TasksManager:
def _main_graceful_exit(self): def _main_graceful_exit(self):
try: try:
os.remove(self.helper.session_file) os.remove(self.helper.session_file)
self.controller.stop_all_servers() self.controller.servers.stop_all_servers()
except: except:
logger.info("Caught error during shutdown", exc_info=True) logger.info("Caught error during shutdown", exc_info=True)
@ -517,10 +521,10 @@ class TasksManager:
) )
# one for now, # one for now,
self.controller.stats.record_stats() self.controller.servers.stats.record_stats()
# one for later # one for later
self.scheduler.add_job( self.scheduler.add_job(
self.controller.stats.record_stats, self.controller.servers.stats.record_stats,
"interval", "interval",
seconds=stats_update_frequency, seconds=stats_update_frequency,
id="stats", id="stats",

View File

@ -286,7 +286,7 @@ class AjaxHandler(BaseHandler):
logger.warning("Server ID not found in send_command ajax call") logger.warning("Server ID not found in send_command ajax call")
Console.warning("Server ID not found in send_command ajax call") Console.warning("Server ID not found in send_command ajax call")
srv_obj = self.controller.get_server_obj(server_id) srv_obj = self.controller.servers.get_server_instance_by_id(server_id)
if command == srv_obj.settings["stop_command"]: if command == srv_obj.settings["stop_command"]:
logger.info( logger.info(
@ -334,7 +334,7 @@ class AjaxHandler(BaseHandler):
logger.error("Server ID is none. Canceling backup!") logger.error("Server ID is none. Canceling backup!")
return return
server = self.controller.get_server_obj(server_id) server = self.controller.servers.get_server_instance_by_id(server_id)
self.controller.management.add_to_audit_log_raw( self.controller.management.add_to_audit_log_raw(
self.controller.users.get_user_by_id(exec_user["user_id"])["username"], self.controller.users.get_user_by_id(exec_user["user_id"])["username"],
exec_user["user_id"], exec_user["user_id"],
@ -356,7 +356,7 @@ class AjaxHandler(BaseHandler):
self.redirect("/panel/error?error=Unauthorized access to Commands") self.redirect("/panel/error?error=Unauthorized access to Commands")
return return
server_id = self.get_argument("id", None) server_id = self.get_argument("id", None)
svr = self.controller.get_server_obj(server_id) svr = self.controller.servers.get_server_instance_by_id(server_id)
try: try:
svr.kill() svr.kill()
time.sleep(5) time.sleep(5)
@ -369,7 +369,7 @@ class AjaxHandler(BaseHandler):
return return
elif page == "eula": elif page == "eula":
server_id = self.get_argument("id", None) server_id = self.get_argument("id", None)
svr = self.controller.get_server_obj(server_id) svr = self.controller.servers.get_server_instance_by_id(server_id)
svr.agree_eula(exec_user["user_id"]) svr.agree_eula(exec_user["user_id"])
elif page == "restore_backup": elif page == "restore_backup":
@ -394,7 +394,7 @@ class AjaxHandler(BaseHandler):
server_data["server_port"], server_data["server_port"],
) )
new_server_id = new_server new_server_id = new_server
new_server = self.controller.get_server_data(new_server) new_server = self.controller.servers.get_server_data(new_server)
self.controller.rename_backup_dir( self.controller.rename_backup_dir(
server_id, new_server_id, new_server["server_uuid"] server_id, new_server_id, new_server["server_uuid"]
) )
@ -412,7 +412,7 @@ class AjaxHandler(BaseHandler):
server_data["server_port"], server_data["server_port"],
) )
new_server_id = new_server new_server_id = new_server
new_server = self.controller.get_server_data(new_server) new_server = self.controller.servers.get_server_data(new_server)
self.controller.rename_backup_dir( self.controller.rename_backup_dir(
server_id, new_server_id, new_server["server_uuid"] server_id, new_server_id, new_server["server_uuid"]
) )
@ -522,7 +522,7 @@ class AjaxHandler(BaseHandler):
f"{self.controller.servers.get_server_friendly_name(server_id)}" f"{self.controller.servers.get_server_friendly_name(server_id)}"
) )
server_data = self.controller.get_server_data(server_id) server_data = self.controller.servers.get_server_data(server_id)
server_name = server_data["server_name"] server_name = server_data["server_name"]
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(
@ -546,7 +546,7 @@ class AjaxHandler(BaseHandler):
f"{self.controller.servers.get_server_friendly_name(server_id)}" f"{self.controller.servers.get_server_friendly_name(server_id)}"
) )
server_data = self.controller.get_server_data(server_id) server_data = self.controller.servers.get_server_data(server_id)
server_name = server_data["server_name"] server_name = server_data["server_name"]
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(

View File

@ -127,7 +127,7 @@ class NodeStats(ApiHandler):
return return
# Get node stats # Get node stats
node_stats = self.controller.stats.get_node_stats() node_stats = self.controller.servers.stats.get_node_stats()
self.return_response(200, {"code": node_stats["node_stats"]}) self.return_response(200, {"code": node_stats["node_stats"]})
@ -161,7 +161,7 @@ class SendCommand(ApiHandler):
command = self.get_argument("command", default=None, strip=True) command = self.get_argument("command", default=None, strip=True)
server_id = self.get_argument("id") server_id = self.get_argument("id")
if command: if command:
server = self.controller.get_server_obj(server_id) server = self.controller.servers.get_server_instance_by_id(server_id)
if server.check_running: if server.check_running:
server.send_command(command) server.send_command(command)
self.return_response(200, {"run": True}) self.return_response(200, {"run": True})
@ -198,7 +198,7 @@ class ServerBackup(ApiHandler):
self.access_denied(user) self.access_denied(user)
return return
server = self.controller.get_server_obj(server_id) server = self.controller.servers.get_server_instance_by_id(server_id)
server.backup_server() server.backup_server()
@ -232,7 +232,7 @@ class StartServer(ApiHandler):
self.access_denied("unknown") self.access_denied("unknown")
return return
server = self.controller.get_server_obj(server_id) server = self.controller.servers.get_server_instance_by_id(server_id)
if not server.check_running(): if not server.check_running():
self.controller.management.send_command( self.controller.management.send_command(
@ -270,7 +270,7 @@ class StopServer(ApiHandler):
self.access_denied(user) self.access_denied(user)
return return
server = self.controller.get_server_obj(server_id) server = self.controller.servers.get_server_instance_by_id(server_id)
if server.check_running(): if server.check_running():
self.controller.management.send_command( self.controller.management.send_command(

View File

@ -180,7 +180,7 @@ class BaseHandler(tornado.web.RequestHandler):
exec_user_role = set() exec_user_role = set()
if superuser: if superuser:
authorized_servers = self.controller.list_defined_servers() authorized_servers = self.controller.servers.get_all_defined_servers()
exec_user_role.add("Super User") exec_user_role.add("Super User")
exec_user_crafty_permissions = ( exec_user_crafty_permissions = (
self.controller.crafty_perms.list_defined_crafty_permissions() self.controller.crafty_perms.list_defined_crafty_permissions()

View File

@ -18,10 +18,12 @@ from tzlocal import get_localzone
from croniter import croniter from croniter import croniter
from app.classes.controllers.roles_controller import RolesController from app.classes.controllers.roles_controller import RolesController
from app.classes.models.servers import Servers
from app.classes.models.server_permissions import EnumPermissionsServer from app.classes.models.server_permissions import EnumPermissionsServer
from app.classes.models.crafty_permissions import EnumPermissionsCrafty from app.classes.models.crafty_permissions import EnumPermissionsCrafty
from app.classes.models.management import HelpersManagement from app.classes.models.management import HelpersManagement
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
from app.classes.shared.main_models import DatabaseShortcuts
from app.classes.web.base_handler import BaseHandler from app.classes.web.base_handler import BaseHandler
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -39,7 +41,7 @@ class PanelHandler(BaseHandler):
def get_role_servers(self) -> t.List[RolesController.RoleServerJsonType]: def get_role_servers(self) -> t.List[RolesController.RoleServerJsonType]:
servers = [] servers = []
for server in self.controller.list_defined_servers(): for server in self.controller.servers.get_all_defined_servers():
argument = self.get_argument(f"server_{server['server_id']}_access", "0") argument = self.get_argument(f"server_{server['server_id']}_access", "0")
if argument == "0": if argument == "0":
continue continue
@ -190,7 +192,7 @@ class PanelHandler(BaseHandler):
total_players = 0 total_players = 0
for server in page_data["servers"]: for server in page_data["servers"]:
total_players += len( total_players += len(
self.controller.stats.get_server_players( self.controller.servers.stats.get_server_players(
server["server_data"]["server_id"] server["server_data"]["server_id"]
) )
) )
@ -222,7 +224,7 @@ class PanelHandler(BaseHandler):
superuser = superuser and api_key.superuser superuser = superuser and api_key.superuser
if superuser: # TODO: Figure out a better solution if superuser: # TODO: Figure out a better solution
defined_servers = self.controller.list_defined_servers() defined_servers = self.controller.servers.list_defined_servers()
exec_user_role = {"Super User"} exec_user_role = {"Super User"}
exec_user_crafty_permissions = ( exec_user_crafty_permissions = (
self.controller.crafty_perms.list_defined_crafty_permissions() self.controller.crafty_perms.list_defined_crafty_permissions()
@ -254,15 +256,20 @@ class PanelHandler(BaseHandler):
for server_id in user_order[:]: for server_id in user_order[:]:
for server in defined_servers[:]: for server in defined_servers[:]:
if str(server["server_id"]) == str(server_id): if str(server.server_id) == str(server_id):
page_servers.append(server) page_servers.append(
DatabaseShortcuts.get_data_obj(server.server_object)
)
user_order.remove(server_id) user_order.remove(server_id)
defined_servers.remove(server) defined_servers.remove(server)
for server in defined_servers: for server in defined_servers:
server_ids.append(str(server["server_id"])) server_ids.append(str(server.server_id))
if server not in page_servers: if server not in page_servers:
page_servers.append(server) page_servers.append(
DatabaseShortcuts.get_data_obj(server.server_object)
)
for server_id in user_order[:]: for server_id in user_order[:]:
# remove IDs in list that user no longer has access to # remove IDs in list that user no longer has access to
if str(server_id) not in server_ids: if str(server_id) not in server_ids:
@ -284,10 +291,10 @@ class PanelHandler(BaseHandler):
}, },
"server_stats": { "server_stats": {
"total": len(defined_servers), "total": len(defined_servers),
"running": len(self.controller.list_running_servers()), "running": len(self.controller.servers.list_running_servers()),
"stopped": ( "stopped": (
len(self.controller.list_defined_servers()) len(self.controller.servers.list_defined_servers())
- len(self.controller.list_running_servers()) - len(self.controller.servers.list_running_servers())
), ),
}, },
"menu_servers": defined_servers, "menu_servers": defined_servers,
@ -397,7 +404,7 @@ class PanelHandler(BaseHandler):
"servers" "servers"
] = self.controller.servers.get_all_servers_stats() ] = self.controller.servers.get_all_servers_stats()
except IndexError: except IndexError:
self.controller.stats.record_stats() self.controller.servers.stats.record_stats()
page_data[ page_data[
"servers" "servers"
] = self.controller.servers.get_all_servers_stats() ] = self.controller.servers.get_all_servers_stats()
@ -407,7 +414,7 @@ class PanelHandler(BaseHandler):
exec_user["user_id"] exec_user["user_id"]
) )
except IndexError: except IndexError:
self.controller.stats.record_stats() self.controller.servers.stats.record_stats()
user_auth = self.controller.servers.get_authorized_servers_stats( user_auth = self.controller.servers.get_authorized_servers_stats(
exec_user["user_id"] exec_user["user_id"]
) )
@ -490,7 +497,7 @@ class PanelHandler(BaseHandler):
"schedules", "schedules",
] ]
server = self.controller.get_server_obj(server_id) server = self.controller.servers.get_server_instance_by_id(server_id)
# server_data isn't needed since the server_stats also pulls server data # server_data isn't needed since the server_stats also pulls server data
page_data["server_data"] = self.controller.servers.get_server_data_by_id( page_data["server_data"] = self.controller.servers.get_server_data_by_id(
server_id server_id
@ -508,9 +515,9 @@ class PanelHandler(BaseHandler):
except Exception as e: except Exception as e:
logger.error(f"Failed to get server waiting to start: {e}") logger.error(f"Failed to get server waiting to start: {e}")
page_data["waiting_start"] = False page_data["waiting_start"] = False
page_data["get_players"] = lambda: self.controller.stats.get_server_players( page_data[
server_id "get_players"
) ] = lambda: self.controller.servers.stats.get_server_players(server_id)
page_data["active_link"] = subpage page_data["active_link"] = subpage
page_data["permissions"] = { page_data["permissions"] = {
"Commands": EnumPermissionsServer.COMMANDS, "Commands": EnumPermissionsServer.COMMANDS,
@ -638,10 +645,14 @@ class PanelHandler(BaseHandler):
page_data[ page_data[
"exclusions" "exclusions"
] = self.controller.management.get_excluded_backup_dirs(server_id) ] = self.controller.management.get_excluded_backup_dirs(server_id)
page_data["backing_up"] = self.controller.get_server_obj( page_data[
"backing_up"
] = self.controller.servers.get_server_instance_by_id(
server_id server_id
).is_backingup ).is_backingup
page_data["backup_stats"] = self.controller.get_server_obj( page_data[
"backup_stats"
] = self.controller.servers.get_server_instance_by_id(
server_id server_id
).send_backup_status() ).send_backup_status()
# makes it so relative path is the only thing shown # makes it so relative path is the only thing shown
@ -651,7 +662,7 @@ class PanelHandler(BaseHandler):
else: else:
exclusions.append(file.replace(server_info["path"] + "/", "")) exclusions.append(file.replace(server_info["path"] + "/", ""))
page_data["exclusions"] = exclusions page_data["exclusions"] = exclusions
self.controller.refresh_server_settings(server_id) self.controller.servers.refresh_server_settings(server_id)
try: try:
page_data["backup_list"] = server.list_backups() page_data["backup_list"] = server.list_backups()
except: except:
@ -727,8 +738,8 @@ class PanelHandler(BaseHandler):
) )
servers = [] servers = []
for server in user_servers: for server in user_servers:
if server["server_name"] not in servers: if server.name not in servers:
servers.append(server["server_name"]) servers.append(server.name)
new_item = {user.user_id: servers} new_item = {user.user_id: servers}
auth_servers.update(new_item) auth_servers.update(new_item)
data = {user.user_id: user_roles_list} data = {user.user_id: user_roles_list}
@ -791,7 +802,7 @@ class PanelHandler(BaseHandler):
page_data["roles_all"] = self.controller.roles.get_all_roles() page_data["roles_all"] = self.controller.roles.get_all_roles()
page_data["servers"] = [] page_data["servers"] = []
page_data["servers_all"] = self.controller.list_defined_servers() page_data["servers_all"] = self.controller.servers.get_all_defined_servers()
page_data["role-servers"] = [] page_data["role-servers"] = []
page_data[ page_data[
"permissions_all" "permissions_all"
@ -825,9 +836,9 @@ class PanelHandler(BaseHandler):
page_data["schedules"] = HelpersManagement.get_schedules_by_server( page_data["schedules"] = HelpersManagement.get_schedules_by_server(
server_id server_id
) )
page_data["get_players"] = lambda: self.controller.stats.get_server_players( page_data[
server_id "get_players"
) ] = lambda: self.controller.servers.stats.get_server_players(server_id)
page_data["active_link"] = "schedules" page_data["active_link"] = "schedules"
page_data["permissions"] = { page_data["permissions"] = {
"Commands": EnumPermissionsServer.COMMANDS, "Commands": EnumPermissionsServer.COMMANDS,
@ -889,9 +900,9 @@ class PanelHandler(BaseHandler):
if sch_id is None: if sch_id is None:
self.redirect("/panel/error?error=Invalid Schedule ID") self.redirect("/panel/error?error=Invalid Schedule ID")
schedule = self.controller.management.get_scheduled_task_model(sch_id) schedule = self.controller.management.get_scheduled_task_model(sch_id)
page_data["get_players"] = lambda: self.controller.stats.get_server_players( page_data[
server_id "get_players"
) ] = lambda: self.controller.servers.stats.get_server_players(server_id)
page_data["active_link"] = "schedules" page_data["active_link"] = "schedules"
page_data["permissions"] = { page_data["permissions"] = {
"Commands": EnumPermissionsServer.COMMANDS, "Commands": EnumPermissionsServer.COMMANDS,
@ -963,13 +974,13 @@ class PanelHandler(BaseHandler):
role_servers = self.controller.servers.get_authorized_servers(user_id) role_servers = self.controller.servers.get_authorized_servers(user_id)
page_role_servers = [] page_role_servers = []
for server in role_servers: for server in role_servers:
page_role_servers.append(server["server_id"]) page_role_servers.append(server.server_id)
page_data["new_user"] = False page_data["new_user"] = False
page_data["user"] = self.controller.users.get_user_by_id(user_id) page_data["user"] = self.controller.users.get_user_by_id(user_id)
page_data["servers"] = set() page_data["servers"] = set()
page_data["role-servers"] = page_role_servers page_data["role-servers"] = page_role_servers
page_data["roles_all"] = self.controller.roles.get_all_roles() page_data["roles_all"] = self.controller.roles.get_all_roles()
page_data["servers_all"] = self.controller.list_defined_servers() page_data["servers_all"] = self.controller.servers.get_all_defined_servers()
page_data["superuser"] = superuser page_data["superuser"] = superuser
page_data[ page_data[
"permissions_all" "permissions_all"
@ -1097,7 +1108,7 @@ class PanelHandler(BaseHandler):
) )
return return
page_data["servers_all"] = self.controller.list_defined_servers() page_data["servers_all"] = self.controller.servers.get_all_defined_servers()
page_data[ page_data[
"permissions_all" "permissions_all"
] = self.controller.server_perms.list_defined_permissions() ] = self.controller.server_perms.list_defined_permissions()
@ -1109,7 +1120,7 @@ class PanelHandler(BaseHandler):
page_data["new_role"] = False page_data["new_role"] = False
role_id = self.get_argument("id", None) role_id = self.get_argument("id", None)
page_data["role"] = self.controller.roles.get_role_with_servers(role_id) page_data["role"] = self.controller.roles.get_role_with_servers(role_id)
page_data["servers_all"] = self.controller.list_defined_servers() page_data["servers_all"] = self.controller.servers.get_all_defined_servers()
page_data[ page_data[
"permissions_all" "permissions_all"
] = self.controller.server_perms.list_defined_permissions() ] = self.controller.server_perms.list_defined_permissions()
@ -1256,7 +1267,7 @@ class PanelHandler(BaseHandler):
"Players": EnumPermissionsServer.PLAYERS, "Players": EnumPermissionsServer.PLAYERS,
} }
if superuser: if superuser:
# defined_servers = self.controller.list_defined_servers() # defined_servers = self.controller.servers.list_defined_servers()
exec_user_role = {"Super User"} exec_user_role = {"Super User"}
exec_user_crafty_permissions = ( exec_user_crafty_permissions = (
self.controller.crafty_perms.list_defined_crafty_permissions() self.controller.crafty_perms.list_defined_crafty_permissions()
@ -1313,7 +1324,7 @@ class PanelHandler(BaseHandler):
if server_id is None: if server_id is None:
return return
server_obj = self.controller.servers.get_server_obj(server_id) server_obj: Servers = self.controller.servers.get_server_obj(server_id)
stale_executable = server_obj.executable stale_executable = server_obj.executable
# Compares old jar name to page data being passed. # Compares old jar name to page data being passed.
# If they are different we replace the executable name in the # If they are different we replace the executable name in the
@ -1351,9 +1362,9 @@ class PanelHandler(BaseHandler):
server_obj.crash_detection = crash_detection server_obj.crash_detection = crash_detection
server_obj.logs_delete_after = logs_delete_after server_obj.logs_delete_after = logs_delete_after
self.controller.servers.update_server(server_obj) self.controller.servers.update_server(server_obj)
self.controller.crash_detection(server_obj) self.controller.servers.crash_detection(server_obj)
self.controller.refresh_server_settings(server_id) self.controller.servers.refresh_server_settings(server_id)
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(
exec_user["user_id"], exec_user["user_id"],

View File

@ -104,7 +104,7 @@ class ApiRolesIndexHandler(BaseApiHandler):
server_ids = ( server_ids = (
( (
{server["server_id"] for server in data["servers"]} {server["server_id"] for server in data["servers"]}
& set(self.controller.get_all_server_ids()) & set(self.controller.servers.get_all_server_ids())
) # Only allow existing servers ) # Only allow existing servers
if "servers" in data if "servers" in data
else set() else set()

View File

@ -670,7 +670,7 @@ class ApiServersIndexHandler(BaseApiHandler):
# Increase the server creation counter # Increase the server creation counter
self.controller.crafty_perms.add_server_creation(user["user_id"]) self.controller.crafty_perms.add_server_creation(user["user_id"])
self.controller.stats.record_stats() self.controller.servers.stats.record_stats()
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(
user["user_id"], user["user_id"],

View File

@ -90,7 +90,7 @@ class ApiServersServerActionHandler(BaseApiHandler):
server_data.get("server_port"), server_data.get("server_port"),
) )
self.controller.init_all_servers() self.controller.servers.init_all_servers()
self.finish_json( self.finish_json(
200, 200,

View File

@ -1,6 +1,6 @@
import logging import logging
from app.classes.models.server_stats import HelperServerStats
from app.classes.web.base_api_handler import BaseApiHandler from app.classes.web.base_api_handler import BaseApiHandler
from app.classes.controllers.servers_controller import ServersController
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -16,10 +16,13 @@ class ApiServersServerStatsHandler(BaseApiHandler):
# if the user doesn't have access to the server, return an error # if the user doesn't have access to the server, return an error
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"}) return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
srv = ServersController().get_server_instance_by_id(server_id)
latest = srv.stats_helper.get_latest_server_stats()
self.finish_json( self.finish_json(
200, 200,
{ {
"status": "ok", "status": "ok",
"data": HelperServerStats.get_server_stats_by_id(server_id), "data": latest,
}, },
) )

View File

@ -10,6 +10,7 @@ import requests
from app.classes.models.crafty_permissions import EnumPermissionsCrafty from app.classes.models.crafty_permissions import EnumPermissionsCrafty
from app.classes.shared.helpers import Helpers from app.classes.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers from app.classes.shared.file_helpers import FileHelpers
from app.classes.shared.main_models import DatabaseShortcuts
from app.classes.web.base_handler import BaseHandler from app.classes.web.base_handler import BaseHandler
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -28,7 +29,7 @@ class ServerHandler(BaseHandler):
superuser = superuser and api_key.superuser superuser = superuser and api_key.superuser
if superuser: if superuser:
defined_servers = self.controller.list_defined_servers() defined_servers = self.controller.servers.list_defined_servers()
exec_user_role = {"Super User"} exec_user_role = {"Super User"}
exec_user_crafty_permissions = ( exec_user_crafty_permissions = (
self.controller.crafty_perms.list_defined_crafty_permissions() self.controller.crafty_perms.list_defined_crafty_permissions()
@ -52,6 +53,14 @@ class ServerHandler(BaseHandler):
exec_user_role.add(role["role_name"]) exec_user_role.add(role["role_name"])
list_roles.append(self.controller.roles.get_role(role["role_id"])) list_roles.append(self.controller.roles.get_role(role["role_id"]))
page_servers = []
for server in defined_servers:
if server not in page_servers:
page_servers.append(
DatabaseShortcuts.get_data_obj(server.server_object)
)
defined_servers = page_servers
template = "public/404.html" template = "public/404.html"
page_data = { page_data = {
@ -66,11 +75,11 @@ class ServerHandler(BaseHandler):
"Roles_Config": EnumPermissionsCrafty.ROLES_CONFIG, "Roles_Config": EnumPermissionsCrafty.ROLES_CONFIG,
}, },
"server_stats": { "server_stats": {
"total": len(self.controller.list_defined_servers()), "total": len(self.controller.servers.list_defined_servers()),
"running": len(self.controller.list_running_servers()), "running": len(self.controller.servers.list_running_servers()),
"stopped": ( "stopped": (
len(self.controller.list_defined_servers()) len(self.controller.servers.list_defined_servers())
- len(self.controller.list_running_servers()) - len(self.controller.servers.list_running_servers())
), ),
}, },
"hosts_data": self.controller.management.get_latest_hosts_stats(), "hosts_data": self.controller.management.get_latest_hosts_stats(),
@ -240,7 +249,7 @@ class ServerHandler(BaseHandler):
server_port, server_port,
) )
self.controller.init_all_servers() self.controller.servers.init_all_servers()
return return
@ -370,7 +379,7 @@ class ServerHandler(BaseHandler):
new_server_id, role_id, "11111111" new_server_id, role_id, "11111111"
) )
self.controller.stats.record_stats() self.controller.servers.stats.record_stats()
self.redirect("/panel/dashboard") self.redirect("/panel/dashboard")
if page == "bedrock_step1": if page == "bedrock_step1":
@ -487,7 +496,7 @@ class ServerHandler(BaseHandler):
new_server_id, role_id, "11111111" new_server_id, role_id, "11111111"
) )
self.controller.stats.record_stats() self.controller.servers.stats.record_stats()
self.redirect("/panel/dashboard") self.redirect("/panel/dashboard")
try: try:

View File

@ -151,8 +151,7 @@ if __name__ == "__main__":
# init servers # init servers
logger.info("Initializing all servers defined") logger.info("Initializing all servers defined")
Console.info("Initializing all servers defined") Console.info("Initializing all servers defined")
controller.init_all_servers() controller.servers.init_all_servers()
servers = controller.list_defined_servers()
def tasks_starter(): def tasks_starter():
# start stats logging # start stats logging

View File

@ -1,6 +1,6 @@
apscheduler==3.8.1 apscheduler==3.8.1
argon2-cffi==20.1 argon2-cffi==21.3
bleach==4.1 bleach==4.1
cached_property==1.5.2 cached_property==1.5.2
colorama==0.4 colorama==0.4
@ -9,7 +9,7 @@ cryptography==3.4.8
libgravatar==1.0.0 libgravatar==1.0.0
peewee==3.13 peewee==3.13
pexpect==4.8 pexpect==4.8
psutil==5.7 psutil==5.9
pyOpenSSL==19.1.0 pyOpenSSL==19.1.0
pyjwt==2.3 pyjwt==2.3
PyYAML==5.4 PyYAML==5.4