2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from app.classes.shared.helpers import helper
|
|
|
|
from app.classes.shared.main_models import db_helper
|
|
|
|
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
from peewee import (
|
|
|
|
Model,
|
|
|
|
ForeignKeyField,
|
|
|
|
CharField,
|
|
|
|
AutoField,
|
|
|
|
DateTimeField,
|
|
|
|
BooleanField,
|
|
|
|
IntegerField,
|
|
|
|
FloatField,
|
|
|
|
)
|
2022-04-10 20:33:54 +00:00
|
|
|
from playhouse.sqliteq import SqliteQueueDatabase
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
2022-03-08 04:40:44 +00:00
|
|
|
helper.auto_installer_fix(e)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-23 02:50:12 +00:00
|
|
|
peewee_logger = logging.getLogger("peewee")
|
2022-03-08 04:40:44 +00:00
|
|
|
peewee_logger.setLevel(logging.INFO)
|
2022-04-10 20:33:54 +00:00
|
|
|
database = SqliteQueueDatabase(
|
|
|
|
helper.db_path
|
|
|
|
# pragmas={"journal_mode": "wal", "cache_size": -1024 * 10}
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
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
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
class Servers(Model):
|
|
|
|
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"
|
|
|
|
database = database
|
|
|
|
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers Stats Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
class Server_Stats(Model):
|
|
|
|
stats_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = ForeignKeyField(Servers, backref="server", index=True)
|
2021-09-08 22:01:10 +00:00
|
|
|
started = CharField(default="")
|
|
|
|
running = BooleanField(default=False)
|
|
|
|
cpu = FloatField(default=0)
|
|
|
|
mem = FloatField(default=0)
|
|
|
|
mem_percent = FloatField(default=0)
|
|
|
|
world_name = CharField(default="")
|
|
|
|
world_size = CharField(default="")
|
|
|
|
server_port = IntegerField(default=25565)
|
|
|
|
int_ping_results = CharField(default="")
|
|
|
|
online = IntegerField(default=0)
|
|
|
|
max = IntegerField(default=0)
|
|
|
|
players = CharField(default="")
|
|
|
|
desc = CharField(default="Unable to Connect")
|
|
|
|
version = CharField(default="")
|
|
|
|
updating = BooleanField(default=False)
|
2021-09-21 19:13:17 +00:00
|
|
|
waiting_start = BooleanField(default=False)
|
2022-01-09 18:14:59 +00:00
|
|
|
first_run = BooleanField(default=True)
|
2022-01-28 01:43:23 +00:00
|
|
|
crashed = BooleanField(default=False)
|
2022-03-04 00:36:36 +00:00
|
|
|
downloading = BooleanField(default=False)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "server_stats"
|
|
|
|
database = database
|
|
|
|
|
|
|
|
|
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
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
class helper_servers:
|
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-03-23 02:50:12 +00:00
|
|
|
server_port=25565,
|
|
|
|
):
|
|
|
|
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,
|
|
|
|
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()
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def remove_server(server_id):
|
2022-04-10 20:33:54 +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:
|
|
|
|
return db_helper.return_rows(query)[0]
|
|
|
|
except IndexError:
|
|
|
|
return {}
|
|
|
|
|
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()
|
|
|
|
return db_helper.return_rows(query)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_all_servers_stats():
|
|
|
|
servers = servers_helper.get_all_defined_servers()
|
|
|
|
server_data = []
|
2022-03-01 01:02:30 +00:00
|
|
|
try:
|
|
|
|
for s in servers:
|
2022-03-23 02:50:12 +00:00
|
|
|
latest = (
|
|
|
|
Server_Stats.select()
|
|
|
|
.where(Server_Stats.server_id == s.get("server_id"))
|
|
|
|
.order_by(Server_Stats.created.desc())
|
|
|
|
.limit(1)
|
|
|
|
)
|
|
|
|
server_data.append(
|
|
|
|
{
|
|
|
|
"server_data": s,
|
|
|
|
"stats": db_helper.return_rows(latest)[0],
|
|
|
|
"user_command_permission": True,
|
|
|
|
}
|
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
except IndexError as ex:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.error(
|
|
|
|
f"Stats collection failed with error: {ex}. Was a server just created?"
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
return server_data
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_friendly_name(server_id):
|
|
|
|
server_data = servers_helper.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
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Servers_Stats Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_latest_server_stats(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
return (
|
|
|
|
Server_Stats.select()
|
|
|
|
.where(Server_Stats.server_id == server_id)
|
|
|
|
.order_by(Server_Stats.created.desc())
|
|
|
|
.limit(1)
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_server_stats_by_id(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
stats = (
|
|
|
|
Server_Stats.select()
|
|
|
|
.where(Server_Stats.server_id == server_id)
|
|
|
|
.order_by(Server_Stats.created.desc())
|
|
|
|
.limit(1)
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
return db_helper.return_rows(stats)[0]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def server_id_exists(server_id):
|
|
|
|
if not servers_helper.get_server_data_by_id(server_id):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-01-28 01:43:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def sever_crashed(server_id):
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(crashed=True).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2022-01-28 01:43:23 +00:00
|
|
|
|
2022-03-04 00:36:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_download(server_id):
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(downloading=True).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2022-03-04 00:36:36 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def finish_download(server_id):
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(downloading=False).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2022-03-04 00:36:36 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_download_status(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
download_status = (
|
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id).get()
|
|
|
|
)
|
2022-03-04 00:36:36 +00:00
|
|
|
return download_status.downloading
|
|
|
|
|
2022-01-28 01:43:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def server_crash_reset(server_id):
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(crashed=False).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2022-01-28 01:43:23 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_crashed(server_id):
|
|
|
|
svr = Server_Stats.select().where(Server_Stats.server_id == server_id).get()
|
2022-04-02 01:58:31 +00:00
|
|
|
if svr.crashed is True:
|
2022-01-28 01:43:23 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_update(server_id, value):
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
# Checks if server even exists
|
2022-01-26 01:45:30 +00:00
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
except Exception as ex:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Database entry not found! {ex}")
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(updating=value).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-10 19:58:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_update_status(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
update_status = (
|
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id).get()
|
|
|
|
)
|
2022-01-28 01:43:23 +00:00
|
|
|
return update_status.updating
|
2022-01-10 19:58:38 +00:00
|
|
|
|
2022-01-09 18:14:59 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_first_run(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
# Sets first run to false
|
2022-01-09 18:14:59 +00:00
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
# Checks if server even exists
|
2022-01-26 01:45:30 +00:00
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id)
|
2022-01-09 18:14:59 +00:00
|
|
|
except Exception as ex:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Database entry not found! {ex}")
|
|
|
|
return
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(first_run=False).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2022-01-09 18:14:59 +00:00
|
|
|
|
2022-01-10 19:58:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_first_run(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
first_run = (
|
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id).get()
|
|
|
|
)
|
2022-01-10 19:58:38 +00:00
|
|
|
return first_run.first_run
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_TTL_without_player(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
last_stat = (
|
|
|
|
Server_Stats.select()
|
|
|
|
.where(Server_Stats.server_id == server_id)
|
|
|
|
.order_by(Server_Stats.created.desc())
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
last_stat_with_player = (
|
|
|
|
Server_Stats.select()
|
|
|
|
.where(Server_Stats.server_id == server_id)
|
|
|
|
.where(Server_Stats.online > 0)
|
|
|
|
.order_by(Server_Stats.created.desc())
|
|
|
|
.first()
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
return last_stat.created - last_stat_with_player.created
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def can_stop_no_players(server_id, time_limit):
|
|
|
|
can = False
|
2021-09-25 21:59:01 +00:00
|
|
|
ttl_no_players = servers_helper.get_TTL_without_player(server_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
if (time_limit == -1) or (ttl_no_players > time_limit):
|
|
|
|
can = True
|
|
|
|
return can
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2021-09-21 19:13:17 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_waiting_start(server_id, value):
|
|
|
|
try:
|
2022-01-26 01:45:30 +00:00
|
|
|
# Checks if server even exists
|
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id)
|
2021-09-21 19:13:17 +00:00
|
|
|
except Exception as ex:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.error(f"Database entry not found! {ex}")
|
2022-04-10 20:33:54 +00:00
|
|
|
Server_Stats.update(waiting_start=value).where(
|
|
|
|
Server_Stats.server_id == server_id
|
|
|
|
).execute()
|
2021-09-21 19:13:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_waiting_start(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
waiting_start = (
|
|
|
|
Server_Stats.select().where(Server_Stats.server_id == server_id).get()
|
|
|
|
)
|
2021-09-21 19:13:17 +00:00
|
|
|
return waiting_start.waiting_start
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
servers_helper = helper_servers()
|