Phase 25: templates as stack folders (0.31.0)

Templates are now stack-shaped folders (compose.yaml + .env.example +
template.json) instead of DB rows + manifest.json + {{VAR}} rendering.
Pull copies the folder into a new stack; custom templates persist under
DATA_DIR/templates. Adds POST /api/templates/from-stack and a one-time
startup migration for pre-0.31 DB templates (drops the template table).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
menzelj
2026-06-12 07:29:54 +00:00
co-authored by Claude Fable 5
parent 34cb215266
commit 1609b8bcc3
29 changed files with 564 additions and 329 deletions
+47 -16
View File
@@ -1,4 +1,8 @@
"""Template library endpoints."""
"""Template library endpoints.
Templates are stack-shaped folders on disk. Listing reads them; "instantiate"
(pull) copies the whole folder into a new stack, which is then editable.
"""
from __future__ import annotations
import os
@@ -10,7 +14,11 @@ from auth import get_current_user, require_admin
from database import get_session
from models.agent import Agent
from models.stack import Stack
from models.template import TemplateInstantiateRequest, TemplateSaveRequest
from models.template import (
TemplateFromStackRequest,
TemplateInstantiateRequest,
TemplateSaveRequest,
)
from models.user import User
from services import agent_service, audit_service, compose_service, template_service
from services.agent_service import AgentError
@@ -24,19 +32,17 @@ def _ip(request: Request) -> str:
@router.get("")
def list_templates(
session: Session = Depends(get_session),
_user: User = Depends(get_current_user),
) -> list[dict]:
return template_service.list_templates(session)
return template_service.list_templates()
@router.get("/{template_id}")
def get_template(
template_id: str,
session: Session = Depends(get_session),
_user: User = Depends(get_current_user),
) -> dict:
tpl = template_service.get_template(session, template_id)
tpl = template_service.get_template(template_id)
if not tpl:
raise HTTPException(status_code=404, detail="Template not found")
return tpl
@@ -49,13 +55,32 @@ def save_template(
session: Session = Depends(get_session),
user: User = Depends(require_admin),
) -> dict:
tpl = template_service.save_custom(
session, body.name, body.yaml, body.description or "", body.tags
slug = template_service.save_custom(
body.name, body.compose, body.env, body.description or "", body.tags, body.gpu
)
audit_service.record(
session, user=user.username, action="template.save", target=tpl.slug, ip=_ip(request)
session, user=user.username, action="template.save", target=slug, ip=_ip(request)
)
return {"id": f"custom:{tpl.slug}", "name": tpl.name}
return {"id": f"custom:{slug}", "name": body.name}
@router.post("/from-stack")
def save_from_stack(
body: TemplateFromStackRequest,
request: Request,
session: Session = Depends(get_session),
user: User = Depends(require_admin),
) -> dict:
if not session.get(Stack, body.stack_id) and not os.path.isdir(
compose_service.stack_dir(body.stack_id)
):
raise HTTPException(status_code=404, detail=f"Stack '{body.stack_id}' not found")
slug = template_service.save_from_stack(body.stack_id, body.name, body.description or "")
audit_service.record(
session, user=user.username, action="template.save",
target=slug, detail=body.stack_id, ip=_ip(request),
)
return {"id": f"custom:{slug}", "name": body.name}
@router.delete("/custom/{slug}")
@@ -65,7 +90,7 @@ def delete_template(
session: Session = Depends(get_session),
user: User = Depends(require_admin),
) -> dict:
if not template_service.delete_custom(session, slug):
if not template_service.delete_custom(slug):
raise HTTPException(status_code=404, detail="Custom template not found")
audit_service.record(
session, user=user.username, action="template.delete", target=slug, ip=_ip(request)
@@ -81,12 +106,11 @@ async def instantiate(
session: Session = Depends(get_session),
user: User = Depends(require_admin),
) -> dict:
tpl = template_service.get_template(session, template_id)
tpl = template_service.get_template(template_id)
if not tpl:
raise HTTPException(status_code=404, detail="Template not found")
rendered = template_service.render(tpl["yaml"], body.values)
# Remote host: agents don't share our filesystem, so ship compose + env.
if body.agent_id is not None:
agent = session.get(Agent, body.agent_id)
if not agent:
@@ -94,7 +118,7 @@ async def instantiate(
try:
result = await agent_service.call(
session, agent, "POST", "/agent/stacks",
json={"name": body.name, "yaml": rendered, "env": None},
json={"name": body.name, "yaml": tpl["compose"], "env": tpl["env"] or None},
)
except AgentError as exc:
raise HTTPException(
@@ -107,11 +131,18 @@ async def instantiate(
)
return {"id": result.get("id"), "name": body.name, "agent_id": agent.id}
# Local host: copy the whole template folder into a new stack.
stack_id = compose_service.slugify(body.name)
if session.get(Stack, stack_id) or os.path.isdir(compose_service.stack_dir(stack_id)):
raise HTTPException(status_code=409, detail=f"Stack '{stack_id}' already exists")
compose_service.write_compose(stack_id, rendered)
try:
template_service.copy_into_stack(template_id, stack_id)
except FileExistsError:
raise HTTPException(status_code=409, detail=f"Stack '{stack_id}' already exists")
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Template not found")
stack = Stack(id=stack_id, name=body.name, description=tpl.get("description"))
session.add(stack)
session.commit()