2020-08-17 02:47:53 +00:00
|
|
|
import os
|
2020-08-13 01:33:36 +00:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from app.classes.shared.helpers import helper
|
|
|
|
from app.classes.shared.console import console
|
2020-08-17 02:47:53 +00:00
|
|
|
from app.classes.minecraft.server_props import ServerProps
|
2020-08-13 01:33:36 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
try:
|
|
|
|
from peewee import *
|
|
|
|
from playhouse.shortcuts import model_to_dict
|
2020-08-17 02:47:53 +00:00
|
|
|
import yaml
|
2020-08-13 01:33:36 +00:00
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
logger.critical("Import Error: Unable to load {} module".format(e, e.name))
|
|
|
|
console.critical("Import Error: Unable to load {} module".format(e, e.name))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
database = SqliteDatabase(helper.db_path, pragmas={
|
|
|
|
'journal_mode': 'wal',
|
|
|
|
'cache_size': -1024 * 10})
|
|
|
|
|
|
|
|
|
|
|
|
class BaseModel(Model):
|
|
|
|
class Meta:
|
|
|
|
database = database
|
|
|
|
|
|
|
|
|
|
|
|
class Users(BaseModel):
|
|
|
|
user_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
|
|
|
last_login = DateTimeField(default=datetime.datetime.now)
|
|
|
|
last_ip = CharField(default="")
|
|
|
|
username = CharField(default="")
|
|
|
|
password = CharField(default="")
|
|
|
|
enabled = BooleanField(default=True)
|
|
|
|
api_token = CharField(default="")
|
|
|
|
allowed_servers = CharField(default="[]")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "users"
|
|
|
|
|
|
|
|
|
|
|
|
class Host_Stats(BaseModel):
|
|
|
|
time = DateTimeField(default=datetime.datetime.now)
|
|
|
|
boot_time = CharField(default="")
|
|
|
|
cpu_usage = FloatField(default=0)
|
|
|
|
cpu_cores = IntegerField(default=0)
|
|
|
|
cpu_cur_freq = FloatField(default=0)
|
|
|
|
cpu_max_freq = FloatField(default=0)
|
|
|
|
mem_percent = FloatField(default=0)
|
|
|
|
mem_usage = CharField(default="")
|
|
|
|
mem_total = CharField(default="")
|
2020-08-19 01:04:43 +00:00
|
|
|
disk_json = TextField(default="")
|
2020-08-13 01:33:36 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "host_stats"
|
|
|
|
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
class Servers(BaseModel):
|
|
|
|
server_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
2020-08-23 22:43:28 +00:00
|
|
|
server_uuid = CharField(default="")
|
2020-08-17 02:47:53 +00:00
|
|
|
server_name = CharField(default="Server")
|
|
|
|
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")
|
2020-08-24 02:17:36 +00:00
|
|
|
server_port = IntegerField(default=25565)
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
class Meta:
|
|
|
|
table_name = "servers"
|
|
|
|
|
|
|
|
|
2020-08-24 17:08:17 +00:00
|
|
|
class Server_Stats(BaseModel):
|
|
|
|
stats_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
|
|
|
server_id = ForeignKeyField(Servers, backref='server')
|
|
|
|
started = CharField(default="")
|
|
|
|
running = BooleanField(default=False)
|
|
|
|
cpu = FloatField(default=0)
|
|
|
|
mem = FloatField(default=0)
|
|
|
|
world_name = CharField(default="")
|
|
|
|
world_size = CharField(default="")
|
|
|
|
server_port = IntegerField(default=25565)
|
|
|
|
int_ping_results = CharField(default="")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "server_stats"
|
|
|
|
|
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
class Webhooks(BaseModel):
|
|
|
|
id = AutoField()
|
|
|
|
name = CharField(max_length=64, unique=True)
|
|
|
|
method = CharField(default="POST")
|
|
|
|
url = CharField(unique=True)
|
|
|
|
event = CharField(default="")
|
|
|
|
send_data = BooleanField(default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "webhooks"
|
|
|
|
|
|
|
|
|
|
|
|
class Backups(BaseModel):
|
|
|
|
directories = CharField()
|
|
|
|
storage_location = CharField()
|
|
|
|
max_backups = IntegerField()
|
|
|
|
server_id = IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = 'backups'
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
class db_builder:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_tables():
|
|
|
|
with database:
|
|
|
|
database.create_tables([
|
|
|
|
Backups,
|
|
|
|
Users,
|
|
|
|
Host_Stats,
|
2020-08-17 02:47:53 +00:00
|
|
|
Webhooks,
|
2020-08-19 01:04:43 +00:00
|
|
|
Servers,
|
2020-08-24 17:08:17 +00:00
|
|
|
Server_Stats
|
2020-08-13 01:33:36 +00:00
|
|
|
])
|
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def default_settings():
|
2020-08-19 01:50:24 +00:00
|
|
|
|
|
|
|
default_data = helper.find_default_password()
|
|
|
|
|
|
|
|
username = default_data.get("username", 'Admin')
|
|
|
|
password = default_data.get("password", 'crafty')
|
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
Users.insert({
|
2020-08-19 01:50:24 +00:00
|
|
|
Users.username: username,
|
|
|
|
Users.password: helper.encode_pass(password),
|
2020-08-13 01:33:36 +00:00
|
|
|
Users.api_token: helper.random_string_generator(32),
|
|
|
|
Users.enabled: True
|
|
|
|
}).execute()
|
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_fresh_install():
|
|
|
|
if helper.check_file_exists(helper.db_path):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
|
|
|
|
class db_shortcuts:
|
|
|
|
|
|
|
|
def return_rows(self, query):
|
|
|
|
rows = []
|
|
|
|
|
2020-08-23 22:43:28 +00:00
|
|
|
try:
|
|
|
|
if query.count() > 0:
|
|
|
|
for s in query:
|
|
|
|
rows.append(model_to_dict(s))
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning("Database Error: {}".format(e))
|
|
|
|
pass
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
return rows
|
|
|
|
|
|
|
|
def get_all_defined_servers(self):
|
|
|
|
query = Servers.select()
|
|
|
|
return self.return_rows(query)
|
|
|
|
|
2020-08-24 17:08:17 +00:00
|
|
|
def get_all_servers_stats(self):
|
|
|
|
query = Server_Stats.select().order_by(Server_Stats.stats_id.desc()).limit(1)
|
|
|
|
return self.return_rows(query)
|
|
|
|
|
2020-08-19 01:04:43 +00:00
|
|
|
def get_latest_hosts_stats(self):
|
|
|
|
query = Host_Stats.select().order_by(Host_Stats.id.desc()).get()
|
|
|
|
return model_to_dict(query)
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
installer = db_builder()
|
2020-08-23 22:43:28 +00:00
|
|
|
db_helper = db_shortcuts()
|