From 07eb1e4eaadd931896e830b71a7c7ab8f11f7005 Mon Sep 17 00:00:00 2001 From: "M. Rehan" Date: Mon, 29 Jul 2024 11:44:36 +0500 Subject: [PATCH] Pass more context to apps when doing app operations --- src/middlewared/middlewared/plugins/apps/crud.py | 4 ++-- .../middlewared/plugins/apps/ix_apps/lifecycle.py | 6 ++++-- src/middlewared/middlewared/plugins/apps/rollback.py | 2 +- src/middlewared/middlewared/plugins/apps/upgrade.py | 12 ++++++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/middlewared/middlewared/plugins/apps/crud.py b/src/middlewared/middlewared/plugins/apps/crud.py index 9333203afb743..f70f717309e0f 100644 --- a/src/middlewared/middlewared/plugins/apps/crud.py +++ b/src/middlewared/middlewared/plugins/apps/crud.py @@ -183,7 +183,7 @@ def do_create(self, job, data): app_version_details = self.middleware.call_sync( 'catalog.app_version_details', get_installed_app_version_path(app_name, version) ) - new_values = add_context_to_values(app_name, new_values, install=True) + new_values = add_context_to_values(app_name, new_values, app_version_details['app_metadata'], install=True) update_app_config(app_name, version, new_values) update_app_metadata(app_name, app_version_details) @@ -239,7 +239,7 @@ def update_internal(self, job, app, data, progress_keyword='Update'): job.set_progress(25, 'Initial Validation completed') - new_values = add_context_to_values(app_name, new_values, update=True) + new_values = add_context_to_values(app_name, new_values, app['metadata'], update=True) update_app_config(app_name, app['version'], new_values) update_app_metadata_for_portals(app_name, app['version']) job.set_progress(60, 'Configuration updated, updating docker resources') diff --git a/src/middlewared/middlewared/plugins/apps/ix_apps/lifecycle.py b/src/middlewared/middlewared/plugins/apps/ix_apps/lifecycle.py index 503cb37e19be3..ed91c48962f6e 100644 --- a/src/middlewared/middlewared/plugins/apps/ix_apps/lifecycle.py +++ b/src/middlewared/middlewared/plugins/apps/ix_apps/lifecycle.py @@ -53,12 +53,13 @@ def get_action_context(app_name: str) -> dict[str, typing.Any]: 'is_upgrade': False, 'upgrade_metadata': {}, 'app_name': app_name, + 'app_metadata': {}, }) def add_context_to_values( - app_name: str, values: dict[str, typing.Any], *, install: bool = False, update: bool = False, upgrade: bool = False, - upgrade_metadata: dict[str, typing.Any] = None, rollback: bool = False, + app_name: str, values: dict[str, typing.Any], app_metadata: dict, *, install: bool = False, update: bool = False, + upgrade: bool = False, upgrade_metadata: dict[str, typing.Any] = None, rollback: bool = False, ) -> dict[str, typing.Any]: assert install or update or upgrade or rollback, 'At least one of install, update, rollback or upgrade must be True' assert sum([install, rollback, update, upgrade]) <= 1, 'Only one of install, update, or upgrade can be True.' @@ -77,6 +78,7 @@ def add_context_to_values( for operation, _ in filter(lambda i: i[1], operation_map.items()): action_context.update({ 'operation': operation, + 'app_metadata': app_metadata, f'is_{operation.lower()}': True, **({'upgrade_metadata': upgrade_metadata} if operation == 'UPGRADE' else {}) }) diff --git a/src/middlewared/middlewared/plugins/apps/rollback.py b/src/middlewared/middlewared/plugins/apps/rollback.py index 4dc0ae0ebcb54..8fe985d33f28e 100644 --- a/src/middlewared/middlewared/plugins/apps/rollback.py +++ b/src/middlewared/middlewared/plugins/apps/rollback.py @@ -45,7 +45,7 @@ def rollback(self, job, app_name, options): 'app.schema.normalize_and_validate_values', rollback_version, config, False, get_installed_app_path(app_name), app, ) - new_values = add_context_to_values(app_name, new_values, rollback=True) + new_values = add_context_to_values(app_name, new_values, rollback_version['app_metadata'], rollback=True) update_app_config(app_name, options['app_version'], new_values) job.set_progress( diff --git a/src/middlewared/middlewared/plugins/apps/upgrade.py b/src/middlewared/middlewared/plugins/apps/upgrade.py index c6e0db25801ef..262b03c37a2d6 100644 --- a/src/middlewared/middlewared/plugins/apps/upgrade.py +++ b/src/middlewared/middlewared/plugins/apps/upgrade.py @@ -49,15 +49,19 @@ def upgrade(self, job, app_name, options): # 5) Docker should be notified to recreate resources and to let upgrade to commence # 6) Update collective metadata config to reflect new version # 7) Finally create ix-volumes snapshot for rollback - with upgrade_config(app_name, upgrade_version) as version_path: + with upgrade_config(app_name, upgrade_version): config = get_current_app_config(app_name, app['version']) config.update(options['values']) - app_version_details = self.middleware.call_sync('catalog.app_version_details', version_path) new_values = self.middleware.call_sync( - 'app.schema.normalize_and_validate_values', app_version_details, config, False, + 'app.schema.normalize_and_validate_values', upgrade_version, config, False, get_installed_app_path(app_name), app, ) - new_values = add_context_to_values(app_name, new_values, upgrade=True, upgrade_metadata={}) + new_values = add_context_to_values( + app_name, new_values, upgrade_version['app_metadata'], upgrade=True, upgrade_metadata={ + 'old_version_metadata': app['metadata'], + 'new_version_metadata': upgrade_version['app_metadata'], + } + ) update_app_config(app_name, upgrade_version['version'], new_values) job.set_progress(40, f'Configuration updated for {app_name!r}, upgrading app')