From 32b470bc7cb7334e8d077f20fe3c50a813eb2138 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Wed, 23 Jan 2019 10:35:00 +0530 Subject: [PATCH] backups: Automatically install required apps before restore Fixes #1460 Signed-off-by: Joseph Nuthalapati Reviewed-by: Sunil Mohan Adapa --- plinth/modules/backups/api.py | 17 ++++++++++++----- plinth/modules/backups/tests/test_api.py | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/plinth/modules/backups/api.py b/plinth/modules/backups/api.py index fb1bfc876..59eb6e69d 100644 --- a/plinth/modules/backups/api.py +++ b/plinth/modules/backups/api.py @@ -27,7 +27,7 @@ TODO: import logging -from plinth import actions, action_utils, module_loader +from plinth import actions, action_utils, module_loader, setup logger = logging.getLogger(__name__) @@ -204,6 +204,7 @@ def restore_apps(restore_handler, app_names=None, create_subvolume=True, restore_root = subvolume['mount_path'] subvolume = True else: + _install_apps_before_restore(apps) _lockdown_apps(apps, lockdown=True) original_state = _shutdown_services(apps) restore_root = '/' @@ -220,6 +221,16 @@ def restore_apps(restore_handler, app_names=None, create_subvolume=True, _lockdown_apps(apps, lockdown=False) +def _install_apps_before_restore(apps): + """Automatically install any applications from the backup archive + if they are not installed yet.""" + modules_to_setup = [] + for backup_app in apps: + if backup_app.app.setup_helper.get_state() == 'needs-setup': + modules_to_setup.append(backup_app.name) + setup.run_setup_on_modules(modules_to_setup) + + class BackupApp: """A application that can be backed up and its manifest.""" @@ -228,10 +239,6 @@ class BackupApp: self.name = name self.app = app - # Not installed - if app.setup_helper.get_state() == 'needs-setup': - raise TypeError - # Has no backup related meta data try: self.manifest = app.backup diff --git a/plinth/modules/backups/tests/test_api.py b/plinth/modules/backups/tests/test_api.py index 317af376e..b1b31048c 100644 --- a/plinth/modules/backups/tests/test_api.py +++ b/plinth/modules/backups/tests/test_api.py @@ -110,7 +110,8 @@ class TestBackupProcesses(unittest.TestCase): backup_handler.assert_called_once() @staticmethod - def test_restore_apps(): + @patch('plinth.modules.backups.api._install_apps_before_restore') + def test_restore_apps(mock_install): """Test that restore_handler is called.""" restore_handler = MagicMock() api.restore_apps(restore_handler)