mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
Enhancing Controller to run scripts, starting with the repair and upgrade scripts for onboarding use.
This commit is contained in:
parent
72988bbf99
commit
603a2c010a
8 changed files with 1430 additions and 70 deletions
|
|
@ -263,3 +263,65 @@ class ServicesListErrorResponse(BaseModel):
|
|||
|
||||
error: str = Field(description="Error message")
|
||||
reason: Optional[str] = Field(default=None, description="Detailed reason")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Scripts
|
||||
# ============================================================================
|
||||
|
||||
class ScriptParameter(BaseModel):
|
||||
"""Definition of a script input parameter."""
|
||||
|
||||
name: str = Field(description="Parameter name (used as env var name)")
|
||||
label: str = Field(description="Display label for the input field")
|
||||
type: str = Field(description="Parameter type: 'branch' or 'text'")
|
||||
required: bool = Field(default=False, description="Whether the parameter must have a value")
|
||||
|
||||
|
||||
class ScriptInfo(BaseModel):
|
||||
"""Information about an available script."""
|
||||
|
||||
name: str = Field(description="Script identifier")
|
||||
display_name: str = Field(description="Human-readable script name")
|
||||
description: str = Field(description="What the script does")
|
||||
warnings: list[str] = Field(description="Warnings to show before running")
|
||||
available: bool = Field(description="Whether the script can be run")
|
||||
parameters: list[ScriptParameter] = Field(default_factory=list, description="Input parameters for the script")
|
||||
|
||||
|
||||
class ScriptStatusResponse(BaseModel):
|
||||
"""Response model for script running status."""
|
||||
|
||||
running: bool = Field(description="Whether a script is currently running")
|
||||
script_name: Optional[str] = Field(default=None, description="Name of running script")
|
||||
service_name: Optional[str] = Field(default=None, description="Systemd service name")
|
||||
started_at: Optional[str] = Field(default=None, description="ISO timestamp when started")
|
||||
elapsed_seconds: Optional[int] = Field(default=None, description="Seconds since start")
|
||||
|
||||
|
||||
class ScriptStartResponse(BaseModel):
|
||||
"""Response model for starting a script."""
|
||||
|
||||
success: bool = Field(description="Whether the script started successfully")
|
||||
message: Optional[str] = Field(default=None, description="Success message")
|
||||
error: Optional[str] = Field(default=None, description="Error message if failed")
|
||||
|
||||
|
||||
class ScriptOutputResponse(BaseModel):
|
||||
"""Response model for script output."""
|
||||
|
||||
output: str = Field(description="Script log output")
|
||||
lines: int = Field(description="Number of lines requested")
|
||||
script_name: str = Field(description="Script name")
|
||||
|
||||
|
||||
class ScriptStartRequest(BaseModel):
|
||||
"""Request model for starting a script."""
|
||||
|
||||
params: Optional[dict[str, str]] = Field(default=None, description="Parameter values (e.g., {'branch': 'release'})")
|
||||
|
||||
|
||||
class BranchResponse(BaseModel):
|
||||
"""Response model for current git branch."""
|
||||
|
||||
branch: Optional[str] = Field(description="Current git branch, or null if unavailable")
|
||||
|
|
|
|||
90
controller/api/scripts.py
Normal file
90
controller/api/scripts.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
"""
|
||||
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,
|
||||
ScriptStartRequest,
|
||||
ScriptStartResponse,
|
||||
ScriptStatusResponse,
|
||||
)
|
||||
from controller.lib.config import is_docker_mode
|
||||
from controller.lib.scripts import (
|
||||
get_current_branch,
|
||||
get_script_output,
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue