diff --git a/hassio_api/hassio/__main__.py b/hassio_api/hassio/__main__.py index a62f36f65..071664383 100644 --- a/hassio_api/hassio/__main__.py +++ b/hassio_api/hassio/__main__.py @@ -14,12 +14,11 @@ from .const import CONF_HOMEASSISTANT_TAG _LOGGER = logging.getLogger(__name__) -def main(): +async def main(loop): """Start HassIO.""" bootstrap.initialize_logging() # init asyncio & aiohttp client - loop = asyncio.get_event_loop() websession = aiohttp.ClientSession(loop=loop) dock = docker.Client(base_url='unix://var/run/docker.sock', version='auto') @@ -41,7 +40,7 @@ def main(): while True: current = await tools.fetch_current_versions(websession) if current and CONF_HOMEASSISTANT_TAG in current: - if docker_hass.install(current[CONF_SUPERVISOR_TAG]): + if await docker_hass.install(current[CONF_SUPERVISOR_TAG]): break _LOGGER.waring("Can't fetch info from github. Retry in 60") await asyncio.sleep(60, loop=loop) @@ -49,10 +48,10 @@ def main(): config.homeassistant_tag = current[CONF_HOMEASSISTANT_TAG] # run HomeAssistant - docker_hass.run() - - await loop.run_forever() + await docker_hass.run() if __name__ == "__main__": - sys.exit(main()) + loop = asyncio.get_event_loop() + loop.create_task(main(loop)) + loop.run_forever() diff --git a/hassio_api/hassio/docker/__init__.py b/hassio_api/hassio/docker/__init__.py index 1acc2c17d..a4da8fd32 100644 --- a/hassio_api/hassio/docker/__init__.py +++ b/hassio_api/hassio/docker/__init__.py @@ -46,14 +46,33 @@ class DockerBase(object): return False return True - def run(): + def is_running(self): + """Return True if docker is Running. + + Return a Future. + """ + return self.loop.run_in_executor(None, self._is_running) + + def _is_running(self): + """Return True if docker is Running. + + Need run inside executor. + """ + if not self.container: + try: + self.container = self.dock.containers.get(self.docker_name) + except docker.errors.DockerException: + return False + return self.container.status == 'running' + + def run(self): """Run docker image. Return a Future. """ return self.loop.run_in_executor(None, self._run, tag) - def _run(): + def _run(self): """Run docker image. Need run inside executor. diff --git a/hassio_api/hassio/docker/homeassistant.py b/hassio_api/hassio/docker/homeassistant.py index 6f5c6fcc8..c810c01c4 100644 --- a/hassio_api/hassio/docker/homeassistant.py +++ b/hassio_api/hassio/docker/homeassistant.py @@ -18,11 +18,14 @@ class DockerHomeAssistant(DockerBase): """Return name of docker container.""" return HASS_DOCKER_NAME - def _run(): + def _run(self): """Run docker image. Need run inside executor. """ + if self._is_running(): + return + try: self.container = self.dock.containers.run( self.image, @@ -40,7 +43,7 @@ class DockerHomeAssistant(DockerBase): self.config.path_ssl_docker: {'bind': '/ssl', 'mode': 'rw'}, }) - except docker.errors.APIError as err: + except docker.errors.DockerException as err: _LOGGER.error("Can't run %s", self.image) return False diff --git a/hassio_api/hassio/tools.py b/hassio_api/hassio/tools.py index 0be6325ab..83e7113e4 100644 --- a/hassio_api/hassio/tools.py +++ b/hassio_api/hassio/tools.py @@ -7,6 +7,8 @@ import async_timeout from .const import URL_SUPERVISOR_VERSION +_LOGGER = logging.getLogger(__name__) + async def fetch_current_versions(websession): """Fetch current versions from github.""" @@ -15,5 +17,6 @@ async def fetch_current_versions(websession): async with websession.get(URL_SUPERVISOR_VERSION) as request: return (await request.json()) - except Exception: # pylint: disable=broad-except + except Exception as err: # pylint: disable=broad-except + _LOGGER.warning("Can't fetch versions from github! %s", err) return None