Files
ServerKit/backend/app/models/proxy_stack.py
T
Juan DenisandClaude Opus 4.8 478b24a3cb feat: Phase 3 team & scale — projects/environments, shared resources, PR previews, proxy stacks
- Project / Environment hierarchy (C2): Workspace -> Project -> Environment
  org model. New Project/Environment models + CRUD APIs, nullable
  applications.project_id/environment_id (opt-in, no destructive backfill),
  ProjectService.ensure_default, a Projects list + ProjectDetail (env tabs),
  a project/environment selector in the create wizard, and sidebar nav.

- Polymorphic shared resources facade (C9): ResourceTag + SharedVariableGroup
  /SharedVariable/attachment models keyed by (resource_type, resource_id),
  reusing the env-var Fernet encryption; SharedResourceService resolves merged
  variables (most-recent attachment wins) with secrets masked. /api/v1/shared
  endpoints + reusable TagsPanel/EnvironmentVariablesPanel + a Shared Variables
  management page. Existing per-resource tables untouched (facade first).

- PR preview environments (C3): ApplicationPreview + settings models, a
  PreviewService with pure domain-templating + reconcile (open PRs vs active
  previews) cores and best-effort provisioning (incl. guarded WordPress
  file+DB clone), a pull_request webhook endpoint, preview.* jobs, and a
  Previews tab on the app detail page.

- Per-server managed proxy stack (C6): opt-in Dockerized Traefik/Caddy as a
  Compose stack (host Nginx stays default and superior for PHP/WordPress).
  ProxyStack model + ProxyStackService with pure compose/config generators and
  versioned config backups, /api/v1/servers/<id>/proxy endpoints, and a Proxy
  tab on the server detail page.

Migrations 031-034 add the new tables/columns; the full chain applies cleanly
from an empty DB. Full backend suite: 871 passed, 14 skipped; frontend builds.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:33:44 -04:00

83 lines
3.0 KiB
Python

"""ProxyStack model — per-server managed reverse-proxy state.
ServerKit treats the reverse proxy as a first-class managed service. Host
Nginx remains the *default* (and the better choice for PHP/WordPress), but a
server may opt into a Dockerized proxy (Traefik or Caddy) deployed as a
Compose stack. This table records that choice + the on-disk compose path and
config snippet for one server. It is intentionally keyed 1:1 on server_id
(unique) — a server runs at most one managed proxy stack at a time.
"""
import uuid
import json
from datetime import datetime
from app import db
class ProxyStack(db.Model):
"""One managed reverse-proxy stack per server."""
__tablename__ = 'proxy_stacks'
# Proxy types this table understands. 'nginx' means "host nginx, no
# Docker stack" — the default. The other two deploy a Compose stack.
PROXY_NGINX = 'nginx'
PROXY_TRAEFIK = 'traefik'
PROXY_CADDY = 'caddy'
id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
# 1:1 with a server. Unique so get_or_create never creates duplicates;
# indexed for the per-server lookup the API does on every request.
server_id = db.Column(
db.String(36),
db.ForeignKey('servers.id'),
nullable=False,
index=True,
unique=True,
)
proxy_type = db.Column(db.String(20), nullable=False, default=PROXY_NGINX)
# stopped | running | error | unknown
status = db.Column(db.String(20), nullable=False, default='unknown')
# Where the compose stack lives on disk (None for host-nginx).
compose_path = db.Column(db.String(512))
# JSON list of docker network names the proxy attaches to.
networks = db.Column(db.Text)
# Operator-supplied raw config appended to the generated proxy config.
custom_snippet = db.Column(db.Text)
last_regenerated_at = db.Column(db.DateTime)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
def networks_list(self):
"""Decode the networks JSON column to a list (empty on missing/bad)."""
if not self.networks:
return []
try:
data = json.loads(self.networks)
return data if isinstance(data, list) else []
except (TypeError, ValueError):
return []
def to_dict(self):
return {
'id': self.id,
'server_id': self.server_id,
'proxy_type': self.proxy_type,
'status': self.status,
'compose_path': self.compose_path,
'networks': self.networks_list(),
'custom_snippet': self.custom_snippet,
'last_regenerated_at': self.last_regenerated_at.isoformat() if self.last_regenerated_at else None,
'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
}
def __repr__(self):
return f'<ProxyStack server={self.server_id} type={self.proxy_type}>'