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
from app.classes.controllers.servers_controller import ServersController
from app.classes.models.server_permissions import (
PermissionsServers,
@ -7,7 +8,6 @@ from app.classes.models.server_permissions import (
from app.classes.models.users import HelperUsers, ApiKeys
from app.classes.models.roles import HelperRoles
from app.classes.models.servers import HelperServers
from app.classes.models.server_stats import HelperServerStats
logger = logging.getLogger(__name__)
@ -106,7 +106,8 @@ class ServerPermsController:
)
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": server,

View File

@ -1,24 +1,39 @@
import os
import logging
import time
import json
import typing as t
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.server_stats import HelperServerStats
from app.classes.models.users import HelperUsers, ApiKeys
from app.classes.models.server_permissions import (
PermissionsServers,
EnumPermissionsServer,
)
from app.classes.shared.helpers import Helpers
logger = logging.getLogger(__name__)
class ServersController:
def __init__(self, servers_helper):
self.servers_helper = servers_helper
class ServersController(metaclass=Singleton):
servers_list: ServerInstance
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
@ -82,15 +97,18 @@ class ServersController:
@staticmethod
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
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
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):
roles_list = PermissionsServers.get_roles_from_server(server_id)
@ -109,6 +127,107 @@ class ServersController:
# **********************************************************************************
# 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
def get_all_defined_servers():
return HelperServers.get_all_defined_servers()
@ -122,7 +241,11 @@ class ServersController:
user.role_id
)
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
@ -140,21 +263,39 @@ class ServersController:
return user_ids
@staticmethod
def get_all_servers_stats():
return HelperServerStats.get_all_servers_stats()
def get_all_servers_stats(self):
server_data = []
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
def get_authorized_servers_stats_api_key(api_key: ApiKeys):
server_data = []
authorized_servers = ServersController.get_authorized_servers(
authorized_servers = ServersController().get_authorized_servers(
api_key.user_id # TODO: API key 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(
api_key, server.get("server_id")
api_key, server.server_id
)
if EnumPermissionsServer.COMMANDS in key_permissions:
user_command_permission = True
@ -162,7 +303,7 @@ class ServersController:
user_command_permission = False
server_data.append(
{
"server_data": server,
"server_data": DatabaseShortcuts.get_data_obj(server.server_object),
"stats": latest,
"user_command_permission": user_command_permission,
}
@ -175,11 +316,11 @@ class ServersController:
authorized_servers = ServersController.get_authorized_servers(user_id)
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
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:
user_command_permission = True
@ -187,7 +328,7 @@ class ServersController:
user_command_permission = False
server_data.append(
{
"server_data": server,
"server_data": DatabaseShortcuts.get_data_obj(server.server_object),
"stats": latest,
"user_command_permission": user_command_permission,
}
@ -199,16 +340,105 @@ class ServersController:
def 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
# **********************************************************************************
@staticmethod
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
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
def get_server_type_by_id(server_id):
@ -227,7 +457,8 @@ class ServersController:
@staticmethod
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
def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool:
@ -238,34 +469,41 @@ class ServersController:
@staticmethod
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
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
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
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
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
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
# **********************************************************************************
@staticmethod
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"]
path = os.path.join(server_path, "banned-players.json")

View File

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

View File

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

View File

@ -5,19 +5,23 @@ import threading
import logging
import getpass
from app.classes.shared.console import Console
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__)
class MainPrompt(cmd.Cmd):
def __init__(self, helper, tasks_manager, migration_manager, main_controller):
super().__init__()
self.helper = helper
self.tasks_manager = tasks_manager
self.migration_manager = migration_manager
self.controller = main_controller
self.helper: Helpers = helper
self.tasks_manager: TasksManager = tasks_manager
self.migration_manager: MigrationManager = migration_manager
self.controller: Controller = main_controller
# overrides the default Prompt
self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > "

View File

@ -5,7 +5,6 @@ import shutil
import time
import logging
import tempfile
import typing as t
from peewee import DoesNotExist
# 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.console import Console
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.minecraft.server_props import ServerProps
from app.classes.minecraft.serverjars import ServerJars
from app.classes.minecraft.stats import Stats
logger = logging.getLogger(__name__)
class Controller:
def __init__(self, database, helper):
self.helper = helper
self.server_jars = ServerJars(helper)
self.users_helper = HelperUsers(database, self.helper)
self.roles_helper = HelperRoles(database)
self.servers_helper = HelperServers(database)
self.management_helper = HelpersManagement(database, self.helper)
self.authentication = Authentication(self.helper)
self.servers_list = []
self.stats = Stats(self.helper, self)
self.crafty_perms = CraftyPermsController()
self.management = ManagementController(self.management_helper)
self.roles = RolesController(self.users_helper, self.roles_helper)
self.server_perms = ServerPermsController()
self.servers = ServersController(self.servers_helper)
self.users = UsersController(
self.helper: Helpers = helper
self.server_jars: ServerJars = ServerJars(helper)
self.users_helper: HelperUsers = HelperUsers(database, self.helper)
self.roles_helper: HelperRoles = HelperRoles(database)
self.servers_helper: HelperServers = HelperServers(database)
self.management_helper: HelpersManagement = HelpersManagement(
database, self.helper
)
self.authentication: Authentication = Authentication(self.helper)
self.crafty_perms: CraftyPermsController = CraftyPermsController()
self.management: ManagementController = ManagementController(
self.management_helper
)
self.roles: RolesController = RolesController(
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
)
tz = get_localzone()
self.support_scheduler = BackgroundScheduler(timezone=str(tz))
self.support_scheduler: BackgroundScheduler = BackgroundScheduler(
timezone=str(tz)
)
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
def check_system_user():
return HelperUsers.get_user_id_by_name("system") is not None
@ -240,24 +157,6 @@ class Controller:
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):
results = Helpers.calc_percent(source_path, dest_path)
self.log_stats = results
@ -273,81 +172,6 @@ class Controller:
except:
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):
server_fs_uuid = Helpers.create_uuid()
new_server_path = os.path.join(self.helper.servers_dir, server_fs_uuid)
@ -986,17 +810,17 @@ class Controller:
return False
# let's re-init all servers
self.init_all_servers()
self.servers.init_all_servers()
return new_id
def remove_server(self, server_id, files):
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 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"]
logger.info(f"Deleting Server: ID {server_id} | Name: {server_name} ")
@ -1007,7 +831,7 @@ class Controller:
running = srv_obj.check_running()
if running:
self.stop_server(server_id)
self.servers.stop_server(server_id)
if files:
try:
FileHelpers.del_dirs(
@ -1040,7 +864,7 @@ class Controller:
self.servers.remove_server(server_id)
# remove the server from servers list
self.servers_list.pop(counter)
self.servers.servers_list.pop(counter)
counter += 1

View File

@ -58,3 +58,7 @@ class DatabaseShortcuts:
def return_db_rows(model):
data = [model_to_dict(row) for row in model]
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.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.management import HelpersManagement
from app.classes.models.users import HelperUsers
@ -96,8 +96,14 @@ class ServerOutBuf:
# **********************************************************************************
# Minecraft Server Class
# **********************************************************************************
class Server:
def __init__(self, helper, management_helper, stats):
class ServerInstance:
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.management_helper = management_helper
# holders for our process
@ -109,12 +115,14 @@ class Server:
self.server_thread = None
self.settings = None
self.updating = False
self.server_id = None
self.server_id = server_id
self.jar_update_url = None
self.name = None
self.is_crashed = False
self.restart_count = 0
self.stats = stats
self.server_object = HelperServers.get_server_obj(self.server_id)
self.stats_helper = HelperServerStats(self.server_id)
tz = get_localzone()
self.server_scheduler = BackgroundScheduler(timezone=str(tz))
self.server_scheduler.start()
@ -123,8 +131,8 @@ class Server:
)
self.is_backingup = False
# Reset crash and update at initialization
HelperServerStats.server_crash_reset(self.server_id)
HelperServerStats.set_update(self.server_id, False)
self.stats_helper.server_crash_reset()
self.stats_helper.set_update(False)
# **********************************************************************************
# Minecraft Server Management
@ -147,7 +155,7 @@ class Server:
self.name = server_name
self.settings = server_data_obj
HelperServerStats.init_database(server_id)
self.stats_helper.init_database(server_id)
self.record_server_stats()
# build our server run command
@ -169,7 +177,7 @@ class Server:
Console.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.
HelperServerStats.set_waiting_start(self.server_id, False)
self.stats_helper.set_waiting_start(False)
self.run_threaded_server(None)
# remove the scheduled job since it's ran
@ -236,7 +244,7 @@ class Server:
else:
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:
self.helper.websocket_helper.broadcast_user(
user_id,
@ -409,7 +417,7 @@ class Server:
).start()
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"))
@ -417,7 +425,7 @@ class Server:
logger.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
HelperServerStats.server_crash_reset(self.server_id)
self.stats_helper.server_crash_reset()
self.record_server_stats()
check_internet_thread = threading.Thread(
target=self.check_internet_thread,
@ -430,11 +438,9 @@ class Server:
)
check_internet_thread.start()
# Checks if this is the servers first run.
if HelperServerStats.get_first_run(self.server_id):
HelperServerStats.set_first_run(self.server_id)
loc_server_port = HelperServerStats.get_server_stats_by_id(
self.server_id
)["server_port"]
if self.stats_helper.get_first_run():
self.stats_helper.set_first_run()
loc_server_port = self.stats_helper.get_server_stats()["server_port"]
# Sends port reminder message.
self.helper.websocket_helper.broadcast_user(
user_id,
@ -716,7 +722,7 @@ class Server:
self.server_scheduler.remove_job("stats_" + str(self.server_id))
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 self.restart_count <= 3:
@ -740,7 +746,7 @@ class Server:
self.restart_count = 0
self.is_crashed = True
HelperServerStats.sever_crashed(self.server_id)
self.stats_helper.sever_crashed()
# cancel the watcher task
self.server_scheduler.remove_job("c_" + str(self.server_id))
@ -969,7 +975,7 @@ class Server:
return []
def jar_update(self):
HelperServerStats.set_update(self.server_id, True)
self.stats_helper.set_update(True)
update_thread = threading.Thread(
target=self.a_jar_update, daemon=True, name=f"exe_update_{self.name}"
)
@ -977,7 +983,7 @@ class Server:
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
else:
return False
@ -1050,11 +1056,11 @@ class Server:
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:
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:
# There are clients
self.check_update()
@ -1452,11 +1458,11 @@ class Server:
def record_server_stats(self):
server_stats = self.get_servers_stats()
HelperServerStats.insert_server_stats(server_stats)
self.stats_helper.insert_server_stats(server_stats)
# delete old data
max_age = self.helper.get_setting("history_max_age")
now = datetime.datetime.now()
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.schedulers.background import BackgroundScheduler
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.users import HelperUsers
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.web.tornado_handler import Webserver
@ -36,14 +38,14 @@ class TasksManager:
controller: Controller
def __init__(self, helper, controller):
self.helper = helper
self.controller = controller
self.tornado = Webserver(helper, controller, self)
self.helper: Helpers = helper
self.controller: Controller = controller
self.tornado: Webserver = Webserver(helper, controller, self)
self.tz = get_localzone()
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(
target=self.tornado.run_tornado, daemon=True, name="tornado_thread"
@ -84,7 +86,9 @@ class TasksManager:
commands = HelpersManagement.get_unactioned_commands()
for cmd in commands:
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:
logger.error(
"Server value requested does not exist! "
@ -131,7 +135,7 @@ class TasksManager:
def _main_graceful_exit(self):
try:
os.remove(self.helper.session_file)
self.controller.stop_all_servers()
self.controller.servers.stop_all_servers()
except:
logger.info("Caught error during shutdown", exc_info=True)
@ -517,10 +521,10 @@ class TasksManager:
)
# one for now,
self.controller.stats.record_stats()
self.controller.servers.stats.record_stats()
# one for later
self.scheduler.add_job(
self.controller.stats.record_stats,
self.controller.servers.stats.record_stats,
"interval",
seconds=stats_update_frequency,
id="stats",

View File

@ -286,7 +286,7 @@ class AjaxHandler(BaseHandler):
logger.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"]:
logger.info(
@ -334,7 +334,7 @@ class AjaxHandler(BaseHandler):
logger.error("Server ID is none. Canceling backup!")
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.users.get_user_by_id(exec_user["user_id"])["username"],
exec_user["user_id"],
@ -356,7 +356,7 @@ class AjaxHandler(BaseHandler):
self.redirect("/panel/error?error=Unauthorized access to Commands")
return
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:
svr.kill()
time.sleep(5)
@ -369,7 +369,7 @@ class AjaxHandler(BaseHandler):
return
elif page == "eula":
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"])
elif page == "restore_backup":
@ -394,7 +394,7 @@ class AjaxHandler(BaseHandler):
server_data["server_port"],
)
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(
server_id, new_server_id, new_server["server_uuid"]
)
@ -412,7 +412,7 @@ class AjaxHandler(BaseHandler):
server_data["server_port"],
)
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(
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)}"
)
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"]
self.controller.management.add_to_audit_log(
@ -546,7 +546,7 @@ class AjaxHandler(BaseHandler):
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"]
self.controller.management.add_to_audit_log(

View File

@ -127,7 +127,7 @@ class NodeStats(ApiHandler):
return
# 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"]})
@ -161,7 +161,7 @@ class SendCommand(ApiHandler):
command = self.get_argument("command", default=None, strip=True)
server_id = self.get_argument("id")
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:
server.send_command(command)
self.return_response(200, {"run": True})
@ -198,7 +198,7 @@ class ServerBackup(ApiHandler):
self.access_denied(user)
return
server = self.controller.get_server_obj(server_id)
server = self.controller.servers.get_server_instance_by_id(server_id)
server.backup_server()
@ -232,7 +232,7 @@ class StartServer(ApiHandler):
self.access_denied("unknown")
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():
self.controller.management.send_command(
@ -270,7 +270,7 @@ class StopServer(ApiHandler):
self.access_denied(user)
return
server = self.controller.get_server_obj(server_id)
server = self.controller.servers.get_server_instance_by_id(server_id)
if server.check_running():
self.controller.management.send_command(

View File

@ -180,7 +180,7 @@ class BaseHandler(tornado.web.RequestHandler):
exec_user_role = set()
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_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 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.crafty_permissions import EnumPermissionsCrafty
from app.classes.models.management import HelpersManagement
from app.classes.shared.helpers import Helpers
from app.classes.shared.main_models import DatabaseShortcuts
from app.classes.web.base_handler import BaseHandler
logger = logging.getLogger(__name__)
@ -39,7 +41,7 @@ class PanelHandler(BaseHandler):
def get_role_servers(self) -> t.List[RolesController.RoleServerJsonType]:
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")
if argument == "0":
continue
@ -190,7 +192,7 @@ class PanelHandler(BaseHandler):
total_players = 0
for server in page_data["servers"]:
total_players += len(
self.controller.stats.get_server_players(
self.controller.servers.stats.get_server_players(
server["server_data"]["server_id"]
)
)
@ -222,7 +224,7 @@ class PanelHandler(BaseHandler):
superuser = superuser and api_key.superuser
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_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 in defined_servers[:]:
if str(server["server_id"]) == str(server_id):
page_servers.append(server)
if str(server.server_id) == str(server_id):
page_servers.append(
DatabaseShortcuts.get_data_obj(server.server_object)
)
user_order.remove(server_id)
defined_servers.remove(server)
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:
page_servers.append(server)
page_servers.append(
DatabaseShortcuts.get_data_obj(server.server_object)
)
for server_id in user_order[:]:
# remove IDs in list that user no longer has access to
if str(server_id) not in server_ids:
@ -284,10 +291,10 @@ class PanelHandler(BaseHandler):
},
"server_stats": {
"total": len(defined_servers),
"running": len(self.controller.list_running_servers()),
"running": len(self.controller.servers.list_running_servers()),
"stopped": (
len(self.controller.list_defined_servers())
- len(self.controller.list_running_servers())
len(self.controller.servers.list_defined_servers())
- len(self.controller.servers.list_running_servers())
),
},
"menu_servers": defined_servers,
@ -397,7 +404,7 @@ class PanelHandler(BaseHandler):
"servers"
] = self.controller.servers.get_all_servers_stats()
except IndexError:
self.controller.stats.record_stats()
self.controller.servers.stats.record_stats()
page_data[
"servers"
] = self.controller.servers.get_all_servers_stats()
@ -407,7 +414,7 @@ class PanelHandler(BaseHandler):
exec_user["user_id"]
)
except IndexError:
self.controller.stats.record_stats()
self.controller.servers.stats.record_stats()
user_auth = self.controller.servers.get_authorized_servers_stats(
exec_user["user_id"]
)
@ -490,7 +497,7 @@ class PanelHandler(BaseHandler):
"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
page_data["server_data"] = self.controller.servers.get_server_data_by_id(
server_id
@ -508,9 +515,9 @@ class PanelHandler(BaseHandler):
except Exception as e:
logger.error(f"Failed to get server waiting to start: {e}")
page_data["waiting_start"] = False
page_data["get_players"] = lambda: self.controller.stats.get_server_players(
server_id
)
page_data[
"get_players"
] = lambda: self.controller.servers.stats.get_server_players(server_id)
page_data["active_link"] = subpage
page_data["permissions"] = {
"Commands": EnumPermissionsServer.COMMANDS,
@ -638,10 +645,14 @@ class PanelHandler(BaseHandler):
page_data[
"exclusions"
] = 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
).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
).send_backup_status()
# makes it so relative path is the only thing shown
@ -651,7 +662,7 @@ class PanelHandler(BaseHandler):
else:
exclusions.append(file.replace(server_info["path"] + "/", ""))
page_data["exclusions"] = exclusions
self.controller.refresh_server_settings(server_id)
self.controller.servers.refresh_server_settings(server_id)
try:
page_data["backup_list"] = server.list_backups()
except:
@ -727,8 +738,8 @@ class PanelHandler(BaseHandler):
)
servers = []
for server in user_servers:
if server["server_name"] not in servers:
servers.append(server["server_name"])
if server.name not in servers:
servers.append(server.name)
new_item = {user.user_id: servers}
auth_servers.update(new_item)
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["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[
"permissions_all"
@ -825,9 +836,9 @@ class PanelHandler(BaseHandler):
page_data["schedules"] = HelpersManagement.get_schedules_by_server(
server_id
)
page_data["get_players"] = lambda: self.controller.stats.get_server_players(
server_id
)
page_data[
"get_players"
] = lambda: self.controller.servers.stats.get_server_players(server_id)
page_data["active_link"] = "schedules"
page_data["permissions"] = {
"Commands": EnumPermissionsServer.COMMANDS,
@ -889,9 +900,9 @@ class PanelHandler(BaseHandler):
if sch_id is None:
self.redirect("/panel/error?error=Invalid Schedule ID")
schedule = self.controller.management.get_scheduled_task_model(sch_id)
page_data["get_players"] = lambda: self.controller.stats.get_server_players(
server_id
)
page_data[
"get_players"
] = lambda: self.controller.servers.stats.get_server_players(server_id)
page_data["active_link"] = "schedules"
page_data["permissions"] = {
"Commands": EnumPermissionsServer.COMMANDS,
@ -963,13 +974,13 @@ class PanelHandler(BaseHandler):
role_servers = self.controller.servers.get_authorized_servers(user_id)
page_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["user"] = self.controller.users.get_user_by_id(user_id)
page_data["servers"] = set()
page_data["role-servers"] = page_role_servers
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[
"permissions_all"
@ -1097,7 +1108,7 @@ class PanelHandler(BaseHandler):
)
return
page_data["servers_all"] = self.controller.list_defined_servers()
page_data["servers_all"] = self.controller.servers.get_all_defined_servers()
page_data[
"permissions_all"
] = self.controller.server_perms.list_defined_permissions()
@ -1109,7 +1120,7 @@ class PanelHandler(BaseHandler):
page_data["new_role"] = False
role_id = self.get_argument("id", None)
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[
"permissions_all"
] = self.controller.server_perms.list_defined_permissions()
@ -1256,7 +1267,7 @@ class PanelHandler(BaseHandler):
"Players": EnumPermissionsServer.PLAYERS,
}
if superuser:
# defined_servers = self.controller.list_defined_servers()
# defined_servers = self.controller.servers.list_defined_servers()
exec_user_role = {"Super User"}
exec_user_crafty_permissions = (
self.controller.crafty_perms.list_defined_crafty_permissions()
@ -1313,7 +1324,7 @@ class PanelHandler(BaseHandler):
if server_id is None:
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
# Compares old jar name to page data being passed.
# 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.logs_delete_after = logs_delete_after
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(
exec_user["user_id"],

View File

@ -104,7 +104,7 @@ class ApiRolesIndexHandler(BaseApiHandler):
server_ids = (
(
{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
if "servers" in data
else set()

View File

@ -670,7 +670,7 @@ class ApiServersIndexHandler(BaseApiHandler):
# Increase the server creation counter
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(
user["user_id"],

View File

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

View File

@ -1,6 +1,6 @@
import logging
from app.classes.models.server_stats import HelperServerStats
from app.classes.web.base_api_handler import BaseApiHandler
from app.classes.controllers.servers_controller import ServersController
logger = logging.getLogger(__name__)
@ -16,10 +16,13 @@ class ApiServersServerStatsHandler(BaseApiHandler):
# if the user doesn't have access to the server, return an error
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(
200,
{
"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.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers
from app.classes.shared.main_models import DatabaseShortcuts
from app.classes.web.base_handler import BaseHandler
logger = logging.getLogger(__name__)
@ -28,7 +29,7 @@ class ServerHandler(BaseHandler):
superuser = superuser and api_key.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_crafty_permissions = (
self.controller.crafty_perms.list_defined_crafty_permissions()
@ -52,6 +53,14 @@ class ServerHandler(BaseHandler):
exec_user_role.add(role["role_name"])
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"
page_data = {
@ -66,11 +75,11 @@ class ServerHandler(BaseHandler):
"Roles_Config": EnumPermissionsCrafty.ROLES_CONFIG,
},
"server_stats": {
"total": len(self.controller.list_defined_servers()),
"running": len(self.controller.list_running_servers()),
"total": len(self.controller.servers.list_defined_servers()),
"running": len(self.controller.servers.list_running_servers()),
"stopped": (
len(self.controller.list_defined_servers())
- len(self.controller.list_running_servers())
len(self.controller.servers.list_defined_servers())
- len(self.controller.servers.list_running_servers())
),
},
"hosts_data": self.controller.management.get_latest_hosts_stats(),
@ -240,7 +249,7 @@ class ServerHandler(BaseHandler):
server_port,
)
self.controller.init_all_servers()
self.controller.servers.init_all_servers()
return
@ -370,7 +379,7 @@ class ServerHandler(BaseHandler):
new_server_id, role_id, "11111111"
)
self.controller.stats.record_stats()
self.controller.servers.stats.record_stats()
self.redirect("/panel/dashboard")
if page == "bedrock_step1":
@ -487,7 +496,7 @@ class ServerHandler(BaseHandler):
new_server_id, role_id, "11111111"
)
self.controller.stats.record_stats()
self.controller.servers.stats.record_stats()
self.redirect("/panel/dashboard")
try:

View File

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

View File

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