From 6297eb11a17043bb41538c0f0fc0f1e248a216ad Mon Sep 17 00:00:00 2001 From: pvizeli Date: Tue, 4 Apr 2017 10:19:14 +0200 Subject: [PATCH] Simplify handling with load docker --- hassio_api/hassio/api/homeassistant.py | 5 ++++- hassio_api/hassio/config.py | 12 ------------ hassio_api/hassio/core.py | 5 ++--- hassio_api/hassio/dock/__init__.py | 24 ++++++++++++++++++++++-- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/hassio_api/hassio/api/homeassistant.py b/hassio_api/hassio/api/homeassistant.py index 06f901a2b..830e14dac 100644 --- a/hassio_api/hassio/api/homeassistant.py +++ b/hassio_api/hassio/api/homeassistant.py @@ -33,6 +33,9 @@ class APIHomeAssistant(object): version = body.get(ATTR_VERSION, self.config.current_homeassistant) if self.dock_hass.in_progress: - raise RuntimeError("Other task is in progress!") + raise RuntimeError("Other task is in progress.") + + if version == self.dock_hass.version: + raise RuntimeError("%s is already in use.", version) return await asyncio.shield(self.dock_hass.update(version)) diff --git a/hassio_api/hassio/config.py b/hassio_api/hassio/config.py index db6bfa222..ca5bc3749 100644 --- a/hassio_api/hassio/config.py +++ b/hassio_api/hassio/config.py @@ -11,7 +11,6 @@ _LOGGER = logging.getLogger(__name__) HOMEASSISTANT_CONFIG = "{}/homeassistant_config" HOMEASSISTANT_SSL = "{}/homeassistant_ssl" HOMEASSISTANT_IMAGE = 'homeassistant_image' -HOMEASSISTANT_TAG = 'homeassistant_tag' HOMEASSISTANT_CURRENT = 'homeassistant_current' HASSIO_CURRENT = 'hassio_current' @@ -67,17 +66,6 @@ class CoreConfig(object): """Return docker homeassistant repository.""" return self._data.get(HOMEASSISTANT_IMAGE) - @property - def homeassistant_tag(self): - """Return docker homeassistant tag.""" - return self._data.get(HOMEASSISTANT_TAG) - - @homeassistant_tag.setter - def homeassistant_tag(self, value): - """Set docker homeassistant tag.""" - self._data[HOMEASSISTANT_TAG] = value - self.save() - @property def current_homeassistant(self): """Actual version of homeassistant.""" diff --git a/hassio_api/hassio/core.py b/hassio_api/hassio/core.py index df4e1ff77..9f2c95160 100644 --- a/hassio_api/hassio/core.py +++ b/hassio_api/hassio/core.py @@ -66,7 +66,7 @@ class HassIO(object): first_run=True) # first start of supervisor? - if self.config.homeassistant_tag is None: + if await self.homeassistant.exists(): _LOGGER.info("No HomeAssistant docker found.") await self._setup_homeassistant() @@ -100,5 +100,4 @@ class HassIO(object): await asyncio.sleep(60, loop=self.loop) # store version - self.config.homeassistant_tag = self.config.current_homeassistant - _LOGGER.info("HomeAssistant docker now exists.") + _LOGGER.info("HomeAssistant docker now installed.") diff --git a/hassio_api/hassio/dock/__init__.py b/hassio_api/hassio/dock/__init__.py index e4caf8e89..78873c601 100644 --- a/hassio_api/hassio/dock/__init__.py +++ b/hassio_api/hassio/dock/__init__.py @@ -60,6 +60,26 @@ class DockerBase(object): return False return True + def exists(self): + """Return True if docker image exists in local repo. + + Return a Future. + """ + return self.loop.run_in_executor(None, self._is_running) + + def _exists(self): + """Return True if docker image exists in local repo. + + Need run inside executor. + """ + try: + image = self.dock.images.get(self.image) + self.version = get_version_from_env(image.attrs['Config']['Env']) + except docker.errors.DockerException: + return False + + return True + def is_running(self): """Return True if docker is Running. @@ -118,8 +138,8 @@ class DockerBase(object): return False async with self._lock: - _LOGGER.info("Run docker image %s.", - self.image) + _LOGGER.info("Run docker image %s with version %s.", + self.image, self.version) return await self.loop.run_in_executor(None, self._run) def _run(self):