Enforce internet host/system (#2263)

This commit is contained in:
Pascal Vizeli 2020-11-18 11:29:23 +01:00 committed by GitHub
parent 550fca4bcd
commit 80f4309799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 15 deletions

View File

@ -18,7 +18,8 @@ class JobCondition(str, Enum):
FREE_SPACE = "free_space"
HEALTHY = "healthy"
INTERNET = "internet"
INTERNET_SYSTEM = "internet_system"
INTERNET_HOST = "internet_host"
class Job:
@ -95,21 +96,31 @@ class Job:
)
return False
if JobCondition.INTERNET in self.conditions:
if any(
internet in self.conditions
for internet in (
JobCondition.INTERNET_SYSTEM,
JobCondition.INTERNET_HOST,
)
):
if self._coresys.core.state not in (
CoreState.SETUP,
CoreState.RUNNING,
):
return True
if not self._coresys.supervisor.connectivity:
if (
JobCondition.INTERNET_SYSTEM in self.conditions
and not self._coresys.supervisor.connectivity
):
_LOGGER.warning(
"'%s' blocked from execution, no supervisor internet connection",
self._method.__qualname__,
)
return False
elif (
self._coresys.host.network.connectivity is not None
JobCondition.INTERNET_HOST in self.conditions
and self._coresys.host.network.connectivity is not None
and not self._coresys.host.network.connectivity
):
_LOGGER.warning(

View File

@ -120,7 +120,13 @@ class Tasks(CoreSysAttributes):
_LOGGER.info("All core tasks are scheduled")
@Job(conditions=[JobCondition.HEALTHY, JobCondition.FREE_SPACE])
@Job(
conditions=[
JobCondition.HEALTHY,
JobCondition.FREE_SPACE,
JobCondition.INTERNET_HOST,
]
)
async def _update_addons(self):
"""Check if an update is available for an Add-on and update it."""
for addon in self.sys_addons.all:
@ -144,7 +150,7 @@ class Tasks(CoreSysAttributes):
except AddonsError:
_LOGGER.error("Can't auto update Add-on %s", addon.slug)
@Job(conditions=[JobCondition.FREE_SPACE])
@Job(conditions=[JobCondition.FREE_SPACE, JobCondition.INTERNET_HOST])
async def _update_supervisor(self):
"""Check and run update of Supervisor Supervisor."""
if not self.sys_supervisor.need_update:

View File

@ -56,7 +56,7 @@ class StoreManager(CoreSysAttributes):
await self.load()
self._read_addons()
@Job(conditions=[JobCondition.INTERNET, JobCondition.HEALTHY])
@Job(conditions=[JobCondition.INTERNET_SYSTEM, JobCondition.HEALTHY])
async def update_repositories(self, list_repositories):
"""Add a new custom repository."""
job = self.sys_jobs.get_job("storemanager_update_repositories")

View File

@ -159,7 +159,7 @@ class Updater(JsonConfig, CoreSysAttributes):
self._data[ATTR_CHANNEL] = value
@AsyncThrottle(timedelta(seconds=30))
@Job(conditions=[JobCondition.INTERNET])
@Job(conditions=[JobCondition.INTERNET_SYSTEM])
async def fetch_data(self):
"""Fetch current versions from Github.

View File

@ -44,8 +44,13 @@ async def test_internet(coresys: CoreSys):
"""Initialize the test class."""
self.coresys = coresys
@Job(conditions=[JobCondition.INTERNET])
async def execute(self):
@Job(conditions=[JobCondition.INTERNET_HOST])
async def execute_host(self):
"""Execute the class method."""
return True
@Job(conditions=[JobCondition.INTERNET_SYSTEM])
async def execute_system(self):
"""Execute the class method."""
return True
@ -53,19 +58,23 @@ async def test_internet(coresys: CoreSys):
coresys.host.network._connectivity = True
coresys.supervisor._connectivity = True
assert await test.execute()
assert await test.execute_host()
assert await test.execute_system()
coresys.host.network._connectivity = True
coresys.supervisor._connectivity = False
assert not await test.execute()
assert await test.execute_host()
assert not await test.execute_system()
coresys.host.network._connectivity = None
coresys.supervisor._connectivity = True
assert await test.execute()
assert await test.execute_host()
assert await test.execute_system()
coresys.host.network._connectivity = False
coresys.supervisor._connectivity = True
assert not await test.execute()
assert not await test.execute_host()
assert await test.execute_system()
async def test_free_space(coresys: CoreSys):
@ -101,7 +110,7 @@ async def test_internet_connectivity_with_core_state(coresys: CoreSys):
"""Initialize the test class."""
self.coresys = coresys
@Job(conditions=[JobCondition.INTERNET])
@Job(conditions=[JobCondition.INTERNET_SYSTEM, JobCondition.INTERNET_HOST])
async def execute(self):
"""Execute the class method."""
return True