Skip to content

Commit

Permalink
Allow add-on to run on host network (#67)
Browse files Browse the repository at this point in the history
* Allow add-on to run on host network

* cleanup name

* fix lint
  • Loading branch information
pvizeli authored May 22, 2017
1 parent 0b17ffc commit 0cdef0d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
8 changes: 7 additions & 1 deletion hassio/addons/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions hassio/addons/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))?$"
Expand All @@ -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),
Expand All @@ -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)},
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions hassio/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
ATTR_BUILD = 'build'
ATTR_DEVICES = 'devices'
ATTR_ENVIRONMENT = 'environment'
ATTR_HOST_NETWORK = 'host_network'

STARTUP_INITIALIZE = 'initialize'
STARTUP_BEFORE = 'before'
Expand Down
4 changes: 2 additions & 2 deletions hassio/dock/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0cdef0d

Please sign in to comment.