Don't relay on latest with HA/Addons (#1175)

* Don't relay on latest with HA/Addons

* Fix latest on install

* Revert some options

* Fix attach

* migrate to new version handling

* Fix thread

* Fix is running

* Allow wait

* debug code

* Fix debug value

* Fix list

* Fix regex

* Some better log output

* Fix logic

* Improve cleanup handling

* Fix bug

* Cleanup old code

* Improve version handling

* Fix the way to attach
This commit is contained in:
Pascal Vizeli
2019-08-07 09:51:27 +02:00
committed by GitHub
parent 882586b246
commit 778bc46848
18 changed files with 175 additions and 85 deletions

View File

@@ -1,8 +1,10 @@
"""Init file for Hass.io Docker object."""
from distutils.version import StrictVersion
from contextlib import suppress
from ipaddress import IPv4Address
import logging
from typing import Awaitable
import re
from typing import Awaitable, List, Optional
import docker
@@ -13,30 +15,31 @@ from .interface import CommandReturn, DockerInterface
_LOGGER = logging.getLogger(__name__)
HASS_DOCKER_NAME = "homeassistant"
RE_VERSION = re.compile(r"(?P<version>\d+\.\d+\.\d+(?:b\d+|d\d+)?)")
class DockerHomeAssistant(DockerInterface):
"""Docker Hass.io wrapper for Home Assistant."""
@property
def machine(self):
def machine(self) -> Optional[str]:
"""Return machine of Home Assistant Docker image."""
if self._meta and LABEL_MACHINE in self._meta["Config"]["Labels"]:
return self._meta["Config"]["Labels"][LABEL_MACHINE]
return None
@property
def image(self):
def image(self) -> str:
"""Return name of Docker image."""
return self.sys_homeassistant.image
@property
def name(self):
def name(self) -> str:
"""Return name of Docker container."""
return HASS_DOCKER_NAME
@property
def timeout(self) -> str:
def timeout(self) -> int:
"""Return timeout for Docker actions."""
return 60
@@ -60,6 +63,7 @@ class DockerHomeAssistant(DockerInterface):
# Create & Run container
docker_container = self.sys_docker.run(
self.image,
version=self.sys_homeassistant.version,
name=self.name,
hostname=self.name,
detach=True,
@@ -84,8 +88,8 @@ class DockerHomeAssistant(DockerInterface):
},
)
_LOGGER.info("Start homeassistant %s with version %s", self.image, self.version)
self._meta = docker_container.attrs
_LOGGER.info("Start homeassistant %s with version %s", self.image, self.version)
def _execute_command(self, command: str) -> CommandReturn:
"""Create a temporary container and run command.
@@ -94,7 +98,8 @@ class DockerHomeAssistant(DockerInterface):
"""
return self.sys_docker.run_command(
self.image,
command,
version=self.sys_homeassistant.version,
command=command,
privileged=True,
init=True,
detach=True,
@@ -134,3 +139,33 @@ class DockerHomeAssistant(DockerInterface):
return False
return True
def get_latest_version(self) -> Awaitable[str]:
"""Return latest version of local Home Asssistant image."""
return self.sys_run_in_executor(self._get_latest_version)
def _get_latest_version(self) -> str:
"""Return latest version of local Home Asssistant image.
Need run inside executor.
"""
available_version: List[str] = []
try:
for image in self.sys_docker.images.list(self.image):
for tag in image.tags:
match = RE_VERSION.search(tag)
if not match:
continue
available_version.append(match.group("version"))
assert available_version
except (docker.errors.DockerException, AssertionError):
_LOGGER.warning("No local HA version found")
raise DockerAPIError()
else:
_LOGGER.debug("Found HA versions: %s", available_version)
# Sort version and return latest version
available_version.sort(key=StrictVersion, reverse=True)
return available_version[0]