implement reboot & shutdown (#70)

* implement reboot & shutdown
* fix waiting for script by using event loop
* implemented suggestions
* remove unused import
* implement suggestions #2
This commit is contained in:
Christoph Stenglein 2022-03-23 20:20:58 +01:00 committed by GitHub
parent f057f37cf0
commit 924f0643ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 5 deletions

View file

@ -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()}")

View file

@ -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"
)