From 57c1170935b23a576149ba7debfd6b2f2e30b77f Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Mon, 23 Dec 2024 01:50:52 +0500 Subject: [PATCH] Add ability to delete app backup --- .../middlewared/api/v25_04_0/docker.py | 9 +++++++++ .../middlewared/plugins/docker/backup.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/middlewared/middlewared/api/v25_04_0/docker.py b/src/middlewared/middlewared/api/v25_04_0/docker.py index 5f02c267a3db..0d02dc567cb0 100644 --- a/src/middlewared/middlewared/api/v25_04_0/docker.py +++ b/src/middlewared/middlewared/api/v25_04_0/docker.py @@ -11,6 +11,7 @@ 'DockerEntry', 'DockerUpdateArgs', 'DockerUpdateResult', 'DockerStatusArgs', 'DockerStatusResult', 'DockerNvidiaPresentArgs', 'DockerNvidiaPresentResult', 'DockerBackupArgs', 'DockerBackupResult', 'DockerListBackupArgs', 'DockerListBackupResult', 'DockerRestoreBackupArgs', 'DockerRestoreBackupResult', + 'DockerDeleteBackupArgs', 'DockerDeleteBackupResult', ] @@ -120,3 +121,11 @@ class DockerRestoreBackupArgs(BaseModel): class DockerRestoreBackupResult(BaseModel): result: None + + +class DockerDeleteBackupArgs(BaseModel): + backup_name: NonEmptyString + + +class DockerDeleteBackupResult(BaseModel): + result: None diff --git a/src/middlewared/middlewared/plugins/docker/backup.py b/src/middlewared/middlewared/plugins/docker/backup.py index 71e79879150b..dbdf5eb5d492 100644 --- a/src/middlewared/middlewared/plugins/docker/backup.py +++ b/src/middlewared/middlewared/plugins/docker/backup.py @@ -6,7 +6,8 @@ from middlewared.api import api_method from middlewared.api.current import ( - DockerBackupResult, DockerUpdateArgs, DockerListBackupArgs, DockerListBackupResult, + DockerBackupResult, DockerUpdateArgs, DockerListBackupArgs, DockerListBackupResult, DockerDeleteBackupArgs, + DockerDeleteBackupResult, ) from middlewared.plugins.apps.ix_apps.path import get_collective_config_path, get_collective_metadata_path from middlewared.plugins.zfs_.validation_utils import validate_snapshot_name @@ -108,3 +109,17 @@ def list_backups(self): } return backups + + @api_method(DockerDeleteBackupArgs, DockerDeleteBackupResult, roles=['DOCKER_WRITE']) + def delete_backup(self, backup_name): + """ + Delete `backup_name` app backup. + """ + self.middleware.call_sync('docker.state.validate') + + backup = self.middleware.call_sync('docker.list_backups').get(backup_name) + if not backup: + raise CallError(f'Backup {backup_name!r} does not exist', errno=errno.ENOENT) + + self.middleware.call_sync('zfs.snapshot.delete', backup['snapshot_name'], {'recursive': True}) + shutil.rmtree(backup['backup_path'], True)