ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix: preserve container port semantics for previews of slot deployments
Juan Denisa3d64e5a52f9
diff --git a/backend/app/services/preview_service.py b/backend/app/services/preview_service.pyindex 13ca10e4..1ef19dca 100644--- a/backend/app/services/preview_service.py+++ b/backend/app/services/preview_service.py@@ -485,7 +485,7 @@ class PreviewService:def _fetch_open_prs(cls, app):"""Best-effort: list open PRs for the app's connected repo. Returns alist of ``{pr_number, branch, commit_sha, pr_title}`` dicts, or ``[]``- when no provider is connected (e.g. dev/test)."""+ when a provider returns an empty list. Unavailable discovery returns None."""try:from app.services.source_connection_service import SourceConnectionServicefn = getattr(SourceConnectionService, 'list_open_pull_requests', None)diff --git a/backend/app/services/previews/runtime.py b/backend/app/services/previews/runtime.pyindex 64689a5c..50104d66 100644--- a/backend/app/services/previews/runtime.py+++ b/backend/app/services/previews/runtime.py@@ -30,6 +30,13 @@ def inspect(kind, name):def start(app, preview, state, inherit_environment=False):name = identity(preview.id, state['token'])+ container_port = app.port+ if app.active_slot:+ from app.models.app_slot import AppSlot+ slot = AppSlot.query.filter_by(application_id=app.id, slot=app.active_slot).first()+ if not slot or not slot.container_port:+ raise PreviewError('The active deployment slot has no recorded container port.')+ container_port = slot.container_portstate['container'] = Truesave(preview, state) # intent before docker run, even if it partially failsenv = {'SERVERKIT_PREVIEW': 'true', 'SERVERKIT_PR_NUMBER': str(preview.pr_number),@@ -41,7 +48,7 @@ def start(app, preview, state, inherit_environment=False):raise PreviewError('Inherited environment is incomplete; preview deployment stopped.')env = {**{key: value['value'] for key, value in entries.items()}, **env}result = DockerService.run_container(- image=name + ':preview', name=name, ports=[f'127.0.0.1::{app.port}'],+ image=name + ':preview', name=name, ports=[f'127.0.0.1::{container_port}'],env=env, restart_policy='unless-stopped', labels={LABEL: str(preview.id)},)if not result.get('success'):@@ -49,7 +56,7 @@ def start(app, preview, state, inherit_environment=False):info = inspect('container', name)if not info or info.get('Config', {}).get('Labels', {}).get(LABEL) != str(preview.id):raise PreviewError('Preview container ownership could not be verified.')- bindings = info.get('NetworkSettings', {}).get('Ports', {}).get(f'{app.port}/tcp') or []+ bindings = info.get('NetworkSettings', {}).get('Ports', {}).get(f'{container_port}/tcp') or []binding = next((b for b in bindings if b.get('HostIp') == '127.0.0.1'), None)if not binding:raise PreviewError('Preview container has no loopback HTTP port.')diff --git a/backend/tests/test_generic_previews.py b/backend/tests/test_generic_previews.pyindex a91e3b99..66165798 100644--- a/backend/tests/test_generic_previews.py+++ b/backend/tests/test_generic_previews.py@@ -368,3 +368,20 @@ def test_purge_cleans_preview_before_deleting_ownership_record(host, monkeypatchassert ok, warningassert not resources['containers'] and not resources['images']assert db.session.get(ApplicationPreview, result['id']) is None+++def test_preview_uses_slot_container_port_without_mutating_production_slot(host):+ from app.models.app_slot import AppSlot+ parent, resources = host+ parent.active_slot = 'a'+ parent.slot_deploys_enabled = True+ parent.port = 39005 # live host binding, not the image's listening port+ slot = AppSlot(application_id=parent.id, slot='a', container_port=8400,+ host_port=39005, container_name='production-slot', state='live')+ db.session.add(slot)+ db.session.commit()+ assert PreviewService.create_preview(parent, PR)['status'] == 'running'+ run = next(call[1] for call in resources['calls'] if call[0] == 'run')+ assert run['ports'] == ['127.0.0.1::8400']+ db.session.refresh(slot)+ assert slot.host_port == 39005 and slot.container_name == 'production-slot'