Update hassio api & init scripts

This commit is contained in:
Pascal Vizeli 2017-03-20 00:19:37 +01:00
parent 13f0a30fff
commit 3ec832184c
9 changed files with 164 additions and 4 deletions

View File

@ -0,0 +1,49 @@
"""Main file for HassIO."""
import asyncio
import logging
import aiohttp
from aiohttp import web
import docker
import .bootstrap
import .tools
from .docker.homeassistant import DockerHomeAssistant
_LOGGER = logging.getLogger(__name__)
def main():
"""Start HassIO."""
bootstrap.initialize_logging()
# init asyncio & aiohttp client
loop = asyncio.get_event_loop()
websession = aiohttp.ClientSession(loop=loop)
dock = docker.from_env()
# init system
versions = bootstrap.initialize_system_data()
# init HomeAssistant Docker
docker_hass = DockerHomeAssistant(
loop, dock, versions[CONF_HOMEASSISTANT_IMAGE],
versions[CONF_HOMEASSISTANT_TAG])
# first start of supervisor?
if versions['CONF_HOMEASSISTANT_TAG'] is None:
_LOGGER.info("First start of supervisor, read version from github.")
# read homeassistant tag and install it
current = None
while True:
current = await tools.fetch_current_versions(websession)
if current and 'homeassistant_tag' in current:
if docker_hass.install(current['homeassistant_tag']):
break
_LOGGER.waring("Can't fetch info from github. Retry in 60")
await asyncio.sleep(60, loop=loop)
if __name__ == "__main__":
sys.exit(main())

View File

@ -1,17 +1,24 @@
"""Bootstrap HassIO."""
import asyncio
import json
import logging
import os
from colorlog import ColoredFormatter
from .const import (
FILE_HASSIO_ADDONS, FILE_HASSIO_VERSION, FILE_RESIN_CONFIG,
HOMEASSISTANT_CONFIG, CONF_SUPERVISOR_TAG, CONF_SUPERVISOR_IMAGE)
_LOGGER = logging.getLogger(__name__)
def initialize_system_data():
"""Setup default config and create folders."""
# homeassistant config folder
if not os.path.isdir(HOMEASSISTANT_CONFIG):
_LOGGER.info(
"Create Home-Assistant config folder %s", HOMEASSISTANT_CONFIG)
os.mkdir(HOMEASSISTANT_CONFIG)
# installed addons
@ -38,3 +45,31 @@ def initialize_system_data():
with open(FILE_HASSIO_VERSION, 'w') as conf_file:
conf_file.write(json.dumps(versions))
_LOGGER("initialize system done: %s", versions)
return versions
def initialize_logging():
"""Setup the logging."""
logging.basicConfig(level=logging.INFO)
fmt = ("%(asctime)s %(levelname)s (%(threadName)s) "
"[%(name)s] %(message)s")
colorfmt = "%(log_color)s{}%(reset)s".format(fmt)
datefmt = '%y-%m-%d %H:%M:%S'
# suppress overly verbose logs from libraries that aren't helpful
logging.getLogger("aiohttp.access").setLevel(logging.WARNING)
logging.getLogger().handlers[0].setFormatter(ColoredFormatter(
colorfmt,
datefmt=datefmt,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
}
))

View File

@ -1,4 +1,6 @@
"""Const file for HassIO."""
import os
URL_SUPERVISOR_VERSION = \
'https://raw.githubusercontent.com/pvizeli/hassio/master/version.json'
@ -8,7 +10,8 @@ FILE_RESIN_CONFIG = '/boot/config.json'
FILE_HASSIO_ADDONS = '/data/addons.json'
FILE_HASSIO_VERSION = '/data/version.json'
HOMEASSISTANT_CONFIG = '/resin-data/config'
HOMEASSISTANT_SHARE = os.environ['SUPERVISOR_SHARE']
HOMEASSISTANT_CONFIG = "{}/config".format(HOMEASSISTANT_SHARE)
HTTP_PORT = 9123

View File

@ -0,0 +1,28 @@
"""Init file for HassIO docker object."""
import asyncio
import docker
class DockerBase(object):
"""Docker hassio wrapper."""
def __init__(self, loop, dock, image, tag=None):
"""Initialize docker base wrapper."""
self.loop = loop
self.dock = dock
self.image = image
self.tag = tag
async def install(tag='latest'):
"""Pull docker image."""
try:
self.dock.images.pull(self.image, tag=tag)
except docker.errors.APIError as err:
_LOGGER.error("Can't pull %s:%s", self.image, tag)
return False
return True
async def run():
"""Run docker image."""
raise NotImplementedError()

View File

@ -0,0 +1,20 @@
"""Init file for HassIO docker object."""
import asyncio
import docker
import . from DockerBase
class DockerHomeAssistant(DockerBase):
"""Docker hassio wrapper for HomeAssistant."""
async def run():
"""Run docker image."""
try:
self.docker.images.pull(self.image, tag=tag)
except docker.errors.APIError as err:
_LOGGER.error("Can't pull %s:%s", self.image, tag)
return False
return True

View File

@ -0,0 +1,19 @@
"""Tools file for HassIO."""
import asyncio
import logging
import aiohttp
import async_timeout
from .const import URL_SUPERVISOR_VERSION
async def fetch_current_versions(websession):
"""Fetch current versions from github."""
try:
with async_timeout.timeout(10, loop=websession.loop):
async with websession.get(URL_SUPERVISOR_VERSION) as request:
return (await request.json())
except Exception: # pylint: disable=broad-except
return None

View File

@ -33,6 +33,7 @@ setup(
install_requires=[
'async_timeout',
'aiohttp',
'docker-py'
'docker-py',
'colorlog'
]
)

View File

@ -12,7 +12,7 @@ runSupervisor() {
-v /var/run/docker.sock:/var/run/docker.sock \
-v $CONFIG_PATH:/boot/config.json \
-v /resin-data/resin-supervisor:/data \
-v /resin-data:/resin-data \
-v /resin-data:/shared-data \
-v /proc/net/fib_trie:/mnt/fib_trie \
-v /var/log/supervisor-log:/var/log \
-v /:/mnt/root \
@ -22,6 +22,8 @@ runSupervisor() {
-e LED_FILE=${LED_FILE} \
-e SUPERVISOR_IMAGE=${SUPERVISOR_IMAGE} \
-e SUPERVISOR_TAG=${SUPERVISOR_TAG} \
-e SUPERVISOR_SHARE=/shared-data \
-e SUPERVISOR_NAME=resin_supervisor \
-e HOMEASSISTANT_REPOSITORY=${HOMEASSISTANT_REPOSITORY} \
${SUPERVISOR_IMAGE}
}

View File

@ -11,4 +11,7 @@ ENV LANG C.UTF-8
RUN apk add --no-cache python3 git
# install aiohttp
RUN pip3 install pip --no-cache --upgrade && pip3 install --no-cache aiohttp && pip3 install --no-cache docker
RUN pip3 install pip --no-cache --upgrade \
&& pip3 install --no-cache aiohttp \
&& pip3 install --no-cache docker \
&& pip3 install --no-cache colorlog