refactoring

This commit is contained in:
Thies Lennart Alff 2024-11-29 10:22:34 +01:00
parent 277cabc965
commit 849471b510
Signed by: lennartalff
GPG key ID: 4EC67D34D594104D
3 changed files with 41 additions and 45 deletions

View file

@ -1,4 +1,5 @@
import requests
import subprocess
class Gotify:
@ -20,5 +21,20 @@ class Gotify:
return self.send(priority=5, title=title, text=text)
def send_error(self, title="", text=""):
return self.send(priority=10, title=title, text=text)
return self.send(priority=10, title=f"❗💀❗ {title}", text=text)
def send_subprocess_error(self, title, error: subprocess.CalledProcessError):
text = f"stdout:\n{error.stdout}\nstderr:\n{error.stderr}"
return self.send_error(title=title, text=text)
def send_backup_successful(self, borg_result: subprocess.CompletedProcess):
return self.send_success("Backup completed", borg_result)
def send_success(self, title, process_result: subprocess.CompletedProcess):
text = "\n".join([process_result.stdout, process_result.stderr])
return self.send_info(f"{title}", text)
def send_warning(self, title, text):
return self.send(priority=5, title=f"{title}", text=text)

View file

@ -27,19 +27,17 @@ class BackupManager:
cmd, shell=True, text=True, check=True, capture_output=True
)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
"❗💀❗ Enabling maintenace failed",
f"stdout:\n{e.stdout}\nstderr:\n{e.stderr}",
self._gotify.send_subprocess_error(
title="Enabling maintenance failed.", error=e
)
return False
if "Maintenance mode already enabled" in result.stdout:
self._gotify.send_info(
"❗ Maintenance unexpectedly enabled",
(
"Maintenance mode was already enabled. "
"Did not expect that. Will continue."
),
title = "Maintenance unexpectedly enabled"
text = (
"Maintenance mode was already enabled. "
"Did not expect that. Will continue."
)
self._gotify.send_warning(title, text)
return True
def disable_maintenance(self):
@ -49,19 +47,15 @@ class BackupManager:
cmd, shell=True, text=True, check=True, capture_output=True
)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
"❗💀❗ Disabling maintenace failed",
f"stdout:\n{e.stdout}\nstderr:\n{e.stderr}",
)
self._gotify.send_subprocess_error("Disabling maintenance failed", e)
return False
if "Maintenance mode already disabled" in result.stdout:
self._gotify.send_info(
"❗ Maintenance mode unexpectedly already disabled",
(
"Maintenance mode was already disabled. "
"Did not expect that. Will continue."
),
title = "Maintenance mode unexpectedly already disabled"
text = (
"Maintenance mode was already disabled. "
"Did not expect that. Will continue."
)
self._gotify.send_warning(title, text)
return True
def dump_database(self):
@ -79,13 +73,9 @@ class BackupManager:
capture_output=True,
)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
"❗💀❗ Dumping Database failed",
f"stdout:\n{e.stdout}\nstderr:\n{e.stderr}",
)
self._gotify.send_subprocess_error("Dumping database failed", e)
return False
text = "\n".join([result.stdout, result.stderr])
self._gotify.send_info("✅ Database dumped", f"Result:\n{text}")
self._gotify.send_backup_successful(result)
return True
def borg_backup(self):
@ -110,13 +100,9 @@ class BackupManager:
env=borg_env,
)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
title="❗💀❗ Backup failed!",
text=f"stdout: \n{e.stdout}\nsterr: \n{e.stderr}",
)
self._gotify.send_subprocess_error("Backup failed", e)
return False
text = "\n".join([result.stdout, result.stderr])
self._gotify.send_info("✅ Backup completed", f"Result:\n{text}\n")
self._gotify.send_backup_successful(result)
return True

View file

@ -23,15 +23,14 @@ class BackupManager:
def export_data(self):
cmd = "docker compose exec -it webserver document_exporter ../export -d -f"
try:
result = subprocess.run(cmd, shell=True, text=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
"❗💀❗ Exporting data failed",
f"stdout:\n{e.stdout}\nstderr:\n{e.stderr}",
result = subprocess.run(
cmd, shell=True, text=True, check=True, capture_output=True
)
except subprocess.CalledProcessError as e:
self._gotify.send_subprocess_error("Exporting data failed", e)
return False
self._gotify.send_success("Data exported.", result)
return True
def borg_backup(self):
backup_dirs = " ".join(self._config["BACKUP_DIRS"])
@ -55,13 +54,9 @@ class BackupManager:
env=borg_env,
)
except subprocess.CalledProcessError as e:
self._gotify.send_error(
title="❗💀❗ Backup failed!",
text=f"stdout: \n{e.stdout}\nsterr: \n{e.stderr}",
)
self._gotify.send_subprocess_error("Backup failed", e)
return False
text = "\n".join([result.stdout, result.stderr])
self._gotify.send_info("✅ Backup completed", f"Result:\n{text}\n")
self._gotify.send_backup_successful(result)
return True
@ -78,4 +73,3 @@ def main():
if __name__ == "__main__":
main()