From 0cdef0d118a006aa759b8061e4cdf2a2c094f1f5 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 22 May 2017 21:52:37 +0200 Subject: [PATCH] Allow add-on to run on host network (#67) * Allow add-on to run on host network * cleanup name * fix lint --- hassio/addons/data.py | 8 +++++++- hassio/addons/validate.py | 19 ++++++++++++++++--- hassio/const.py | 1 + hassio/dock/addon.py | 4 ++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/hassio/addons/data.py b/hassio/addons/data.py index c9e13482301..a4da39d10be 100644 --- a/hassio/addons/data.py +++ b/hassio/addons/data.py @@ -16,7 +16,7 @@ FILE_HASSIO_ADDONS, ATTR_NAME, ATTR_VERSION, ATTR_SLUG, ATTR_DESCRIPTON, ATTR_STARTUP, ATTR_BOOT, ATTR_MAP, ATTR_OPTIONS, ATTR_PORTS, BOOT_AUTO, ATTR_SCHEMA, ATTR_IMAGE, ATTR_REPOSITORY, ATTR_URL, ATTR_ARCH, - ATTR_LOCATON, ATTR_DEVICES, ATTR_ENVIRONMENT) + ATTR_LOCATON, ATTR_DEVICES, ATTR_ENVIRONMENT, ATTR_HOST_NETWORK) from ..config import Config from ..tools import read_json_file, write_json_file @@ -294,6 +294,12 @@ def get_ports(self, addon): """Return ports of addon.""" return self._system_data[addon].get(ATTR_PORTS) + def get_network_mode(self, addon): + """Return network mode of addon.""" + if self._system_data[addon][ATTR_HOST_NETWORK]: + return 'host' + return 'bridge' + def get_devices(self, addon): """Return devices of addon.""" return self._system_data[addon].get(ATTR_DEVICES) diff --git a/hassio/addons/validate.py b/hassio/addons/validate.py index e3f4cd262e6..96842e30c47 100644 --- a/hassio/addons/validate.py +++ b/hassio/addons/validate.py @@ -6,7 +6,8 @@ ATTR_BOOT, ATTR_MAP, ATTR_OPTIONS, ATTR_PORTS, STARTUP_ONCE, STARTUP_AFTER, STARTUP_BEFORE, STARTUP_INITIALIZE, BOOT_AUTO, BOOT_MANUAL, ATTR_SCHEMA, ATTR_IMAGE, ATTR_URL, ATTR_MAINTAINER, ATTR_ARCH, ATTR_DEVICES, - ATTR_ENVIRONMENT, ARCH_ARMHF, ARCH_AARCH64, ARCH_AMD64, ARCH_I386) + ATTR_ENVIRONMENT, ATTR_HOST_NETWORK, ARCH_ARMHF, ARCH_AARCH64, ARCH_AMD64, + ARCH_I386) MAP_VOLUME = r"^(config|ssl|addons|backup|share)(?::(rw|:ro))?$" @@ -24,8 +25,19 @@ ARCH_ARMHF, ARCH_AARCH64, ARCH_AMD64, ARCH_I386 ] + +def check_network(data): + """Validate network settings.""" + host_network = data[ATTR_HOST_NETWORK] + + if ATTR_PORTS in data and host_network: + raise vol.Invalid("Hostnetwork & ports are not allow!") + + return data + + # pylint: disable=no-value-for-parameter -SCHEMA_ADDON_CONFIG = vol.Schema({ +SCHEMA_ADDON_CONFIG = vol.Schema(vol.All({ vol.Required(ATTR_NAME): vol.Coerce(str), vol.Required(ATTR_VERSION): vol.Coerce(str), vol.Required(ATTR_SLUG): vol.Coerce(str), @@ -38,6 +50,7 @@ vol.Required(ATTR_BOOT): vol.In([BOOT_AUTO, BOOT_MANUAL]), vol.Optional(ATTR_PORTS): dict, + vol.Optional(ATTR_HOST_NETWORK, default=False): vol.Boolean(), vol.Optional(ATTR_DEVICES): [vol.Match(r"^(.*):(.*):([rwm]{1,3})$")], vol.Optional(ATTR_MAP, default=[]): [vol.Match(MAP_VOLUME)], vol.Optional(ATTR_ENVIRONMENT): {vol.Match(r"\w*"): vol.Coerce(str)}, @@ -48,7 +61,7 @@ ]) }, vol.Optional(ATTR_IMAGE): vol.Match(r"\w*/\w*"), -}, extra=vol.ALLOW_EXTRA) +}, check_network), extra=vol.ALLOW_EXTRA) # pylint: disable=no-value-for-parameter diff --git a/hassio/const.py b/hassio/const.py index adf9007f93e..1d55967a45a 100644 --- a/hassio/const.py +++ b/hassio/const.py @@ -77,6 +77,7 @@ ATTR_BUILD = 'build' ATTR_DEVICES = 'devices' ATTR_ENVIRONMENT = 'environment' +ATTR_HOST_NETWORK = 'host_network' STARTUP_INITIALIZE = 'initialize' STARTUP_BEFORE = 'before' diff --git a/hassio/dock/addon.py b/hassio/dock/addon.py index 1dd1948d314..d4613db3e0a 100644 --- a/hassio/dock/addon.py +++ b/hassio/dock/addon.py @@ -86,11 +86,11 @@ def _run(self): self.image, name=self.docker_name, detach=True, - network_mode='bridge', + network_mode=self.addons_data.get_network_mode(self.addon), ports=self.addons_data.get_ports(self.addon), devices=self.addons_data.get_devices(self.addon), environment=self.addons_data.get_environment(self.addon), - volumes=self.volumes, + volumes=self.volumes ) self.process_metadata()