feat : update agent 2
Some checks failed
Build and Push Docker Images / docker (push) Failing after 8s
Some checks failed
Build and Push Docker Images / docker (push) Failing after 8s
This commit is contained in:
Binary file not shown.
@@ -62,6 +62,10 @@ class ActionRequest(BaseModel):
|
||||
action: str # start | stop | restart
|
||||
|
||||
|
||||
class ComposeUpdateRequest(BaseModel):
|
||||
project: str
|
||||
|
||||
|
||||
class RegisterRequest(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
@@ -209,14 +213,22 @@ async def agent_post(vps: dict, path: str, payload: dict | None = None):
|
||||
async def fetch_vps_status(vps: dict) -> dict:
|
||||
"""Interroge un agent et retourne son état complet."""
|
||||
try:
|
||||
containers = await agent_get(vps, "/containers")
|
||||
containers_res, system_res = await asyncio.gather(
|
||||
agent_get(vps, "/containers"),
|
||||
agent_get(vps, "/system"),
|
||||
return_exceptions=True,
|
||||
)
|
||||
if isinstance(containers_res, Exception):
|
||||
raise containers_res
|
||||
system = system_res if not isinstance(system_res, Exception) else None
|
||||
return {
|
||||
"id": vps["id"],
|
||||
"name": vps["name"],
|
||||
"host": vps["host"],
|
||||
"description": vps.get("description", ""),
|
||||
"online": True,
|
||||
"containers": containers,
|
||||
"containers": containers_res,
|
||||
"system": system,
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
@@ -227,6 +239,7 @@ async def fetch_vps_status(vps: dict) -> dict:
|
||||
"online": False,
|
||||
"error": str(e),
|
||||
"containers": [],
|
||||
"system": None,
|
||||
}
|
||||
|
||||
|
||||
@@ -347,3 +360,24 @@ async def container_action(
|
||||
return await agent_post(vps, f"/containers/{container_id}/action?action={body.action}")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
|
||||
@app.post("/api/vps/{vps_id}/compose/update")
|
||||
async def compose_update(
|
||||
vps_id: str, body: ComposeUpdateRequest,
|
||||
_: Annotated[dict, Depends(get_current_user)] = None
|
||||
):
|
||||
"""Lance docker compose pull + up sur un projet via l'agent."""
|
||||
vps = next((v for v in load_vps() if v["id"] == vps_id), None)
|
||||
if not vps:
|
||||
raise HTTPException(status_code=404, detail="VPS introuvable")
|
||||
try:
|
||||
url = f"http://{vps['host']}:{vps['port']}/compose/update?project={body.project}"
|
||||
headers = {"X-API-Key": vps["api_key"]}
|
||||
timeout = aiohttp.ClientTimeout(total=600)
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.post(url, headers=headers, timeout=timeout) as r:
|
||||
r.raise_for_status()
|
||||
return await r.json()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user