68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
import os
|
|
from fastapi import FastAPI, HTTPException
|
|
import requests_unixsocket
|
|
import yaml # Voor eventuele toekomstige validatie
|
|
|
|
app = FastAPI(title="Podman MVP Control Plane")
|
|
|
|
# Gebruik requests-unixsocket om met de Podman socket te praten
|
|
SESSION = requests_unixsocket.Session()
|
|
|
|
# Podman API URL: %2F is de slash '/' voor de unix socket
|
|
# We gebruiken v5.4.2 zoals gespecificeerd
|
|
PODMAN_API_BASE = "http+unix://%2Frun%2Fuser%2F1000%2Fpodman%2Fpodman.sock/v5.4.2"
|
|
WORKLOADS_DIR = "/app/workloads"
|
|
|
|
@app.get("/workloads")
|
|
def list_workloads():
|
|
"""Scant recursief naar .yaml bestanden in de gemounte map."""
|
|
yaml_files = []
|
|
try:
|
|
for root, dirs, files in os.walk(WORKLOADS_DIR):
|
|
for file in files:
|
|
if file.endswith(".yaml"):
|
|
# Maak een pad relatief aan de workloads map
|
|
full_path = os.path.join(root, file)
|
|
rel_path = os.path.relpath(full_path, WORKLOADS_DIR)
|
|
yaml_files.append(rel_path)
|
|
return {"workloads": sorted(yaml_files)}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@app.post("/workloads/deploy/{filename:path}")
|
|
def deploy_workload(filename: str):
|
|
"""Stuurt de inhoud van een YAML naar de Podman API (kube play)."""
|
|
path = os.path.join(WORKLOADS_DIR, filename)
|
|
|
|
if not os.path.exists(path):
|
|
raise HTTPException(status_code=404, detail=f"Bestand {filename} niet gevonden")
|
|
|
|
try:
|
|
with open(path, 'r') as f:
|
|
yaml_content = f.read()
|
|
|
|
# Podman Kube Play endpoint
|
|
url = f"{PODMAN_API_BASE}/libpod/kube/play"
|
|
|
|
# We versturen de platte tekst van het YAML bestand naar de API
|
|
response = SESSION.post(url, data=yaml_content)
|
|
|
|
if response.status_code >= 400:
|
|
return {
|
|
"status": "error",
|
|
"podman_status_code": response.status_code,
|
|
"details": response.json()
|
|
}
|
|
|
|
return {
|
|
"status": "success",
|
|
"message": f"Deployment van {filename} gestart",
|
|
"podman_response": response.json()
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
# Belangrijk: host 0.0.0.0 zodat hij bereikbaar is buiten de container
|
|
uvicorn.run(app, host="0.0.0.0", port=8000) |