From 7a74d77d4314581474b3a0a634925feaeccde3f6 Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Wed, 8 Jun 2022 15:21:34 -0400 Subject: [PATCH] Move addon repo migration to bootstrap (#3672) * Move addon repo migration to bootstrap * Save data after migrating it --- supervisor/bootstrap.py | 13 +++++++++++++ supervisor/config.py | 4 ---- supervisor/store/__init__.py | 7 ------- tests/conftest.py | 4 ++-- tests/store/test_store_manager.py | 24 ++++++++++++++---------- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/supervisor/bootstrap.py b/supervisor/bootstrap.py index d7f693b48..2f29280e0 100644 --- a/supervisor/bootstrap.py +++ b/supervisor/bootstrap.py @@ -22,6 +22,8 @@ from .auth import Auth from .backups.manager import BackupManager from .bus import Bus from .const import ( + ATTR_ADDONS_CUSTOM_LIST, + ATTR_REPOSITORIES, ENV_HOMEASSISTANT_REPOSITORY, ENV_SUPERVISOR_MACHINE, ENV_SUPERVISOR_NAME, @@ -49,6 +51,7 @@ from .resolution.module import ResolutionManager from .security.module import Security from .services import ServiceManager from .store import StoreManager +from .store.validate import ensure_builtin_repositories from .supervisor import Supervisor from .updater import Updater @@ -216,6 +219,16 @@ def migrate_system_env(coresys: CoreSys) -> None: except OSError: _LOGGER.error("Can't cleanup old Add-on build directory at '%s'", old_build) + # Supervisor 2022.5 -> 2022.6. Can be removed after 2022.9 + # pylint: disable=protected-access + if len(coresys.config.addons_repositories) > 0: + coresys.store._data[ATTR_REPOSITORIES] = ensure_builtin_repositories( + coresys.config.addons_repositories + ) + coresys.config._data[ATTR_ADDONS_CUSTOM_LIST] = [] + coresys.store.save_data() + coresys.config.save_data() + def initialize_logging() -> None: """Initialize the logging.""" diff --git a/supervisor/config.py b/supervisor/config.py index 145dedbf4..ba693ec85 100644 --- a/supervisor/config.py +++ b/supervisor/config.py @@ -327,7 +327,3 @@ class CoreConfig(FileConfiguration): return self._data[ATTR_ADDONS_CUSTOM_LIST].remove(repo) - - def clear_addons_repositories(self) -> None: - """Clear custom repositories list from core config.""" - self._data[ATTR_ADDONS_CUSTOM_LIST] = [] diff --git a/supervisor/store/__init__.py b/supervisor/store/__init__.py index 030d5dfe8..99e1bf9ef 100644 --- a/supervisor/store/__init__.py +++ b/supervisor/store/__init__.py @@ -73,13 +73,6 @@ class StoreManager(CoreSysAttributes, FileConfiguration): """Start up add-on management.""" await self.data.update() - # Backwards compatibility - Remove after 2022.9 - if len(self.sys_config.addons_repositories) > 0: - self._data[ATTR_REPOSITORIES] = ensure_builtin_repositories( - self.sys_config.addons_repositories - ) - self.sys_config.clear_addons_repositories() - # Init custom repositories and load add-ons await self.update_repositories( self._data[ATTR_REPOSITORIES], add_with_errors=True diff --git a/tests/conftest.py b/tests/conftest.py index caeba096f..529ca483d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,7 +16,7 @@ from supervisor import config as su_config from supervisor.addons.addon import Addon from supervisor.api import RestAPI from supervisor.bootstrap import initialize_coresys -from supervisor.const import ATTR_REPOSITORIES, REQUEST_FROM +from supervisor.const import ATTR_ADDONS_CUSTOM_LIST, ATTR_REPOSITORIES, REQUEST_FROM from supervisor.coresys import CoreSys from supervisor.dbus.agent import OSAgent from supervisor.dbus.const import DBUS_SIGNAL_NM_CONNECTION_ACTIVE_CHANGED @@ -322,7 +322,7 @@ async def repository(coresys: CoreSys): coresys.store._data[ATTR_REPOSITORIES].remove( "https://github.com/esphome/home-assistant-addon" ) - coresys.config.clear_addons_repositories() + coresys.config._data[ATTR_ADDONS_CUSTOM_LIST] = [] with patch( "supervisor.store.validate.BUILTIN_REPOSITORIES", {"local", "core"} diff --git a/tests/store/test_store_manager.py b/tests/store/test_store_manager.py index 5e4883ee2..13482a2e9 100644 --- a/tests/store/test_store_manager.py +++ b/tests/store/test_store_manager.py @@ -1,6 +1,7 @@ """Test store manager.""" from unittest.mock import patch +from supervisor.bootstrap import migrate_system_env from supervisor.const import ATTR_ADDONS_CUSTOM_LIST from supervisor.coresys import CoreSys from supervisor.store import StoreManager @@ -70,29 +71,32 @@ async def test_load_with_custom_repository(coresys: CoreSys): async def test_load_from_core_config(coresys: CoreSys): """Test custom repositories loaded from core config when present.""" - store_manager = StoreManager(coresys) - # pylint: disable=protected-access coresys.config._data[ATTR_ADDONS_CUSTOM_LIST] = ["http://example.com"] assert coresys.config.addons_repositories == ["http://example.com"] + migrate_system_env(coresys) + with patch("supervisor.store.repository.Repository.load", return_value=None), patch( "supervisor.store.repository.Repository.validate", return_value=True ), patch("pathlib.Path.exists", return_value=True): - await store_manager.load() + await coresys.store.load() - assert len(store_manager.all) == 5 - assert isinstance(store_manager.get("core"), Repository) - assert isinstance(store_manager.get("local"), Repository) + assert len(coresys.store.all) == 5 + assert isinstance(coresys.store.get("core"), Repository) + assert isinstance(coresys.store.get("local"), Repository) - assert len(store_manager.repository_urls) == 3 + assert len(coresys.store.repository_urls) == 3 assert ( - "https://github.com/hassio-addons/repository" in store_manager.repository_urls + "https://github.com/hassio-addons/repository" in coresys.store.repository_urls ) assert ( "https://github.com/esphome/home-assistant-addon" - in store_manager.repository_urls + in coresys.store.repository_urls ) - assert "http://example.com" in store_manager.repository_urls + assert "http://example.com" in coresys.store.repository_urls assert coresys.config.addons_repositories == [] + + coresys.config.save_data.assert_called_once() + coresys.store.save_data.assert_called_once()