mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Merge branch 'refactor/backups' into refactor/upload-api
This commit is contained in:
commit
4a0dfddde5
@ -66,7 +66,7 @@ pylint:
|
||||
sonarcloud-check:
|
||||
stage: lint
|
||||
image:
|
||||
name: sonarsource/sonar-scanner-cli:5.0.1
|
||||
name: sonarsource/sonar-scanner-cli:latest
|
||||
entrypoint: [""]
|
||||
tags:
|
||||
- saas-linux-medium-amd64
|
||||
|
@ -375,10 +375,13 @@ class HelpersManagement:
|
||||
|
||||
@staticmethod
|
||||
def get_default_server_backup(server_id: str) -> dict:
|
||||
print(server_id)
|
||||
bu_query = Backups.select().where(
|
||||
Backups.server_id == server_id & Backups.default == True
|
||||
Backups.server_id == server_id,
|
||||
Backups.default == True, # pylint: disable=singleton-comparison
|
||||
)
|
||||
|
||||
for item in bu_query:
|
||||
print("HI", item)
|
||||
backup_model = bu_query.first()
|
||||
|
||||
if backup_model:
|
||||
|
@ -769,17 +769,16 @@ class PanelHandler(BaseHandler):
|
||||
return
|
||||
backup_config = self.controller.management.get_backup_config(backup_id)
|
||||
server_info = self.controller.servers.get_server_data_by_id(server_id)
|
||||
backup_location = os.path.join(backup_config["backup_location"], backup_id)
|
||||
backup_file = os.path.abspath(
|
||||
os.path.join(
|
||||
Helpers.get_os_understandable_path(
|
||||
backup_config["backup_location"]
|
||||
),
|
||||
Helpers.get_os_understandable_path(backup_location),
|
||||
file,
|
||||
)
|
||||
)
|
||||
if not self.helper.is_subdir(
|
||||
backup_file,
|
||||
Helpers.get_os_understandable_path(backup_config["backup_location"]),
|
||||
Helpers.get_os_understandable_path(backup_location),
|
||||
) or not os.path.isfile(backup_file):
|
||||
self.redirect("/panel/error?error=Invalid path detected")
|
||||
return
|
||||
|
@ -5,13 +5,7 @@ import logging
|
||||
|
||||
from app.classes.shared.console import Console
|
||||
from app.classes.shared.migration import Migrator, MigrateHistory
|
||||
from app.classes.models.management import (
|
||||
Webhooks,
|
||||
Schedules,
|
||||
Backups,
|
||||
)
|
||||
from app.classes.models.server_permissions import RoleServers
|
||||
from app.classes.models.base_model import BaseModel
|
||||
from app.classes.models.roles import Roles
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -53,6 +47,78 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
table_name = "servers"
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Role Servers Class
|
||||
# **********************************************************************************
|
||||
class RoleServers(peewee.Model):
|
||||
role_id = peewee.ForeignKeyField(Roles, backref="role_server")
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="role_server")
|
||||
permissions = peewee.CharField(default="00000000")
|
||||
|
||||
class Meta:
|
||||
table_name = "role_servers"
|
||||
primary_key = peewee.CompositeKey("role_id", "server_id")
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Webhooks Class
|
||||
# **********************************************************************************
|
||||
class Webhooks(peewee.Model):
|
||||
id = peewee.AutoField()
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="webhook_server", null=True)
|
||||
name = peewee.CharField(default="Custom Webhook", max_length=64)
|
||||
url = peewee.CharField(default="")
|
||||
webhook_type = peewee.CharField(default="Custom")
|
||||
bot_name = peewee.CharField(default="Crafty Controller")
|
||||
trigger = peewee.CharField(default="server_start,server_stop")
|
||||
body = peewee.CharField(default="")
|
||||
color = peewee.CharField(default="#005cd1")
|
||||
enabled = peewee.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
table_name = "webhooks"
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Schedules Class
|
||||
# **********************************************************************************
|
||||
class Schedules(peewee.Model):
|
||||
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
|
||||
enabled = peewee.BooleanField()
|
||||
action = peewee.CharField()
|
||||
interval = peewee.IntegerField()
|
||||
interval_type = peewee.CharField()
|
||||
start_time = peewee.CharField(null=True)
|
||||
command = peewee.CharField(null=True)
|
||||
name = peewee.CharField()
|
||||
one_time = peewee.BooleanField(default=False)
|
||||
cron_string = peewee.CharField(default="")
|
||||
parent = peewee.IntegerField(null=True)
|
||||
delay = peewee.IntegerField(default=0)
|
||||
next_run = peewee.CharField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "schedules"
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Backups Class
|
||||
# **********************************************************************************
|
||||
class Backups(peewee.Model):
|
||||
excluded_dirs = peewee.CharField(null=True)
|
||||
max_backups = peewee.IntegerField()
|
||||
max_backups = peewee.IntegerField()
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="backups_server")
|
||||
compress = peewee.BooleanField(default=False)
|
||||
shutdown = peewee.BooleanField(default=False)
|
||||
before = peewee.CharField(default="")
|
||||
after = peewee.CharField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "backups"
|
||||
database = db
|
||||
|
||||
this_migration = MigrateHistory.get_or_none(
|
||||
MigrateHistory.name == "20240217_rework_servers_uuid_part2"
|
||||
)
|
||||
@ -70,8 +136,8 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
return
|
||||
|
||||
try:
|
||||
logger.info("Migrating Data from Int to UUID (Foreign Keys)")
|
||||
Console.info("Migrating Data from Int to UUID (Foreign Keys)")
|
||||
logger.debug("Migrating Data from Int to UUID (Foreign Keys)")
|
||||
Console.debug("Migrating Data from Int to UUID (Foreign Keys)")
|
||||
|
||||
# Changes on Webhooks Log Table
|
||||
for webhook in Webhooks.select():
|
||||
@ -122,8 +188,8 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
and RoleServers.server_id == old_server_id
|
||||
).execute()
|
||||
|
||||
logger.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS")
|
||||
Console.info("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS")
|
||||
logger.debug("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS")
|
||||
Console.debug("Migrating Data from Int to UUID (Foreign Keys) : SUCCESS")
|
||||
|
||||
except Exception as ex:
|
||||
logger.error("Error while migrating Data from Int to UUID (Foreign Keys)")
|
||||
@ -135,16 +201,16 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
return
|
||||
|
||||
try:
|
||||
logger.info("Migrating Data from Int to UUID (Primary Keys)")
|
||||
Console.info("Migrating Data from Int to UUID (Primary Keys)")
|
||||
logger.debug("Migrating Data from Int to UUID (Primary Keys)")
|
||||
Console.debug("Migrating Data from Int to UUID (Primary Keys)")
|
||||
# Migrating servers from the old id type to the new one
|
||||
for server in Servers.select():
|
||||
Servers.update(server_id=server.server_uuid).where(
|
||||
Servers.server_id == server.server_id
|
||||
).execute()
|
||||
|
||||
logger.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS")
|
||||
Console.info("Migrating Data from Int to UUID (Primary Keys) : SUCCESS")
|
||||
logger.debug("Migrating Data from Int to UUID (Primary Keys) : SUCCESS")
|
||||
Console.debug("Migrating Data from Int to UUID (Primary Keys) : SUCCESS")
|
||||
|
||||
except Exception as ex:
|
||||
logger.error("Error while migrating Data from Int to UUID (Primary Keys)")
|
||||
@ -203,9 +269,81 @@ def rollback(migrator: Migrator, database, **kwargs):
|
||||
table_name = "servers"
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Role Servers Class
|
||||
# **********************************************************************************
|
||||
class RoleServers(peewee.Model):
|
||||
role_id = peewee.ForeignKeyField(Roles, backref="role_server")
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="role_server")
|
||||
permissions = peewee.CharField(default="00000000")
|
||||
|
||||
class Meta:
|
||||
table_name = "role_servers"
|
||||
primary_key = peewee.CompositeKey("role_id", "server_id")
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Webhooks Class
|
||||
# **********************************************************************************
|
||||
class Webhooks(peewee.Model):
|
||||
id = peewee.AutoField()
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="webhook_server", null=True)
|
||||
name = peewee.CharField(default="Custom Webhook", max_length=64)
|
||||
url = peewee.CharField(default="")
|
||||
webhook_type = peewee.CharField(default="Custom")
|
||||
bot_name = peewee.CharField(default="Crafty Controller")
|
||||
trigger = peewee.CharField(default="server_start,server_stop")
|
||||
body = peewee.CharField(default="")
|
||||
color = peewee.CharField(default="#005cd1")
|
||||
enabled = peewee.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
table_name = "webhooks"
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Schedules Class
|
||||
# **********************************************************************************
|
||||
class Schedules(peewee.Model):
|
||||
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
|
||||
enabled = peewee.BooleanField()
|
||||
action = peewee.CharField()
|
||||
interval = peewee.IntegerField()
|
||||
interval_type = peewee.CharField()
|
||||
start_time = peewee.CharField(null=True)
|
||||
command = peewee.CharField(null=True)
|
||||
name = peewee.CharField()
|
||||
one_time = peewee.BooleanField(default=False)
|
||||
cron_string = peewee.CharField(default="")
|
||||
parent = peewee.IntegerField(null=True)
|
||||
delay = peewee.IntegerField(default=0)
|
||||
next_run = peewee.CharField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "schedules"
|
||||
database = db
|
||||
|
||||
# **********************************************************************************
|
||||
# Backups Class
|
||||
# **********************************************************************************
|
||||
class Backups(peewee.Model):
|
||||
excluded_dirs = peewee.CharField(null=True)
|
||||
max_backups = peewee.IntegerField()
|
||||
max_backups = peewee.IntegerField()
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="backups_server")
|
||||
compress = peewee.BooleanField(default=False)
|
||||
shutdown = peewee.BooleanField(default=False)
|
||||
before = peewee.CharField(default="")
|
||||
after = peewee.CharField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "backups"
|
||||
database = db
|
||||
|
||||
try:
|
||||
logger.info("Migrating Data from UUID to Int (Primary Keys)")
|
||||
Console.info("Migrating Data from UUID to Int (Primary Keys)")
|
||||
logger.debug("Migrating Data from UUID to Int (Primary Keys)")
|
||||
Console.debug("Migrating Data from UUID to Int (Primary Keys)")
|
||||
# Migrating servers from the old id type to the new one
|
||||
new_id = 0
|
||||
for server in Servers.select():
|
||||
@ -217,8 +355,8 @@ def rollback(migrator: Migrator, database, **kwargs):
|
||||
Servers.server_id == server.server_id
|
||||
).execute()
|
||||
|
||||
logger.info("Migrating Data from UUID to Int (Primary Keys) : SUCCESS")
|
||||
Console.info("Migrating Data from UUID to Int (Primary Keys) : SUCCESS")
|
||||
logger.debug("Migrating Data from UUID to Int (Primary Keys) : SUCCESS")
|
||||
Console.debug("Migrating Data from UUID to Int (Primary Keys) : SUCCESS")
|
||||
|
||||
except Exception as ex:
|
||||
logger.error("Error while migrating Data from UUID to Int (Primary Keys)")
|
||||
@ -230,8 +368,8 @@ def rollback(migrator: Migrator, database, **kwargs):
|
||||
return
|
||||
|
||||
try:
|
||||
logger.info("Migrating Data from UUID to Int (Foreign Keys)")
|
||||
Console.info("Migrating Data from UUID to Int (Foreign Keys)")
|
||||
logger.debug("Migrating Data from UUID to Int (Foreign Keys)")
|
||||
Console.debug("Migrating Data from UUID to Int (Foreign Keys)")
|
||||
# Changes on Webhooks Log Table
|
||||
for webhook in Webhooks.select():
|
||||
old_server_id = webhook.server_id_id
|
||||
@ -281,8 +419,8 @@ def rollback(migrator: Migrator, database, **kwargs):
|
||||
and RoleServers.server_id == old_server_id
|
||||
).execute()
|
||||
|
||||
logger.info("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS")
|
||||
Console.info("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS")
|
||||
logger.debug("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS")
|
||||
Console.debug("Migrating Data from UUID to Int (Foreign Keys) : SUCCESS")
|
||||
|
||||
except Exception as ex:
|
||||
logger.error("Error while migrating Data from UUID to Int (Foreign Keys)")
|
||||
|
@ -5,7 +5,6 @@ import peewee
|
||||
import logging
|
||||
|
||||
|
||||
from app.classes.models.management import Backups, Schedules
|
||||
from app.classes.shared.helpers import Helpers
|
||||
from app.classes.shared.console import Console
|
||||
from app.classes.shared.migration import Migrator
|
||||
@ -67,6 +66,25 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
table_name = "servers"
|
||||
database = db
|
||||
|
||||
class Backups(peewee.Model):
|
||||
backup_id = peewee.CharField(primary_key=True, default=Helpers.create_uuid)
|
||||
backup_name = peewee.CharField(default="New Backup")
|
||||
backup_location = peewee.CharField(default="")
|
||||
excluded_dirs = peewee.CharField(null=True)
|
||||
max_backups = peewee.IntegerField()
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="backups_server")
|
||||
compress = peewee.BooleanField(default=False)
|
||||
shutdown = peewee.BooleanField(default=False)
|
||||
before = peewee.CharField(default="")
|
||||
after = peewee.CharField(default="")
|
||||
default = peewee.BooleanField(default=False)
|
||||
status = peewee.CharField(default='{"status": "Standby", "message": ""}')
|
||||
enabled = peewee.BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
table_name = "backups"
|
||||
database = db
|
||||
|
||||
class NewBackups(peewee.Model):
|
||||
backup_id = peewee.CharField(primary_key=True, default=Helpers.create_uuid)
|
||||
backup_name = peewee.CharField(default="New Backup")
|
||||
@ -86,6 +104,27 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
table_name = "new_backups"
|
||||
database = db
|
||||
|
||||
class Schedules(peewee.Model):
|
||||
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
|
||||
enabled = peewee.BooleanField()
|
||||
action = peewee.CharField()
|
||||
interval = peewee.IntegerField()
|
||||
interval_type = peewee.CharField()
|
||||
start_time = peewee.CharField(null=True)
|
||||
command = peewee.CharField(null=True)
|
||||
action_id = peewee.CharField(null=True)
|
||||
name = peewee.CharField()
|
||||
one_time = peewee.BooleanField(default=False)
|
||||
cron_string = peewee.CharField(default="")
|
||||
parent = peewee.IntegerField(null=True)
|
||||
delay = peewee.IntegerField(default=0)
|
||||
next_run = peewee.CharField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "schedules"
|
||||
database = db
|
||||
|
||||
class NewSchedules(peewee.Model):
|
||||
schedule_id = peewee.IntegerField(unique=True, primary_key=True)
|
||||
server_id = peewee.ForeignKeyField(Servers, backref="schedule_server")
|
||||
@ -105,6 +144,7 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
|
||||
class Meta:
|
||||
table_name = "new_schedules"
|
||||
database = db
|
||||
|
||||
migrator.create_table(NewBackups)
|
||||
migrator.create_table(NewSchedules)
|
||||
|
Loading…
Reference in New Issue
Block a user