From 950459b7085d8bb8a6f4a79d4c990a9adf16bf3d Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Wed, 22 Mar 2017 21:58:59 +0100 Subject: [PATCH] update hassio api --- hassio_api/hassio/__main__.py | 10 +++--- hassio_api/hassio/bootstrap.py | 24 ++++++------- hassio_api/hassio/{version.py => config.py} | 38 +++++++++++++++------ hassio_api/hassio/const.py | 12 ++++--- hassio_api/hassio/docker/__init__.py | 11 ++++-- hassio_api/hassio/docker/homeassistant.py | 16 ++++++--- 6 files changed, 72 insertions(+), 39 deletions(-) rename hassio_api/hassio/{version.py => config.py} (65%) diff --git a/hassio_api/hassio/__main__.py b/hassio_api/hassio/__main__.py index 478fef4ca..a62f36f65 100644 --- a/hassio_api/hassio/__main__.py +++ b/hassio_api/hassio/__main__.py @@ -24,16 +24,16 @@ def main(): dock = docker.Client(base_url='unix://var/run/docker.sock', version='auto') # init system - conf_version = bootstrap.initialize_system_data() + config = bootstrap.initialize_system_data() # init HomeAssistant Docker docker_hass = DockerHomeAssistant( - loop, dock, conf_version.homeassistant_image, - conf_version.homeassistant_tag + config, loop, dock, config.homeassistant_image, + config.homeassistant_tag ) # first start of supervisor? - if conf_version.homeassistant_tag is None: + if config.homeassistant_tag is None: _LOGGER.info("First start of supervisor, read version from github.") # read homeassistant tag and install it @@ -46,7 +46,7 @@ def main(): _LOGGER.waring("Can't fetch info from github. Retry in 60") await asyncio.sleep(60, loop=loop) - conf_version.homeassistant_tag = current[CONF_HOMEASSISTANT_TAG] + config.homeassistant_tag = current[CONF_HOMEASSISTANT_TAG] # run HomeAssistant docker_hass.run() diff --git a/hassio_api/hassio/bootstrap.py b/hassio_api/hassio/bootstrap.py index 7ee2b1a24..ddeef1186 100644 --- a/hassio_api/hassio/bootstrap.py +++ b/hassio_api/hassio/bootstrap.py @@ -6,35 +6,33 @@ import os from colorlog import ColoredFormatter -from .const import FILE_HASSIO_ADDONS, HOMEASSISTANT_CONFIG, HOMEASSISTANT_SSL -from .version import Version +from .const import FILE_HASSIO_ADDONS +from .version import CoreConfig _LOGGER = logging.getLogger(__name__) def initialize_system_data(): """Setup default config and create folders.""" + config = CoreConfig() + # homeassistant config folder - if not os.path.isdir(HOMEASSISTANT_CONFIG): + if not os.path.isdir(config.path_config): _LOGGER.info( - "Create Home-Assistant config folder %s", HOMEASSISTANT_CONFIG) - os.mkdir(HOMEASSISTANT_CONFIG) + "Create Home-Assistant config folder %s", config.path_config) + os.mkdir(config.path_config) # homeassistant ssl folder - if not os.path.isdir(HOMEASSISTANT_SSL): - _LOGGER.info( - "Create Home-Assistant ssl folder %s", HOMEASSISTANT_SSL) - os.mkdir(HOMEASSISTANT_SSL) + if not os.path.isdir(config.path_ssl): + _LOGGER.info("Create Home-Assistant ssl folder %s", config.path_ssl) + os.mkdir(config.path_ssl) # installed addons if not os.path.isfile(FILE_HASSIO_ADDONS): with open(FILE_HASSIO_ADDONS) as addons_file: addons_file.write(json.dumps({})) - # supervisor/homeassistant image/tag versions - conf_version = Version() - - return conf_version + return config def initialize_logging(): diff --git a/hassio_api/hassio/version.py b/hassio_api/hassio/config.py similarity index 65% rename from hassio_api/hassio/version.py rename to hassio_api/hassio/config.py index 4bf02e4a7..4d9b0e8e5 100644 --- a/hassio_api/hassio/version.py +++ b/hassio_api/hassio/config.py @@ -1,28 +1,26 @@ """Bootstrap HassIO.""" -import asyncio import json import logging import os -from colorlog import ColoredFormatter - from .const import ( - FILE_HASSIO_VERSION, CONF_SUPERVISOR_TAG, CONF_SUPERVISOR_IMAGE, - CONF_HOMEASSISTANT_TAG, CONF_HOMEASSISTANT_IMAGE) + FILE_HASSIO_CONFIG, CONF_SUPERVISOR_TAG, CONF_SUPERVISOR_IMAGE, + CONF_HOMEASSISTANT_TAG, CONF_HOMEASSISTANT_IMAGE, HOMEASSISTANT_SSL, + HOMEASSISTANT_CONFIG, HASSIO_SHARE_EXT, HASSIO_SHARE_INT) _LOGGER = logging.getLogger(__name__) -class Version(Object): - """Hold all version data.""" +class CoreConfig(Object): + """Hold all config data.""" - def __init__(self, config_file=FILE_HASSIO_VERSION): - """Initialize version object.""" + def __init__(self, config_file=FILE_HASSIO_CONFIG): + """Initialize config object.""" self._data = {} self._filename = config_file # init or load data - if os.path.isfile(FILE_HASSIO_VERSION): + if os.path.isfile(self._filename): try: with open(self._filename 'r') as cfile: self._data = json.loads(cfile.read()) @@ -77,3 +75,23 @@ class Version(Object): def supervisor_tag(self): """Return docker supervisor tag.""" return self._data.get(CONF_SUPERVISOR_TAG) + + @property + def path_config_docker(self): + """Return config path extern for docker.""" + return HOMEASSISTANT_CONFIG.format(HASSIO_SHARE_EXT) + + @property + def path_config(self): + """Return config path inside supervisor.""" + return HOMEASSISTANT_CONFIG.format(HASSIO_SHARE_INT) + + @property + def path_ssl_docker(self): + """Return SSL path extern for docker.""" + return HOMEASSISTANT_SSL.format(HASSIO_SHARE_EXT) + + @property + def path_ssl(self): + """Return SSL path inside supervisor.""" + return HOMEASSISTANT_SSL.format(HASSIO_SHARE_INT) diff --git a/hassio_api/hassio/const.py b/hassio_api/hassio/const.py index d7477f86d..6b0337cfc 100644 --- a/hassio_api/hassio/const.py +++ b/hassio_api/hassio/const.py @@ -6,15 +6,17 @@ URL_SUPERVISOR_VERSION = \ URL_ADDONS_REPO = 'https://github.com/pvizeli/hassio-addons' -FILE_RESIN_CONFIG = '/boot/config.json' +FILE_HOST_CONFIG = '/boot/config.json' +FILE_HOST_NETWORK = '/boot/network' FILE_HASSIO_ADDONS = '/data/addons.json' -FILE_HASSIO_VERSION = '/data/version.json' +FILE_HASSIO_CONFIG = '/data/config.json' -HASSIO_SHARE = os.environ['SUPERVISOR_SHARE'] +HASSIO_SHARE_EXT = os.environ['SUPERVISOR_SHARE'] +HASSIO_SHARE_INT = '/shared-data' HASSIO_DOCKER = os.environ['SUPERVISOR_NAME'] -HOMEASSISTANT_CONFIG = "{}/homeassistant_config".format(HASSIO_SHARE) -HOMEASSISTANT_SSL = "{}/homeassistant_ssl".format(HASSIO_SHARE) +HOMEASSISTANT_CONFIG = "{}/homeassistant_config" +HOMEASSISTANT_SSL = "{}/homeassistant_ssl" HTTP_PORT = 9123 diff --git a/hassio_api/hassio/docker/__init__.py b/hassio_api/hassio/docker/__init__.py index 9414ccfc7..1acc2c17d 100644 --- a/hassio_api/hassio/docker/__init__.py +++ b/hassio_api/hassio/docker/__init__.py @@ -1,21 +1,28 @@ """Init file for HassIO docker object.""" -import asyncio +import logging import docker _LOGGER = logging.getLogger(__name__) + class DockerBase(object): """Docker hassio wrapper.""" - def __init__(self, loop, dock, image, tag=None): + def __init__(self, config, loop, dock, image, tag=None): """Initialize docker base wrapper.""" + self.config = config self.loop = loop self.dock = dock self.image = image self.tag = tag self.container = None + @property + def docker_name(self): + """Return name of docker container.""" + return None + def install(self, tag='latest'): """Pull docker image. diff --git a/hassio_api/hassio/docker/homeassistant.py b/hassio_api/hassio/docker/homeassistant.py index f4a43229e..6f5c6fcc8 100644 --- a/hassio_api/hassio/docker/homeassistant.py +++ b/hassio_api/hassio/docker/homeassistant.py @@ -4,14 +4,20 @@ import asyncio import docker import . from DockerBase -from ..const.py import HOMEASSISTANT_CONFIG, HOMEASSISTANT_SSL, HASSIO_DOCKER +from ..const.py import HASSIO_DOCKER _LOGGER = logging.getLogger(__name__) HASS_DOCKER_NAME = 'homeassistant' + class DockerHomeAssistant(DockerBase): """Docker hassio wrapper for HomeAssistant.""" + @property + def docker_name(self): + """Return name of docker container.""" + return HASS_DOCKER_NAME + def _run(): """Run docker image. @@ -20,7 +26,7 @@ class DockerHomeAssistant(DockerBase): try: self.container = self.dock.containers.run( self.image, - name=HASS_DOCKER_NAME, + name=self.docker_nme, remove=True, network_mode='host', restart_policy={ @@ -29,8 +35,10 @@ class DockerHomeAssistant(DockerBase): }, links={HASSIO_DOCKER: 'HASSIO'}, volumes={ - HOMEASSISTANT_CONFIG: {'bind': '/config', 'mode': 'rw'}, - HOMEASSISTANT_SSL: {'bind': '/ssl', 'mode': 'rw'}, + self.config.path_config_docker: + {'bind': '/config', 'mode': 'rw'}, + self.config.path_ssl_docker: + {'bind': '/ssl', 'mode': 'rw'}, }) except docker.errors.APIError as err: _LOGGER.error("Can't run %s", self.image)