2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2023-01-19 16:27:33 +00:00
|
|
|
import queue
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2023-10-07 21:50:56 +00:00
|
|
|
from prometheus_client import CollectorRegistry, Gauge
|
|
|
|
|
2023-06-03 19:05:08 +00:00
|
|
|
from app.classes.models.management import HelpersManagement, HelpersWebhooks
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.servers import HelperServers
|
2024-05-26 02:10:35 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class ManagementController:
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, management_helper):
|
|
|
|
self.management_helper = management_helper
|
2023-01-19 16:27:33 +00:00
|
|
|
self.command_queue = queue.Queue()
|
2023-10-07 21:50:56 +00:00
|
|
|
self.host_registry = CollectorRegistry()
|
|
|
|
self.init_host_registries()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2023-01-17 19:40:16 +00:00
|
|
|
# **********************************************************************************
|
|
|
|
# Config Methods
|
|
|
|
# **********************************************************************************
|
|
|
|
@staticmethod
|
|
|
|
def set_login_image(path):
|
|
|
|
HelpersManagement.set_login_image(path)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_login_image():
|
|
|
|
return HelpersManagement.get_login_image()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_login_opacity(opacity):
|
|
|
|
return HelpersManagement.set_login_opacity(opacity)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_login_opacity():
|
|
|
|
return HelpersManagement.get_login_opacity()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Host_Stats Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_latest_hosts_stats():
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_latest_hosts_stats()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-06-20 19:26:21 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_crafty_api_key(key):
|
|
|
|
HelpersManagement.set_secret_api_key(key)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_crafty_api_key():
|
|
|
|
return HelpersManagement.get_secret_api_key()
|
|
|
|
|
2023-01-29 21:54:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_cookie_secret(key):
|
|
|
|
HelpersManagement.set_cookie_secret(key)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_crafty_row():
|
|
|
|
HelpersManagement.create_crafty_row()
|
|
|
|
|
2023-10-07 21:50:56 +00:00
|
|
|
def init_host_registries(self):
|
|
|
|
# REGISTRY Entries for Server Stats functions
|
|
|
|
self.cpu_usage = Gauge(
|
|
|
|
name="CPU_Usage",
|
|
|
|
documentation="The CPU usage of the server",
|
|
|
|
registry=self.host_registry,
|
|
|
|
)
|
|
|
|
self.mem_usage_percent = Gauge(
|
|
|
|
name="Mem_Usage",
|
|
|
|
documentation="The Memory usage of the server",
|
|
|
|
registry=self.host_registry,
|
|
|
|
)
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Commands Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2024-05-25 18:40:14 +00:00
|
|
|
def send_command(self, user_id, server_id, remote_ip, command, action_id=None):
|
2022-04-14 02:10:25 +00:00
|
|
|
server_name = HelperServers.get_server_friendly_name(server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
# Example: Admin issued command start_server for server Survival
|
2022-04-11 05:23:55 +00:00
|
|
|
self.management_helper.add_to_audit_log(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
f"issued command {command} for server {server_name}",
|
|
|
|
server_id,
|
|
|
|
remote_ip,
|
|
|
|
)
|
2023-01-19 16:27:33 +00:00
|
|
|
self.queue_command(
|
2024-05-25 18:40:14 +00:00
|
|
|
{
|
|
|
|
"server_id": server_id,
|
|
|
|
"user_id": user_id,
|
|
|
|
"command": command,
|
|
|
|
"action_id": action_id,
|
|
|
|
}
|
2023-01-19 16:27:33 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2023-01-19 16:27:33 +00:00
|
|
|
def queue_command(self, command_data):
|
|
|
|
self.command_queue.put(command_data)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Audit_Log Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_to_audit_log(self, user_id, log_msg, server_id=None, source_ip=None):
|
|
|
|
return self.management_helper.add_to_audit_log(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id, log_msg, server_id, source_ip
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_to_audit_log_raw(self, user_name, user_id, server_id, log_msg, source_ip):
|
|
|
|
return self.management_helper.add_to_audit_log_raw(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_name, user_id, server_id, log_msg, source_ip
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Schedules Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-03-23 02:50:12 +00:00
|
|
|
@staticmethod
|
|
|
|
def create_scheduled_task(
|
|
|
|
server_id,
|
|
|
|
action,
|
|
|
|
interval,
|
|
|
|
interval_type,
|
|
|
|
start_time,
|
|
|
|
command,
|
2022-08-05 17:52:50 +00:00
|
|
|
name,
|
2022-03-23 02:50:12 +00:00
|
|
|
enabled=True,
|
2023-01-16 16:20:15 +00:00
|
|
|
one_time=False,
|
|
|
|
cron_string="* * * * *",
|
|
|
|
parent=None,
|
|
|
|
delay=0,
|
2024-05-25 17:51:40 +00:00
|
|
|
action_id=None,
|
2022-03-23 02:50:12 +00:00
|
|
|
):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.create_scheduled_task(
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id,
|
|
|
|
action,
|
|
|
|
interval,
|
|
|
|
interval_type,
|
|
|
|
start_time,
|
|
|
|
command,
|
2022-08-05 17:52:50 +00:00
|
|
|
name,
|
2022-03-23 02:50:12 +00:00
|
|
|
enabled,
|
2023-01-16 20:16:43 +00:00
|
|
|
one_time,
|
|
|
|
cron_string,
|
|
|
|
parent,
|
|
|
|
delay,
|
2024-05-25 17:51:40 +00:00
|
|
|
action_id,
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete_scheduled_task(schedule_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.delete_scheduled_task(schedule_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-11-09 19:13:35 +00:00
|
|
|
@staticmethod
|
2021-09-08 22:01:10 +00:00
|
|
|
def update_scheduled_task(schedule_id, updates):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.update_scheduled_task(schedule_id, updates)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_scheduled_task(schedule_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_scheduled_task(schedule_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-11 22:24:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_scheduled_task_model(schedule_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_scheduled_task_model(schedule_id)
|
2022-01-11 22:24:05 +00:00
|
|
|
|
2022-02-26 00:53:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_child_schedules(sch_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_child_schedules(sch_id)
|
2022-02-26 00:53:38 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_schedules_by_server(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_schedules_by_server(server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_schedules_all():
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_schedules_all()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_schedules_enabled():
|
2022-04-14 02:10:25 +00:00
|
|
|
return HelpersManagement.get_schedules_enabled()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Backups Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2024-04-21 15:26:16 +00:00
|
|
|
def get_backup_config(backup_id):
|
|
|
|
return HelpersManagement.get_backup_config(backup_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2024-03-09 04:22:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_backups_by_server(server_id, model=False):
|
|
|
|
return HelpersManagement.get_backups_by_server(server_id, model)
|
|
|
|
|
2024-05-26 02:10:35 +00:00
|
|
|
@staticmethod
|
|
|
|
def delete_backup_config(backup_id):
|
|
|
|
HelpersManagement.remove_backup_config(backup_id)
|
|
|
|
|
2024-04-20 21:30:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def update_backup_config(backup_id, updates):
|
2024-05-26 02:10:35 +00:00
|
|
|
if "backup_location" in updates:
|
|
|
|
updates["backup_location"] = Helpers.wtol_path(updates["backup_location"])
|
2024-04-20 21:30:37 +00:00
|
|
|
return HelpersManagement.update_backup_config(backup_id, updates)
|
|
|
|
|
2024-05-26 02:10:35 +00:00
|
|
|
def add_backup_config(self, data):
|
|
|
|
if "backup_location" in data:
|
|
|
|
data["backup_location"] = Helpers.wtol_path(data["backup_location"])
|
|
|
|
return self.management_helper.add_backup_config(data)
|
|
|
|
|
|
|
|
def add_default_backup_config(self, server_id, backup_path):
|
2024-04-20 21:30:37 +00:00
|
|
|
return self.management_helper.add_backup_config(
|
2024-05-26 02:10:35 +00:00
|
|
|
{
|
2024-06-14 19:17:00 +00:00
|
|
|
"backup_name": "Default Backup",
|
2024-05-26 02:10:35 +00:00
|
|
|
"backup_location": Helpers.wtol_path(backup_path),
|
|
|
|
"max_backups": 0,
|
|
|
|
"before": "",
|
|
|
|
"after": "",
|
|
|
|
"compress": False,
|
|
|
|
"shutdown": False,
|
|
|
|
"server_id": server_id,
|
|
|
|
"excluded_dirs": [],
|
2024-06-14 19:17:00 +00:00
|
|
|
"default": True,
|
2024-05-26 02:10:35 +00:00
|
|
|
}
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-04-21 15:26:16 +00:00
|
|
|
def get_excluded_backup_dirs(backup_id: int):
|
|
|
|
return HelpersManagement.get_excluded_backup_dirs(backup_id)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_excluded_backup_dir(self, server_id: int, dir_to_add: str):
|
|
|
|
self.management_helper.add_excluded_backup_dir(server_id, dir_to_add)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def del_excluded_backup_dir(self, server_id: int, dir_to_del: str):
|
|
|
|
self.management_helper.del_excluded_backup_dir(server_id, dir_to_del)
|
2023-01-27 00:21:39 +00:00
|
|
|
|
|
|
|
# **********************************************************************************
|
|
|
|
# Crafty Methods
|
|
|
|
# **********************************************************************************
|
|
|
|
@staticmethod
|
|
|
|
def get_master_server_dir():
|
|
|
|
return HelpersManagement.get_master_server_dir()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_master_server_dir(server_dir):
|
|
|
|
HelpersManagement.set_master_server_dir(server_dir)
|
2023-06-03 19:05:08 +00:00
|
|
|
|
|
|
|
# **********************************************************************************
|
|
|
|
# Webhooks Methods
|
|
|
|
# **********************************************************************************
|
|
|
|
@staticmethod
|
|
|
|
def create_webhook(data):
|
|
|
|
return HelpersWebhooks.create_webhook(data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def modify_webhook(webhook_id, data):
|
|
|
|
HelpersWebhooks.modify_webhook(webhook_id, data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_webhook_by_id(webhook_id):
|
|
|
|
return HelpersWebhooks.get_webhook_by_id(webhook_id)
|
|
|
|
|
|
|
|
@staticmethod
|
2023-06-03 22:11:59 +00:00
|
|
|
def get_webhooks_by_server(server_id, model=False):
|
|
|
|
return HelpersWebhooks.get_webhooks_by_server(server_id, model)
|
2023-06-03 19:05:08 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete_webhook(webhook_id):
|
|
|
|
HelpersWebhooks.delete_webhook(webhook_id)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete_webhook_by_server(server_id):
|
|
|
|
HelpersWebhooks.delete_webhooks_by_server(server_id)
|