Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safe mode option to core rebuild #5120

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions supervisor/api/homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ async def restart(self, request: web.Request) -> None:
)

@api_process
def rebuild(self, request: web.Request) -> Awaitable[None]:
async def rebuild(self, request: web.Request) -> None:
"""Rebuild Home Assistant."""
return asyncio.shield(self.sys_homeassistant.core.rebuild())
body = await api_validate(SCHEMA_RESTART, request)

await asyncio.shield(
self.sys_homeassistant.core.rebuild(safe_mode=body[ATTR_SAFE_MODE])
)

@api_process
async def check(self, request: web.Request) -> None:
Expand Down
10 changes: 9 additions & 1 deletion supervisor/homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ async def restart(self, *, safe_mode: bool = False) -> None:
"""Restart Home Assistant Docker."""
# Create safe mode marker file if necessary
if safe_mode:
_LOGGER.debug("Creating safe mode marker file.")
await self.sys_run_in_executor(
(self.sys_config.path_homeassistant / SAFE_MODE_FILENAME).touch
)
Expand All @@ -383,8 +384,15 @@ async def restart(self, *, safe_mode: bool = False) -> None:
limit=JobExecutionLimit.GROUP_ONCE,
on_condition=HomeAssistantJobError,
)
async def rebuild(self) -> None:
async def rebuild(self, *, safe_mode: bool = False) -> None:
"""Rebuild Home Assistant Docker container."""
# Create safe mode marker file if necessary
if safe_mode:
_LOGGER.debug("Creating safe mode marker file.")
await self.sys_run_in_executor(
(self.sys_config.path_homeassistant / SAFE_MODE_FILENAME).touch
)

with suppress(DockerError):
await self.instance.stop()
await self.start()
Expand Down
27 changes: 27 additions & 0 deletions tests/api/test_homeassistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import MagicMock, patch

from aiohttp.test_utils import TestClient
from awesomeversion import AwesomeVersion
import pytest

from supervisor.coresys import CoreSys
Expand Down Expand Up @@ -115,3 +116,29 @@ async def test_api_restart(

assert container.restart.call_count == 2
assert safe_mode_marker.exists()


async def test_api_rebuild(
api_client: TestClient,
coresys: CoreSys,
container: MagicMock,
tmp_supervisor_data: Path,
path_extern,
):
"""Test rebuilding homeassistant."""
coresys.homeassistant.version = AwesomeVersion("2023.09.0")
safe_mode_marker = tmp_supervisor_data / "homeassistant" / "safe-mode"

with patch.object(HomeAssistantCore, "_block_till_run"):
await api_client.post("/homeassistant/rebuild")

assert container.remove.call_count == 2
container.start.assert_called_once()
assert not safe_mode_marker.exists()

with patch.object(HomeAssistantCore, "_block_till_run"):
await api_client.post("/homeassistant/rebuild", json={"safe_mode": True})

assert container.remove.call_count == 4
assert container.start.call_count == 2
assert safe_mode_marker.exists()
Loading