diff --git a/app/repositories/system_impl/raspiblitz.py b/app/repositories/system_impl/raspiblitz.py index 339d5f8..37342c0 100644 --- a/app/repositories/system_impl/raspiblitz.py +++ b/app/repositories/system_impl/raspiblitz.py @@ -1,3 +1,6 @@ +import asyncio +import os + from app.constants import API_VERSION from app.models.system import ( APIPlatform, @@ -7,6 +10,7 @@ from app.models.system import ( SystemInfo, ) from app.repositories.lightning import get_ln_info +from app.repositories.system import SHELL_SCRIPT_PATH from app.utils import redis_get @@ -34,3 +38,26 @@ async def get_system_info_impl() -> SystemInfo: ssh_address=f"admin@{lan}", chain=lninfo.chains[0].network, ) + + +async def shutdown(reboot: bool): + params = "" + if reboot: + params = "reboot" + + script = os.path.join(SHELL_SCRIPT_PATH, "config.scripts", "blitz.shutdown.sh") + cmd = f"sudo bash {script} {params}" + + proc = await asyncio.create_subprocess_shell( + cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + stdout, stderr = await proc.communicate() + + print(f"[{cmd!r} exited with {proc.returncode}]") + if stdout: + print(f"[stdout]\n{stdout.decode()}") + if stderr: + print(f"[stderr]\n{stderr.decode()}") diff --git a/app/routers/system.py b/app/routers/system.py index 5168e0d..b7383d1 100644 --- a/app/routers/system.py +++ b/app/routers/system.py @@ -8,9 +8,10 @@ from fastapi.params import Depends from app.auth.auth_bearer import JWTBearer from app.auth.auth_handler import signJWT from app.external.sse_startlette import EventSourceResponse -from app.models.system import LoginInput, RawDebugLogData, SystemInfo +from app.models.system import APIPlatform, LoginInput, RawDebugLogData, SystemInfo from app.repositories.system import ( HW_INFO_YIELD_TIME, + PLATFORM, get_debug_logs_raw, get_hardware_info, get_system_info, @@ -19,6 +20,7 @@ from app.repositories.system import ( parseKeyValueText, passwordValid ) +from app.repositories.system_impl.raspiblitz import shutdown from app.routers.system_docs import ( get_debug_logs_raw_desc, get_debug_logs_raw_resp_desc, @@ -132,8 +134,14 @@ async def hw_info_sub(request: Request): summary="Reboots the system", dependencies=[Depends(JWTBearer())], ) -def reboot_system(): - return HTTPException(status.HTTP_501_NOT_IMPLEMENTED) +async def reboot_system() -> bool: + if PLATFORM == APIPlatform.RASPIBLITZ: + await shutdown(True) + return True + else: + raise HTTPException( + status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented on native" + ) @router.post( @@ -142,5 +150,11 @@ def reboot_system(): summary="Shuts the system down", dependencies=[Depends(JWTBearer())], ) -def reboot_system(): - return HTTPException(status.HTTP_501_NOT_IMPLEMENTED) +async def shutdown() -> bool: + if PLATFORM == APIPlatform.RASPIBLITZ: + await shutdown(False) + return True + else: + raise HTTPException( + status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented on native" + )