mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-27 02:56:31 +00:00
Check if docker already running
This commit is contained in:
parent
950459b708
commit
344f938c0b
@ -14,12 +14,11 @@ from .const import CONF_HOMEASSISTANT_TAG
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
async def main(loop):
|
||||||
"""Start HassIO."""
|
"""Start HassIO."""
|
||||||
bootstrap.initialize_logging()
|
bootstrap.initialize_logging()
|
||||||
|
|
||||||
# init asyncio & aiohttp client
|
# init asyncio & aiohttp client
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
websession = aiohttp.ClientSession(loop=loop)
|
websession = aiohttp.ClientSession(loop=loop)
|
||||||
dock = docker.Client(base_url='unix://var/run/docker.sock', version='auto')
|
dock = docker.Client(base_url='unix://var/run/docker.sock', version='auto')
|
||||||
|
|
||||||
@ -41,7 +40,7 @@ def main():
|
|||||||
while True:
|
while True:
|
||||||
current = await tools.fetch_current_versions(websession)
|
current = await tools.fetch_current_versions(websession)
|
||||||
if current and CONF_HOMEASSISTANT_TAG in current:
|
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
|
break
|
||||||
_LOGGER.waring("Can't fetch info from github. Retry in 60")
|
_LOGGER.waring("Can't fetch info from github. Retry in 60")
|
||||||
await asyncio.sleep(60, loop=loop)
|
await asyncio.sleep(60, loop=loop)
|
||||||
@ -49,10 +48,10 @@ def main():
|
|||||||
config.homeassistant_tag = current[CONF_HOMEASSISTANT_TAG]
|
config.homeassistant_tag = current[CONF_HOMEASSISTANT_TAG]
|
||||||
|
|
||||||
# run HomeAssistant
|
# run HomeAssistant
|
||||||
docker_hass.run()
|
await docker_hass.run()
|
||||||
|
|
||||||
await loop.run_forever()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.create_task(main(loop))
|
||||||
|
loop.run_forever()
|
||||||
|
@ -46,14 +46,33 @@ class DockerBase(object):
|
|||||||
return False
|
return False
|
||||||
return True
|
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.
|
"""Run docker image.
|
||||||
|
|
||||||
Return a Future.
|
Return a Future.
|
||||||
"""
|
"""
|
||||||
return self.loop.run_in_executor(None, self._run, tag)
|
return self.loop.run_in_executor(None, self._run, tag)
|
||||||
|
|
||||||
def _run():
|
def _run(self):
|
||||||
"""Run docker image.
|
"""Run docker image.
|
||||||
|
|
||||||
Need run inside executor.
|
Need run inside executor.
|
||||||
|
@ -18,11 +18,14 @@ class DockerHomeAssistant(DockerBase):
|
|||||||
"""Return name of docker container."""
|
"""Return name of docker container."""
|
||||||
return HASS_DOCKER_NAME
|
return HASS_DOCKER_NAME
|
||||||
|
|
||||||
def _run():
|
def _run(self):
|
||||||
"""Run docker image.
|
"""Run docker image.
|
||||||
|
|
||||||
Need run inside executor.
|
Need run inside executor.
|
||||||
"""
|
"""
|
||||||
|
if self._is_running():
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.container = self.dock.containers.run(
|
self.container = self.dock.containers.run(
|
||||||
self.image,
|
self.image,
|
||||||
@ -40,7 +43,7 @@ class DockerHomeAssistant(DockerBase):
|
|||||||
self.config.path_ssl_docker:
|
self.config.path_ssl_docker:
|
||||||
{'bind': '/ssl', 'mode': 'rw'},
|
{'bind': '/ssl', 'mode': 'rw'},
|
||||||
})
|
})
|
||||||
except docker.errors.APIError as err:
|
except docker.errors.DockerException as err:
|
||||||
_LOGGER.error("Can't run %s", self.image)
|
_LOGGER.error("Can't run %s", self.image)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ import async_timeout
|
|||||||
|
|
||||||
from .const import URL_SUPERVISOR_VERSION
|
from .const import URL_SUPERVISOR_VERSION
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def fetch_current_versions(websession):
|
async def fetch_current_versions(websession):
|
||||||
"""Fetch current versions from github."""
|
"""Fetch current versions from github."""
|
||||||
@ -15,5 +17,6 @@ async def fetch_current_versions(websession):
|
|||||||
async with websession.get(URL_SUPERVISOR_VERSION) as request:
|
async with websession.get(URL_SUPERVISOR_VERSION) as request:
|
||||||
return (await request.json())
|
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
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user