crafty-4/app/classes/controllers/servers_controller.py

237 lines
8.8 KiB
Python
Raw Normal View History

2021-09-08 22:01:10 +00:00
import os
import logging
import json
2021-09-08 22:01:10 +00:00
2022-03-08 04:40:44 +00:00
from app.classes.controllers.roles_controller import Roles_Controller
2021-09-08 22:01:10 +00:00
from app.classes.models.servers import servers_helper
2022-01-15 00:23:50 +00:00
from app.classes.models.users import users_helper, ApiKeys
2021-09-08 22:01:10 +00:00
from app.classes.models.server_permissions import server_permissions, Enum_Permissions_Server
from app.classes.shared.helpers import helper
from app.classes.shared.main_models import db_helper
2021-09-08 22:01:10 +00:00
logger = logging.getLogger(__name__)
class Servers_Controller:
2021-09-29 18:57:04 +00:00
2021-09-08 22:01:10 +00:00
#************************************************************************************************
# Generic Servers Methods
#************************************************************************************************
@staticmethod
def create_server(
name: str,
server_uuid: str,
server_dir: str,
backup_path: str,
server_command: str,
server_file: str,
server_log_file: str,
server_stop: str,
2022-02-10 20:27:40 +00:00
server_type: str,
server_port=25565):
return servers_helper.create_server(
name,
server_uuid,
server_dir,
backup_path,
server_command,
server_file,
server_log_file,
server_stop,
2022-02-10 20:27:40 +00:00
server_type,
server_port)
2021-09-08 22:01:10 +00:00
@staticmethod
def get_server_obj(server_id):
return servers_helper.get_server_obj(server_id)
@staticmethod
def update_server(server_obj):
return servers_helper.update_server(server_obj)
2022-03-04 00:36:36 +00:00
@staticmethod
def set_download(server_id):
return servers_helper.set_download(server_id)
@staticmethod
def finish_download(server_id):
return servers_helper.finish_download(server_id)
@staticmethod
def get_download_status(server_id):
return servers_helper.get_download_status(server_id)
2021-09-08 22:01:10 +00:00
@staticmethod
def remove_server(server_id):
roles_list = server_permissions.get_roles_from_server(server_id)
2021-09-29 18:57:04 +00:00
for role in roles_list:
role_id = role.role_id
role_data = Roles_Controller.get_role_with_servers(role_id)
role_data['servers'] = {server_id}
2021-09-29 18:57:04 +00:00
server_permissions.delete_roles_permissions(role_id, role_data['servers'])
2021-09-25 21:59:01 +00:00
server_permissions.remove_roles_of_server(server_id)
servers_helper.remove_server(server_id)
2021-09-08 22:01:10 +00:00
@staticmethod
def get_server_data_by_id(server_id):
return servers_helper.get_server_data_by_id(server_id)
#************************************************************************************************
# Servers Methods
#************************************************************************************************
@staticmethod
def get_all_defined_servers():
return servers_helper.get_all_defined_servers()
@staticmethod
def get_authorized_servers(user_id):
server_data = []
user_roles = users_helper.user_role_query(user_id)
for us in user_roles:
role_servers = server_permissions.get_role_servers_from_role_id(us.role_id)
for role in role_servers:
server_data.append(servers_helper.get_server_data_by_id(role.server_id))
return server_data
@staticmethod
def get_all_servers_stats():
return servers_helper.get_all_servers_stats()
2022-01-15 00:23:50 +00:00
@staticmethod
def get_authorized_servers_stats_api_key(api_key: ApiKeys):
server_data = []
authorized_servers = Servers_Controller.get_authorized_servers(api_key.user.user_id)
for s in authorized_servers:
latest = servers_helper.get_latest_server_stats(s.get('server_id'))
key_permissions = server_permissions.get_api_key_permissions_list(api_key, s.get('server_id'))
if Enum_Permissions_Server.Commands in key_permissions:
user_command_permission = True
else:
user_command_permission = False
server_data.append({'server_data': s, "stats": db_helper.return_rows(latest)[0],
"user_command_permission": user_command_permission})
return server_data
2021-09-08 22:01:10 +00:00
@staticmethod
def get_authorized_servers_stats(user_id):
server_data = []
authorized_servers = Servers_Controller.get_authorized_servers(user_id)
for s in authorized_servers:
latest = servers_helper.get_latest_server_stats(s.get('server_id'))
2022-01-15 00:23:50 +00:00
# TODO
user_permissions = server_permissions.get_user_id_permissions_list(user_id, s.get('server_id'))
2021-09-08 22:01:10 +00:00
if Enum_Permissions_Server.Commands in user_permissions:
user_command_permission = True
else:
user_command_permission = False
2022-01-15 00:23:50 +00:00
server_data.append({
'server_data': s,
'stats': db_helper.return_rows(latest)[0],
'user_command_permission': user_command_permission
})
2021-09-08 22:01:10 +00:00
return server_data
@staticmethod
def get_server_friendly_name(server_id):
return servers_helper.get_server_friendly_name(server_id)
#************************************************************************************************
# Servers_Stats Methods
#************************************************************************************************
@staticmethod
def get_server_stats_by_id(server_id):
return servers_helper.get_server_stats_by_id(server_id)
@staticmethod
def server_id_exists(server_id):
return servers_helper.server_id_exists(server_id)
@staticmethod
def get_server_type_by_id(server_id):
return servers_helper.get_server_type_by_id(server_id)
2021-09-08 22:01:10 +00:00
@staticmethod
2022-01-15 00:23:50 +00:00
def server_id_authorized(server_id_a, user_id):
2021-09-08 22:01:10 +00:00
user_roles = users_helper.user_role_query(user_id)
for role in user_roles:
2022-01-15 00:23:50 +00:00
for server_id_b in server_permissions.get_role_servers_from_role_id(role.role_id):
if str(server_id_a) == str(server_id_b.server_id):
2022-01-15 00:23:50 +00:00
return True
return False
2021-09-08 22:01:10 +00:00
2022-01-28 01:43:23 +00:00
@staticmethod
def is_crashed(server_id):
return servers_helper.is_crashed(server_id)
2022-01-15 00:23:50 +00:00
@staticmethod
def server_id_authorized_api_key(server_id: str, api_key: ApiKeys) -> bool:
# TODO
return Servers_Controller.server_id_authorized(server_id, api_key.user.user_id)
# There is no view server permission
# permission_helper.both_have_perm(api_key)
2021-09-08 22:01:10 +00:00
@staticmethod
def set_update(server_id, value):
return servers_helper.set_update(server_id, value)
@staticmethod
def get_TTL_without_player(server_id):
return servers_helper.get_TTL_without_player(server_id)
@staticmethod
def can_stop_no_players(server_id, time_limit):
return servers_helper.can_stop_no_players(server_id, time_limit)
2021-09-25 21:59:01 +00:00
@staticmethod
2021-09-21 19:13:17 +00:00
def set_waiting_start(server_id, value):
servers_helper.set_waiting_start(server_id, value)
@staticmethod
def get_waiting_start(server_id):
return servers_helper.get_waiting_start(server_id)
2021-09-29 18:57:04 +00:00
@staticmethod
def get_update_status(server_id):
return servers_helper.get_update_status(server_id)
2021-09-08 22:01:10 +00:00
#************************************************************************************************
# Servers Helpers Methods
#************************************************************************************************
@staticmethod
def get_banned_players(server_id):
stats = servers_helper.get_server_stats_by_id(server_id)
server_path = stats['server_id']['path']
path = os.path.join(server_path, 'banned-players.json')
try:
with open(helper.get_os_understandable_path(path), encoding='utf-8') as file:
2021-09-08 22:01:10 +00:00
content = file.read()
file.close()
except Exception as ex:
print (ex)
return None
2021-09-29 18:57:04 +00:00
2021-09-08 22:01:10 +00:00
return json.loads(content)
2021-09-29 18:57:04 +00:00
2021-09-08 22:01:10 +00:00
def check_for_old_logs(self):
servers = servers_helper.get_all_defined_servers()
for server in servers:
logs_path = os.path.split(server['log_path'])[0]
latest_log_file = os.path.split(server['log_path'])[1]
logs_delete_after = int(server['logs_delete_after'])
if logs_delete_after == 0:
continue
log_files = list(filter(
lambda val: val != latest_log_file,
os.listdir(logs_path)
))
for log_file in log_files:
log_file_path = os.path.join(logs_path, log_file)
if helper.check_file_exists(log_file_path) and \
helper.is_file_older_than_x_days(log_file_path, logs_delete_after):
2021-09-08 22:01:10 +00:00
os.remove(log_file_path)