2022-05-28 16:00:25 +00:00
|
|
|
from contextlib import redirect_stderr
|
2020-08-17 02:47:53 +00:00
|
|
|
import os
|
2024-01-11 21:11:08 +00:00
|
|
|
import io
|
2020-08-17 02:47:53 +00:00
|
|
|
import re
|
2022-06-06 21:04:45 +00:00
|
|
|
import shutil
|
2020-08-17 02:47:53 +00:00
|
|
|
import time
|
|
|
|
import datetime
|
2022-03-03 21:01:12 +00:00
|
|
|
import base64
|
2020-08-17 02:47:53 +00:00
|
|
|
import threading
|
|
|
|
import logging.config
|
2021-09-25 19:29:28 +00:00
|
|
|
import subprocess
|
2021-08-10 20:17:56 +00:00
|
|
|
import html
|
2022-12-22 18:32:56 +00:00
|
|
|
import glob
|
2023-05-29 23:34:40 +00:00
|
|
|
import json
|
2022-04-11 10:08:36 +00:00
|
|
|
|
2023-02-10 23:03:49 +00:00
|
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
# TZLocal is set as a hidden import on win pipeline
|
2023-10-07 15:59:46 +00:00
|
|
|
from zoneinfo import ZoneInfoNotFoundError
|
2022-04-11 05:23:55 +00:00
|
|
|
from tzlocal import get_localzone
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
2023-09-29 00:00:48 +00:00
|
|
|
from apscheduler.jobstores.base import JobLookupError, ConflictingIdError
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2023-10-07 21:50:56 +00:00
|
|
|
# OpenMetrics/Prometheus Imports
|
|
|
|
from prometheus_client import CollectorRegistry, Gauge, Info
|
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.minecraft.stats import Stats
|
|
|
|
from app.classes.minecraft.mc_ping import ping, ping_bedrock
|
2022-05-26 12:50:20 +00:00
|
|
|
from app.classes.models.servers import HelperServers, Servers
|
2022-04-27 19:07:34 +00:00
|
|
|
from app.classes.models.server_stats import HelperServerStats
|
2023-09-28 21:18:07 +00:00
|
|
|
from app.classes.models.management import HelpersManagement, HelpersWebhooks
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.users import HelperUsers
|
|
|
|
from app.classes.models.server_permissions import PermissionsServers
|
2022-04-12 01:34:46 +00:00
|
|
|
from app.classes.shared.console import Console
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
|
|
|
from app.classes.shared.file_helpers import FileHelpers
|
2022-05-28 16:00:25 +00:00
|
|
|
from app.classes.shared.null_writer import NullWriter
|
2023-08-09 21:47:53 +00:00
|
|
|
from app.classes.shared.websocket_manager import WebSocketManager
|
2023-09-28 21:18:07 +00:00
|
|
|
from app.classes.web.webhooks.webhook_factory import WebhookFactory
|
2022-05-28 16:00:25 +00:00
|
|
|
|
|
|
|
with redirect_stderr(NullWriter()):
|
|
|
|
import psutil
|
|
|
|
from psutil import NoSuchProcess
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2024-01-16 18:53:50 +00:00
|
|
|
SUCCESSMSG = "SUCCESS! Forge install completed"
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2023-10-07 17:06:26 +00:00
|
|
|
def callback(called_func):
|
|
|
|
# Usage of @callback on method
|
|
|
|
# definition to run a webhook check
|
|
|
|
# on method completion
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
res = None
|
|
|
|
logger.debug("Checking for callbacks")
|
|
|
|
try:
|
|
|
|
res = called_func(*args, **kwargs)
|
|
|
|
finally:
|
|
|
|
events = WebhookFactory.get_monitored_events()
|
|
|
|
if called_func.__name__ in events:
|
|
|
|
server_webhooks = HelpersWebhooks.get_webhooks_by_server(
|
|
|
|
args[0].server_id, True
|
|
|
|
)
|
|
|
|
for swebhook in server_webhooks:
|
|
|
|
if called_func.__name__ in str(swebhook.trigger).split(","):
|
|
|
|
logger.info(
|
|
|
|
f"Found callback for event {called_func.__name__}"
|
|
|
|
f" for server {args[0].server_id}"
|
|
|
|
)
|
|
|
|
webhook = HelpersWebhooks.get_webhook_by_id(swebhook.id)
|
|
|
|
webhook_provider = WebhookFactory.create_provider(
|
|
|
|
webhook["webhook_type"]
|
|
|
|
)
|
|
|
|
if res is not False and swebhook.enabled:
|
|
|
|
webhook_provider.send(
|
|
|
|
bot_name=webhook["bot_name"],
|
|
|
|
server_name=args[0].name,
|
|
|
|
title=webhook["name"],
|
|
|
|
url=webhook["url"],
|
|
|
|
message=webhook["body"],
|
|
|
|
color=webhook["color"],
|
|
|
|
)
|
|
|
|
return res
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2023-10-07 17:06:26 +00:00
|
|
|
return wrapper
|
2023-10-07 17:13:35 +00:00
|
|
|
|
|
|
|
|
2021-08-10 20:17:56 +00:00
|
|
|
class ServerOutBuf:
|
|
|
|
lines = {}
|
2021-08-18 16:50:13 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, helper, proc, server_id):
|
|
|
|
self.helper = helper
|
2021-09-25 19:29:28 +00:00
|
|
|
self.proc = proc
|
2021-08-10 20:17:56 +00:00
|
|
|
self.server_id = str(server_id)
|
|
|
|
# Buffers text for virtual_terminal_lines config number of lines
|
2022-04-11 05:23:55 +00:00
|
|
|
self.max_lines = self.helper.get_setting("virtual_terminal_lines")
|
2022-03-23 02:50:12 +00:00
|
|
|
self.line_buffer = ""
|
2021-08-10 20:17:56 +00:00
|
|
|
ServerOutBuf.lines[self.server_id] = []
|
2021-11-15 00:17:23 +00:00
|
|
|
self.lsi = 0
|
2021-08-10 20:17:56 +00:00
|
|
|
|
2021-11-06 17:06:26 +00:00
|
|
|
def process_byte(self, char):
|
2021-11-15 00:17:23 +00:00
|
|
|
if char == os.linesep[self.lsi]:
|
|
|
|
self.lsi += 1
|
|
|
|
else:
|
|
|
|
self.lsi = 0
|
|
|
|
self.line_buffer += char
|
|
|
|
|
|
|
|
if self.lsi >= len(os.linesep):
|
|
|
|
self.lsi = 0
|
2021-11-06 17:06:26 +00:00
|
|
|
ServerOutBuf.lines[self.server_id].append(self.line_buffer)
|
|
|
|
|
|
|
|
self.new_line_handler(self.line_buffer)
|
2022-03-23 02:50:12 +00:00
|
|
|
self.line_buffer = ""
|
2021-11-06 17:06:26 +00:00
|
|
|
# Limit list length to self.max_lines:
|
|
|
|
if len(ServerOutBuf.lines[self.server_id]) > self.max_lines:
|
|
|
|
ServerOutBuf.lines[self.server_id].pop(0)
|
|
|
|
|
2021-08-10 20:17:56 +00:00
|
|
|
def check(self):
|
2024-01-11 21:11:08 +00:00
|
|
|
text_wrapper = io.TextIOWrapper(
|
|
|
|
self.proc.stdout, encoding="UTF-8", errors="ignore", newline=""
|
|
|
|
)
|
2021-11-06 17:06:26 +00:00
|
|
|
while True:
|
|
|
|
if self.proc.poll() is None:
|
2024-01-11 21:11:08 +00:00
|
|
|
char = text_wrapper.read(1) # modified
|
2022-03-23 06:06:13 +00:00
|
|
|
# TODO: we may want to benchmark reading in blocks and userspace
|
|
|
|
# processing it later, reads are kind of expensive as a syscall
|
2021-11-06 17:06:26 +00:00
|
|
|
self.process_byte(char)
|
2021-08-10 20:17:56 +00:00
|
|
|
else:
|
2024-01-11 21:11:08 +00:00
|
|
|
flush = text_wrapper.read() # modified
|
2021-11-06 17:06:26 +00:00
|
|
|
for char in flush:
|
|
|
|
self.process_byte(char)
|
2021-11-20 00:14:32 +00:00
|
|
|
break
|
2021-08-10 20:17:56 +00:00
|
|
|
|
|
|
|
def new_line_handler(self, new_line):
|
2022-03-23 02:50:12 +00:00
|
|
|
new_line = re.sub("(\033\\[(0;)?[0-9]*[A-z]?(;[0-9])?m?)", " ", new_line)
|
|
|
|
new_line = re.sub("[A-z]{2}\b\b", "", new_line)
|
2022-04-11 05:23:55 +00:00
|
|
|
highlighted = self.helper.log_colors(html.escape(new_line))
|
2021-08-10 20:17:56 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug("Broadcasting new virtual terminal line")
|
2021-08-10 20:17:56 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# TODO: Do not send data to clients who do not have permission to view
|
|
|
|
# this server's console
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page_params(
|
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": self.server_id},
|
|
|
|
"vterm_new_line",
|
|
|
|
{"line": highlighted + "<br />"},
|
|
|
|
)
|
2021-08-10 20:17:56 +00:00
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-03-03 21:01:12 +00:00
|
|
|
# Minecraft Server Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-05-30 20:40:34 +00:00
|
|
|
class ServerInstance:
|
2022-05-26 12:50:20 +00:00
|
|
|
server_object: Servers
|
2022-05-26 13:02:47 +00:00
|
|
|
helper: Helpers
|
2022-06-20 19:26:21 +00:00
|
|
|
file_helper: FileHelpers
|
2022-05-26 13:02:47 +00:00
|
|
|
management_helper: HelpersManagement
|
|
|
|
stats: Stats
|
2022-05-26 12:50:20 +00:00
|
|
|
stats_helper: HelperServerStats
|
|
|
|
|
2022-06-20 19:26:21 +00:00
|
|
|
def __init__(self, server_id, helper, management_helper, stats, file_helper):
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper = helper
|
2022-06-20 19:26:21 +00:00
|
|
|
self.file_helper = file_helper
|
2022-04-11 05:23:55 +00:00
|
|
|
self.management_helper = management_helper
|
2020-08-17 02:47:53 +00:00
|
|
|
# holders for our process
|
|
|
|
self.process = None
|
|
|
|
self.line = False
|
|
|
|
self.start_time = None
|
|
|
|
self.server_command = None
|
|
|
|
self.server_path = None
|
|
|
|
self.server_thread = None
|
|
|
|
self.settings = None
|
|
|
|
self.updating = False
|
2022-05-26 12:50:20 +00:00
|
|
|
self.server_id = server_id
|
2021-07-25 01:09:39 +00:00
|
|
|
self.jar_update_url = None
|
2020-08-17 02:47:53 +00:00
|
|
|
self.name = None
|
|
|
|
self.is_crashed = False
|
|
|
|
self.restart_count = 0
|
2021-03-22 04:02:18 +00:00
|
|
|
self.stats = stats
|
2022-05-25 13:54:35 +00:00
|
|
|
self.server_object = HelperServers.get_server_obj(self.server_id)
|
|
|
|
self.stats_helper = HelperServerStats(self.server_id)
|
2022-06-20 21:16:56 +00:00
|
|
|
self.last_backup_failed = False
|
2023-10-07 21:50:56 +00:00
|
|
|
self.server_registry = CollectorRegistry()
|
|
|
|
|
2023-05-29 23:34:40 +00:00
|
|
|
try:
|
|
|
|
with open(
|
|
|
|
os.path.join(self.server_object.path, "db_stats", "players_cache.json"),
|
|
|
|
"r",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as f:
|
|
|
|
self.player_cache = list(json.load(f).values())
|
|
|
|
except:
|
|
|
|
self.player_cache = []
|
2022-06-13 18:29:25 +00:00
|
|
|
try:
|
2023-01-17 15:27:55 +00:00
|
|
|
self.tz = get_localzone()
|
2023-03-09 16:09:58 +00:00
|
|
|
except ZoneInfoNotFoundError as e:
|
2022-06-13 18:31:53 +00:00
|
|
|
logger.error(
|
|
|
|
"Could not capture time zone from system. Falling back to Europe/London"
|
2023-03-09 16:09:58 +00:00
|
|
|
f" error: {e}"
|
2022-06-13 18:31:53 +00:00
|
|
|
)
|
2023-02-10 21:36:20 +00:00
|
|
|
self.tz = ZoneInfo("Europe/London")
|
2023-01-17 15:27:55 +00:00
|
|
|
self.server_scheduler = BackgroundScheduler(timezone=str(self.tz))
|
2023-02-05 18:08:04 +00:00
|
|
|
self.dir_scheduler = BackgroundScheduler(timezone=str(self.tz))
|
2023-10-07 21:50:56 +00:00
|
|
|
self.init_registries()
|
2022-01-28 01:43:23 +00:00
|
|
|
self.server_scheduler.start()
|
2023-02-05 18:08:04 +00:00
|
|
|
self.dir_scheduler.start()
|
|
|
|
self.start_dir_calc_task()
|
2022-01-12 06:24:03 +00:00
|
|
|
self.is_backingup = False
|
2022-03-23 02:50:12 +00:00
|
|
|
# Reset crash and update at initialization
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.server_crash_reset()
|
|
|
|
self.stats_helper.set_update(False)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-03-23 02:50:12 +00:00
|
|
|
# Minecraft Server Management
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-06-12 15:13:39 +00:00
|
|
|
def update_server_instance(self):
|
|
|
|
server_data: Servers = HelperServers.get_server_obj(self.server_id)
|
|
|
|
self.server_path = server_data.path
|
|
|
|
self.jar_update_url = server_data.executable_update_url
|
|
|
|
self.name = server_data.server_name
|
|
|
|
self.server_object = server_data
|
2023-01-30 23:31:46 +00:00
|
|
|
self.stats_helper.select_database()
|
|
|
|
self.reload_server_settings()
|
2022-06-12 15:13:39 +00:00
|
|
|
|
2020-10-07 13:19:47 +00:00
|
|
|
def reload_server_settings(self):
|
2022-04-14 02:10:25 +00:00
|
|
|
server_data = HelperServers.get_server_data_by_id(self.server_id)
|
2020-10-07 13:19:47 +00:00
|
|
|
self.settings = server_data
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
def do_server_setup(self, server_data_obj):
|
2022-04-14 02:10:25 +00:00
|
|
|
server_id = server_data_obj["server_id"]
|
|
|
|
server_name = server_data_obj["server_name"]
|
|
|
|
auto_start = server_data_obj["auto_start"]
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-04-14 02:10:25 +00:00
|
|
|
f"Creating Server object: {server_id} | "
|
|
|
|
f"Server Name: {server_name} | "
|
|
|
|
f"Auto Start: {auto_start}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
self.server_id = server_id
|
|
|
|
self.name = server_name
|
2020-08-17 02:47:53 +00:00
|
|
|
self.settings = server_data_obj
|
|
|
|
|
2022-03-03 22:13:40 +00:00
|
|
|
self.record_server_stats()
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
# build our server run command
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if server_data_obj["auto_start"]:
|
|
|
|
delay = int(self.settings["auto_start_delay"])
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Scheduling server {self.name} to start in {delay} seconds")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Scheduling server {self.name} to start in {delay} seconds")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.run_scheduled_server,
|
|
|
|
"interval",
|
|
|
|
seconds=delay,
|
|
|
|
id=str(self.server_id),
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
def run_scheduled_server(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Starting server ID: {self.server_id} - {self.name}")
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Starting server ID: {self.server_id} - {self.name}")
|
2022-03-23 02:50:12 +00:00
|
|
|
# Sets waiting start to false since we're attempting to start the server.
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.set_waiting_start(False)
|
2021-11-27 22:10:43 +00:00
|
|
|
self.run_threaded_server(None)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
# remove the scheduled job since it's ran
|
2022-01-11 23:30:06 +00:00
|
|
|
return self.server_scheduler.remove_job(str(self.server_id))
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-11-19 18:45:18 +00:00
|
|
|
def run_threaded_server(self, user_id, forge_install=False):
|
2020-08-17 02:47:53 +00:00
|
|
|
# start the server
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_thread = threading.Thread(
|
|
|
|
target=self.start_server,
|
|
|
|
daemon=True,
|
2022-11-19 18:45:18 +00:00
|
|
|
args=(
|
|
|
|
user_id,
|
|
|
|
forge_install,
|
|
|
|
),
|
2022-03-23 02:50:12 +00:00
|
|
|
name=f"{self.server_id}_server_thread",
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
self.server_thread.start()
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
# Register an shedule for polling server stats when running
|
2022-03-03 21:01:12 +00:00
|
|
|
logger.info(f"Polling server statistics {self.name} every {5} seconds")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Polling server statistics {self.name} every {5} seconds")
|
2022-03-08 02:48:06 +00:00
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.realtime_stats,
|
|
|
|
"interval",
|
|
|
|
seconds=5,
|
|
|
|
id="stats_" + str(self.server_id),
|
|
|
|
)
|
2022-03-08 02:48:06 +00:00
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.remove_job("stats_" + str(self.server_id))
|
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.realtime_stats,
|
|
|
|
"interval",
|
|
|
|
seconds=5,
|
|
|
|
id="stats_" + str(self.server_id),
|
|
|
|
)
|
2023-09-02 20:19:17 +00:00
|
|
|
logger.info(f"Saving server statistics {self.name} every {30} seconds")
|
|
|
|
Console.info(f"Saving server statistics {self.name} every {30} seconds")
|
|
|
|
try:
|
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.record_server_stats,
|
|
|
|
"interval",
|
|
|
|
seconds=30,
|
|
|
|
id="save_stats_" + str(self.server_id),
|
|
|
|
)
|
2023-09-29 00:00:48 +00:00
|
|
|
except ConflictingIdError:
|
|
|
|
self.server_scheduler.remove_job("save_stats_" + str(self.server_id))
|
2023-09-02 20:19:17 +00:00
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.record_server_stats,
|
|
|
|
"interval",
|
|
|
|
seconds=30,
|
2023-09-29 00:00:48 +00:00
|
|
|
id="save_stats_" + str(self.server_id),
|
2023-09-02 20:19:17 +00:00
|
|
|
)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
def setup_server_run_command(self):
|
|
|
|
# configure the server
|
2022-04-11 05:23:55 +00:00
|
|
|
server_exec_path = Helpers.get_os_understandable_path(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.settings["executable"]
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.server_command = Helpers.cmdparse(self.settings["execution_command"])
|
2022-06-15 02:33:02 +00:00
|
|
|
if self.helper.is_os_windows() and self.server_command[0] == "java":
|
|
|
|
logger.info(
|
2022-06-15 03:11:40 +00:00
|
|
|
"Detected nebulous java in start command. "
|
|
|
|
"Replacing with full java path."
|
2022-06-15 02:33:02 +00:00
|
|
|
)
|
2022-09-19 15:13:35 +00:00
|
|
|
oracle_path = shutil.which("java")
|
|
|
|
if oracle_path:
|
2022-09-19 22:49:16 +00:00
|
|
|
# Checks for Oracle Java. Only Oracle Java's helper will cause a re-exec
|
2022-09-19 15:13:35 +00:00
|
|
|
if "/Oracle/Java/" in str(self.helper.wtol_path(oracle_path)):
|
|
|
|
logger.info(
|
|
|
|
"Oracle Java detected. Changing"
|
|
|
|
" start command to avoid re-exec."
|
2022-07-30 17:17:01 +00:00
|
|
|
)
|
2022-09-19 15:13:35 +00:00
|
|
|
which_java_raw = self.helper.which_java()
|
|
|
|
try:
|
|
|
|
java_path = which_java_raw + "\\bin\\java"
|
|
|
|
except TypeError:
|
|
|
|
logger.warning(
|
|
|
|
"Could not find java in the registry even though"
|
|
|
|
" Oracle java is installed."
|
|
|
|
" Re-exec expected, but we have no"
|
|
|
|
" other options. CPU stats will not work for process."
|
|
|
|
)
|
|
|
|
java_path = ""
|
|
|
|
if str(which_java_raw) != str(
|
|
|
|
self.helper.get_servers_root_dir
|
|
|
|
) or str(self.helper.get_servers_root_dir) in str(which_java_raw):
|
|
|
|
if java_path != "":
|
|
|
|
self.server_command[0] = java_path
|
|
|
|
else:
|
|
|
|
logger.critcal(
|
|
|
|
"Possible attack detected. User attempted to exec "
|
|
|
|
"java binary from server directory."
|
|
|
|
)
|
|
|
|
return
|
2022-04-11 05:23:55 +00:00
|
|
|
self.server_path = Helpers.get_os_understandable_path(self.settings["path"])
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
# let's do some quick checking to make sure things actually exists
|
|
|
|
full_path = os.path.join(self.server_path, server_exec_path)
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.check_file_exists(full_path):
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.critical(
|
|
|
|
f"Server executable path: {full_path} does not seem to exist"
|
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.critical(
|
2022-03-23 02:50:12 +00:00
|
|
|
f"Server executable path: {full_path} does not seem to exist"
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.check_path_exists(self.server_path):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.critical(f"Server path: {self.server_path} does not seem to exits")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.critical(f"Server path: {self.server_path} does not seem to exits")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.check_writeable(self.server_path):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.critical(f"Unable to write/access {self.server_path}")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.critical(f"Unable to write/access {self.server_path}")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2023-09-28 21:18:07 +00:00
|
|
|
@callback
|
2022-11-19 18:45:18 +00:00
|
|
|
def start_server(self, user_id, forge_install=False):
|
2021-11-27 22:10:43 +00:00
|
|
|
if not user_id:
|
2022-04-11 05:23:55 +00:00
|
|
|
user_lang = self.helper.get_setting("language")
|
2021-11-27 22:10:43 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
user_lang = HelperUsers.get_user_lang_by_id(user_id)
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2023-02-23 16:57:22 +00:00
|
|
|
# Checks if user is currently attempting to move global server
|
|
|
|
# dir
|
|
|
|
if self.helper.dir_migration:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2023-02-23 16:57:22 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
|
|
|
"error": self.helper.translation.translate(
|
|
|
|
"error",
|
|
|
|
"migration",
|
|
|
|
user_lang,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
2022-11-19 19:41:15 +00:00
|
|
|
if self.stats_helper.get_import_status() and not forge_install:
|
2022-03-04 00:36:36 +00:00
|
|
|
if user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
2022-04-11 05:23:55 +00:00
|
|
|
"error": self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"error", "not-downloaded", user_lang
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2022-03-04 00:36:36 +00:00
|
|
|
return False
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Start command detected. Reloading settings from DB for server {self.name}"
|
|
|
|
)
|
2021-07-25 15:28:09 +00:00
|
|
|
self.setup_server_run_command()
|
2020-08-17 02:47:53 +00:00
|
|
|
# fail safe in case we try to start something already running
|
|
|
|
if self.check_running():
|
|
|
|
logger.error("Server is already running - Cancelling Startup")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.error("Server is already running - Cancelling Startup")
|
2020-08-17 02:47:53 +00:00
|
|
|
return False
|
2021-07-26 16:11:46 +00:00
|
|
|
if self.check_update():
|
|
|
|
logger.error("Server is updating. Terminating startup.")
|
2021-07-25 21:04:10 +00:00
|
|
|
return False
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Launching Server {self.name} with command {self.server_command}")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Launching Server {self.name} with command {self.server_command}")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
# Checks for eula. Creates one if none detected.
|
2022-03-23 06:06:13 +00:00
|
|
|
# If EULA is detected and not set to true we offer to set it true.
|
2022-05-16 15:16:04 +00:00
|
|
|
e_flag = False
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.check_file_exists(os.path.join(self.settings["path"], "eula.txt")):
|
2022-05-16 15:16:04 +00:00
|
|
|
with open(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(self.settings["path"], "eula.txt"), "r", encoding="utf-8"
|
2022-05-16 15:16:04 +00:00
|
|
|
) as f:
|
|
|
|
line = f.readline().lower()
|
|
|
|
e_flag = line in [
|
|
|
|
"eula=true",
|
|
|
|
"eula = true",
|
|
|
|
"eula= true",
|
|
|
|
"eula =true",
|
|
|
|
]
|
2022-11-19 18:45:18 +00:00
|
|
|
# If this is a forge installer we're running we can bypass the eula checks.
|
|
|
|
if forge_install is True:
|
|
|
|
e_flag = True
|
2022-08-31 23:45:19 +00:00
|
|
|
if not e_flag and self.settings["type"] == "minecraft-java":
|
2021-11-27 23:43:14 +00:00
|
|
|
if user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id, "send_eula_bootbox", {"id": self.server_id}
|
|
|
|
)
|
2021-11-27 23:43:14 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
2022-03-23 06:06:13 +00:00
|
|
|
"Autostart failed due to EULA being false. "
|
|
|
|
"Agree not sent due to auto start."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-11-23 21:11:23 +00:00
|
|
|
return False
|
2022-04-11 05:23:55 +00:00
|
|
|
if Helpers.is_os_windows():
|
2021-02-13 05:47:30 +00:00
|
|
|
logger.info("Windows Detected")
|
2020-08-17 02:47:53 +00:00
|
|
|
else:
|
2021-09-25 19:29:28 +00:00
|
|
|
logger.info("Unix Detected")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Starting server in {self.server_path} with command: {self.server_command}"
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
# checks to make sure file is openable (downloaded) and exists.
|
2022-02-27 17:22:52 +00:00
|
|
|
try:
|
2022-05-16 15:16:04 +00:00
|
|
|
with open(
|
2022-03-23 02:50:12 +00:00
|
|
|
os.path.join(
|
|
|
|
self.server_path,
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperServers.get_server_data_by_id(self.server_id)["executable"],
|
2022-03-23 02:50:12 +00:00
|
|
|
),
|
|
|
|
"r",
|
|
|
|
encoding="utf-8",
|
2022-05-16 15:16:04 +00:00
|
|
|
):
|
|
|
|
# Can open the file
|
|
|
|
pass
|
2022-02-27 17:22:52 +00:00
|
|
|
|
|
|
|
except:
|
|
|
|
if user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
2022-04-11 05:23:55 +00:00
|
|
|
"error": self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"error", "not-downloaded", user_lang
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2022-02-27 17:22:52 +00:00
|
|
|
return
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if (
|
2022-04-11 05:23:55 +00:00
|
|
|
not Helpers.is_os_windows()
|
2022-04-14 02:10:25 +00:00
|
|
|
and HelperServers.get_server_type_by_id(self.server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
== "minecraft-bedrock"
|
|
|
|
):
|
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Bedrock and Unix detected for server {self.name}. "
|
|
|
|
f"Switching to appropriate execution string"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-02-26 16:59:19 +00:00
|
|
|
my_env = os.environ
|
|
|
|
my_env["LD_LIBRARY_PATH"] = self.server_path
|
|
|
|
try:
|
|
|
|
self.process = subprocess.Popen(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_command,
|
|
|
|
cwd=self.server_path,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
env=my_env,
|
|
|
|
)
|
2022-02-26 16:59:19 +00:00
|
|
|
except Exception as ex:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
|
|
|
f"Server {self.name} failed to start with error code: {ex}"
|
|
|
|
)
|
2022-02-26 16:25:08 +00:00
|
|
|
if user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
2022-04-11 05:23:55 +00:00
|
|
|
"error": self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"error", "start-error", user_lang
|
|
|
|
).format(self.name, ex)
|
|
|
|
},
|
|
|
|
)
|
2022-11-19 21:13:44 +00:00
|
|
|
if forge_install:
|
|
|
|
# Reset import status if failed while forge installing
|
|
|
|
self.stats_helper.finish_import()
|
2022-02-26 16:59:19 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.process = subprocess.Popen(
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_command,
|
|
|
|
cwd=self.server_path,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
)
|
2022-02-26 16:59:19 +00:00
|
|
|
except Exception as ex:
|
2022-03-23 02:50:12 +00:00
|
|
|
# Checks for java on initial fail
|
2023-02-14 18:59:42 +00:00
|
|
|
if not self.helper.detect_java():
|
2022-02-26 16:59:19 +00:00
|
|
|
if user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
2022-04-11 05:23:55 +00:00
|
|
|
"error": self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"error", "noJava", user_lang
|
|
|
|
).format(self.name)
|
|
|
|
},
|
|
|
|
)
|
2022-02-26 16:59:19 +00:00
|
|
|
return False
|
2022-06-14 12:40:57 +00:00
|
|
|
logger.error(
|
|
|
|
f"Server {self.name} failed to start with error code: {ex}"
|
|
|
|
)
|
2022-02-26 16:59:19 +00:00
|
|
|
if user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
2022-04-11 05:23:55 +00:00
|
|
|
"error": self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"error", "start-error", user_lang
|
|
|
|
).format(self.name, ex)
|
|
|
|
},
|
|
|
|
)
|
2022-11-19 21:13:44 +00:00
|
|
|
if forge_install:
|
|
|
|
# Reset import status if failed while forge installing
|
|
|
|
self.stats_helper.finish_import()
|
2021-11-29 21:22:46 +00:00
|
|
|
return False
|
2022-01-09 18:14:59 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
out_buf = ServerOutBuf(self.helper, self.process, self.server_id)
|
2021-08-10 20:17:56 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(f"Starting virtual terminal listener for server {self.name}")
|
|
|
|
threading.Thread(
|
|
|
|
target=out_buf.check, daemon=True, name=f"{self.server_id}_virtual_terminal"
|
|
|
|
).start()
|
2021-08-18 16:50:13 +00:00
|
|
|
|
2021-02-13 05:47:30 +00:00
|
|
|
self.is_crashed = False
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.server_crash_reset()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.start_time = str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2021-09-25 19:29:28 +00:00
|
|
|
if self.process.poll() is None:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Server {self.name} running with PID {self.process.pid}")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Server {self.name} running with PID {self.process.pid}")
|
2021-02-13 05:47:30 +00:00
|
|
|
self.is_crashed = False
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.server_crash_reset()
|
2022-03-03 21:01:12 +00:00
|
|
|
self.record_server_stats()
|
2022-01-26 01:45:30 +00:00
|
|
|
check_internet_thread = threading.Thread(
|
2022-03-23 02:50:12 +00:00
|
|
|
target=self.check_internet_thread,
|
|
|
|
daemon=True,
|
|
|
|
args=(
|
|
|
|
user_id,
|
|
|
|
user_lang,
|
|
|
|
),
|
|
|
|
name=f"{self.name}_Internet",
|
|
|
|
)
|
2022-01-09 18:14:59 +00:00
|
|
|
check_internet_thread.start()
|
2022-03-23 02:50:12 +00:00
|
|
|
# Checks if this is the servers first run.
|
2022-05-26 12:50:20 +00:00
|
|
|
if self.stats_helper.get_first_run():
|
|
|
|
self.stats_helper.set_first_run()
|
|
|
|
loc_server_port = self.stats_helper.get_server_stats()["server_port"]
|
2022-03-23 02:50:12 +00:00
|
|
|
# Sends port reminder message.
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
|
|
|
{
|
2022-04-11 05:23:55 +00:00
|
|
|
"error": self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"error", "portReminder", user_lang
|
|
|
|
).format(self.name, loc_server_port)
|
|
|
|
},
|
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
2022-01-22 01:43:44 +00:00
|
|
|
for user in server_users:
|
|
|
|
if user != user_id:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-01-09 18:14:59 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
2022-01-22 01:43:44 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2020-08-17 02:47:53 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server PID {self.process.pid} died right after starting "
|
|
|
|
f"- is this a server config issue?"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.critical(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server PID {self.process.pid} died right after starting "
|
|
|
|
f"- is this a server config issue?"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if self.settings["crash_detection"]:
|
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server {self.name} has crash detection enabled "
|
|
|
|
f"- starting watcher task"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server {self.name} has crash detection enabled "
|
|
|
|
f"- starting watcher task"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.detect_crash, "interval", seconds=30, id=f"c_{self.server_id}"
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-11-19 18:45:18 +00:00
|
|
|
# If this is a forge install we'll call the watcher to do the things
|
|
|
|
if forge_install:
|
|
|
|
self.forge_install_watcher()
|
|
|
|
|
2021-12-10 04:52:37 +00:00
|
|
|
def check_internet_thread(self, user_id, user_lang):
|
2021-12-09 22:58:36 +00:00
|
|
|
if user_id:
|
2022-04-11 05:23:55 +00:00
|
|
|
if not Helpers.check_internet():
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user_id,
|
|
|
|
"send_start_error",
|
2022-04-11 10:08:36 +00:00
|
|
|
{
|
|
|
|
"error": self.helper.translation.translate(
|
|
|
|
"error", "internet", user_lang
|
|
|
|
)
|
|
|
|
},
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-11-19 18:45:18 +00:00
|
|
|
def forge_install_watcher(self):
|
|
|
|
# Enter for install if that parameter is true
|
|
|
|
while True:
|
|
|
|
# We'll watch the process
|
|
|
|
if self.process.poll() is None:
|
|
|
|
# IF process still has not exited we'll keep looping
|
|
|
|
time.sleep(5)
|
|
|
|
Console.debug("Installing Forge...")
|
|
|
|
else:
|
|
|
|
# Process has exited. Lets do some work to setup the new
|
|
|
|
# run command.
|
|
|
|
# Let's grab the server object we're going to update.
|
2022-12-22 18:32:56 +00:00
|
|
|
server_obj: Servers = HelperServers.get_server_obj(self.server_id)
|
2022-11-19 22:20:21 +00:00
|
|
|
|
2022-11-19 18:45:18 +00:00
|
|
|
# The forge install is done so we can delete that install file.
|
|
|
|
os.remove(os.path.join(server_obj.path, server_obj.executable))
|
2022-11-19 22:20:21 +00:00
|
|
|
|
|
|
|
# We need to grab the exact forge version number.
|
|
|
|
# We know we can find it here in the run.sh/bat script.
|
2022-12-22 02:55:21 +00:00
|
|
|
try:
|
2022-12-22 18:32:56 +00:00
|
|
|
# Getting the forge version from the executable command
|
|
|
|
version = re.findall(
|
2024-05-05 00:29:11 +00:00
|
|
|
r"forge-installer-([0-9\.]+)((?:)|"
|
|
|
|
r"(?:-([0-9\.]+)-[a-zA-Z]+)).jar",
|
2022-12-22 18:32:56 +00:00
|
|
|
server_obj.execution_command,
|
|
|
|
)
|
|
|
|
version_param = version[0][0].split(".")
|
|
|
|
version_major = int(version_param[0])
|
|
|
|
version_minor = int(version_param[1])
|
2024-02-08 23:22:42 +00:00
|
|
|
if len(version_param) > 2:
|
2024-02-08 23:10:26 +00:00
|
|
|
version_sub = int(version_param[2])
|
|
|
|
else:
|
|
|
|
version_sub = 0
|
2022-12-22 18:32:56 +00:00
|
|
|
|
|
|
|
# Checking which version we are with
|
|
|
|
if version_major <= 1 and version_minor < 17:
|
|
|
|
# OLD VERSION < 1.17
|
|
|
|
|
|
|
|
# Retrieving the executable jar filename
|
|
|
|
file_path = glob.glob(
|
|
|
|
f"{server_obj.path}/forge-{version[0][0]}*.jar"
|
|
|
|
)[0]
|
|
|
|
file_name = re.findall(
|
|
|
|
r"(forge[-0-9.]+.jar)",
|
|
|
|
file_path,
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
# Let's set the proper server executable
|
|
|
|
server_obj.executable = os.path.join(file_name)
|
|
|
|
|
|
|
|
# Get memory values
|
|
|
|
memory_values = re.findall(
|
|
|
|
r"-Xms([A-Z0-9\.]+) -Xmx([A-Z0-9\.]+)",
|
|
|
|
server_obj.execution_command,
|
2022-12-22 02:55:21 +00:00
|
|
|
)
|
2022-11-19 22:20:21 +00:00
|
|
|
|
2022-12-22 18:32:56 +00:00
|
|
|
# Now lets set up the new run command.
|
|
|
|
# This is based off the run.sh/bat that
|
|
|
|
# Forge uses in 1.17 and <
|
|
|
|
execution_command = (
|
|
|
|
f"java -Xms{memory_values[0][0]} -Xmx{memory_values[0][1]}"
|
|
|
|
f' -jar "{file_name}" nogui'
|
|
|
|
)
|
|
|
|
server_obj.execution_command = execution_command
|
2024-01-16 18:53:50 +00:00
|
|
|
Console.debug(SUCCESSMSG)
|
2022-11-19 22:20:21 +00:00
|
|
|
|
2024-02-08 23:10:26 +00:00
|
|
|
elif version_major <= 1 and version_minor <= 20 and version_sub < 3:
|
|
|
|
# NEW VERSION >= 1.17 and <= 1.20.2
|
2024-01-03 20:10:33 +00:00
|
|
|
# (no jar file in server dir, only run.bat and run.sh)
|
2022-12-22 18:32:56 +00:00
|
|
|
|
|
|
|
run_file_path = ""
|
|
|
|
if self.helper.is_os_windows():
|
|
|
|
run_file_path = os.path.join(server_obj.path, "run.bat")
|
|
|
|
else:
|
|
|
|
run_file_path = os.path.join(server_obj.path, "run.sh")
|
|
|
|
|
|
|
|
if Helpers.check_file_perms(run_file_path) and os.path.isfile(
|
|
|
|
run_file_path
|
|
|
|
):
|
|
|
|
run_file = open(run_file_path, "r", encoding="utf-8")
|
|
|
|
run_file_text = run_file.read()
|
|
|
|
else:
|
|
|
|
Console.error(
|
|
|
|
"ERROR ! Forge install can't read the scripts files."
|
|
|
|
" Aborting ..."
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
# We get the server command parameters from forge script
|
|
|
|
server_command = re.findall(
|
|
|
|
r"java @([a-zA-Z0-9_\.]+)"
|
2023-01-02 20:59:29 +00:00
|
|
|
r" @([a-z.\/\-]+)([0-9.\-]+)"
|
|
|
|
r"\/\b([a-z_0-9]+\.txt)\b( .{2,4})?",
|
2022-12-22 18:32:56 +00:00
|
|
|
run_file_text,
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
version = server_command[2]
|
|
|
|
executable_path = f"{server_command[1]}{server_command[2]}/"
|
|
|
|
|
|
|
|
# Let's set the proper server executable
|
|
|
|
server_obj.executable = os.path.join(
|
|
|
|
f"{executable_path}forge-{version}-server.jar"
|
|
|
|
)
|
|
|
|
# Now lets set up the new run command.
|
|
|
|
# This is based off the run.sh/bat that
|
|
|
|
# Forge uses in 1.17 and <
|
|
|
|
execution_command = (
|
|
|
|
f"java @{server_command[0]}"
|
|
|
|
f" @{executable_path}{server_command[3]} nogui"
|
2023-02-25 19:39:11 +00:00
|
|
|
f" {server_command[4]}"
|
2022-12-22 18:32:56 +00:00
|
|
|
)
|
|
|
|
server_obj.execution_command = execution_command
|
2024-01-16 18:53:50 +00:00
|
|
|
Console.debug(SUCCESSMSG)
|
2024-01-03 20:10:33 +00:00
|
|
|
else:
|
2024-02-08 23:22:42 +00:00
|
|
|
# NEW VERSION >= 1.20.3
|
2024-01-03 20:10:33 +00:00
|
|
|
# (executable jar is back in server dir)
|
|
|
|
|
|
|
|
# Retrieving the executable jar filename
|
|
|
|
file_path = glob.glob(
|
|
|
|
f"{server_obj.path}/forge-{version[0][0]}*.jar"
|
|
|
|
)[0]
|
|
|
|
file_name = re.findall(
|
|
|
|
r"(forge-[\-0-9.]+-shim.jar)",
|
|
|
|
file_path,
|
|
|
|
)[0]
|
|
|
|
|
|
|
|
# Let's set the proper server executable
|
|
|
|
server_obj.executable = os.path.join(file_name)
|
|
|
|
|
|
|
|
# Get memory values
|
|
|
|
memory_values = re.findall(
|
|
|
|
r"-Xms([A-Z0-9\.]+) -Xmx([A-Z0-9\.]+)",
|
|
|
|
server_obj.execution_command,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Now lets set up the new run command.
|
|
|
|
# This is based off the run.sh/bat that
|
|
|
|
# Forge uses in 1.17 and <
|
|
|
|
execution_command = (
|
|
|
|
f"java -Xms{memory_values[0][0]} -Xmx{memory_values[0][1]}"
|
|
|
|
f' -jar "{file_name}" nogui'
|
|
|
|
)
|
|
|
|
server_obj.execution_command = execution_command
|
2024-01-16 18:53:50 +00:00
|
|
|
Console.debug(SUCCESSMSG)
|
2022-12-22 02:55:21 +00:00
|
|
|
except:
|
|
|
|
logger.debug("Could not find run file.")
|
|
|
|
# TODO Use regex to get version and rebuild simple execution
|
2022-11-19 18:45:18 +00:00
|
|
|
|
|
|
|
# We'll update the server with the new information now.
|
|
|
|
HelperServers.update_server(server_obj)
|
2022-11-19 19:41:15 +00:00
|
|
|
self.stats_helper.finish_import()
|
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
|
|
|
|
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-11-19 18:45:18 +00:00
|
|
|
break
|
|
|
|
|
2022-01-31 02:04:08 +00:00
|
|
|
def stop_crash_detection(self):
|
2022-03-23 06:06:13 +00:00
|
|
|
# This is only used if the crash detection settings change
|
|
|
|
# while the server is running.
|
2022-01-31 02:04:08 +00:00
|
|
|
if self.check_running():
|
|
|
|
logger.info(f"Detected crash detection shut off for server {self.name}")
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.remove_job("c_" + str(self.server_id))
|
2022-01-31 02:04:08 +00:00
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Removing crash watcher for server {self.name} failed. "
|
|
|
|
f"Assuming it was never started."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-31 02:04:08 +00:00
|
|
|
|
|
|
|
def start_crash_detection(self):
|
2022-03-23 06:06:13 +00:00
|
|
|
# This is only used if the crash detection settings change
|
|
|
|
# while the server is running.
|
2022-01-31 02:04:08 +00:00
|
|
|
if self.check_running():
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server {self.name} has crash detection enabled "
|
|
|
|
f"- starting watcher task"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server {self.name} has crash detection enabled "
|
|
|
|
"- starting watcher task"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-06-13 20:13:18 +00:00
|
|
|
try:
|
|
|
|
self.server_scheduler.add_job(
|
|
|
|
self.detect_crash, "interval", seconds=30, id=f"c_{self.server_id}"
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
logger.info(f"Job with id c_{self.server_id} already running...")
|
2022-01-31 02:04:08 +00:00
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
def stop_threaded_server(self):
|
|
|
|
self.stop_server()
|
|
|
|
|
|
|
|
if self.server_thread:
|
|
|
|
self.server_thread.join()
|
|
|
|
|
2023-09-28 21:18:07 +00:00
|
|
|
@callback
|
2020-08-17 02:47:53 +00:00
|
|
|
def stop_server(self):
|
2021-09-25 19:29:28 +00:00
|
|
|
running = self.check_running()
|
|
|
|
if not running:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Can't stop server {self.name} if it's not running")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Can't stop server {self.name} if it's not running")
|
2021-09-25 19:29:28 +00:00
|
|
|
return
|
2023-02-10 19:02:26 +00:00
|
|
|
if self.settings["crash_detection"]:
|
|
|
|
# remove crash detection watcher
|
|
|
|
logger.info(f"Removing crash watcher for server {self.name}")
|
|
|
|
try:
|
|
|
|
self.server_scheduler.remove_job("c_" + str(self.server_id))
|
|
|
|
except:
|
|
|
|
logger.error(
|
|
|
|
f"Removing crash watcher for server {self.name} failed. "
|
|
|
|
f"Assuming it was never started."
|
|
|
|
)
|
|
|
|
if self.settings["stop_command"]:
|
2023-10-04 16:45:23 +00:00
|
|
|
logger.info(f"Stop command requested for {self.settings['server_name']}.")
|
2023-02-10 19:02:26 +00:00
|
|
|
self.send_command(self.settings["stop_command"])
|
2023-05-29 23:34:40 +00:00
|
|
|
self.write_player_cache()
|
2023-02-10 19:02:26 +00:00
|
|
|
else:
|
|
|
|
# windows will need to be handled separately for Ctrl+C
|
|
|
|
self.process.terminate()
|
2022-04-14 02:10:25 +00:00
|
|
|
i = 0
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2021-09-25 19:29:28 +00:00
|
|
|
# caching the name and pid number
|
|
|
|
server_name = self.name
|
|
|
|
server_pid = self.process.pid
|
2022-08-21 16:41:28 +00:00
|
|
|
self.shutdown_timeout = self.settings["shutdown_timeout"]
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2021-09-25 19:29:28 +00:00
|
|
|
while running:
|
2022-04-14 02:10:25 +00:00
|
|
|
i += 1
|
2022-08-21 16:41:28 +00:00
|
|
|
ttk = int(self.shutdown_timeout - (i * 2))
|
2022-08-21 17:00:24 +00:00
|
|
|
if i <= self.shutdown_timeout / 2:
|
|
|
|
logstr = (
|
|
|
|
f"Server {server_name} is still running "
|
|
|
|
"- waiting 2s to see if it stops"
|
|
|
|
f"({ttk} "
|
|
|
|
f"seconds until force close)"
|
|
|
|
)
|
|
|
|
logger.info(logstr)
|
|
|
|
Console.info(logstr)
|
2021-09-25 19:29:28 +00:00
|
|
|
running = self.check_running()
|
|
|
|
time.sleep(2)
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2021-09-25 19:29:28 +00:00
|
|
|
# if we haven't closed in 60 seconds, let's just slam down on the PID
|
2022-08-21 16:41:28 +00:00
|
|
|
if i >= round(self.shutdown_timeout / 2, 0):
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Server {server_name} is still running - Forcing the process down"
|
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 02:50:12 +00:00
|
|
|
f"Server {server_name} is still running - Forcing the process down"
|
|
|
|
)
|
2021-09-25 19:29:28 +00:00
|
|
|
self.kill()
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Stopped Server {server_name} with PID {server_pid}")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Stopped Server {server_name} with PID {server_pid}")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-08-24 23:11:17 +00:00
|
|
|
# massive resetting of variables
|
|
|
|
self.cleanup_server_object()
|
2022-04-14 02:10:25 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2022-06-10 23:04:34 +00:00
|
|
|
try:
|
|
|
|
# remove the stats polling job since server is stopped
|
|
|
|
self.server_scheduler.remove_job("stats_" + str(self.server_id))
|
|
|
|
except JobLookupError as e:
|
|
|
|
logger.error(
|
|
|
|
f"Could not remove job with id stats_{self.server_id} due"
|
|
|
|
+ f" to error: {e}"
|
|
|
|
)
|
2022-03-03 21:01:12 +00:00
|
|
|
self.record_server_stats()
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2022-01-22 01:43:44 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "send_start_reload", {})
|
2022-01-22 01:43:44 +00:00
|
|
|
|
2021-11-27 22:10:43 +00:00
|
|
|
def restart_threaded_server(self, user_id):
|
2024-05-25 18:40:14 +00:00
|
|
|
if self.is_backingup:
|
2022-07-30 18:47:07 +00:00
|
|
|
logger.info(
|
|
|
|
"Restart command detected. Supressing - server has"
|
|
|
|
" backup shutdown enabled and server is currently backing up."
|
|
|
|
)
|
|
|
|
return
|
2022-08-04 22:34:43 +00:00
|
|
|
# if not already running, let's just start
|
|
|
|
if not self.check_running():
|
|
|
|
self.run_threaded_server(user_id)
|
2020-08-24 23:11:17 +00:00
|
|
|
else:
|
2023-10-05 20:22:42 +00:00
|
|
|
logger.info(
|
|
|
|
f"Restart command detected. Sending stop command to {self.server_id}."
|
|
|
|
)
|
2022-08-04 22:34:43 +00:00
|
|
|
self.stop_threaded_server()
|
|
|
|
time.sleep(2)
|
|
|
|
self.run_threaded_server(user_id)
|
2020-08-24 23:11:17 +00:00
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
def cleanup_server_object(self):
|
|
|
|
self.start_time = None
|
2020-08-24 23:11:17 +00:00
|
|
|
self.restart_count = 0
|
|
|
|
self.is_crashed = False
|
|
|
|
self.updating = False
|
|
|
|
self.process = None
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-12-11 14:52:36 +00:00
|
|
|
def check_running(self):
|
2020-08-17 02:47:53 +00:00
|
|
|
# if process is None, we never tried to start
|
2021-09-25 19:29:28 +00:00
|
|
|
if self.process is None:
|
|
|
|
return False
|
|
|
|
poll = self.process.poll()
|
|
|
|
if poll is None:
|
|
|
|
return True
|
2022-06-14 12:40:57 +00:00
|
|
|
self.last_rc = poll
|
|
|
|
return False
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2023-09-28 21:18:07 +00:00
|
|
|
@callback
|
2020-08-17 02:47:53 +00:00
|
|
|
def send_command(self, command):
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_running() and command.lower() != "start":
|
|
|
|
logger.warning(f'Server not running, unable to send command "{command}"')
|
2020-08-17 02:47:53 +00:00
|
|
|
return False
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"COMMAND TIME: {command}")
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Sending command {command} to server")
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
# send it
|
2022-03-23 02:50:12 +00:00
|
|
|
self.process.stdin.write(f"{command}\n".encode("utf-8"))
|
2021-09-25 19:29:28 +00:00
|
|
|
self.process.stdin.flush()
|
2022-05-30 21:11:17 +00:00
|
|
|
return True
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2023-09-28 21:18:07 +00:00
|
|
|
@callback
|
2020-08-17 02:47:53 +00:00
|
|
|
def crash_detected(self, name):
|
2020-12-11 14:52:36 +00:00
|
|
|
# clear the old scheduled watcher task
|
2022-01-30 20:28:48 +00:00
|
|
|
self.server_scheduler.remove_job(f"c_{self.server_id}")
|
2022-03-04 20:09:13 +00:00
|
|
|
# remove the stats polling job since server is stopped
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.remove_job("stats_" + str(self.server_id))
|
2020-12-11 14:52:36 +00:00
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
# the server crashed, or isn't found - so let's reset things.
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
|
|
|
f"The server {name} seems to have vanished unexpectedly, did it crash?"
|
|
|
|
)
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if self.settings["crash_detection"]:
|
|
|
|
logger.warning(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"The server {name} has crashed and will be restarted. "
|
|
|
|
f"Restarting server"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.critical(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"The server {name} has crashed and will be restarted. "
|
|
|
|
f"Restarting server"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2023-05-17 23:05:59 +00:00
|
|
|
|
2021-11-27 22:10:43 +00:00
|
|
|
self.run_threaded_server(None)
|
2020-08-17 02:47:53 +00:00
|
|
|
return True
|
2022-06-14 12:40:57 +00:00
|
|
|
logger.critical(
|
|
|
|
f"The server {name} has crashed, "
|
|
|
|
f"crash detection is disabled and it will not be restarted"
|
|
|
|
)
|
|
|
|
Console.critical(
|
|
|
|
f"The server {name} has crashed, "
|
|
|
|
f"crash detection is disabled and it will not be restarted"
|
|
|
|
)
|
|
|
|
return False
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2023-09-28 21:18:07 +00:00
|
|
|
@callback
|
2021-09-25 19:29:28 +00:00
|
|
|
def kill(self):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Terminating server {self.server_id} and all child processes")
|
2022-04-13 01:52:53 +00:00
|
|
|
try:
|
|
|
|
process = psutil.Process(self.process.pid)
|
|
|
|
except NoSuchProcess:
|
|
|
|
logger.info(f"Cannot kill {self.process.pid} as we cannot find that pid.")
|
|
|
|
return
|
2020-08-17 02:47:53 +00:00
|
|
|
# for every sub process...
|
|
|
|
for proc in process.children(recursive=True):
|
2022-03-23 06:06:13 +00:00
|
|
|
# kill all the child processes
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Sending SIGKILL to server {proc.name}")
|
2020-08-17 02:47:53 +00:00
|
|
|
proc.kill()
|
|
|
|
# kill the main process we are after
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info("Sending SIGKILL to parent")
|
2022-06-10 22:29:47 +00:00
|
|
|
try:
|
|
|
|
self.server_scheduler.remove_job("stats_" + str(self.server_id))
|
|
|
|
except JobLookupError as e:
|
2022-06-10 22:31:48 +00:00
|
|
|
logger.error(
|
|
|
|
f"Could not remove job with id stats_{self.server_id} due"
|
|
|
|
+ f" to error: {e}"
|
|
|
|
)
|
2021-09-25 19:29:28 +00:00
|
|
|
self.process.kill()
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
def get_start_time(self):
|
2022-06-14 12:40:57 +00:00
|
|
|
return self.start_time if self.check_running() else False
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2021-09-13 19:03:47 +00:00
|
|
|
def get_pid(self):
|
2022-06-14 12:40:57 +00:00
|
|
|
return self.process.pid if self.process is not None else None
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2020-12-11 14:52:36 +00:00
|
|
|
def detect_crash(self):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Detecting possible crash for server: {self.name} ")
|
2020-12-11 14:52:36 +00:00
|
|
|
|
|
|
|
running = self.check_running()
|
|
|
|
|
|
|
|
# if all is okay, we just exit out
|
|
|
|
if running:
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
# check the exit code -- This could be a fix for /stop
|
2023-02-10 19:02:26 +00:00
|
|
|
if str(self.process.returncode) in self.settings["ignored_exits"].split(","):
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Process {self.process.pid} exited with code "
|
|
|
|
f"{self.process.returncode}. This is considered a clean exit"
|
|
|
|
f" supressing crash handling."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-02-11 06:16:45 +00:00
|
|
|
# cancel the watcher task
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.remove_job("c_" + str(self.server_id))
|
2022-05-25 18:26:13 +00:00
|
|
|
self.server_scheduler.remove_job("stats_" + str(self.server_id))
|
2022-02-11 06:16:45 +00:00
|
|
|
return
|
2020-12-11 14:52:36 +00:00
|
|
|
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.sever_crashed()
|
2020-12-11 14:52:36 +00:00
|
|
|
# if we haven't tried to restart more 3 or more times
|
|
|
|
if self.restart_count <= 3:
|
|
|
|
# start the server if needed
|
|
|
|
server_restarted = self.crash_detected(self.name)
|
|
|
|
|
|
|
|
if server_restarted:
|
|
|
|
# add to the restart count
|
|
|
|
self.restart_count = self.restart_count + 1
|
|
|
|
|
|
|
|
# we have tried to restart 4 times...
|
|
|
|
elif self.restart_count == 4:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.critical(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server {self.name} has been restarted {self.restart_count}"
|
|
|
|
f" times. It has crashed, not restarting."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.critical(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server {self.name} has been restarted {self.restart_count}"
|
|
|
|
f" times. It has crashed, not restarting."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2020-12-11 14:52:36 +00:00
|
|
|
|
2022-01-28 01:43:23 +00:00
|
|
|
self.restart_count = 0
|
2020-12-11 14:52:36 +00:00
|
|
|
self.is_crashed = True
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.sever_crashed()
|
2020-12-11 14:52:36 +00:00
|
|
|
|
|
|
|
# cancel the watcher task
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.remove_job("c_" + str(self.server_id))
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-12-11 14:52:36 +00:00
|
|
|
def remove_watcher_thread(self):
|
|
|
|
logger.info("Removing old crash detection watcher thread")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Removing old crash detection watcher thread")
|
2022-03-23 02:50:12 +00:00
|
|
|
self.server_scheduler.remove_job("c_" + str(self.server_id))
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2021-11-27 22:10:43 +00:00
|
|
|
def agree_eula(self, user_id):
|
2022-06-17 22:27:55 +00:00
|
|
|
eula_file = os.path.join(self.server_path, "eula.txt")
|
|
|
|
with open(eula_file, "w", encoding="utf-8") as f:
|
|
|
|
f.write("eula=true")
|
2021-11-27 22:10:43 +00:00
|
|
|
self.run_threaded_server(user_id)
|
2021-11-23 21:11:23 +00:00
|
|
|
|
2024-05-26 17:45:13 +00:00
|
|
|
def server_backup_threader(self, backup_id, update=False):
|
2024-05-26 19:10:03 +00:00
|
|
|
# Check to see if we're already backing up
|
|
|
|
if self.check_backup_by_id(backup_id):
|
|
|
|
return False
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
backup_thread = threading.Thread(
|
2024-05-25 18:40:14 +00:00
|
|
|
target=self.backup_server,
|
|
|
|
daemon=True,
|
2024-05-26 19:10:03 +00:00
|
|
|
name=f"backup_{backup_id}",
|
2024-05-26 17:45:13 +00:00
|
|
|
args=[backup_id, update],
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
|
|
|
logger.info(
|
|
|
|
f"Starting Backup Thread for server {self.settings['server_name']}."
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
if self.server_path is None:
|
2022-04-11 05:23:55 +00:00
|
|
|
self.server_path = Helpers.get_os_understandable_path(self.settings["path"])
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
"Backup Thread - Local server path not defined. "
|
|
|
|
"Setting local server path variable."
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2024-05-26 19:10:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
backup_thread.start()
|
|
|
|
except Exception as ex:
|
|
|
|
logger.error(f"Failed to start backup: {ex}")
|
2021-07-25 15:05:16 +00:00
|
|
|
return False
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Backup Thread started for server {self.settings['server_name']}.")
|
2021-07-25 15:05:16 +00:00
|
|
|
|
2024-03-07 16:06:39 +00:00
|
|
|
@callback
|
2024-05-26 17:45:13 +00:00
|
|
|
def backup_server(self, backup_id, update):
|
2022-06-20 21:16:56 +00:00
|
|
|
was_server_running = None
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Starting server {self.name} (ID {self.server_id}) backup")
|
2022-04-14 02:10:25 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
2024-06-23 23:05:55 +00:00
|
|
|
# Alert the start of the backup to the authorized users.
|
2022-03-01 03:40:11 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user,
|
|
|
|
"notification",
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.translation.translate(
|
2022-04-14 02:10:25 +00:00
|
|
|
"notify", "backupStarted", HelperUsers.get_user_lang_by_id(user)
|
2022-03-23 02:50:12 +00:00
|
|
|
).format(self.name),
|
|
|
|
)
|
2022-08-04 23:18:01 +00:00
|
|
|
time.sleep(3)
|
2024-06-23 23:05:55 +00:00
|
|
|
|
|
|
|
# Get the backup config
|
2024-05-25 18:40:14 +00:00
|
|
|
conf = HelpersManagement.get_backup_config(backup_id)
|
2024-06-23 23:05:55 +00:00
|
|
|
# Adjust the location to include the backup ID for destination.
|
2024-06-14 19:17:00 +00:00
|
|
|
backup_location = os.path.join(conf["backup_location"], conf["backup_id"])
|
2024-06-23 23:05:55 +00:00
|
|
|
|
|
|
|
# Check if the backup location even exists.
|
2024-05-25 18:40:14 +00:00
|
|
|
if not backup_location:
|
|
|
|
Console.critical("No backup path found. Canceling")
|
|
|
|
return None
|
2023-01-21 18:13:02 +00:00
|
|
|
if conf["before"]:
|
2024-06-23 23:05:55 +00:00
|
|
|
logger.debug(
|
|
|
|
"Found running server and send command option. Sending command"
|
|
|
|
)
|
|
|
|
self.send_command(conf["before"])
|
|
|
|
# Pause to let command run
|
|
|
|
time.sleep(5)
|
2023-01-21 18:16:01 +00:00
|
|
|
|
2022-06-20 21:16:56 +00:00
|
|
|
if conf["shutdown"]:
|
|
|
|
logger.info(
|
|
|
|
"Found shutdown preference. Delaying"
|
|
|
|
+ "backup start. Shutting down server."
|
|
|
|
)
|
2024-05-26 17:45:13 +00:00
|
|
|
if not update:
|
2024-06-23 23:05:55 +00:00
|
|
|
was_server_running = False
|
2024-05-26 17:45:13 +00:00
|
|
|
if self.check_running():
|
|
|
|
self.stop_server()
|
|
|
|
was_server_running = True
|
2022-06-20 21:16:56 +00:00
|
|
|
|
2024-05-25 18:40:14 +00:00
|
|
|
self.helper.ensure_dir_exists(backup_location)
|
2024-06-23 23:05:55 +00:00
|
|
|
|
2021-08-12 04:33:18 +00:00
|
|
|
try:
|
2022-03-23 06:06:13 +00:00
|
|
|
backup_filename = (
|
2024-05-25 18:40:14 +00:00
|
|
|
f"{backup_location}/"
|
2023-01-17 15:27:55 +00:00
|
|
|
f"{datetime.datetime.now().astimezone(self.tz).strftime('%Y-%m-%d_%H-%M-%S')}" # pylint: disable=line-too-long
|
2022-03-23 06:06:13 +00:00
|
|
|
)
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Creating backup of server '{self.settings['server_name']}'"
|
2022-03-23 06:06:13 +00:00
|
|
|
f" (ID#{self.server_id}, path={self.server_path}) "
|
|
|
|
f"at '{backup_filename}'"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2024-05-25 18:40:14 +00:00
|
|
|
excluded_dirs = HelpersManagement.get_excluded_backup_dirs(backup_id)
|
2022-04-11 05:23:55 +00:00
|
|
|
server_dir = Helpers.get_os_understandable_path(self.settings["path"])
|
2024-05-25 18:40:14 +00:00
|
|
|
|
|
|
|
self.file_helper.make_backup(
|
|
|
|
Helpers.get_os_understandable_path(backup_filename),
|
|
|
|
server_dir,
|
|
|
|
excluded_dirs,
|
|
|
|
self.server_id,
|
2024-05-25 20:33:28 +00:00
|
|
|
backup_id,
|
2024-05-25 19:12:55 +00:00
|
|
|
conf["backup_name"],
|
2024-05-25 18:40:14 +00:00
|
|
|
conf["compress"],
|
|
|
|
)
|
2022-03-23 02:50:12 +00:00
|
|
|
|
|
|
|
while (
|
2024-06-07 18:08:05 +00:00
|
|
|
len(self.list_backups(conf)) > conf["max_backups"]
|
2022-03-23 02:50:12 +00:00
|
|
|
and conf["max_backups"] > 0
|
|
|
|
):
|
2024-06-07 18:08:05 +00:00
|
|
|
backup_list = self.list_backups(conf)
|
2021-08-12 04:33:18 +00:00
|
|
|
oldfile = backup_list[0]
|
2024-05-25 18:40:14 +00:00
|
|
|
oldfile_path = f"{backup_location}/{oldfile['path']}"
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Removing old backup '{oldfile['path']}'")
|
2022-04-11 05:23:55 +00:00
|
|
|
os.remove(Helpers.get_os_understandable_path(oldfile_path))
|
2022-03-01 01:02:30 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.info(f"Backup of server: {self.name} completed")
|
2024-05-25 20:33:28 +00:00
|
|
|
results = {
|
|
|
|
"percent": 100,
|
|
|
|
"total_files": 0,
|
|
|
|
"current_file": 0,
|
|
|
|
"backup_id": backup_id,
|
|
|
|
}
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page_params(
|
2022-03-23 02:50:12 +00:00
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(self.server_id)},
|
|
|
|
"backup_status",
|
|
|
|
results,
|
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
2022-03-01 03:40:11 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user,
|
|
|
|
"notification",
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.translation.translate(
|
2022-03-23 02:50:12 +00:00
|
|
|
"notify",
|
|
|
|
"backupComplete",
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperUsers.get_user_lang_by_id(user),
|
2022-03-23 02:50:12 +00:00
|
|
|
).format(self.name),
|
|
|
|
)
|
2022-06-20 21:16:56 +00:00
|
|
|
if was_server_running:
|
|
|
|
logger.info(
|
|
|
|
"Backup complete. User had shutdown preference. Starting server."
|
|
|
|
)
|
2022-06-23 23:34:27 +00:00
|
|
|
self.run_threaded_server(HelperUsers.get_user_id_by_name("system"))
|
2022-03-01 03:40:11 +00:00
|
|
|
time.sleep(3)
|
2023-01-21 18:13:02 +00:00
|
|
|
if conf["after"]:
|
|
|
|
if self.check_running():
|
|
|
|
logger.debug(
|
|
|
|
"Found running server and send command option. Sending command"
|
|
|
|
)
|
|
|
|
self.send_command(conf["after"])
|
|
|
|
# pause to let people read message.
|
2024-05-26 19:38:38 +00:00
|
|
|
HelpersManagement.update_backup_config(
|
|
|
|
backup_id,
|
|
|
|
{"status": json.dumps({"status": "Standby", "message": ""})},
|
|
|
|
)
|
2023-01-21 18:13:02 +00:00
|
|
|
time.sleep(5)
|
2024-05-26 19:10:03 +00:00
|
|
|
except Exception as e:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.exception(
|
|
|
|
f"Failed to create backup of server {self.name} (ID {self.server_id})"
|
|
|
|
)
|
2024-05-25 20:33:28 +00:00
|
|
|
results = {
|
|
|
|
"percent": 100,
|
|
|
|
"total_files": 0,
|
|
|
|
"current_file": 0,
|
|
|
|
"backup_id": backup_id,
|
|
|
|
}
|
2023-08-09 21:47:53 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
|
|
|
WebSocketManager().broadcast_page_params(
|
2022-03-23 02:50:12 +00:00
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(self.server_id)},
|
|
|
|
"backup_status",
|
|
|
|
results,
|
|
|
|
)
|
2022-06-20 21:16:56 +00:00
|
|
|
if was_server_running:
|
|
|
|
logger.info(
|
|
|
|
"Backup complete. User had shutdown preference. Starting server."
|
|
|
|
)
|
2022-06-24 01:57:50 +00:00
|
|
|
self.run_threaded_server(HelperUsers.get_user_id_by_name("system"))
|
2024-05-26 19:10:03 +00:00
|
|
|
HelpersManagement.update_backup_config(
|
2024-05-26 19:20:18 +00:00
|
|
|
backup_id,
|
|
|
|
{"status": json.dumps({"status": "Failed", "message": f"{e}"})},
|
2024-05-26 19:10:03 +00:00
|
|
|
)
|
|
|
|
self.set_backup_status()
|
2022-03-13 13:26:38 +00:00
|
|
|
|
2022-06-20 21:16:56 +00:00
|
|
|
def last_backup_status(self):
|
|
|
|
return self.last_backup_failed
|
|
|
|
|
2024-05-26 19:10:03 +00:00
|
|
|
def set_backup_status(self):
|
|
|
|
backups = HelpersManagement.get_backups_by_server(self.server_id, True)
|
|
|
|
alert = False
|
|
|
|
for backup in backups:
|
|
|
|
if json.loads(backup.status)["status"] == "Failed":
|
|
|
|
alert = True
|
|
|
|
self.last_backup_failed = alert
|
|
|
|
|
2024-06-07 18:08:05 +00:00
|
|
|
def list_backups(self, backup_config: dict) -> list:
|
|
|
|
if not backup_config:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"Error putting backup file list for server with ID: {self.server_id}"
|
|
|
|
)
|
|
|
|
return []
|
2024-06-07 18:08:05 +00:00
|
|
|
backup_location = os.path.join(
|
|
|
|
backup_config["backup_location"], backup_config["backup_id"]
|
|
|
|
)
|
2022-06-14 12:40:57 +00:00
|
|
|
if not Helpers.check_path_exists(
|
2024-05-25 18:40:14 +00:00
|
|
|
Helpers.get_os_understandable_path(backup_location)
|
2022-06-14 12:40:57 +00:00
|
|
|
):
|
|
|
|
return []
|
|
|
|
files = Helpers.get_human_readable_files_sizes(
|
|
|
|
Helpers.list_dir_by_date(
|
2024-05-25 18:40:14 +00:00
|
|
|
Helpers.get_os_understandable_path(backup_location)
|
2022-06-14 12:40:57 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"path": os.path.relpath(
|
|
|
|
f["path"],
|
2024-05-25 18:40:14 +00:00
|
|
|
start=Helpers.get_os_understandable_path(backup_location),
|
2022-06-14 12:40:57 +00:00
|
|
|
),
|
|
|
|
"size": f["size"],
|
|
|
|
}
|
|
|
|
for f in files
|
2022-08-07 16:19:58 +00:00
|
|
|
if f["path"].endswith(".zip")
|
2022-06-14 12:40:57 +00:00
|
|
|
]
|
2021-07-25 01:09:39 +00:00
|
|
|
|
2023-09-28 21:18:07 +00:00
|
|
|
@callback
|
2021-07-25 01:09:39 +00:00
|
|
|
def jar_update(self):
|
2022-05-26 12:50:20 +00:00
|
|
|
self.stats_helper.set_update(True)
|
2022-03-23 02:50:12 +00:00
|
|
|
update_thread = threading.Thread(
|
2024-05-26 17:45:13 +00:00
|
|
|
target=self.threaded_jar_update, daemon=True, name=f"exe_update_{self.name}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-07-25 21:04:10 +00:00
|
|
|
update_thread.start()
|
|
|
|
|
2023-05-29 23:34:40 +00:00
|
|
|
def write_player_cache(self):
|
|
|
|
write_json = {}
|
|
|
|
for item in self.player_cache:
|
|
|
|
write_json[item["name"]] = item
|
|
|
|
with open(
|
|
|
|
os.path.join(self.server_path, "db_stats", "players_cache.json"),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as f:
|
|
|
|
f.write(json.dumps(write_json, indent=4))
|
|
|
|
logger.info("Cache file refreshed")
|
|
|
|
|
|
|
|
def cache_players(self):
|
|
|
|
server_players = self.get_server_players()
|
|
|
|
for p in self.player_cache[:]:
|
|
|
|
if p["status"] == "Online" and p["name"] not in server_players:
|
|
|
|
p["status"] = "Offline"
|
2023-05-30 04:01:54 +00:00
|
|
|
p["last_seen"] = datetime.datetime.now().strftime("%d/%m/%Y %H:%M")
|
2023-05-29 23:34:40 +00:00
|
|
|
elif p["name"] in server_players:
|
|
|
|
self.player_cache.remove(p)
|
|
|
|
for player in server_players:
|
|
|
|
if player == "Anonymous Player":
|
|
|
|
# Skip Anonymous Player
|
|
|
|
continue
|
|
|
|
if player in self.player_cache:
|
|
|
|
self.player_cache.remove(player)
|
|
|
|
self.player_cache.append(
|
|
|
|
{
|
|
|
|
"name": player,
|
|
|
|
"status": "Online",
|
2023-05-30 04:01:54 +00:00
|
|
|
"last_seen": datetime.datetime.now().strftime("%d/%m/%Y %H:%M"),
|
2023-05-29 23:34:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-07-26 16:11:46 +00:00
|
|
|
def check_update(self):
|
2022-06-14 12:40:57 +00:00
|
|
|
return self.stats_helper.get_server_stats()["updating"]
|
2021-07-26 16:11:46 +00:00
|
|
|
|
2024-05-26 17:45:13 +00:00
|
|
|
def threaded_jar_update(self):
|
2022-09-04 21:50:08 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(self.server_id)
|
2024-07-30 18:30:30 +00:00
|
|
|
# check to make sure a backup config actually exists before starting the update
|
|
|
|
if len(self.management_helper.get_backups_by_server(self.server_id, True)) <= 0:
|
|
|
|
for user in server_users:
|
|
|
|
WebSocketManager().broadcast_user(
|
|
|
|
user,
|
|
|
|
"notification",
|
|
|
|
"Backup config does not exist for "
|
|
|
|
+ self.name
|
|
|
|
+ ". canceling update.",
|
|
|
|
)
|
|
|
|
logger.error(f"Back config does not exist for {self.name}. Update Failed.")
|
|
|
|
self.stats_helper.set_update(False)
|
2024-08-06 18:59:10 +00:00
|
|
|
return
|
2022-04-14 02:10:25 +00:00
|
|
|
was_started = "-1"
|
2024-05-26 17:45:13 +00:00
|
|
|
# Get default backup configuration
|
|
|
|
backup_config = HelpersManagement.get_default_server_backup(self.server_id)
|
|
|
|
# start threaded backup
|
|
|
|
self.server_backup_threader(backup_config["backup_id"], True)
|
2022-03-23 02:50:12 +00:00
|
|
|
# checks if server is running. Calls shutdown if it is running.
|
2021-07-25 01:09:39 +00:00
|
|
|
if self.check_running():
|
2022-04-14 02:10:25 +00:00
|
|
|
was_started = True
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Server with PID {self.process.pid} is running. "
|
|
|
|
f"Sending shutdown command"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-07-25 01:09:39 +00:00
|
|
|
self.stop_threaded_server()
|
2021-07-25 15:05:16 +00:00
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
was_started = False
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
2021-07-27 01:45:33 +00:00
|
|
|
# There are clients
|
|
|
|
self.check_update()
|
2022-03-23 02:50:12 +00:00
|
|
|
message = (
|
|
|
|
'<a data-id="' + str(self.server_id) + '" class=""> UPDATING...</i></a>'
|
|
|
|
)
|
2022-09-04 21:50:08 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user_page(
|
2022-03-23 02:50:12 +00:00
|
|
|
"/panel/server_detail",
|
2022-09-04 21:50:08 +00:00
|
|
|
user,
|
2022-03-23 02:50:12 +00:00
|
|
|
"update_button_status",
|
|
|
|
{
|
|
|
|
"isUpdating": self.check_update(),
|
|
|
|
"server_id": self.server_id,
|
2022-04-14 02:10:25 +00:00
|
|
|
"wasRunning": was_started,
|
2022-03-23 02:50:12 +00:00
|
|
|
"string": message,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
current_executable = os.path.join(
|
2022-04-11 05:23:55 +00:00
|
|
|
Helpers.get_os_understandable_path(self.settings["path"]),
|
2022-03-23 02:50:12 +00:00
|
|
|
self.settings["executable"],
|
|
|
|
)
|
2024-05-26 19:10:03 +00:00
|
|
|
backing_up = True
|
2022-09-03 17:23:30 +00:00
|
|
|
# wait for backup
|
2024-05-26 19:10:03 +00:00
|
|
|
while backing_up:
|
|
|
|
# Check to see if we're already backing up
|
|
|
|
backing_up = self.check_backup_by_id(backup_config["backup_id"])
|
2024-06-14 22:17:50 +00:00
|
|
|
time.sleep(2)
|
2022-09-03 17:23:30 +00:00
|
|
|
|
|
|
|
# check if backup was successful
|
2024-06-14 22:17:50 +00:00
|
|
|
backup_status = json.loads(
|
2024-05-26 19:10:03 +00:00
|
|
|
HelpersManagement.get_backup_config(backup_config["backup_id"])["status"]
|
|
|
|
)["status"]
|
|
|
|
if backup_status == "Failed":
|
2022-09-03 17:23:30 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-09-03 17:23:30 +00:00
|
|
|
user,
|
|
|
|
"notification",
|
|
|
|
"Backup failed for " + self.name + ". canceling update.",
|
|
|
|
)
|
2024-07-30 18:30:30 +00:00
|
|
|
self.stats_helper.set_update(False)
|
2024-08-06 18:59:10 +00:00
|
|
|
return
|
2022-09-03 17:23:30 +00:00
|
|
|
|
|
|
|
# lets download the files
|
2022-08-31 15:59:33 +00:00
|
|
|
if HelperServers.get_server_type_by_id(self.server_id) != "minecraft-bedrock":
|
2024-02-20 01:19:39 +00:00
|
|
|
|
|
|
|
jar_dir = os.path.dirname(current_executable)
|
|
|
|
jar_file_name = os.path.basename(current_executable)
|
|
|
|
|
|
|
|
downloaded = FileHelpers.ssl_get_file(
|
|
|
|
self.settings["executable_update_url"], jar_dir, jar_file_name
|
2022-08-31 15:59:33 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# downloads zip from remote url
|
2022-08-31 23:59:46 +00:00
|
|
|
try:
|
|
|
|
bedrock_url = Helpers.get_latest_bedrock_url()
|
2024-02-20 01:19:39 +00:00
|
|
|
if bedrock_url:
|
|
|
|
# Use the new method for secure download
|
|
|
|
download_path = os.path.join(
|
|
|
|
self.settings["path"], "bedrock_server.zip"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2024-02-20 01:19:39 +00:00
|
|
|
downloaded = FileHelpers.ssl_get_file(
|
|
|
|
bedrock_url, self.settings["path"], "bedrock_server.zip"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-08-31 23:59:46 +00:00
|
|
|
|
2024-02-20 01:19:39 +00:00
|
|
|
if downloaded:
|
|
|
|
unzip_path = download_path
|
|
|
|
unzip_path = self.helper.wtol_path(unzip_path)
|
|
|
|
|
|
|
|
# unzips archive that was downloaded.
|
|
|
|
FileHelpers.unzip_file(unzip_path, server_update=True)
|
|
|
|
|
|
|
|
# adjusts permissions for execution if os is not windows
|
|
|
|
if not self.helper.is_os_windows():
|
|
|
|
os.chmod(
|
|
|
|
os.path.join(self.settings["path"], "bedrock_server"),
|
|
|
|
0o0744,
|
|
|
|
)
|
|
|
|
|
|
|
|
# we'll delete the zip we downloaded now
|
|
|
|
os.remove(download_path)
|
|
|
|
else:
|
|
|
|
logger.error("Failed to download the Bedrock server zip.")
|
|
|
|
downloaded = False
|
2022-08-31 23:59:46 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.critical(
|
|
|
|
f"Failed to download bedrock executable for update \n{e}"
|
|
|
|
)
|
2023-02-25 21:15:50 +00:00
|
|
|
downloaded = False
|
2021-07-25 01:09:39 +00:00
|
|
|
|
2022-09-03 17:23:30 +00:00
|
|
|
if downloaded:
|
|
|
|
logger.info("Executable updated successfully. Starting Server")
|
2021-08-12 03:33:18 +00:00
|
|
|
|
2022-09-03 17:23:30 +00:00
|
|
|
self.stats_helper.set_update(False)
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
2022-09-03 17:23:30 +00:00
|
|
|
# There are clients
|
|
|
|
self.check_update()
|
2022-01-20 18:37:45 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
user,
|
|
|
|
"notification",
|
|
|
|
"Executable update finished for " + self.name,
|
|
|
|
)
|
2022-09-03 17:42:30 +00:00
|
|
|
# sleep so first notif can completely run
|
2022-09-03 17:23:30 +00:00
|
|
|
time.sleep(3)
|
2022-09-04 21:50:08 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user_page(
|
2022-09-03 17:23:30 +00:00
|
|
|
"/panel/server_detail",
|
2022-09-04 21:50:08 +00:00
|
|
|
user,
|
2022-09-03 17:23:30 +00:00
|
|
|
"update_button_status",
|
|
|
|
{
|
|
|
|
"isUpdating": self.check_update(),
|
|
|
|
"server_id": self.server_id,
|
|
|
|
"wasRunning": was_started,
|
|
|
|
},
|
|
|
|
)
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user_page(
|
2022-09-04 21:50:08 +00:00
|
|
|
user, "/panel/dashboard", "send_start_reload", {}
|
2022-09-03 17:23:30 +00:00
|
|
|
)
|
|
|
|
self.management_helper.add_to_audit_log_raw(
|
|
|
|
"Alert",
|
|
|
|
"-1",
|
|
|
|
self.server_id,
|
|
|
|
"Executable update finished for " + self.name,
|
|
|
|
self.settings["server_ip"],
|
|
|
|
)
|
|
|
|
if was_started:
|
2022-09-04 23:06:07 +00:00
|
|
|
self.run_threaded_server(HelperUsers.get_user_id_by_name("system"))
|
2022-09-03 17:23:30 +00:00
|
|
|
else:
|
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(
|
2022-09-03 17:23:30 +00:00
|
|
|
user,
|
|
|
|
"notification",
|
|
|
|
"Executable update failed for "
|
|
|
|
+ self.name
|
|
|
|
+ ". Check log file for details.",
|
|
|
|
)
|
|
|
|
logger.error("Executable download failed.")
|
|
|
|
self.stats_helper.set_update(False)
|
2022-09-04 21:50:08 +00:00
|
|
|
for user in server_users:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_user(user, "remove_spinner", {})
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2023-02-05 18:08:04 +00:00
|
|
|
def start_dir_calc_task(self):
|
|
|
|
server_dt = HelperServers.get_server_data_by_id(self.server_id)
|
|
|
|
self.server_size = self.stats.get_server_dir_size(server_dt["path"])
|
|
|
|
self.dir_scheduler.add_job(
|
|
|
|
self.calc_dir_size,
|
|
|
|
"interval",
|
|
|
|
minutes=self.helper.get_setting("dir_size_poll_freq_minutes"),
|
|
|
|
id=str(self.server_id) + "_dir_poll",
|
|
|
|
)
|
2023-05-30 04:01:54 +00:00
|
|
|
self.dir_scheduler.add_job(
|
|
|
|
self.cache_players,
|
|
|
|
"interval",
|
|
|
|
seconds=5,
|
|
|
|
id=str(self.server_id) + "_players_poll",
|
|
|
|
)
|
2023-02-05 18:08:04 +00:00
|
|
|
|
|
|
|
def calc_dir_size(self):
|
|
|
|
server_dt = HelperServers.get_server_data_by_id(self.server_id)
|
|
|
|
self.server_size = self.stats.get_server_dir_size(server_dt["path"])
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-03-23 02:50:12 +00:00
|
|
|
# Minecraft Servers Statistics
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
def realtime_stats(self):
|
2022-10-20 19:33:51 +00:00
|
|
|
# only get stats if clients are connected.
|
|
|
|
# no point in burning cpu
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(WebSocketManager().clients) > 0:
|
2022-10-20 19:33:51 +00:00
|
|
|
total_players = 0
|
|
|
|
max_players = 0
|
|
|
|
servers_ping = []
|
|
|
|
raw_ping_result = []
|
|
|
|
raw_ping_result = self.get_raw_server_stats(self.server_id)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-10-20 19:33:51 +00:00
|
|
|
if f"{raw_ping_result.get('icon')}" == "b''":
|
|
|
|
raw_ping_result["icon"] = False
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-10-20 19:33:51 +00:00
|
|
|
servers_ping.append(
|
2022-03-03 21:01:12 +00:00
|
|
|
{
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": raw_ping_result.get("id"),
|
|
|
|
"started": raw_ping_result.get("started"),
|
|
|
|
"running": raw_ping_result.get("running"),
|
|
|
|
"cpu": raw_ping_result.get("cpu"),
|
|
|
|
"mem": raw_ping_result.get("mem"),
|
|
|
|
"mem_percent": raw_ping_result.get("mem_percent"),
|
|
|
|
"world_name": raw_ping_result.get("world_name"),
|
|
|
|
"world_size": raw_ping_result.get("world_size"),
|
|
|
|
"server_port": raw_ping_result.get("server_port"),
|
|
|
|
"int_ping_results": raw_ping_result.get("int_ping_results"),
|
|
|
|
"online": raw_ping_result.get("online"),
|
|
|
|
"max": raw_ping_result.get("max"),
|
|
|
|
"players": raw_ping_result.get("players"),
|
|
|
|
"desc": raw_ping_result.get("desc"),
|
|
|
|
"version": raw_ping_result.get("version"),
|
|
|
|
"icon": raw_ping_result.get("icon"),
|
2022-05-25 18:26:13 +00:00
|
|
|
"crashed": self.is_crashed,
|
2024-01-13 21:43:16 +00:00
|
|
|
"count_players": self.server_object.count_players,
|
2022-10-20 19:33:51 +00:00
|
|
|
}
|
2022-03-03 21:01:12 +00:00
|
|
|
)
|
2023-09-05 21:00:52 +00:00
|
|
|
|
|
|
|
WebSocketManager().broadcast_page_params(
|
|
|
|
"/panel/server_detail",
|
|
|
|
{"id": str(self.server_id)},
|
|
|
|
"update_server_details",
|
|
|
|
{
|
|
|
|
"id": raw_ping_result.get("id"),
|
|
|
|
"started": raw_ping_result.get("started"),
|
|
|
|
"running": raw_ping_result.get("running"),
|
|
|
|
"cpu": raw_ping_result.get("cpu"),
|
|
|
|
"mem": raw_ping_result.get("mem"),
|
|
|
|
"mem_percent": raw_ping_result.get("mem_percent"),
|
|
|
|
"world_name": raw_ping_result.get("world_name"),
|
|
|
|
"world_size": raw_ping_result.get("world_size"),
|
|
|
|
"server_port": raw_ping_result.get("server_port"),
|
|
|
|
"int_ping_results": raw_ping_result.get("int_ping_results"),
|
|
|
|
"online": raw_ping_result.get("online"),
|
|
|
|
"max": raw_ping_result.get("max"),
|
|
|
|
"players": raw_ping_result.get("players"),
|
|
|
|
"desc": raw_ping_result.get("desc"),
|
|
|
|
"version": raw_ping_result.get("version"),
|
|
|
|
"icon": raw_ping_result.get("icon"),
|
|
|
|
"crashed": self.is_crashed,
|
|
|
|
"created": datetime.datetime.now().strftime("%Y/%m/%d, %H:%M:%S"),
|
|
|
|
"players_cache": self.player_cache,
|
|
|
|
},
|
|
|
|
)
|
2022-10-20 19:33:51 +00:00
|
|
|
total_players += int(raw_ping_result.get("online"))
|
|
|
|
max_players += int(raw_ping_result.get("max"))
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2023-09-02 20:19:17 +00:00
|
|
|
# self.record_server_stats()
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2023-09-05 21:00:52 +00:00
|
|
|
if len(servers_ping) > 0:
|
2022-10-20 19:33:51 +00:00
|
|
|
try:
|
2023-08-09 21:47:53 +00:00
|
|
|
WebSocketManager().broadcast_page(
|
2022-10-20 19:33:51 +00:00
|
|
|
"/panel/dashboard", "update_server_status", servers_ping
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
Console.critical("Can't broadcast server status to websocket")
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2024-05-26 19:39:52 +00:00
|
|
|
def check_backup_by_id(self, backup_id: str) -> bool:
|
2024-05-26 19:10:03 +00:00
|
|
|
# Check to see if we're already backing up
|
|
|
|
for thread in threading.enumerate():
|
|
|
|
if thread.getName() == f"backup_{backup_id}":
|
|
|
|
Console.debug(f"Backup with id {backup_id} already running!")
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-03-03 21:01:12 +00:00
|
|
|
def get_servers_stats(self):
|
|
|
|
server_stats = {}
|
|
|
|
|
|
|
|
logger.info("Getting Stats for Server " + self.name + " ...")
|
|
|
|
|
|
|
|
server_id = self.server_id
|
2022-04-14 02:10:25 +00:00
|
|
|
server = HelperServers.get_server_data_by_id(server_id)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(f"Getting stats for server: {server_id}")
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
# get our server object, settings and data dictionaries
|
|
|
|
self.reload_server_settings()
|
|
|
|
|
|
|
|
# process stats
|
2022-06-18 21:18:21 +00:00
|
|
|
p_stats = Stats._try_get_process_stats(self.process, self.check_running())
|
2022-03-23 02:50:12 +00:00
|
|
|
internal_ip = server["server_ip"]
|
|
|
|
server_port = server["server_port"]
|
|
|
|
server_name = server.get("server_name", f"ID#{server_id}")
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-04-12 01:34:46 +00:00
|
|
|
logger.debug(f"Pinging server '{server}' on {internal_ip}:{server_port}")
|
2022-04-14 02:10:25 +00:00
|
|
|
if HelperServers.get_server_type_by_id(server_id) == "minecraft-bedrock":
|
2022-03-03 21:01:12 +00:00
|
|
|
int_mc_ping = ping_bedrock(internal_ip, int(server_port))
|
|
|
|
else:
|
2022-07-16 23:05:00 +00:00
|
|
|
try:
|
|
|
|
int_mc_ping = ping(internal_ip, int(server_port))
|
|
|
|
except:
|
|
|
|
int_mc_ping = False
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
int_data = False
|
|
|
|
ping_data = {}
|
|
|
|
|
|
|
|
# if we got a good ping return, let's parse it
|
|
|
|
if int_mc_ping:
|
|
|
|
int_data = True
|
2022-03-23 02:50:12 +00:00
|
|
|
if (
|
2022-04-14 02:10:25 +00:00
|
|
|
HelperServers.get_server_type_by_id(server["server_id"])
|
2022-03-23 02:50:12 +00:00
|
|
|
== "minecraft-bedrock"
|
|
|
|
):
|
2022-04-14 02:10:25 +00:00
|
|
|
ping_data = Stats.parse_server_raknet_ping(int_mc_ping)
|
2022-03-03 21:01:12 +00:00
|
|
|
else:
|
|
|
|
ping_data = Stats.parse_server_ping(int_mc_ping)
|
2022-03-23 06:06:13 +00:00
|
|
|
# Makes sure we only show stats when a server is online
|
|
|
|
# otherwise people have gotten confused.
|
2022-03-03 21:01:12 +00:00
|
|
|
if self.check_running():
|
|
|
|
server_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": server_id,
|
|
|
|
"started": self.get_start_time(),
|
|
|
|
"running": self.check_running(),
|
|
|
|
"cpu": p_stats.get("cpu_usage", 0),
|
|
|
|
"mem": p_stats.get("memory_usage", 0),
|
|
|
|
"mem_percent": p_stats.get("mem_percentage", 0),
|
|
|
|
"world_name": server_name,
|
2023-02-05 18:08:04 +00:00
|
|
|
"world_size": self.server_size,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_port": server_port,
|
|
|
|
"int_ping_results": int_data,
|
|
|
|
"online": ping_data.get("online", False),
|
2022-03-03 21:01:12 +00:00
|
|
|
"max": ping_data.get("max", False),
|
2022-03-23 02:50:12 +00:00
|
|
|
"players": ping_data.get("players", False),
|
|
|
|
"desc": ping_data.get("server_description", False),
|
|
|
|
"version": ping_data.get("server_version", False),
|
2023-09-02 19:51:28 +00:00
|
|
|
"icon": ping_data.get("server_icon"),
|
2022-03-03 21:01:12 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
server_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": server_id,
|
|
|
|
"started": self.get_start_time(),
|
|
|
|
"running": self.check_running(),
|
|
|
|
"cpu": p_stats.get("cpu_usage", 0),
|
|
|
|
"mem": p_stats.get("memory_usage", 0),
|
|
|
|
"mem_percent": p_stats.get("mem_percentage", 0),
|
|
|
|
"world_name": server_name,
|
2023-02-05 18:08:04 +00:00
|
|
|
"world_size": self.server_size,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_port": server_port,
|
|
|
|
"int_ping_results": int_data,
|
|
|
|
"online": False,
|
2022-03-03 21:01:12 +00:00
|
|
|
"max": False,
|
2022-03-23 02:50:12 +00:00
|
|
|
"players": False,
|
|
|
|
"desc": False,
|
|
|
|
"version": False,
|
2023-09-02 19:51:28 +00:00
|
|
|
"icon": None,
|
2022-03-03 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return server_stats
|
|
|
|
|
|
|
|
def get_server_players(self):
|
2022-04-14 02:10:25 +00:00
|
|
|
server = HelperServers.get_server_data_by_id(self.server_id)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2023-10-04 16:45:23 +00:00
|
|
|
logger.debug(f"Getting players for server {server['server_name']}")
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
internal_ip = server["server_ip"]
|
|
|
|
server_port = server["server_port"]
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-04-12 01:34:46 +00:00
|
|
|
logger.debug(f"Pinging {internal_ip} on port {server_port}")
|
2022-04-14 02:10:25 +00:00
|
|
|
if HelperServers.get_server_type_by_id(self.server_id) != "minecraft-bedrock":
|
2022-03-03 21:01:12 +00:00
|
|
|
int_mc_ping = ping(internal_ip, int(server_port))
|
|
|
|
|
|
|
|
ping_data = {}
|
|
|
|
|
|
|
|
# if we got a good ping return, let's parse it
|
|
|
|
if int_mc_ping:
|
|
|
|
ping_data = Stats.parse_server_ping(int_mc_ping)
|
2022-03-23 02:50:12 +00:00
|
|
|
return ping_data["players"]
|
2022-03-03 21:01:12 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
def get_raw_server_stats(self, server_id):
|
|
|
|
try:
|
2022-04-14 02:10:25 +00:00
|
|
|
server = HelperServers.get_server_obj(server_id)
|
2022-03-03 21:01:12 +00:00
|
|
|
except:
|
2022-03-23 02:50:12 +00:00
|
|
|
return {
|
|
|
|
"id": server_id,
|
|
|
|
"started": False,
|
|
|
|
"running": False,
|
|
|
|
"cpu": 0,
|
|
|
|
"mem": 0,
|
|
|
|
"mem_percent": 0,
|
|
|
|
"world_name": None,
|
|
|
|
"world_size": None,
|
|
|
|
"server_port": None,
|
|
|
|
"int_ping_results": False,
|
|
|
|
"online": False,
|
|
|
|
"max": False,
|
|
|
|
"players": False,
|
|
|
|
"desc": False,
|
|
|
|
"version": False,
|
|
|
|
"icon": False,
|
|
|
|
}
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
server_stats = {}
|
|
|
|
if not server:
|
|
|
|
return {}
|
2022-04-14 02:10:25 +00:00
|
|
|
server_dt = HelperServers.get_server_data_by_id(server_id)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(f"Getting stats for server: {server_id}")
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
# get our server object, settings and data dictionaries
|
|
|
|
self.reload_server_settings()
|
|
|
|
|
|
|
|
# world data
|
2022-03-23 02:50:12 +00:00
|
|
|
server_name = server_dt["server_name"]
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
# process stats
|
2022-06-18 21:18:21 +00:00
|
|
|
p_stats = Stats._try_get_process_stats(self.process, self.check_running())
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
internal_ip = server_dt["server_ip"]
|
|
|
|
server_port = server_dt["server_port"]
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-03-03 22:13:40 +00:00
|
|
|
logger.debug(f"Pinging server '{self.name}' on {internal_ip}:{server_port}")
|
2022-04-14 02:10:25 +00:00
|
|
|
if HelperServers.get_server_type_by_id(server_id) == "minecraft-bedrock":
|
2022-03-03 21:01:12 +00:00
|
|
|
int_mc_ping = ping_bedrock(internal_ip, int(server_port))
|
|
|
|
else:
|
|
|
|
int_mc_ping = ping(internal_ip, int(server_port))
|
|
|
|
|
|
|
|
int_data = False
|
|
|
|
ping_data = {}
|
2022-03-23 06:06:13 +00:00
|
|
|
# Makes sure we only show stats when a server is online
|
|
|
|
# otherwise people have gotten confused.
|
2022-03-03 21:01:12 +00:00
|
|
|
if self.check_running():
|
|
|
|
# if we got a good ping return, let's parse it
|
2022-04-14 02:10:25 +00:00
|
|
|
if HelperServers.get_server_type_by_id(server_id) != "minecraft-bedrock":
|
2022-03-03 21:01:12 +00:00
|
|
|
if int_mc_ping:
|
|
|
|
int_data = True
|
|
|
|
ping_data = Stats.parse_server_ping(int_mc_ping)
|
|
|
|
|
|
|
|
server_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": server_id,
|
|
|
|
"started": self.get_start_time(),
|
|
|
|
"running": self.check_running(),
|
|
|
|
"cpu": p_stats.get("cpu_usage", 0),
|
|
|
|
"mem": p_stats.get("memory_usage", 0),
|
|
|
|
"mem_percent": p_stats.get("mem_percentage", 0),
|
|
|
|
"world_name": server_name,
|
2023-02-05 18:08:04 +00:00
|
|
|
"world_size": self.server_size,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_port": server_port,
|
|
|
|
"int_ping_results": int_data,
|
|
|
|
"online": ping_data.get("online", False),
|
2022-03-03 21:01:12 +00:00
|
|
|
"max": ping_data.get("max", False),
|
2022-03-23 02:50:12 +00:00
|
|
|
"players": ping_data.get("players", False),
|
|
|
|
"desc": ping_data.get("server_description", False),
|
|
|
|
"version": ping_data.get("server_version", False),
|
|
|
|
"icon": ping_data.get("server_icon", False),
|
2022-03-03 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else:
|
|
|
|
if int_mc_ping:
|
|
|
|
int_data = True
|
2022-04-14 02:10:25 +00:00
|
|
|
ping_data = Stats.parse_server_raknet_ping(int_mc_ping)
|
2022-03-03 21:01:12 +00:00
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
server_icon = base64.encodebytes(ping_data["icon"])
|
|
|
|
except Exception as ex:
|
2022-03-03 21:01:12 +00:00
|
|
|
server_icon = False
|
2022-03-04 00:41:30 +00:00
|
|
|
logger.info(f"Unable to read the server icon : {ex}")
|
2022-03-03 21:01:12 +00:00
|
|
|
|
|
|
|
server_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": server_id,
|
|
|
|
"started": self.get_start_time(),
|
|
|
|
"running": self.check_running(),
|
|
|
|
"cpu": p_stats.get("cpu_usage", 0),
|
|
|
|
"mem": p_stats.get("memory_usage", 0),
|
|
|
|
"mem_percent": p_stats.get("mem_percentage", 0),
|
|
|
|
"world_name": server_name,
|
2023-02-05 18:08:04 +00:00
|
|
|
"world_size": self.server_size,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_port": server_port,
|
|
|
|
"int_ping_results": int_data,
|
|
|
|
"online": ping_data["online"],
|
|
|
|
"max": ping_data["max"],
|
|
|
|
"players": [],
|
|
|
|
"desc": ping_data["server_description"],
|
|
|
|
"version": ping_data["server_version"],
|
|
|
|
"icon": server_icon,
|
2022-03-03 21:01:12 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
server_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": server_id,
|
|
|
|
"started": self.get_start_time(),
|
|
|
|
"running": self.check_running(),
|
|
|
|
"cpu": p_stats.get("cpu_usage", 0),
|
|
|
|
"mem": p_stats.get("memory_usage", 0),
|
|
|
|
"mem_percent": p_stats.get("mem_percentage", 0),
|
|
|
|
"world_name": server_name,
|
2023-02-05 18:08:04 +00:00
|
|
|
"world_size": self.server_size,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_port": server_port,
|
|
|
|
"int_ping_results": int_data,
|
|
|
|
"online": False,
|
|
|
|
"max": False,
|
|
|
|
"players": False,
|
|
|
|
"desc": False,
|
|
|
|
"version": False,
|
|
|
|
"icon": False,
|
2022-03-03 21:01:12 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
server_stats = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"id": server_id,
|
|
|
|
"started": self.get_start_time(),
|
|
|
|
"running": self.check_running(),
|
|
|
|
"cpu": p_stats.get("cpu_usage", 0),
|
|
|
|
"mem": p_stats.get("memory_usage", 0),
|
|
|
|
"mem_percent": p_stats.get("mem_percentage", 0),
|
|
|
|
"world_name": server_name,
|
2023-02-05 18:08:04 +00:00
|
|
|
"world_size": self.server_size,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_port": server_port,
|
|
|
|
"int_ping_results": int_data,
|
|
|
|
"online": False,
|
2022-03-03 21:01:12 +00:00
|
|
|
"max": False,
|
2022-03-23 02:50:12 +00:00
|
|
|
"players": False,
|
|
|
|
"desc": False,
|
|
|
|
"version": False,
|
2022-03-03 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return server_stats
|
|
|
|
|
|
|
|
def record_server_stats(self):
|
2022-05-25 13:15:51 +00:00
|
|
|
server_stats = self.get_servers_stats()
|
2022-06-01 21:52:49 +00:00
|
|
|
self.stats_helper.insert_server_stats(server_stats)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2023-10-07 21:50:56 +00:00
|
|
|
self.cpu_usage.labels(f"{self.server_id}").set(server_stats.get("cpu"))
|
|
|
|
self.mem_usage_percent.labels(f"{self.server_id}").set(
|
|
|
|
server_stats.get("mem_percent")
|
|
|
|
)
|
|
|
|
self.minecraft_version.labels(f"{self.server_id}").info(
|
|
|
|
{"version": f"{server_stats.get('version')}"}
|
|
|
|
)
|
|
|
|
self.online_players.labels(f"{self.server_id}").set(server_stats.get("online"))
|
|
|
|
|
2022-03-03 21:01:12 +00:00
|
|
|
# delete old data
|
2022-04-11 05:23:55 +00:00
|
|
|
max_age = self.helper.get_setting("history_max_age")
|
2022-03-03 21:01:12 +00:00
|
|
|
now = datetime.datetime.now()
|
2022-05-25 13:14:42 +00:00
|
|
|
minimum_to_exist = now - datetime.timedelta(days=max_age)
|
2022-03-03 21:01:12 +00:00
|
|
|
|
2022-06-01 21:52:49 +00:00
|
|
|
self.stats_helper.remove_old_stats(minimum_to_exist)
|
2023-10-07 21:50:56 +00:00
|
|
|
|
|
|
|
def init_registries(self):
|
|
|
|
# REGISTRY Entries for Server Stats functions
|
|
|
|
self.cpu_usage = Gauge(
|
|
|
|
name="CPU_Usage",
|
|
|
|
documentation="The CPU usage of the server",
|
|
|
|
labelnames=["server_id"],
|
|
|
|
registry=self.server_registry,
|
|
|
|
)
|
|
|
|
self.mem_usage_percent = Gauge(
|
|
|
|
name="Mem_Usage",
|
|
|
|
documentation="The Memory usage of the server",
|
|
|
|
labelnames=["server_id"],
|
|
|
|
registry=self.server_registry,
|
|
|
|
)
|
|
|
|
self.minecraft_version = Info(
|
|
|
|
name="Minecraft_Version",
|
|
|
|
documentation="The version of the minecraft of this server",
|
|
|
|
labelnames=["server_id"],
|
|
|
|
registry=self.server_registry,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.online_players = Gauge(
|
|
|
|
name="online_players",
|
|
|
|
documentation="The number of players online for a server",
|
|
|
|
labelnames=["server_id"],
|
|
|
|
registry=self.server_registry,
|
|
|
|
)
|
2023-10-07 21:55:58 +00:00
|
|
|
|
2023-08-27 11:50:22 +00:00
|
|
|
def get_server_history(self):
|
2023-09-19 19:46:31 +00:00
|
|
|
history = self.stats_helper.get_history_stats(self.server_id, 1)
|
2023-08-27 12:10:20 +00:00
|
|
|
return history
|