feat(agent): FastAPI endpoints for container management (TDD)
Add agent/main.py with REST endpoints wrapping DockerOps: health, list containers, start/stop/restart, logs, and pull. DockerOps instantiation handles missing Docker socket gracefully for test environments. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
from fastapi import FastAPI, Query
|
||||
from agent.docker_ops import DockerOps
|
||||
|
||||
app = FastAPI(title="Farm Manager Agent")
|
||||
|
||||
try:
|
||||
ops = DockerOps()
|
||||
except Exception:
|
||||
ops = None # Will be mocked in tests
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return ops.get_health()
|
||||
|
||||
|
||||
@app.get("/containers")
|
||||
def list_containers():
|
||||
return ops.list_containers()
|
||||
|
||||
|
||||
@app.post("/containers/{container_id}/start")
|
||||
def start_container(container_id: str):
|
||||
return ops.start_container(container_id)
|
||||
|
||||
|
||||
@app.post("/containers/{container_id}/stop")
|
||||
def stop_container(container_id: str):
|
||||
return ops.stop_container(container_id)
|
||||
|
||||
|
||||
@app.post("/containers/{container_id}/restart")
|
||||
def restart_container(container_id: str):
|
||||
return ops.restart_container(container_id)
|
||||
|
||||
|
||||
@app.get("/containers/{container_id}/logs")
|
||||
def get_logs(container_id: str, tail: int = Query(default=200)):
|
||||
return ops.get_logs(container_id, tail=tail)
|
||||
|
||||
|
||||
@app.post("/containers/{container_id}/pull")
|
||||
def pull_image(container_id: str):
|
||||
return ops.pull_image(container_id)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8889)
|
||||
Reference in New Issue
Block a user