Add support for git repo

This commit is contained in:
Pascal Vizeli 2017-04-08 00:06:10 +02:00
parent 9afb136648
commit f127de8059
5 changed files with 94 additions and 2 deletions

13
hassio/addons/__init__.py Normal file
View File

@ -0,0 +1,13 @@
"""Init file for HassIO addons."""
import logging
_LOGGER = logging.getLogger(__name__)
class AddonManager(object):
"""Manage addons inside HassIO."""
def __init__(self, config, loop):
"""Initialize docker base wrapper."""
self.config = config
self.loop = loop

59
hassio/addons/git.py Normal file
View File

@ -0,0 +1,59 @@
"""Init file for HassIO addons git."""
import asyncio
import logging
import os
import git
from ..const import URL_HASSIO_ADDONS
_LOGGER = logging.getLogger(__name__)
class AddonsRepo(object):
"""Manage addons git repo."""
def __init__(self, config, loop):
"""Initialize docker base wrapper."""
self.config = config
self.loop = loop
self.repo = None
self._lock = asyncio.Lock(loop=loop)
async def init(self):
"""Init git addon repo."""
if not os.path.isdir(self.config.path_addons_repo):
return await self.clone()
await with self._lock:
try:
self.repo = await self.loop.run_in_executor(
None, git.Repo(self.config.path_addons_repo))
except (git.InvalidGitRepositoryError, git.NoSuchPathError) as err:
_LOGGER.error("Can't load addons repo: %s.", err)
async def clone(self):
"""Clone git addon repo."""
await with self._lock:
try:
self.repo = await self.loop.run_in_executor(
None, git.Repo.clone_from, URL_HASSIO_ADDONS,
self.config.path_addons_repo)
except (git.InvalidGitRepositoryError, git.NoSuchPathError) as err:
_LOGGER.error("Can't clone addons repo: %s.", err)
async def pull(self):
"""Pull git addon repo."""
if self._lock.locked():
_LOGGER.warning("It is already a task in progress.")
return
await with self._lock:
try:
yield from self.loop.run_in_executor(
None, self.repo.remotes.origin.pull)
except (git.InvalidGitRepositoryError, git.NoSuchPathError) as err:
_LOGGER.error("Can't pull addons repo: %s.", err)

View File

@ -14,6 +14,10 @@ HOMEASSISTANT_CURRENT = 'homeassistant_current'
HASSIO_SSL = "{}/ssl"
HASSIO_CURRENT = 'hassio_current'
ADDONS_REPO = "{}/addons"
ADDONS_DATA = "{}/addons_data"
UPSTREAM_BETA = 'upstream_beta'
@ -112,3 +116,18 @@ class CoreConfig(object):
def path_ssl(self):
"""Return SSL path inside supervisor."""
return HASSIO_SSL.format(HASSIO_SHARE)
@property
def path_addons_repo(self):
"""Return git repo path for addons."""
return ADDONS_REPO.format(HASSIO_SHARE)
@property
def path_addons_data(self):
"""Return root addon data folder."""
return ADDONS_DATA.format(HASSIO_SHARE)
@property
def path_addons_data_docker(self):
"""Return root addon data folder extern for docker."""
return ADDONS_DATA.format(os.environ['SUPERVISOR_SHARE'])

View File

@ -6,7 +6,7 @@ URL_HASSIO_VERSION = \
URL_HASSIO_VERSION_BETA = \
'https://raw.githubusercontent.com/pvizeli/hassio/master/version_beta.json'
URL_ADDONS_REPO = 'https://github.com/pvizeli/hassio-addons'
URL_HASSIO_ADDONS = 'https://github.com/pvizeli/hassio-addons'
HASSIO_SHARE = "/data"

View File

@ -29,7 +29,7 @@ setup(
keywords=['docker', 'home-assistant', 'api'],
zip_safe=False,
platforms='any',
packages=['hassio', 'hassio.dock', 'hassio.api'],
packages=['hassio', 'hassio.dock', 'hassio.api', 'hassio.addons'],
include_package_data=True,
install_requires=[
'async_timeout',
@ -37,5 +37,6 @@ setup(
'docker',
'colorlog',
'voluptuous',
'gitpython',
]
)