mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
* A failed upgrade is shown on the upgrade page instead of redirecting home
A failed upgrade restarts the services it stopped, so the upgrade page saw
them come back and redirected home as if the upgrade had worked. The
Controller now reports how the last run of a script ended
(GET /api/scripts/{name}/result, from systemd's Result and exit status),
and the upgrade page reads it before redirecting. A failure turns the
banner red, gives the exit code, opens the output, and offers a link back.
Only a run the page has seen start counts, because the result of an
earlier run is still reported until the new run begins.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* An upgrade's result survives the daemon-reload it does
The upgrade reinstalls its unit and reloads systemd, which clears the run's
timestamps but keeps Result and the exit status. A failure is now known
from those alone, and the result reports whether the script is running, so
the upgrade page waits on that instead of on a timestamp that may never come.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
"""
|
|
Scripts API endpoints for WROLPi Controller.
|
|
|
|
Provides endpoints for running and monitoring maintenance scripts.
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from controller.api.schemas import (
|
|
BranchResponse,
|
|
ScriptInfo,
|
|
ScriptOutputResponse,
|
|
ScriptResultResponse,
|
|
ScriptStartRequest,
|
|
ScriptStartResponse,
|
|
ScriptStatusResponse,
|
|
)
|
|
from controller.lib.config import is_docker_mode
|
|
from controller.lib.scripts import (
|
|
get_current_branch,
|
|
get_script_output,
|
|
get_script_result,
|
|
get_script_status,
|
|
list_available_scripts,
|
|
start_script,
|
|
)
|
|
|
|
router = APIRouter(tags=["scripts"])
|
|
|
|
|
|
@router.get("/api/scripts", response_model=list[ScriptInfo])
|
|
async def scripts_list() -> list[ScriptInfo]:
|
|
"""List all available maintenance scripts."""
|
|
scripts = list_available_scripts()
|
|
return [ScriptInfo(**s) for s in scripts]
|
|
|
|
|
|
@router.get("/api/scripts/status", response_model=ScriptStatusResponse)
|
|
async def scripts_status() -> ScriptStatusResponse:
|
|
"""Get the status of any running script."""
|
|
status = get_script_status()
|
|
return ScriptStatusResponse(**status)
|
|
|
|
|
|
@router.get("/api/scripts/branch", response_model=BranchResponse)
|
|
async def scripts_branch() -> BranchResponse:
|
|
"""Get the current git branch of /opt/wrolpi."""
|
|
if is_docker_mode():
|
|
raise HTTPException(
|
|
status_code=501,
|
|
detail="Branch info not available in Docker mode"
|
|
)
|
|
|
|
branch = get_current_branch()
|
|
return BranchResponse(branch=branch)
|
|
|
|
|
|
@router.post("/api/scripts/{name}/start", response_model=ScriptStartResponse)
|
|
async def scripts_start(
|
|
name: str,
|
|
request: Optional[ScriptStartRequest] = None,
|
|
) -> ScriptStartResponse:
|
|
"""Start a maintenance script."""
|
|
if is_docker_mode():
|
|
raise HTTPException(
|
|
status_code=501,
|
|
detail="Scripts are not available in Docker mode"
|
|
)
|
|
|
|
params = request.params if request else None
|
|
result = start_script(name, params=params)
|
|
if not result.get("success"):
|
|
raise HTTPException(status_code=400, detail=result.get("error", "Failed"))
|
|
|
|
return ScriptStartResponse(**result)
|
|
|
|
|
|
@router.get("/api/scripts/{name}/output", response_model=ScriptOutputResponse)
|
|
async def scripts_output(name: str, lines: int = 100) -> ScriptOutputResponse:
|
|
"""Get output from a script's log."""
|
|
if is_docker_mode():
|
|
raise HTTPException(
|
|
status_code=501,
|
|
detail="Scripts are not available in Docker mode"
|
|
)
|
|
|
|
# Limit lines to prevent abuse
|
|
lines = min(max(1, lines), 5000)
|
|
result = get_script_output(name, lines)
|
|
return ScriptOutputResponse(**result)
|
|
|
|
|
|
@router.get("/api/scripts/{name}/result", response_model=ScriptResultResponse)
|
|
async def scripts_result(name: str) -> ScriptResultResponse:
|
|
"""How the last run of a script ended."""
|
|
if is_docker_mode():
|
|
raise HTTPException(
|
|
status_code=501,
|
|
detail="Scripts are not available in Docker mode"
|
|
)
|
|
|
|
result = get_script_result(name)
|
|
if result is None:
|
|
raise HTTPException(status_code=404, detail=f"Unknown script: {name}")
|
|
return ScriptResultResponse(**result)
|