mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-09-05 04:51:21 +01:00
* Refactory code / object handling * Next step * fix lint * Step 2 * Cleanup API code * cleanup addons code * cleanup data handling * Cleanup addons data handling * Cleanup docker api * clean docker api p2 * next cleanup round * cleanup start on snapshots * update format strings * fix setup * fix lint * fix lint * fix lint * fix tox * Fix wrong import of datetime module * Fix bug with attributes * fix extraction * Update core * Update logs * Expand scheduler * add support for time interval objects * next updates on tasks * Fix some things * Cleanup code / supervisor * fix lint * Fix some code styles * rename stuff * cleanup api call reload * fix lock replacment * fix lint * fix lint * fix bug * fix wrong config links * fix bugs * fix bug * Update version on startup * Fix some bugs * fix bug * Fix snapshot * Add wait boot options * fix lint * fix default config * fix snapshot * fix snapshot * load snapshots on startup * add log message at the end * Some cleanups * fix bug * add logger * add logger for supervisor update * Add more logger
109 lines
3.0 KiB
Python
109 lines
3.0 KiB
Python
"""Init file for HassIO docker object."""
|
|
from contextlib import suppress
|
|
import logging
|
|
|
|
import docker
|
|
|
|
from .network import DockerNetwork
|
|
from ..const import SOCKET_DOCKER
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class DockerAPI(object):
|
|
"""Docker hassio wrapper.
|
|
|
|
This class is not AsyncIO safe!
|
|
"""
|
|
|
|
def __init__(self):
|
|
"""Initialize docker base wrapper."""
|
|
self.docker = docker.DockerClient(
|
|
base_url="unix:/{}".format(str(SOCKET_DOCKER)), version='auto')
|
|
self.network = DockerNetwork(self.docker)
|
|
|
|
@property
|
|
def images(self):
|
|
"""Return api images."""
|
|
return self.docker.images
|
|
|
|
@property
|
|
def containers(self):
|
|
"""Return api containers."""
|
|
return self.docker.containers
|
|
|
|
@property
|
|
def api(self):
|
|
"""Return api containers."""
|
|
return self.docker.api
|
|
|
|
def run(self, image, **kwargs):
|
|
""""Create a docker and run it.
|
|
|
|
Need run inside executor.
|
|
"""
|
|
name = kwargs.get('name', image)
|
|
network_mode = kwargs.get('network_mode')
|
|
hostname = kwargs.get('hostname')
|
|
|
|
# setup network
|
|
if network_mode:
|
|
kwargs['dns'] = [str(self.network.supervisor)]
|
|
else:
|
|
kwargs['network'] = None
|
|
|
|
# create container
|
|
try:
|
|
container = self.docker.containers.create(image, **kwargs)
|
|
except docker.errors.DockerException as err:
|
|
_LOGGER.error("Can't create container from %s: %s", name, err)
|
|
return False
|
|
|
|
# attach network
|
|
if not network_mode:
|
|
alias = [hostname] if hostname else None
|
|
if self.network.attach_container(container, alias=alias):
|
|
self.network.detach_default_bridge(container)
|
|
else:
|
|
_LOGGER.warning("Can't attach %s to hassio-net!", name)
|
|
|
|
# run container
|
|
try:
|
|
container.start()
|
|
except docker.errors.DockerException as err:
|
|
_LOGGER.error("Can't start %s: %s", name, err)
|
|
return False
|
|
|
|
return True
|
|
|
|
def run_command(self, image, command=None, **kwargs):
|
|
"""Create a temporary container and run command.
|
|
|
|
Need run inside executor.
|
|
"""
|
|
stdout = kwargs.get('stdout', True)
|
|
stderr = kwargs.get('stderr', True)
|
|
|
|
_LOGGER.info("Run command '%s' on %s", command, image)
|
|
try:
|
|
container = self.docker.containers.run(
|
|
image,
|
|
command=command,
|
|
network=self.network.name,
|
|
**kwargs
|
|
)
|
|
|
|
# wait until command is done
|
|
exit_code = container.wait()
|
|
output = container.logs(stdout=stdout, stderr=stderr)
|
|
|
|
except docker.errors.DockerException as err:
|
|
_LOGGER.error("Can't execute command: %s", err)
|
|
return (None, b"")
|
|
|
|
# cleanup container
|
|
with suppress(docker.errors.DockerException):
|
|
container.remove(force=True)
|
|
|
|
return (exit_code, output)
|