letsbe-orchestrator/app/playbooks/nextcloud.py

100 lines
2.7 KiB
Python
Raw Normal View History

"""Nextcloud deployment playbook.
Defines the steps required to set Nextcloud domain on a tenant server
that already has stacks and env templates under /opt/letsbe.
v2: Configures Nextcloud via NEXTCLOUD_SET_DOMAIN task, then reloads the stack.
"""
import uuid
from typing import Any
from pydantic import BaseModel, Field
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.task import Task, TaskStatus
class CompositeStep(BaseModel):
"""A single step in a composite playbook."""
type: str = Field(..., description="Task type (e.g., ENV_UPDATE, DOCKER_RELOAD)")
payload: dict[str, Any] = Field(
default_factory=dict, description="Payload for this step"
)
# LetsBe standard paths
NEXTCLOUD_STACK_DIR = "/opt/letsbe/stacks/nextcloud"
def build_nextcloud_set_domain_steps(*, public_url: str, pull: bool) -> list[CompositeStep]:
"""
Build the sequence of steps required to set Nextcloud domain (v2).
Args:
public_url: The public URL for Nextcloud (e.g., "https://cloud.example.com")
pull: Whether to pull images before reloading the stack
Returns:
List of 2 CompositeStep objects:
1. NEXTCLOUD_SET_DOMAIN - configures Nextcloud via occ commands
2. DOCKER_RELOAD - restarts the Nextcloud stack
"""
steps = [
# Step 1: Configure Nextcloud domain via occ
CompositeStep(
type="NEXTCLOUD_SET_DOMAIN",
payload={
"public_url": public_url,
},
),
# Step 2: Reload Docker stack
CompositeStep(
type="DOCKER_RELOAD",
payload={
"compose_dir": NEXTCLOUD_STACK_DIR,
"pull": pull,
},
),
]
return steps
async def create_nextcloud_set_domain_task(
*,
db: AsyncSession,
tenant_id: uuid.UUID,
agent_id: uuid.UUID,
public_url: str,
pull: bool,
) -> Task:
"""
Create and persist a COMPOSITE task for Nextcloud set-domain.
Args:
db: Async database session
tenant_id: UUID of the tenant
agent_id: UUID of the agent to assign the task to
public_url: The public URL for Nextcloud
pull: Whether to pull images before reloading
Returns:
The created Task object with type="COMPOSITE"
"""
steps = build_nextcloud_set_domain_steps(public_url=public_url, pull=pull)
task = Task(
tenant_id=tenant_id,
agent_id=agent_id,
type="COMPOSITE",
payload={"steps": [step.model_dump() for step in steps]},
status=TaskStatus.PENDING.value,
)
db.add(task)
await db.commit()
await db.refresh(task)
return task