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

View File

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

View File

@ -1024,12 +1024,12 @@ class ServerInstance:
) )
time.sleep(3) time.sleep(3)
conf = HelpersManagement.get_backup_config(self.server_id) conf = HelpersManagement.get_backup_config(self.server_id)
if conf["command"]: if conf["before"]:
if self.check_running(): if self.check_running():
logger.debug( logger.debug(
"Found running server and send command option. Sending command" "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. # pause to let people read message.
time.sleep(5) time.sleep(5)
if conf["shutdown"]: if conf["shutdown"]:
@ -1113,6 +1113,14 @@ class ServerInstance:
self.run_threaded_server(HelperUsers.get_user_id_by_name("system")) self.run_threaded_server(HelperUsers.get_user_id_by_name("system"))
time.sleep(3) time.sleep(3)
self.last_backup_failed = False 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: except:
logger.exception( logger.exception(
f"Failed to create backup of server {self.name} (ID {self.server_id})" 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) compress = self.get_argument("compress", False)
shutdown = self.get_argument("shutdown", False) shutdown = self.get_argument("shutdown", False)
check_changed = self.get_argument("changed") 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): if str(check_changed) == str(1):
checked = self.get_body_arguments("root_path") checked = self.get_body_arguments("root_path")
else: else:
@ -1702,7 +1703,8 @@ class PanelHandler(BaseHandler):
excluded_dirs=checked, excluded_dirs=checked,
compress=bool(compress), compress=bool(compress),
shutdown=bool(shutdown), shutdown=bool(shutdown),
command=command, before=before,
after=after,
) )
self.controller.management.add_to_audit_log( self.controller.management.add_to_audit_log(

View File

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