Add support for stats & code cleanup (#297)

* Add support for stats & code cleanup

* Add more stats

* Move code into own object

* Add to API

* Update API

* Add error handling

* fix lint

* fix block io
This commit is contained in:
Pascal Vizeli
2018-01-07 15:53:54 +01:00
committed by GitHub
parent e992b70f92
commit eebe90bd14
18 changed files with 254 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ import logging
import docker
from .utils import docker_process
from .stats import DockerStats
from ..const import LABEL_VERSION, LABEL_ARCH
from ..coresys import CoreSysAttributes
@@ -325,3 +326,24 @@ class DockerInterface(CoreSysAttributes):
Need run inside executor.
"""
raise NotImplementedError()
def stats(self):
"""Read and return stats from container."""
return self._loop.run_in_executor(None, self._stats)
def _stats(self):
"""Create a temporary container and run command.
Need run inside executor.
"""
try:
container = self._docker.containers.get(self.name)
except docker.errors.DockerException:
return None
try:
stats = container.stats(stream=False)
return DockerStats(stats)
except docker.errors.DockerException as err:
_LOGGER.error("Can't read stats from %s: %s", self.name, err)
return None