Feat : Lot of stuff
All checks were successful
Build and Push Docker Images / docker (push) Successful in 51s
All checks were successful
Build and Push Docker Images / docker (push) Successful in 51s
This commit is contained in:
@@ -6,6 +6,7 @@ Expose une API REST utilisée par le backend central pour interroger les contene
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
||||
@@ -18,6 +19,11 @@ from fastapi.security import APIKeyHeader
|
||||
|
||||
# ─── Config ───────────────────────────────────────────────────────────────────
|
||||
|
||||
AGENT_VERSION = "1.1.0"
|
||||
|
||||
REPO_BASE = os.getenv("AGENT_REPO_BASE", "https://git.jeanbonapp.com/jeanbon/ScriptVPS/raw/branch/main")
|
||||
INSTALL_DIR = os.getenv("AGENT_INSTALL_DIR", "/opt/vps-monitor-agent")
|
||||
|
||||
API_KEY = os.getenv("AGENT_API_KEY", "changeme-please")
|
||||
AGENT_PORT = int(os.getenv("AGENT_PORT", "8001"))
|
||||
|
||||
@@ -55,6 +61,45 @@ def health():
|
||||
return {"status": "ok", "timestamp": datetime.now(timezone.utc).isoformat()}
|
||||
|
||||
|
||||
@app.get("/version")
|
||||
def get_version():
|
||||
"""Retourne la version de l'agent — sans authentification."""
|
||||
return {"version": AGENT_VERSION}
|
||||
|
||||
|
||||
@app.post("/self-update")
|
||||
def self_update(_: None = Depends(require_api_key)):
|
||||
"""Télécharge la dernière version de l'agent depuis le dépôt et redémarre le service."""
|
||||
|
||||
def _do_update():
|
||||
time.sleep(0.5) # laisse la réponse HTTP partir
|
||||
try:
|
||||
for filename in ("agent.py", "requirements.txt"):
|
||||
src = f"{REPO_BASE}/vps-monitor/agent/{filename}"
|
||||
dst = f"{INSTALL_DIR}/{filename}"
|
||||
subprocess.run(
|
||||
["curl", "-fsSL", src, "-o", dst],
|
||||
timeout=60,
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(
|
||||
[f"{INSTALL_DIR}/venv/bin/pip", "install", "-r",
|
||||
f"{INSTALL_DIR}/requirements.txt", "-q"],
|
||||
timeout=120,
|
||||
check=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["systemctl", "restart", "vps-monitor-agent"],
|
||||
timeout=30,
|
||||
check=True,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
threading.Thread(target=_do_update, daemon=True).start()
|
||||
return {"status": "update_started"}
|
||||
|
||||
|
||||
@app.get("/containers")
|
||||
def list_containers(_: None = Depends(require_api_key)):
|
||||
"""Retourne tous les conteneurs (actifs et arrêtés)."""
|
||||
|
||||
Reference in New Issue
Block a user