2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
import datetime
|
2022-05-07 21:07:55 +00:00
|
|
|
import typing as t
|
2022-04-11 05:23:55 +00:00
|
|
|
from peewee import (
|
|
|
|
CharField,
|
|
|
|
AutoField,
|
|
|
|
DateTimeField,
|
|
|
|
BooleanField,
|
|
|
|
IntegerField,
|
|
|
|
)
|
2022-05-07 21:07:55 +00:00
|
|
|
from playhouse.shortcuts import model_to_dict
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.shared.main_models import DatabaseShortcuts
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.models.base_model import BaseModel
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-05-25 13:54:35 +00:00
|
|
|
# Servers Model
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class Servers(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
server_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
|
|
|
server_uuid = CharField(default="", index=True)
|
|
|
|
server_name = CharField(default="Server", index=True)
|
|
|
|
path = CharField(default="")
|
|
|
|
backup_path = CharField(default="")
|
|
|
|
executable = CharField(default="")
|
|
|
|
log_path = CharField(default="")
|
|
|
|
execution_command = CharField(default="")
|
|
|
|
auto_start = BooleanField(default=0)
|
|
|
|
auto_start_delay = IntegerField(default=10)
|
|
|
|
crash_detection = BooleanField(default=0)
|
|
|
|
stop_command = CharField(default="stop")
|
|
|
|
executable_update_url = CharField(default="")
|
|
|
|
server_ip = CharField(default="127.0.0.1")
|
|
|
|
server_port = IntegerField(default=25565)
|
|
|
|
logs_delete_after = IntegerField(default=0)
|
2022-02-01 01:50:25 +00:00
|
|
|
type = CharField(default="minecraft-java")
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "servers"
|
|
|
|
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class HelperServers:
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, database):
|
|
|
|
self.database = database
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Generic Servers Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-01-26 01:45:30 +00:00
|
|
|
def create_server(
|
|
|
|
name: str,
|
|
|
|
server_uuid: str,
|
|
|
|
server_dir: str,
|
|
|
|
backup_path: str,
|
|
|
|
server_command: str,
|
|
|
|
server_file: str,
|
|
|
|
server_log_file: str,
|
|
|
|
server_stop: str,
|
2022-02-10 23:20:36 +00:00
|
|
|
server_type: str,
|
2022-05-05 00:32:09 +00:00
|
|
|
server_port: int = 25565,
|
2022-05-05 11:02:23 +00:00
|
|
|
server_host: str = "127.0.0.1",
|
2022-05-05 00:32:09 +00:00
|
|
|
) -> int:
|
|
|
|
"""Create a server in the database
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: The name of the server
|
|
|
|
server_uuid: This is the UUID of the server
|
|
|
|
server_dir: The directory where the server is located
|
|
|
|
backup_path: The path to the backup folder
|
|
|
|
server_command: The command to start the server
|
|
|
|
server_file: The name of the server file
|
|
|
|
server_log_file: The path to the server log file
|
|
|
|
server_stop: This is the command to stop the server
|
|
|
|
server_type: This is the type of server you're creating.
|
2022-05-05 11:02:23 +00:00
|
|
|
server_port: The port the server will be monitored on, defaults to 25565
|
|
|
|
server_host: The host the server will be monitored on, defaults to 127.0.0.1
|
2022-05-05 00:32:09 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: The new server's id
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
PeeweeException: If the server already exists
|
|
|
|
"""
|
2022-03-23 02:50:12 +00:00
|
|
|
return Servers.insert(
|
|
|
|
{
|
|
|
|
Servers.server_name: name,
|
|
|
|
Servers.server_uuid: server_uuid,
|
|
|
|
Servers.path: server_dir,
|
|
|
|
Servers.executable: server_file,
|
|
|
|
Servers.execution_command: server_command,
|
|
|
|
Servers.auto_start: False,
|
|
|
|
Servers.auto_start_delay: 10,
|
|
|
|
Servers.crash_detection: False,
|
|
|
|
Servers.log_path: server_log_file,
|
|
|
|
Servers.server_port: server_port,
|
2022-05-05 11:02:23 +00:00
|
|
|
Servers.server_ip: server_host,
|
2022-03-23 02:50:12 +00:00
|
|
|
Servers.stop_command: server_stop,
|
|
|
|
Servers.backup_path: backup_path,
|
|
|
|
Servers.type: server_type,
|
|
|
|
}
|
|
|
|
).execute()
|
2021-11-21 10:52:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_obj(server_id):
|
|
|
|
return Servers.get_by_id(server_id)
|
|
|
|
|
2022-02-10 23:20:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_type_by_id(server_id):
|
|
|
|
server_type = Servers.select().where(Servers.server_id == server_id).get()
|
|
|
|
return server_type.type
|
|
|
|
|
2021-11-21 10:52:29 +00:00
|
|
|
@staticmethod
|
|
|
|
def update_server(server_obj):
|
|
|
|
return server_obj.save()
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def remove_server(self, server_id):
|
2022-05-15 22:18:08 +00:00
|
|
|
Servers.delete().where(Servers.server_id == server_id).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_data_by_id(server_id):
|
|
|
|
query = Servers.select().where(Servers.server_id == server_id).limit(1)
|
|
|
|
try:
|
2022-04-14 02:10:25 +00:00
|
|
|
return DatabaseShortcuts.return_rows(query)[0]
|
2021-09-08 22:01:10 +00:00
|
|
|
except IndexError:
|
|
|
|
return {}
|
|
|
|
|
2022-05-07 21:07:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_columns(
|
|
|
|
server_id: t.Union[str, int], column_names: t.List[str]
|
|
|
|
) -> t.List[t.Any]:
|
|
|
|
columns = [getattr(Servers, column) for column in column_names]
|
|
|
|
return model_to_dict(
|
|
|
|
Servers.select(*columns).where(Servers.server_id == server_id).get(),
|
|
|
|
only=columns,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_column(server_id: t.Union[str, int], column_name: str) -> t.Any:
|
|
|
|
column = getattr(Servers, column_name)
|
2022-05-20 10:15:50 +00:00
|
|
|
return getattr(
|
2022-05-07 21:07:55 +00:00
|
|
|
Servers.select(column).where(Servers.server_id == server_id).get(),
|
2022-05-20 10:15:50 +00:00
|
|
|
column_name,
|
|
|
|
)
|
2022-05-07 21:07:55 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_defined_servers():
|
|
|
|
query = Servers.select()
|
2022-04-14 02:10:25 +00:00
|
|
|
return DatabaseShortcuts.return_rows(query)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-05-09 23:08:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all_server_ids() -> t.List[int]:
|
|
|
|
return [server.server_id for server in Servers.select(Servers.server_id)]
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_server_friendly_name(server_id):
|
2022-04-14 02:10:25 +00:00
|
|
|
server_data = HelperServers.get_server_data_by_id(server_id)
|
2022-03-23 06:06:13 +00:00
|
|
|
friendly_name = (
|
|
|
|
f"{server_data.get('server_name', None)} "
|
|
|
|
f"with ID: {server_data.get('server_id', 0)}"
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
return friendly_name
|