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:
@@ -5,9 +5,12 @@ Expose une API REST utilisée par le backend central pour interroger les contene
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import docker
|
||||
import psutil
|
||||
from docker.errors import DockerException, NotFound
|
||||
from fastapi import Depends, FastAPI, HTTPException, Security
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
@@ -59,14 +62,18 @@ def list_containers(_: None = Depends(require_api_key)):
|
||||
result = []
|
||||
for c in client.containers.list(all=True):
|
||||
image_tag = c.image.tags[0] if c.image.tags else c.image.short_id
|
||||
health_state = c.attrs.get("State", {}).get("Health", {})
|
||||
health = health_state.get("Status", "none") if health_state else "none"
|
||||
result.append({
|
||||
"id": c.short_id,
|
||||
"name": c.name.lstrip("/"),
|
||||
"status": c.status,
|
||||
"health": health,
|
||||
"image": image_tag,
|
||||
"created": c.attrs.get("Created", ""),
|
||||
"compose_project": c.labels.get("com.docker.compose.project", ""),
|
||||
"compose_service": c.labels.get("com.docker.compose.service", ""),
|
||||
"compose_working_dir": c.labels.get("com.docker.compose.project.working_dir", ""),
|
||||
"ports": {
|
||||
host: [{"HostIp": b["HostIp"], "HostPort": b["HostPort"]} for b in bindings]
|
||||
for host, bindings in (c.ports or {}).items()
|
||||
@@ -102,6 +109,69 @@ def container_action(container_id: str, action: str, _: None = Depends(require_a
|
||||
raise HTTPException(status_code=404, detail="Conteneur introuvable")
|
||||
|
||||
|
||||
@app.get("/system")
|
||||
def system_info(_: None = Depends(require_api_key)):
|
||||
"""Retourne les informations système : CPU, RAM et bande passante."""
|
||||
cpu_percent = psutil.cpu_percent(interval=0.5)
|
||||
mem = psutil.virtual_memory()
|
||||
|
||||
net1 = psutil.net_io_counters()
|
||||
time.sleep(0.5)
|
||||
net2 = psutil.net_io_counters()
|
||||
net_sent_per_sec = (net2.bytes_sent - net1.bytes_sent) * 2
|
||||
net_recv_per_sec = (net2.bytes_recv - net1.bytes_recv) * 2
|
||||
|
||||
return {
|
||||
"cpu_percent": cpu_percent,
|
||||
"ram_used": mem.used,
|
||||
"ram_total": mem.total,
|
||||
"ram_percent": mem.percent,
|
||||
"net_sent_per_sec": net_sent_per_sec,
|
||||
"net_recv_per_sec": net_recv_per_sec,
|
||||
}
|
||||
|
||||
|
||||
@app.post("/compose/update")
|
||||
def compose_update(project: str, _: None = Depends(require_api_key)):
|
||||
"""Pull les nouvelles images et recrée les conteneurs d'un projet compose."""
|
||||
client = get_docker_client()
|
||||
|
||||
working_dir = None
|
||||
for c in client.containers.list(all=True):
|
||||
if c.labels.get("com.docker.compose.project") == project:
|
||||
working_dir = c.labels.get("com.docker.compose.project.working_dir")
|
||||
if working_dir:
|
||||
break
|
||||
|
||||
if not working_dir:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"Projet compose '{project}' introuvable ou sans répertoire de travail",
|
||||
)
|
||||
|
||||
output = ""
|
||||
|
||||
pull = subprocess.run(
|
||||
["docker", "compose", "pull"],
|
||||
cwd=working_dir,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=300,
|
||||
)
|
||||
output += pull.stdout + pull.stderr
|
||||
|
||||
up = subprocess.run(
|
||||
["docker", "compose", "up", "-d", "--remove-orphans"],
|
||||
cwd=working_dir,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=120,
|
||||
)
|
||||
output += up.stdout + up.stderr
|
||||
|
||||
return {"output": output, "project": project, "working_dir": working_dir}
|
||||
|
||||
|
||||
# ─── Entrée ───────────────────────────────────────────────────────────────────
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
fastapi>=0.111.0
|
||||
uvicorn[standard]>=0.30.0
|
||||
docker>=7.1.0
|
||||
psutil>=5.9.0
|
||||
|
||||
Reference in New Issue
Block a user