refactor: use app.utils.redis_get() everywhere

This commit is contained in:
fusion44 2022-03-19 12:36:03 +01:00
parent 6546240dff
commit b1dc854dd2
No known key found for this signature in database
GPG key ID: 645FA807E935D9D5
3 changed files with 50 additions and 66 deletions

View file

@ -37,7 +37,7 @@ from app.repositories.lightning import (
from app.repositories.system import register_hardware_info_gatherer
from app.repositories.utils import get_client_warmup_data
from app.routers import apps, bitcoin, lightning, setup, system
from app.utils import SSE, send_sse_message
from app.utils import SSE, redis_get, send_sse_message
@registered_configuration
@ -191,10 +191,7 @@ async def check_defer_register_handlers():
await register_all_handlers(redis_plugin.redis)
else:
# Handle Raspiblitz
res = await redis_plugin.redis.get("setupPhase")
setup_phase = ""
if res != None:
setup_phase = res.decode("utf-8")
setup_phase = await redis_get("setupPhase")
if setup_phase == "done":
await register_all_handlers(redis_plugin.redis)

View file

@ -1,5 +1,6 @@
import time
import logging
from app.utils import redis_get
HW_INFO_YIELD_TIME = 2
@ -7,20 +8,10 @@ HW_INFO_YIELD_TIME = 2
from fastapi_plugins import redis_plugin as r
async def _redis_get(key: str) -> str:
v = await r.redis.get(key)
if not v:
logging.warning(f"Key '{key}' not found in Redis DB.")
return ""
return v.decode("utf-8")
async def get_hardware_info_impl() -> map:
info = {}
loads = (await _redis_get("system_cpu_load")).split(",")
loads = (await redis_get("system_cpu_load")).split(",")
iloads = []
total = 0
for l in loads:
@ -30,10 +21,10 @@ async def get_hardware_info_impl() -> map:
info["cpu_overall_percent"] = round(total / len(loads), 2)
info["cpu_per_cpu_percent"] = iloads
info["vram_total_bytes"] = int(await _redis_get("system_ram_mb")) * 1000 * 1000
info["vram_total_bytes"] = int(await redis_get("system_ram_mb")) * 1000 * 1000
info["vram_available_bytes"] = (
int(await _redis_get("system_ram_available_mb")) * 1000 * 1000
int(await redis_get("system_ram_available_mb")) * 1000 * 1000
)
info["vram_used_bytes"] = info["vram_total_bytes"] - info["vram_available_bytes"]
@ -42,16 +33,16 @@ async def get_hardware_info_impl() -> map:
)
info["temperatures_celsius"] = {
"system_temp": float(await _redis_get("system_temp_celsius")),
"system_temp": float(await redis_get("system_temp_celsius")),
"coretemp": [],
}
now = time.time()
boot = float(await _redis_get("system_up"))
boot = float(await redis_get("system_up"))
info["boot_time_timestamp"] = now - boot
total = int(await _redis_get("hdd_capacity_bytes"))
free = int(await _redis_get("hdd_free_bytes"))
total = int(await redis_get("hdd_capacity_bytes"))
free = int(await redis_get("hdd_free_bytes"))
info["disks"] = [
{
"device": "/",
@ -65,10 +56,10 @@ async def get_hardware_info_impl() -> map:
]
info["networks"] = {
"internet_online": await _redis_get("internet_online"),
"tor_web_addr": await _redis_get("tor_web_addr"),
"internet_localip": await _redis_get("internet_localip"),
"internet_localiprange": await _redis_get("internet_localiprange"),
"internet_online": await redis_get("internet_online"),
"tor_web_addr": await redis_get("tor_web_addr"),
"internet_localip": await redis_get("internet_localip"),
"internet_localiprange": await redis_get("internet_localiprange"),
}
return info

View file

@ -1,25 +1,16 @@
import asyncio
import logging
from enum import Enum
from os import stat
from fastapi import APIRouter, HTTPException, status
from fastapi.params import Depends
from aioredis import Redis
from app.utils import redis_get
from fastapi_plugins import depends_redis, redis_plugin
from app.auth.auth_bearer import JWTBearer
from app.auth.auth_handler import signJWT
from app.utils import redis_get
from fastapi import APIRouter, HTTPException, status
from fastapi.params import Depends
from fastapi_plugins import depends_redis
router = APIRouter(prefix="/setup", tags=["Setup"])
# helper function to deal with redis
async def _redis_get(key: str) -> str:
v = await redis_plugin.redis.get(key)
if not v:
logging.warning(f"Key '{key}' not found in Redis DB.")
return ""
return v.decode("utf-8")
# can always be called without credentials to check if
# the system needs or is in setup (setupPhase!="done")
# for example in the beginning setupPhase can be (see controlSetupDialog.sh)
@ -29,50 +20,53 @@ async def _redis_get(key: str) -> str:
# 4) setup = a fresh blitz to setup
@router.get("/status")
async def get_status():
setupPhase = await _redis_get("setupPhase")
state = await _redis_get("state")
message = await _redis_get("message")
return {
"setupPhase": setupPhase,
"state": state,
"message": message
}
setupPhase = await redis_get("setupPhase")
state = await redis_get("state")
message = await redis_get("message")
return {"setupPhase": setupPhase, "state": state, "message": message}
# if setupPhase!="done" && state="waitsetup" then
# 'setup/setup_start_info' should be called
# We can do the "MIGRATION" option later - because it would need an additional step after formatting hdd
# People that need to migrate can do for now by SSH option
@router.get("/setup_start_info")
async def setup_start_info():
# first check that node is really in setup state
setupPhase = await _redis_get("setupPhase")
state = await _redis_get("state")
setupPhase = await redis_get("setupPhase")
state = await redis_get("state")
if setupPhase != "done":
logging.warning(f"/setup_start_info can only be called when nodes awaits setup (setupPhase)")
logging.warning(
f"/setup_start_info can only be called when nodes awaits setup (setupPhase)"
)
return HTTPException(status.HTTP_503_SERVICE_UNAVAILABLE)
if state != "waitsetup":
logging.warning(f"/setup_start_info can only be called when nodes awaits setup (state)")
logging.warning(
f"/setup_start_info can only be called when nodes awaits setup (state)"
)
return HTTPException(status.HTTP_503_SERVICE_UNAVAILABLE)
# get all the additional info needed to do setup dialog
hddGotMigrationData = await _redis_get("hddGotMigrationData")
hddGotBlockchain = await _redis_get("hddBlocksBitcoin")
migrationMode = await _redis_get("migrationMode")
lan = await _redis_get("internet_localip")
tor = await _redis_get("tor_web_addr")
hddGotMigrationData = await redis_get("hddGotMigrationData")
hddGotBlockchain = await redis_get("hddBlocksBitcoin")
migrationMode = await redis_get("migrationMode")
lan = await redis_get("internet_localip")
tor = await redis_get("tor_web_addr")
# return info as JSON
return {
"setupPhase": setupPhase,
"migrationMode": migrationMode, # 'normal', 'outdatedLightning'
"hddGotMigrationData": hddGotMigrationData, # 'umbrel', 'mynode', 'citadel'
"migrationMode": migrationMode, # 'normal', 'outdatedLightning'
"hddGotMigrationData": hddGotMigrationData, # 'umbrel', 'mynode', 'citadel'
"hddGotBlockchain": hddGotBlockchain,
"ssh_login": f"ssh admin@{lan}",
"tor_web_ui": tor
"tor_web_ui": tor,
}
# With all this info the WebUi can run its own runs its dialogs and in the end makes a call to
# With all this info the WebUi can run its own runs its dialogs and in the end makes a call to
@router.post("/setup_start_done")
async def setup_start_done(redis: Redis = Depends(depends_redis)):
# TODO: Following input parameters:
@ -90,13 +84,14 @@ async def setup_start_done(redis: Redis = Depends(depends_redis)):
# migrationFile=[path] (might be used later if we offer raspiblitz migration)
# those values get stored in: /var/cache/raspiblitz/temp/raspiblitz.setup
# also a skeleton raspiblitz.conf gets created (see controlSetupDialog.sh Line 318)
# and then API sets state to `waitprovision` to kick-off provision
# and then API sets state to `waitprovision` to kick-off provision
await redis.publish_json("default", {"data": "Starting setup"})
await asyncio.sleep(1)
return signJWT()
# WebUI now loops getting status until state=`waitfinal` then calls:
@router.get("/setup_final_info",dependencies=[Depends(JWTBearer())])
@router.get("/setup_final_info", dependencies=[Depends(JWTBearer())])
async def setup_final_info(redis: Redis = Depends(depends_redis)):
# TODO: return info on setup final
# during the process some data might be written to /var/cache/raspiblitz/temp/raspiblitz.setup
@ -105,8 +100,9 @@ async def setup_final_info(redis: Redis = Depends(depends_redis)):
# syncProgressFull=[percent] = (later WebUi can offer sync from another RaspiBlitz)
return HTTPException(status.HTTP_501_NOT_IMPLEMENTED)
# When WebUI displayed seed words & user confirmed write the calls:
@router.post("/setup_final_done",dependencies=[Depends(JWTBearer())])
# When WebUI displayed seed words & user confirmed write the calls:
@router.post("/setup_final_done", dependencies=[Depends(JWTBearer())])
async def setup_final_done(redis: Redis = Depends(depends_redis)):
# TODO: like controlFinalDialog.sh Line 86 kicks off the AFTER FINAL TASKS and reboots
return HTTPException(status.HTTP_501_NOT_IMPLEMENTED)