From abbac30b7cb605d25ea7c79d1e6b3e12bff97dfa Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Tue, 24 Dec 2024 00:29:21 +0500 Subject: [PATCH] Minor fixes --- .../middlewared/api/v25_04_0/docker.py | 8 +++++++- .../middlewared/plugins/docker/backup.py | 16 ++++++++-------- .../middlewared/plugins/docker/restore_backup.py | 10 +++++----- .../middlewared/plugins/docker/state_utils.py | 4 ++-- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/middlewared/middlewared/api/v25_04_0/docker.py b/src/middlewared/middlewared/api/v25_04_0/docker.py index 0d02dc567cb03..db1c88a77734e 100644 --- a/src/middlewared/middlewared/api/v25_04_0/docker.py +++ b/src/middlewared/middlewared/api/v25_04_0/docker.py @@ -99,9 +99,15 @@ class DockerListBackupArgs(BaseModel): pass +class AppInfo(BaseModel): + id: NonEmptyString + name: NonEmptyString + state: NonEmptyString + + class BackupInfo(BaseModel): name: NonEmptyString - apps: list[NonEmptyString] + apps: list[AppInfo] snapshot_name: NonEmptyString created_on: NonEmptyString backup_path: NonEmptyString diff --git a/src/middlewared/middlewared/plugins/docker/backup.py b/src/middlewared/middlewared/plugins/docker/backup.py index 8ba3efdc512e6..cc8bf9ec86c52 100644 --- a/src/middlewared/middlewared/plugins/docker/backup.py +++ b/src/middlewared/middlewared/plugins/docker/backup.py @@ -7,8 +7,8 @@ from middlewared.api import api_method from middlewared.api.current import ( - DockerBackupResult, DockerUpdateArgs, DockerListBackupArgs, DockerListBackupResult, DockerDeleteBackupArgs, - DockerDeleteBackupResult, + DockerBackupArgs, DockerBackupResult, 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 @@ -26,7 +26,7 @@ class DockerService(Service): class Config: cli_namespace = 'app.docker' - @api_method(DockerUpdateArgs, DockerBackupResult, roles=['DOCKER_WRITE']) + @api_method(DockerBackupArgs, DockerBackupResult, roles=['DOCKER_WRITE']) @job(lock='docker_backup') def backup(self, job, backup_name): """ @@ -57,7 +57,7 @@ def backup(self, job, backup_name): shutil.copy(get_collective_metadata_path(), os.path.join(backup_dir, 'collective_metadata.yaml')) shutil.copy(get_collective_config_path(), os.path.join(backup_dir, 'collective_config.yaml')) - with open(backup_apps_state_file_path(), 'w') as f: + with open(backup_apps_state_file_path(name), 'w') as f: f.write(yaml.safe_dump({app['name']: app for app in self.middleware.call_sync('app.query')})) with open(os.path.join(backup_dir, 'docker_config.yaml'), 'w') as f: @@ -97,18 +97,18 @@ def list_backups(self): continue try: - with open(backup_apps_state_file_path(), 'r') as f: + with open(backup_apps_state_file_path(backup_name), 'r') as f: apps = yaml.safe_load(f.read()) except (FileNotFoundError, yaml.YAMLError): continue backups[backup_name] = { 'name': backup_name, - 'apps': list(apps), + 'apps': [{k: app[k] for k in ('id', 'name', 'state')} for app in apps.values()], 'snapshot_name': snapshot['name'], - 'created_on': self.middleware.call_sync( + 'created_on': str(self.middleware.call_sync( 'zfs.snapshot.get_instance', snapshot['name'] - )['properties']['creation']['parsed'], + )['properties']['creation']['parsed']), 'backup_path': backup_path, } diff --git a/src/middlewared/middlewared/plugins/docker/restore_backup.py b/src/middlewared/middlewared/plugins/docker/restore_backup.py index 93c74bb06535f..bf9ca5995e735 100644 --- a/src/middlewared/middlewared/plugins/docker/restore_backup.py +++ b/src/middlewared/middlewared/plugins/docker/restore_backup.py @@ -54,13 +54,13 @@ def restore_backup(self, job, backup_name): self.middleware.call_sync('docker.fs_manage.mount') apps_to_start = [] - for app_name, app_state in backup['apps']: - if os.path.exists(get_installed_app_path(app_name)) is False: - logger.debug('App %r path not found, skipping restoring', app_name) + for app_info in backup['apps']: + if os.path.exists(get_installed_app_path(app_info['id'])) is False: + logger.debug('App %r path not found, skipping restoring', app_info['id']) continue - if app_state['state'] is AppState.RUNNING.name: - apps_to_start.append(app_name) + if app_info['state'] == AppState.RUNNING.name: + apps_to_start.append(app_info['id']) metadata_job = self.middleware.call_sync('app.metadata.generate') metadata_job.wait_sync() diff --git a/src/middlewared/middlewared/plugins/docker/state_utils.py b/src/middlewared/middlewared/plugins/docker/state_utils.py index 4a0ad51fe8a5e..7e38b58e2568a 100644 --- a/src/middlewared/middlewared/plugins/docker/state_utils.py +++ b/src/middlewared/middlewared/plugins/docker/state_utils.py @@ -76,8 +76,8 @@ def catalog_ds_path() -> str: return os.path.join(IX_APPS_MOUNT_PATH, CATALOG_DATASET_NAME) -def backup_apps_state_file_path() -> str: - return os.path.join(backup_ds_path(), 'apps_state.json') +def backup_apps_state_file_path(backup_name: str) -> str: + return os.path.join(backup_ds_path(), backup_name, 'apps_state.json') def backup_ds_path() -> str: