2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
import datetime
|
2022-04-11 05:23:55 +00:00
|
|
|
from peewee import (
|
|
|
|
ForeignKeyField,
|
|
|
|
CharField,
|
|
|
|
IntegerField,
|
|
|
|
DateTimeField,
|
|
|
|
FloatField,
|
|
|
|
TextField,
|
|
|
|
AutoField,
|
|
|
|
BooleanField,
|
|
|
|
)
|
|
|
|
from playhouse.shortcuts import model_to_dict
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
from app.classes.models.base_model import BaseModel
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.users import Users, HelperUsers
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.models.servers import Servers
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.models.server_permissions import PermissionsServers
|
|
|
|
from app.classes.shared.main_models import DatabaseShortcuts
|
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
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Audit_Log Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class AuditLog(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
audit_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
|
|
|
user_name = CharField(default="")
|
|
|
|
user_id = IntegerField(default=0, index=True)
|
2022-03-23 02:50:12 +00:00
|
|
|
source_ip = CharField(default="127.0.0.1")
|
|
|
|
server_id = IntegerField(
|
|
|
|
default=None, index=True
|
|
|
|
) # When auditing global events, use server ID 0
|
|
|
|
log_msg = TextField(default="")
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 23:34:30 +00:00
|
|
|
class Meta:
|
|
|
|
table_name = "audit_log"
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Host_Stats Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-14 02:10:25 +00:00
|
|
|
class HostStats(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
time = DateTimeField(default=datetime.datetime.now, index=True)
|
|
|
|
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="")
|
|
|
|
disk_json = TextField(default="")
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "host_stats"
|
|
|
|
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Commands Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class Commands(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
command_id = AutoField()
|
|
|
|
created = DateTimeField(default=datetime.datetime.now)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = ForeignKeyField(Servers, backref="server", index=True)
|
|
|
|
user = ForeignKeyField(Users, backref="user", index=True)
|
|
|
|
source_ip = CharField(default="127.0.0.1")
|
|
|
|
command = CharField(default="")
|
2021-09-08 22:01:10 +00:00
|
|
|
executed = BooleanField(default=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "commands"
|
|
|
|
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Webhooks Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class Webhooks(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
id = AutoField()
|
|
|
|
name = CharField(max_length=64, unique=True, index=True)
|
|
|
|
method = CharField(default="POST")
|
|
|
|
url = CharField(unique=True)
|
|
|
|
event = CharField(default="")
|
|
|
|
send_data = BooleanField(default=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
table_name = "webhooks"
|
|
|
|
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Schedules Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class Schedules(BaseModel):
|
2021-09-08 22:01:10 +00:00
|
|
|
schedule_id = IntegerField(unique=True, primary_key=True)
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = ForeignKeyField(Servers, backref="schedule_server")
|
2021-09-08 22:01:10 +00:00
|
|
|
enabled = BooleanField()
|
|
|
|
action = CharField()
|
|
|
|
interval = IntegerField()
|
|
|
|
interval_type = CharField()
|
|
|
|
start_time = CharField(null=True)
|
|
|
|
command = CharField(null=True)
|
|
|
|
comment = CharField()
|
2022-01-11 06:29:27 +00:00
|
|
|
one_time = BooleanField(default=False)
|
2022-01-11 17:17:57 +00:00
|
|
|
cron_string = CharField(default="")
|
2022-02-24 03:58:48 +00:00
|
|
|
parent = IntegerField(null=True)
|
|
|
|
delay = IntegerField(default=0)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
2022-03-23 02:50:12 +00:00
|
|
|
table_name = "schedules"
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Backups Class
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2022-04-11 05:23:55 +00:00
|
|
|
class Backups(BaseModel):
|
2022-03-01 01:02:30 +00:00
|
|
|
excluded_dirs = CharField(null=True)
|
2021-09-08 22:01:10 +00:00
|
|
|
max_backups = IntegerField()
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = ForeignKeyField(Servers, backref="backups_server")
|
2022-03-02 16:34:26 +00:00
|
|
|
compress = BooleanField(default=False)
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
class Meta:
|
2022-03-23 02:50:12 +00:00
|
|
|
table_name = "backups"
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class HelpersManagement:
|
2022-04-11 05:23:55 +00:00
|
|
|
def __init__(self, database, helper):
|
|
|
|
self.database = database
|
|
|
|
self.helper = helper
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Host_Stats Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_latest_hosts_stats():
|
2022-03-23 02:50:12 +00:00
|
|
|
# pylint: disable=no-member
|
2022-04-14 02:10:25 +00:00
|
|
|
query = HostStats.select().order_by(HostStats.id.desc()).get()
|
2021-09-08 22:01:10 +00:00
|
|
|
return model_to_dict(query)
|
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Commands Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_command(server_id, user_id, remote_ip, command):
|
2022-03-23 02:50:12 +00:00
|
|
|
Commands.insert(
|
|
|
|
{
|
|
|
|
Commands.server_id: server_id,
|
|
|
|
Commands.user: user_id,
|
|
|
|
Commands.source_ip: remote_ip,
|
|
|
|
Commands.command: command,
|
|
|
|
}
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_unactioned_commands():
|
|
|
|
query = Commands.select().where(Commands.executed == 0)
|
2022-01-18 19:43:22 +00:00
|
|
|
return query
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def mark_command_complete(command_id=None):
|
|
|
|
if command_id is not None:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Marking Command {command_id} completed")
|
2022-03-23 02:50:12 +00:00
|
|
|
Commands.update({Commands.executed: True}).where(
|
|
|
|
Commands.command_id == command_id
|
|
|
|
).execute()
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Audit_Log Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_actity_log():
|
2022-04-14 02:10:25 +00:00
|
|
|
query = AuditLog.select()
|
|
|
|
return DatabaseShortcuts.return_db_rows(query)
|
2021-11-21 10:52:29 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_to_audit_log(self, user_id, log_msg, server_id=None, source_ip=None):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Adding to audit log User:{user_id} - Message: {log_msg} ")
|
2022-04-14 02:10:25 +00:00
|
|
|
user_data = HelperUsers.get_user(user_id)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
audit_msg = f"{str(user_data['username']).capitalize()} {log_msg}"
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
server_users = PermissionsServers.get_server_user_list(server_id)
|
2022-01-20 18:37:45 +00:00
|
|
|
for user in server_users:
|
2022-05-18 13:51:46 +00:00
|
|
|
try:
|
|
|
|
self.helper.websocket_helper.broadcast_user(
|
|
|
|
user, "notification", audit_msg
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error broadcasting to user {user} - {e}")
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
AuditLog.insert(
|
2022-03-23 02:50:12 +00:00
|
|
|
{
|
2022-04-14 02:10:25 +00:00
|
|
|
AuditLog.user_name: user_data["username"],
|
|
|
|
AuditLog.user_id: user_id,
|
|
|
|
AuditLog.server_id: server_id,
|
|
|
|
AuditLog.log_msg: audit_msg,
|
|
|
|
AuditLog.source_ip: source_ip,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
).execute()
|
2022-05-05 00:32:09 +00:00
|
|
|
# deletes records when there's more than 300
|
2022-04-14 02:10:25 +00:00
|
|
|
ordered = AuditLog.select().order_by(+AuditLog.created)
|
2022-01-29 06:36:37 +00:00
|
|
|
for item in ordered:
|
2022-04-11 05:23:55 +00:00
|
|
|
if not self.helper.get_setting("max_audit_entries"):
|
2022-01-31 23:09:23 +00:00
|
|
|
max_entries = 300
|
|
|
|
else:
|
2022-04-11 05:23:55 +00:00
|
|
|
max_entries = self.helper.get_setting("max_audit_entries")
|
2022-04-14 02:10:25 +00:00
|
|
|
if AuditLog.select().count() > max_entries:
|
|
|
|
AuditLog.delete().where(AuditLog.audit_id == item.audit_id).execute()
|
2022-01-29 06:36:37 +00:00
|
|
|
else:
|
|
|
|
return
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def add_to_audit_log_raw(self, user_name, user_id, server_id, log_msg, source_ip):
|
2022-04-14 02:10:25 +00:00
|
|
|
AuditLog.insert(
|
2022-03-23 02:50:12 +00:00
|
|
|
{
|
2022-04-14 02:10:25 +00:00
|
|
|
AuditLog.user_name: user_name,
|
|
|
|
AuditLog.user_id: user_id,
|
|
|
|
AuditLog.server_id: server_id,
|
|
|
|
AuditLog.log_msg: log_msg,
|
|
|
|
AuditLog.source_ip: source_ip,
|
2022-03-23 02:50:12 +00:00
|
|
|
}
|
|
|
|
).execute()
|
2022-05-05 00:32:09 +00:00
|
|
|
# deletes records when there's more than 300
|
2022-04-14 02:10:25 +00:00
|
|
|
ordered = AuditLog.select().order_by(+AuditLog.created)
|
2022-01-29 06:36:37 +00:00
|
|
|
for item in ordered:
|
2022-03-23 02:50:12 +00:00
|
|
|
# configurable through app/config/config.json
|
2022-04-11 05:23:55 +00:00
|
|
|
if not self.helper.get_setting("max_audit_entries"):
|
2022-01-31 23:09:23 +00:00
|
|
|
max_entries = 300
|
|
|
|
else:
|
2022-04-11 05:23:55 +00:00
|
|
|
max_entries = self.helper.get_setting("max_audit_entries")
|
2022-04-14 02:10:25 +00:00
|
|
|
if AuditLog.select().count() > max_entries:
|
|
|
|
AuditLog.delete().where(AuditLog.audit_id == item.audit_id).execute()
|
2022-01-29 06:36:37 +00:00
|
|
|
else:
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Schedules Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
2022-01-26 01:45:30 +00:00
|
|
|
def create_scheduled_task(
|
|
|
|
server_id,
|
|
|
|
action,
|
|
|
|
interval,
|
|
|
|
interval_type,
|
|
|
|
start_time,
|
|
|
|
command,
|
|
|
|
comment=None,
|
|
|
|
enabled=True,
|
|
|
|
one_time=False,
|
2022-03-23 02:50:12 +00:00
|
|
|
cron_string="* * * * *",
|
2022-02-24 05:19:54 +00:00
|
|
|
parent=None,
|
2022-03-23 02:50:12 +00:00
|
|
|
delay=0,
|
|
|
|
):
|
|
|
|
sch_id = Schedules.insert(
|
|
|
|
{
|
|
|
|
Schedules.server_id: server_id,
|
|
|
|
Schedules.action: action,
|
|
|
|
Schedules.enabled: enabled,
|
|
|
|
Schedules.interval: interval,
|
|
|
|
Schedules.interval_type: interval_type,
|
|
|
|
Schedules.start_time: start_time,
|
|
|
|
Schedules.command: command,
|
|
|
|
Schedules.comment: comment,
|
|
|
|
Schedules.one_time: one_time,
|
|
|
|
Schedules.cron_string: cron_string,
|
|
|
|
Schedules.parent: parent,
|
|
|
|
Schedules.delay: delay,
|
|
|
|
}
|
|
|
|
).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
return sch_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete_scheduled_task(schedule_id):
|
2022-05-15 22:18:08 +00:00
|
|
|
return Schedules.delete().where(Schedules.schedule_id == schedule_id).execute()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def update_scheduled_task(schedule_id, updates):
|
|
|
|
Schedules.update(updates).where(Schedules.schedule_id == schedule_id).execute()
|
|
|
|
|
2021-11-29 21:22:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def delete_scheduled_task_by_server(server_id):
|
2022-01-26 01:45:30 +00:00
|
|
|
Schedules.delete().where(Schedules.server_id == int(server_id)).execute()
|
2021-11-29 21:22:46 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_scheduled_task(schedule_id):
|
2022-01-26 01:45:30 +00:00
|
|
|
return model_to_dict(Schedules.get(Schedules.schedule_id == schedule_id))
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-01-11 06:29:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_scheduled_task_model(schedule_id):
|
|
|
|
return Schedules.select().where(Schedules.schedule_id == schedule_id).get()
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_schedules_by_server(server_id):
|
|
|
|
return Schedules.select().where(Schedules.server_id == server_id).execute()
|
|
|
|
|
2022-02-24 03:58:48 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_child_schedules_by_server(schedule_id, server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
return (
|
|
|
|
Schedules.select()
|
|
|
|
.where(Schedules.server_id == server_id, Schedules.parent == schedule_id)
|
|
|
|
.execute()
|
|
|
|
)
|
2022-02-24 03:58:48 +00:00
|
|
|
|
2022-02-24 05:19:54 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_child_schedules(schedule_id):
|
|
|
|
return Schedules.select().where(Schedules.parent == schedule_id)
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_schedules_all():
|
|
|
|
return Schedules.select().execute()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_schedules_enabled():
|
2022-04-02 16:54:30 +00:00
|
|
|
return (
|
|
|
|
Schedules.select()
|
|
|
|
.where(Schedules.enabled == True) # pylint: disable=singleton-comparison
|
|
|
|
.execute()
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Backups Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_backup_config(server_id):
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
row = (
|
|
|
|
Backups.select().where(Backups.server_id == server_id).join(Servers)[0]
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
conf = {
|
|
|
|
"backup_path": row.server_id.backup_path,
|
2022-03-01 01:02:30 +00:00
|
|
|
"excluded_dirs": row.excluded_dirs,
|
2021-09-08 22:01:10 +00:00
|
|
|
"max_backups": row.max_backups,
|
2022-05-15 22:02:17 +00:00
|
|
|
"server_id": row.server_id_id,
|
2022-03-23 02:50:12 +00:00
|
|
|
"compress": row.compress,
|
2021-09-08 22:01:10 +00:00
|
|
|
}
|
|
|
|
except IndexError:
|
|
|
|
conf = {
|
|
|
|
"backup_path": None,
|
2022-03-01 01:02:30 +00:00
|
|
|
"excluded_dirs": None,
|
2021-09-08 22:01:10 +00:00
|
|
|
"max_backups": 0,
|
2022-03-02 16:34:26 +00:00
|
|
|
"server_id": server_id,
|
|
|
|
"compress": False,
|
2021-09-08 22:01:10 +00:00
|
|
|
}
|
|
|
|
return conf
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
def set_backup_config(
|
2022-04-11 05:23:55 +00:00
|
|
|
self,
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id: int,
|
|
|
|
backup_path: str = None,
|
|
|
|
max_backups: int = None,
|
|
|
|
excluded_dirs: list = None,
|
|
|
|
compress: bool = False,
|
|
|
|
):
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Updating server {server_id} backup config with {locals()}")
|
2022-05-25 11:24:34 +00:00
|
|
|
if Backups.select().where(Backups.server_id == server_id).exists():
|
2021-09-08 22:01:10 +00:00
|
|
|
new_row = False
|
|
|
|
conf = {}
|
2022-01-29 21:37:00 +00:00
|
|
|
else:
|
2021-09-08 22:01:10 +00:00
|
|
|
conf = {
|
2022-03-01 01:02:30 +00:00
|
|
|
"excluded_dirs": None,
|
2021-09-08 22:01:10 +00:00
|
|
|
"max_backups": 0,
|
2022-03-23 02:50:12 +00:00
|
|
|
"server_id": server_id,
|
|
|
|
"compress": False,
|
2021-09-08 22:01:10 +00:00
|
|
|
}
|
|
|
|
new_row = True
|
|
|
|
if max_backups is not None:
|
2022-03-23 02:50:12 +00:00
|
|
|
conf["max_backups"] = max_backups
|
2022-03-01 01:02:30 +00:00
|
|
|
if excluded_dirs is not None:
|
|
|
|
dirs_to_exclude = ",".join(excluded_dirs)
|
2022-03-23 02:50:12 +00:00
|
|
|
conf["excluded_dirs"] = dirs_to_exclude
|
|
|
|
conf["compress"] = compress
|
2021-09-08 22:01:10 +00:00
|
|
|
if not new_row:
|
2022-04-11 05:23:55 +00:00
|
|
|
with self.database.atomic():
|
2022-04-10 22:00:44 +00:00
|
|
|
if backup_path is not None:
|
2022-04-14 02:10:25 +00:00
|
|
|
server_rows = (
|
2022-04-10 22:00:44 +00:00
|
|
|
Servers.update(backup_path=backup_path)
|
|
|
|
.where(Servers.server_id == server_id)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
else:
|
2022-04-14 02:10:25 +00:00
|
|
|
server_rows = 0
|
|
|
|
backup_rows = (
|
2022-04-10 22:00:44 +00:00
|
|
|
Backups.update(conf).where(Backups.server_id == server_id).execute()
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-04-14 02:10:25 +00:00
|
|
|
logger.debug(
|
|
|
|
f"Updating existing backup record. "
|
|
|
|
f"{server_rows}+{backup_rows} rows affected"
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
else:
|
2022-04-11 05:23:55 +00:00
|
|
|
with self.database.atomic():
|
2022-04-10 22:00:44 +00:00
|
|
|
conf["server_id"] = server_id
|
|
|
|
if backup_path is not None:
|
|
|
|
Servers.update(backup_path=backup_path).where(
|
|
|
|
Servers.server_id == server_id
|
|
|
|
)
|
|
|
|
Backups.create(**conf)
|
|
|
|
logger.debug("Creating new backup record.")
|
2022-02-27 21:37:48 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_excluded_backup_dirs(server_id: int):
|
2022-04-14 02:10:25 +00:00
|
|
|
excluded_dirs = HelpersManagement.get_backup_config(server_id)["excluded_dirs"]
|
2022-03-01 01:02:30 +00:00
|
|
|
if excluded_dirs is not None and excluded_dirs != "":
|
|
|
|
dir_list = excluded_dirs.split(",")
|
|
|
|
else:
|
|
|
|
dir_list = []
|
|
|
|
return dir_list
|
|
|
|
|
|
|
|
def add_excluded_backup_dir(self, server_id: int, dir_to_add: str):
|
2022-04-14 12:33:53 +00:00
|
|
|
dir_list = self.get_excluded_backup_dirs(server_id)
|
2022-03-01 01:02:30 +00:00
|
|
|
if dir_to_add not in dir_list:
|
|
|
|
dir_list.append(dir_to_add)
|
|
|
|
excluded_dirs = ",".join(dir_list)
|
|
|
|
self.set_backup_config(server_id=server_id, excluded_dirs=excluded_dirs)
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(
|
2022-03-23 06:16:22 +00:00
|
|
|
f"Not adding {dir_to_add} to excluded directories - "
|
|
|
|
f"already in the excluded directory list for server ID {server_id}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-03-01 01:02:30 +00:00
|
|
|
|
|
|
|
def del_excluded_backup_dir(self, server_id: int, dir_to_del: str):
|
2022-04-14 12:33:53 +00:00
|
|
|
dir_list = self.get_excluded_backup_dirs(server_id)
|
2022-03-01 01:02:30 +00:00
|
|
|
if dir_to_del in dir_list:
|
|
|
|
dir_list.remove(dir_to_del)
|
|
|
|
excluded_dirs = ",".join(dir_list)
|
|
|
|
self.set_backup_config(server_id=server_id, excluded_dirs=excluded_dirs)
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(
|
2022-03-23 06:16:22 +00:00
|
|
|
f"Not removing {dir_to_del} from excluded directories - "
|
|
|
|
f"not in the excluded directory list for server ID {server_id}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-03-02 18:07:00 +00:00
|
|
|
|
2022-03-02 17:55:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def clear_unexecuted_commands():
|
2022-04-02 01:34:12 +00:00
|
|
|
Commands.update({Commands.executed: True}).where(
|
2022-04-02 16:54:30 +00:00
|
|
|
Commands.executed == False # pylint: disable=singleton-comparison
|
2022-04-02 01:34:12 +00:00
|
|
|
).execute()
|