Include option for after command

This commit is contained in:
Andrew 2023-01-21 13:13:02 -05:00
parent cf9ad77879
commit 248a5a63a9
6 changed files with 70 additions and 27 deletions

View File

@ -145,7 +145,8 @@ class ManagementController:
excluded_dirs: list = None,
compress: bool = False,
shutdown: bool = False,
command: str = "",
before: str = "",
after: str = "",
):
return self.management_helper.set_backup_config(
server_id,
@ -154,7 +155,8 @@ class ManagementController:
excluded_dirs,
compress,
shutdown,
command,
before,
after,
)
@staticmethod

View File

@ -131,7 +131,8 @@ class Backups(BaseModel):
server_id = ForeignKeyField(Servers, backref="backups_server")
compress = BooleanField(default=False)
shutdown = BooleanField(default=False)
command = CharField(default="")
before = CharField(default="")
after = CharField(default="")
class Meta:
table_name = "backups"
@ -370,7 +371,8 @@ class HelpersManagement:
"server_id": row.server_id_id,
"compress": row.compress,
"shutdown": row.shutdown,
"command": row.command,
"before": row.before,
"after": row.after,
}
except IndexError:
conf = {
@ -380,7 +382,8 @@ class HelpersManagement:
"server_id": server_id,
"compress": False,
"shutdown": False,
"command": "",
"before": "",
"after": "",
}
return conf
@ -396,7 +399,8 @@ class HelpersManagement:
excluded_dirs: list = None,
compress: bool = False,
shutdown: bool = False,
command: str = "",
before: str = "",
after: str = "",
):
logger.debug(f"Updating server {server_id} backup config with {locals()}")
if Backups.select().where(Backups.server_id == server_id).exists():
@ -409,7 +413,8 @@ class HelpersManagement:
"server_id": server_id,
"compress": False,
"shutdown": False,
"command": "",
"before": "",
"after": "",
}
new_row = True
if max_backups is not None:
@ -419,7 +424,8 @@ class HelpersManagement:
conf["excluded_dirs"] = dirs_to_exclude
conf["compress"] = compress
conf["shutdown"] = shutdown
conf["command"] = command
conf["before"] = before
conf["after"] = after
if not new_row:
with self.database.atomic():
if backup_path is not None:

View File

@ -1024,12 +1024,12 @@ class ServerInstance:
)
time.sleep(3)
conf = HelpersManagement.get_backup_config(self.server_id)
if conf["command"]:
if conf["before"]:
if self.check_running():
logger.debug(
"Found running server and send command option. Sending command"
)
self.send_command(conf["command"])
self.send_command(conf["before"])
# pause to let people read message.
time.sleep(5)
if conf["shutdown"]:
@ -1113,6 +1113,14 @@ class ServerInstance:
self.run_threaded_server(HelperUsers.get_user_id_by_name("system"))
time.sleep(3)
self.last_backup_failed = False
if conf["after"]:
if self.check_running():
logger.debug(
"Found running server and send command option. Sending command"
)
self.send_command(conf["after"])
# pause to let people read message.
time.sleep(5)
except:
logger.exception(
f"Failed to create backup of server {self.name} (ID {self.server_id})"

View File

@ -1678,7 +1678,8 @@ class PanelHandler(BaseHandler):
compress = self.get_argument("compress", False)
shutdown = self.get_argument("shutdown", False)
check_changed = self.get_argument("changed")
command = self.get_argument("backup_command", "")
before = self.get_argument("backup_before", "")
after = self.get_argument("backup_after", "")
if str(check_changed) == str(1):
checked = self.get_body_arguments("root_path")
else:
@ -1702,7 +1703,8 @@ class PanelHandler(BaseHandler):
excluded_dirs=checked,
compress=bool(compress),
shutdown=bool(shutdown),
command=command,
before=before,
after=after,
)
self.controller.management.add_to_audit_log(

View File

@ -109,18 +109,35 @@
</div>
<div class="form-group">
<label for="command-check" class="form-check-label ml-4 mb-4"></label>
{% if data['backup_config']['command'] %}
<input type="checkbox" class="form-check-input" id="command-check" name="command-check" checked>Run
{% if data['backup_config']['before'] %}
<input type="checkbox" class="form-check-input" id="before-check" name="before-check" checked>Run
Command Before Backup
<br>
<input type="text" class="form-control" name="backup_command" id="backup_command"
value="{{ data['backup_config']['command'] }}" placeholder="We enter the / for you"
<input type="text" class="form-control" name="backup_before" id="backup_before"
value="{{ data['backup_config']['before'] }}" placeholder="We enter the / for you"
style="display: inline-block;">
{% else %}
<input type="checkbox" class="form-check-input" id="command-check" name="command-check">Run Command
<input type="checkbox" class="form-check-input" id="before-check" name="before-check">Run Command
Before Backup
<br>
<input type="text" class="form-control" name="backup_command" id="backup_command" value=""
<input type="text" class="form-control" name="backup_before" id="backup_before" value=""
placeholder="We enter the / for you." style="display: none;">
{% end %}
</div>
<div class="form-group">
<label for="command-check" class="form-check-label ml-4 mb-4"></label>
{% if data['backup_config']['after'] %}
<input type="checkbox" class="form-check-input" id="after-check" name="after-check" checked>Run
Command After Backup
<br>
<input type="text" class="form-control" name="backup_after" id="backup_after"
value="{{ data['backup_config']['after'] }}" placeholder="We enter the / for you"
style="display: inline-block;">
{% else %}
<input type="checkbox" class="form-check-input" id="after-check" name="after-check">Run Command
Before Backup
<br>
<input type="text" class="form-control" name="backup_after" id="backup_after" value=""
placeholder="We enter the / for you." style="display: none;">
{% end %}
</div>
@ -361,14 +378,20 @@
});
}
$("#command-check").on("click", function () {
console.log("in command-check")
if ($("#command-check:checked").val()) {
$("#backup_command").css("display", "inline-block");
console.log("in if")
$("#before-check").on("click", function () {
if ($("#before-check:checked").val()) {
$("#backup_before").css("display", "inline-block");
} else {
$("#backup_command").css("display", "none");
$("#backup_command").val("");
$("#backup_before").css("display", "none");
$("#backup_before").val("");
}
});
$("#after-check").on("click", function () {
if ($("#after-check:checked").val()) {
$("#backup_after").css("display", "inline-block");
} else {
$("#backup_after").css("display", "none");
$("#backup_after").val("");
}
});

View File

@ -3,14 +3,16 @@ import peewee
def migrate(migrator, database, **kwargs):
migrator.add_columns("backups", command=peewee.CharField(default=""))
migrator.add_columns("backups", before=peewee.CharField(default=""))
migrator.add_columns("backups", after=peewee.CharField(default=""))
"""
Write your migrations here.
"""
def rollback(migrator, database, **kwargs):
migrator.drop_columns("backups", ["command"])
migrator.drop_columns("backups", ["before"])
migrator.drop_columns("backups", ["after"])
"""
Write your rollback migrations here.
"""