Check if docker already running

This commit is contained in:
Pascal Vizeli 2017-03-22 23:36:06 +01:00
parent 950459b708
commit 344f938c0b
4 changed files with 36 additions and 12 deletions

View File

@ -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()

View File

@ -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.

View File

@ -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

View File

@ -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