mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-11-08 21:14:47 +09:00
- 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>
123 lines
4.9 KiB
Python
123 lines
4.9 KiB
Python
"""PR Preview Environments — data model.
|
|
|
|
When a Git pull request is opened/updated, ServerKit deploys an isolated
|
|
preview of that branch to a temporary domain and tears it down when the PR
|
|
closes. Preview config lives here (its own tables) rather than on Application
|
|
so the application schema stays untouched.
|
|
|
|
Two tables:
|
|
* ``application_previews`` — one row per live PR preview.
|
|
* ``application_preview_settings`` — per-application opt-in + domain template.
|
|
"""
|
|
import json
|
|
from datetime import datetime
|
|
|
|
from app import db
|
|
|
|
|
|
class ApplicationPreview(db.Model):
|
|
"""A single PR preview environment for an application."""
|
|
__tablename__ = 'application_previews'
|
|
|
|
# Status lifecycle (string column, not an enum, to match the rest of the
|
|
# codebase's loose status columns):
|
|
STATUS_QUEUED = 'queued'
|
|
STATUS_BUILDING = 'building'
|
|
STATUS_RUNNING = 'running'
|
|
STATUS_STOPPED = 'stopped'
|
|
STATUS_FAILED = 'failed'
|
|
STATUS_DESTROYED = 'destroyed'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
application_id = db.Column(
|
|
db.Integer, db.ForeignKey('applications.id'), nullable=False, index=True)
|
|
|
|
pr_number = db.Column(db.Integer, nullable=False)
|
|
pr_title = db.Column(db.String(500), nullable=True)
|
|
branch = db.Column(db.String(255), nullable=True)
|
|
|
|
status = db.Column(db.String(20), default=STATUS_QUEUED, nullable=False)
|
|
domain = db.Column(db.String(255), nullable=True)
|
|
|
|
# JSON-encoded list of provisioned container ids (best-effort; may be empty
|
|
# when Docker isn't available, e.g. dev/test).
|
|
container_ids = db.Column(db.Text, nullable=True)
|
|
|
|
commit_sha = db.Column(db.String(64), nullable=True)
|
|
expires_at = db.Column(db.DateTime, nullable=True)
|
|
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
deleted_at = db.Column(db.DateTime, nullable=True)
|
|
|
|
def get_container_ids(self):
|
|
"""The provisioned container ids as a list (empty when unset/invalid)."""
|
|
if not self.container_ids:
|
|
return []
|
|
try:
|
|
val = json.loads(self.container_ids)
|
|
return val if isinstance(val, list) else []
|
|
except (ValueError, TypeError):
|
|
return []
|
|
|
|
def set_container_ids(self, ids):
|
|
self.container_ids = json.dumps(list(ids or []))
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'application_id': self.application_id,
|
|
'pr_number': self.pr_number,
|
|
'pr_title': self.pr_title,
|
|
'branch': self.branch,
|
|
'status': self.status,
|
|
'domain': self.domain,
|
|
'url': (f'https://{self.domain}' if self.domain else None),
|
|
'container_ids': self.get_container_ids(),
|
|
'commit_sha': self.commit_sha,
|
|
'short_sha': self.commit_sha[:7] if self.commit_sha else None,
|
|
'expires_at': self.expires_at.isoformat() if self.expires_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,
|
|
'deleted_at': self.deleted_at.isoformat() if self.deleted_at else None,
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f'<ApplicationPreview app={self.application_id} PR#{self.pr_number} ({self.status})>'
|
|
|
|
|
|
class ApplicationPreviewSettings(db.Model):
|
|
"""Per-application PR-preview configuration (opt-in + domain template)."""
|
|
__tablename__ = 'application_preview_settings'
|
|
|
|
DEFAULT_DOMAIN_TEMPLATE = 'pr-{pr_number}.{app_domain}'
|
|
|
|
application_id = db.Column(
|
|
db.Integer, db.ForeignKey('applications.id'),
|
|
primary_key=True, unique=True, nullable=False)
|
|
|
|
enabled = db.Column(db.Boolean, default=False, nullable=False)
|
|
domain_template = db.Column(
|
|
db.String(255), default=DEFAULT_DOMAIN_TEMPLATE, nullable=False)
|
|
# Optional: deploy previews onto a specific server in the fleet.
|
|
target_server_id = db.Column(db.String(36), nullable=True)
|
|
# Optional TTL: previews are auto-destroyed this many days after creation.
|
|
ttl_days = db.Column(db.Integer, nullable=True)
|
|
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'application_id': self.application_id,
|
|
'enabled': bool(self.enabled),
|
|
'domain_template': self.domain_template or self.DEFAULT_DOMAIN_TEMPLATE,
|
|
'target_server_id': self.target_server_id,
|
|
'ttl_days': self.ttl_days,
|
|
'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'<ApplicationPreviewSettings app={self.application_id} enabled={self.enabled}>'
|