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
Deploy compose apps through A/B slots
Juan Denisd0b11714e71a
diff --git a/backend/app/api/app_slots.py b/backend/app/api/app_slots.pyindex cea8a276..81398bda 100644--- a/backend/app/api/app_slots.py+++ b/backend/app/api/app_slots.py@@ -8,6 +8,10 @@POST /<app_id>/slots/switch-back — make the standby live again (a rollbackto the release it holds: seconds whileit is warm)+ GET /<app_id>/slots/compose-split — preview moving stateful compose services+ into the shared <name>-data project+ POST /<app_id>/slots/compose-split — {confirm: true}: do it (one announced+ outage; the data stays on its volumes)POST /<app_id>/slots/restore-db — {deployment_id}: restore the databasesnapshot taken before that deployment'srelease ran. Destroys later writes; an@@ -76,3 +80,27 @@ def restore_db(app_id):if not result.get('success'):return jsonify(result), 409return jsonify(result)+++@app_slots_bp.route('/<int:app_id>/slots/compose-split', methods=['GET'])+@viewer_required+def compose_split_preview(app_id):+ app, _ = _app(app_id)+ result = SlotDeployService.compose_split_preview(app)+ if not result.get('success'):+ return jsonify(result), 409+ return jsonify(result)+++@app_slots_bp.route('/<int:app_id>/slots/compose-split', methods=['POST'])+@developer_required+def compose_split_apply(app_id):+ app, _ = _app(app_id, write=True)+ data = request.get_json(silent=True) or {}+ if data.get('confirm') is not True:+ raise ValidationError("Moving the stateful services stops the app once; send "+ "{'confirm': true} after reading the preview")+ result = SlotDeployService.compose_split_apply(app)+ if not result.get('success'):+ return jsonify(result), 409+ return jsonify(result)diff --git a/backend/app/api/git.py b/backend/app/api/git.pyindex bdd1be0e..8c8e892b 100644--- a/backend/app/api/git.py+++ b/backend/app/api/git.py@@ -77,7 +77,6 @@ def create_webhook():deploy_on_push=data.get('deployOnPush', False),pre_deploy_script=data.get('preDeployScript'),post_deploy_script=data.get('postDeployScript'),- zero_downtime=data.get('zeroDowntime', False))if result.get('success'):diff --git a/backend/app/models/webhook.py b/backend/app/models/webhook.pyindex 37e291f2..b091d02f 100644--- a/backend/app/models/webhook.py+++ b/backend/app/models/webhook.py@@ -31,7 +31,6 @@ class GitWebhook(TimestampMixin, db.Model):deploy_on_push = db.Column(db.Boolean, default=False)pre_deploy_script = db.Column(db.Text, nullable=True)post_deploy_script = db.Column(db.Text, nullable=True)- zero_downtime = db.Column(db.Boolean, default=False)# Status trackingis_active = db.Column(db.Boolean, default=True)@@ -61,7 +60,6 @@ class GitWebhook(TimestampMixin, db.Model):'deploy_on_push': self.deploy_on_push,'pre_deploy_script': self.pre_deploy_script,'post_deploy_script': self.post_deploy_script,- 'zero_downtime': self.zero_downtime,'is_active': self.is_active,'last_sync_at': self.last_sync_at.isoformat() if self.last_sync_at else None,'last_sync_status': self.last_sync_status,diff --git a/backend/app/services/app_image_retention.py b/backend/app/services/app_image_retention.pyindex 02245167..2639f88d 100644--- a/backend/app/services/app_image_retention.py+++ b/backend/app/services/app_image_retention.py@@ -2,15 +2,16 @@Every build used to be tagged ``serverkit-app-<id>:latest``, so "roll back tov11" re-ran ``:latest`` — the image v12 had just overwritten — and rolled-forward instead. A deploy now builds ``serverkit-app-<id>:d<deployment id>``,-the deployment row keeps that tag, and a rollback runs the image that actually+forward instead. A deploy now builds ``serverkit-app-<id>:d<deployment id>``+(a compose slot deploy: ``serverkit-app-<id>-<service>:d<deployment id>``),+the deployment keeps that tag, and a rollback runs the image that actuallyran.-The price is one image per deploy, and plan 85 is about exactly that kind of-leak, so the tags are pruned on every successful deploy: the newest-``keep`` deployments' images stay, plus any image a slot or the live-deployment still references. ``docker rmi`` without ``-f`` refuses an image a-container is using, which is a second guard, not the first.+The price is images per deploy, and plan 85 is about exactly that kind of+leak, so they are pruned on every successful deploy: images of the newest+``keep`` deployments stay, plus anything a slot or the live deployment still+references. ``docker rmi`` without ``-f`` refuses an image a container is+using, which is a second guard, not the first."""import loggingimport re@@ -19,6 +20,7 @@ logger = logging.getLogger(__name__)DEFAULT_KEEP = 3_TAG = re.compile(r'^d(\d+)$')+_REF_DEPLOYMENT = re.compile(r'd(\d+)$')def repository(app_id) -> str:@@ -29,40 +31,56 @@ def deploy_image_tag(app_id, deployment_id) -> str:return f'{repository(app_id)}:d{deployment_id}'-def _local_tags(app_id):+def _owned(app_id, repo: str) -> bool:+ base = repository(app_id)+ return repo == base or repo.startswith(base + '-')+++def _local_refs(app_id):+ """``[(repository, tag)]`` of this app's images (single and per-service)."""from app.services.docker_service import DockerService- result = DockerService.run(['images', repository(app_id), '--format', '{{.Tag}}'], timeout=30)+ result = DockerService.run(['images', '--format', '{{.Repository}}:{{.Tag}}',+ '--filter', f'reference={repository(app_id)}*'], timeout=30)if not result.get('success'):return None- return [t.strip() for t in (result.get('output') or '').splitlines() if t.strip()]+ refs = []+ for line in (result.get('output') or '').splitlines():+ repo, _, tag = line.strip().rpartition(':')+ if repo and _owned(app_id, repo):+ refs.append((repo, tag))+ return refs+++def _deployment_id(ref):+ match = _REF_DEPLOYMENT.search(ref or '')+ return int(match.group(1)) if match else Nonedef prune(app_id, keep: int = DEFAULT_KEEP, protect=()) -> list:"""Remove this app's ``d<N>`` images beyond the newest ``keep`` deployments.- ``protect`` is extra image refs that must survive (slot images, the live- deployment's). Best-effort: returns the refs removed, never raises.+ ``protect`` is image refs that must survive (slot images — for a compose+ slot a ``compose:d<N>`` marker — and the like). Best-effort: returns the+ refs removed, never raises."""from app.models.deployment import Deploymentfrom app.services.docker_service import DockerServicetry:- tags = _local_tags(app_id)- if not tags:+ refs = _local_refs(app_id)+ if not refs:return []- recent = [d.image_tag for d in Deployment.query.filter_by(app_id=app_id)- .filter(Deployment.image_tag.isnot(None))- .order_by(Deployment.id.desc()).limit(max(int(keep), 1)).all()]+ keep_ids = {d.id for d in Deployment.query.filter_by(app_id=app_id)+ .order_by(Deployment.id.desc()).limit(max(int(keep), 1)).all()}live = Deployment.get_current(app_id)- keep_refs = set(recent) | {r for r in protect if r}- if live and live.image_tag:- keep_refs.add(live.image_tag)+ if live:+ keep_ids.add(live.id)+ keep_ids.update(i for i in (_deployment_id(r) for r in protect) if i is not None)removed = []- for tag in tags:- if not _TAG.match(tag):+ for repo, tag in refs:+ match = _TAG.match(tag)+ if not match or int(match.group(1)) in keep_ids:continue # :latest and anything a human tagged stay- ref = f'{repository(app_id)}:{tag}'- if ref in keep_refs:- continue+ ref = f'{repo}:{tag}'if DockerService.remove_image(ref).get('success'):removed.append(ref)if removed:diff --git a/backend/app/services/deploy_settings.py b/backend/app/services/deploy_settings.pyindex 5bb11870..a700ae66 100644--- a/backend/app/services/deploy_settings.py+++ b/backend/app/services/deploy_settings.py@@ -28,6 +28,12 @@ DEFAULTS = {'release_command': None,'snapshot_databases': True,'stop_old_before_release': False,+ # Compose slots (§C), written by the panel, never by the settings API:+ # the web service, the port it published before slots, and whether the+ # stateful services have moved to the shared data project.+ 'compose_web': None,+ 'compose_web_port': None,+ 'compose_data_split': False,}# key -> (type, min, max). Anything outside is rejected, not clamped: a typo'd@@ -103,6 +109,15 @@ def validate(changes) -> tuple:return clean, None+def set_internal(app, key, value) -> None:+ """Record a panel-owned setting (no validation, no commit)."""+ if key not in DEFAULTS or key in _RULES:+ raise KeyError(key)+ current = stored(app)+ current[key] = value+ app.deploy_settings = json.dumps(current)++def update(app, changes) -> tuple:"""Merge validated ``changes`` into the app's stored settings (no commit)."""clean, error = validate(changes)diff --git a/backend/app/services/deployment_service.py b/backend/app/services/deployment_service.pyindex d5000c9b..38261983 100644--- a/backend/app/services/deployment_service.py+++ b/backend/app/services/deployment_service.py@@ -154,7 +154,9 @@ class DeploymentService:deploy_result = cls._deploy_compose(app, deployment, log_callback)deployment.build_completed_at = datetime.utcnow()if not deploy_result.get('success'):- return cls._fail(app, deployment, deploy_result.get('error') or 'Deploy failed')+ return cls._fail(app, deployment, deploy_result.get('error') or 'Deploy failed',+ status=deploy_result.get('deployment_status', 'failed'),+ still_serving=bool(deploy_result.get('still_serving')))else:# Step 1: Builddeployment.status = 'building'@@ -242,7 +244,7 @@ class DeploymentService:# One image per deploy is a disk leak unless something prunes it# (plan 85). Keeps the newest few for rollback.- if deployment.image_tag:+ if app.app_type == 'docker' and not app.server_id:from app.services import app_image_retention, deploy_settingsapp_image_retention.prune(app_id, keep=deploy_settings.get(app, 'keep_images'),protect=cls._protected_images(app))@@ -347,12 +349,37 @@ class DeploymentService:deployment.status = 'deploying'deployment.deploy_started_at = datetime.utcnow()db.session.commit()++ from app.services.slot_deploy_service import SlotDeployService, SlotDeployError+ if app.slot_deploys_enabled:+ # A/B slots (plan 87 §C): the project boots as <name>-<idle slot>+ # beside the live one instead of recreating it in place.+ verdict = SlotDeployService.eligibility(app)+ if not verdict['eligible']:+ return {'success': False, 'still_serving': True,+ 'error': 'Slot deploys are on but this app no longer qualifies: '+ + ' '.join(verdict['reasons'])}+ try:+ return SlotDeployService.deploy_compose(app, deployment, log=log_callback)+ except SlotDeployError as exc:+ return {'success': False, 'still_serving': True, 'error': str(exc)}++ # Slots switched off: fold the slot projects away; the in-place+ # project publishes its own port again.+ released_port = SlotDeployService.release_slots(app, log=log_callback)if log_callback:log_callback(f"Starting compose project {app.compose_file}...")result = DockerService.compose_up(app.root_path, detach=True, build=True,compose_file=app.compose_file)if not result.get('success'):return {'success': False, 'error': result.get('error') or 'docker compose up failed'}+ if released_port and released_port != app.port:+ app.port = released_port+ db.session.commit()+ if app.live_domains:+ from app.services.site_domain_service import SiteDomainService+ SiteDomainService.write_app_vhost(app)+ SlotDeployService._reapply_vhost_extras(app)return {'success': True}@classmethod@@ -576,7 +603,10 @@ class DeploymentService:if not target:return {'success': False, 'error': 'No previous deployment found to rollback to'}- if not target.image_tag and not target.commit_hash:+ # A compose slot app can roll back to its standby, which needs neither+ # an image tag nor a commit: the release is still there, stopped or warm.+ slot_compose = cls._uses_compose(app) and app.slot_deploys_enabled+ if not target.image_tag and not target.commit_hash and not slot_compose:return {'success': False,'error': 'Target deployment has no artifacts to rollback to'@@ -616,7 +646,11 @@ class DeploymentService:db.session.commit()# Deploy the previous version- if target.image_tag:+ if cls._uses_compose(app) and app.slot_deploys_enabled:+ from app.services.slot_deploy_service import SlotDeployService+ deploy_result = SlotDeployService.rollback_compose(+ app, target, rollback_deployment, log_callback)+ elif target.image_tag:# Docker rollbackdeploy_result = cls._deploy_docker(app, rollback_deployment, log_callback)else:@@ -673,15 +707,9 @@ class DeploymentService:app_path = app.root_pathtry:- if log_callback:- log_callback(f"Checking out commit {target.commit_hash[:8]}...")-- # Checkout the specific commit- result = run_checked(['git', '-C', app_path, 'checkout', target.commit_hash],- timeout=None)-+ result = cls._checkout(app, target.commit_hash, log_callback)if not result['success']:- return {'success': False, 'error': result['error']}+ return result# Run post-deploy script if configureddeploy_config = GitService.get_app_config(app.id)@@ -726,6 +754,16 @@ class DeploymentService:except Exception as e:return {'success': False, 'error': str(e)}+ @staticmethod+ def _checkout(app: Application, commit_hash: str,+ log_callback: Callable[[str], None] = None) -> Dict:+ if log_callback:+ log_callback(f"Checking out commit {commit_hash[:8]}...")+ result = run_checked(['git', '-C', app.root_path, 'checkout', commit_hash], timeout=None)+ if not result['success']:+ return {'success': False, 'error': result['error']}+ return {'success': True}+@classmethoddef _generate_diff(cls, deployment: Deployment) -> None:"""Generate diff between this deployment and the previous one."""diff --git a/backend/app/services/git_deploy_service.py b/backend/app/services/git_deploy_service.pyindex 70146ed5..6aeae092 100644--- a/backend/app/services/git_deploy_service.py+++ b/backend/app/services/git_deploy_service.py@@ -10,8 +10,6 @@ from typing import Dict, Optionalfrom app.utils.system import run_checkedfrom app.utils.git_security import git_argv, git_env, validate_ref_name-from app.services import health_gate-from app.services.health_gate import HealthGateErrorfrom app.services.deploy_lock import with_deploy_locklogger = logging.getLogger(__name__)@@ -116,10 +114,7 @@ class GitDeployService:# Step 3: Restart applicationlogs.append("\n=== Restarting application ===")- if webhook and webhook.zero_downtime:- restart_result = cls._zero_downtime_restart(app)- else:- restart_result = cls._standard_restart(app)+ restart_result = cls._standard_restart(app)deployment.deploy_output = '\n'.join(logs) + '\n' + restart_result.get('output', '')@@ -415,6 +410,16 @@ class GitDeployService:except Exception:pass+ if getattr(app, 'slot_deploys_enabled', False):+ # A slot app (plan 87 §C) never stops its live stack: the whole+ # deploy — preflight, slot boot, gate, switch, watch — runs through+ # the deploy pipeline, which records it as a Deployment too. The+ # deploy lock is already held by this thread.+ from app.services.deployment_service import DeploymentService+ result = DeploymentService._deploy_locked(app.id, None, False, 'webhook', None, _log)+ return {'success': bool(result.get('success')), 'output': '\n'.join(output),+ 'error': result.get('error')}+checks = preflight.preflight_compose_project(app.root_path, log=_log)if not checks.ok:# Nothing was stopped — the previous release is still serving.@@ -440,76 +445,6 @@ class GitDeployService:'error': up_result.get('error')}- @classmethod- def _zero_downtime_restart(cls, app) -> Dict:- """Zero-downtime restart using rolling update."""- from app.services.docker_service import DockerService-- output = []-- try:- # Build new images first- build_result = run_checked(['docker', 'compose', 'build'],- cwd=app.root_path, timeout=300)- output.append(f"Build: {build_result['output']}")-- if not build_result['success']:- return {- 'success': False,- 'error': build_result['error'],- 'output': '\n'.join(output)- }-- # Rolling update - start new containers before stopping old- up_result = run_checked(- ['docker', 'compose', 'up', '-d', '--no-deps', '--scale', 'web=2'],- cwd=app.root_path, timeout=120)- if not up_result['success']:- return {'success': False, 'error': up_result['error'],- 'output': '\n'.join(output)}-- # The new container must pass the health gate. A failure used to be- # logged and the rollout "succeeded" anyway (plan 87 §A3).- try:- output.append(cls._wait_for_health(app))- except HealthGateError as exc:- output.append(str(exc))- return {'success': False, 'error': str(exc), 'output': '\n'.join(output)}- finally:- # Scale back down either way: one live copy per app.- run_checked(- ['docker', 'compose', 'up', '-d', '--no-deps', '--scale', 'web=1'],- cwd=app.root_path, timeout=60)-- output.append("Rolling update completed")-- return {- 'success': True,- 'output': '\n'.join(output)- }-- except subprocess.TimeoutExpired:- return {'success': False, 'error': 'Restart timed out', 'output': '\n'.join(output)}- except Exception as e:- return {'success': False, 'error': str(e), 'output': '\n'.join(output)}-- @classmethod- def _wait_for_health(cls, app, fallback: int = 5, **overrides) -> str:- """Block until the app passes the health gate; raise if it never does.-- Probes ``healthcheck_path`` (default ``/``) on the app's port with the- app's gate settings: 2xx/3xx only (4xx only when allowed), several in- a row. An app with no port cannot be probed and keeps the old fixed- wait. Raises :class:`HealthGateError` on failure (plan 87 §A3).- """- import time-- if not getattr(app, 'port', None):- time.sleep(fallback)- return f"Waited {fallback}s (no port to health-check)"- result = health_gate.gate_for_app(app, **overrides)- return f"Health check passed ({result['url']} -> {result['status']})"-@classmethoddef _run_script(cls, script: str, cwd: str) -> Dict:"""Run a deployment script."""diff --git a/backend/app/services/slot_compose_service.py b/backend/app/services/slot_compose_service.pynew file mode 100644index 00000000..65ec36a0--- /dev/null+++ b/backend/app/services/slot_compose_service.py@@ -0,0 +1,296 @@+"""Slot deploys for compose apps (plan 87 §C).++Each slot is its own compose project, ``<name>-a`` / ``<name>-b``, rendered+from the app's merged compose config (``docker compose config`` over the base+file plus the ServerKit env override, so variables are interpolated and every+relative path is already absolute). The render:++* strips ``container_name`` — two slots cannot both own one name, and 119 of+ 120 templates set it;+* publishes only the web service, on the slot's loopback port;+* drops every other host publish (a second copy would collide);+* PINS every named volume to the name it has today. Without an explicit+ ``name:``, compose prefixes volumes with the project name, and slot B would+ silently start on empty volumes;+* leaves networks per-slot (a shared default network would give two services+ the same DNS name), and joins the shared ``<name>-data`` network.++A stack with stateful services (a database, a cache, a queue — anything but the+web service that mounts a named volume) cannot run twice. Those move ONCE, on+an explicit, previewed split, into a ``<name>-data`` project that both slots+reach over its network by the same service names as before.+"""+import copy+import logging+import os+import re+from typing import Dict, List, Optional, Tuple++import yaml++logger = logging.getLogger(__name__)++SLOT_DIR = '.serverkit-slots'+DATA = 'data'+++def project_base(app) -> str:+ """``<name>`` for the ``<name>-a`` / ``-b`` / ``-data`` projects."""+ base = re.sub(r'[^a-z0-9_-]+', '-', (app.name or f'app{app.id}').lower()).strip('-_')+ return base or f'app{app.id}'+++def slot_project(app, slot: str) -> str:+ return f'{project_base(app)}-{slot}'+++def data_network(app) -> str:+ return f'{project_base(app)}-data'+++def original_project(app) -> str:+ """The project name compose derives for the in-place deploy."""+ base = os.path.basename(os.path.normpath(app.root_path or ''))+ return re.sub(r'[^a-z0-9_-]', '', base.lower())+++def slot_file(app, slot: str) -> str:+ return os.path.join(app.root_path, SLOT_DIR, f'{slot}.yml')+++# ── reading the merged config ────────────────────────────────────────────────++def merged_config(app) -> Tuple[Optional[dict], Optional[str]]:+ """The app's compose config as compose resolves it, with the env override."""+ from app.services.docker_service import DockerService+ cmd = DockerService._compose_cmd_with_overlay(app.root_path, app.compose_file) + ['config']+ result = DockerService.run_compose(cmd, cwd=app.root_path, timeout=120)+ if not result.get('success'):+ return None, result.get('error') or 'docker compose config failed'+ try:+ config = yaml.safe_load(result.get('output') or '')+ except yaml.YAMLError as exc:+ return None, f'could not read the compose config: {exc}'+ if not isinstance(config, dict) or not isinstance(config.get('services'), dict):+ return None, 'the compose config has no services'+ return config, None+++def _published(port) -> Tuple[Optional[int], Optional[int]]:+ """``(host, container)`` for a long- or short-syntax port entry."""+ if isinstance(port, dict):+ published, target = port.get('published'), port.get('target')+ try:+ return (int(published) if published not in (None, '') else None,+ int(target) if target is not None else None)+ except (TypeError, ValueError):+ return None, None+ parts = str(port).split('/')[0].split(':')+ try:+ if len(parts) == 1:+ return None, int(parts[0])+ return int(parts[-2]), int(parts[-1])+ except ValueError:+ return None, None+++def web_service(config: dict, port: int) -> Tuple[Optional[str], Optional[int]]:+ """The service that publishes the app's port, and the port it listens on."""+ for name, service in (config.get('services') or {}).items():+ for entry in service.get('ports') or []:+ host, target = _published(entry)+ if host == port:+ return name, target+ return None, None+++def _named_volumes(service: dict) -> List[str]:+ names = []+ for mount in service.get('volumes') or []:+ if isinstance(mount, dict) and mount.get('type') == 'volume' and mount.get('source'):+ names.append(mount['source'])+ return names+++def classify(config: dict, web: str) -> Tuple[List[str], List[str]]:+ """``(stateless, stateful)`` service names. Stateful = mounts a named+ volume and is not the web service; the web service's volumes are shared+ between slots (and need the operator's word, like a single container)."""+ stateless, stateful = [], []+ for name, service in (config.get('services') or {}).items():+ if name != web and _named_volumes(service):+ stateful.append(name)+ else:+ stateless.append(name)+ return stateless, stateful+++# ── eligibility ──────────────────────────────────────────────────────────────++def eligibility(app, config: Optional[dict] = None) -> Dict:+ from app.services import deploy_settings+ reasons, warnings = [], []+ if config is None:+ config, error = merged_config(app)+ if config is None:+ return {'reasons': [f'Could not read the compose file: {error}'], 'warnings': [],+ 'split_needed': False}+ settings = deploy_settings.effective(app)+ # Once on slots, app.port is the live slot's port; the compose file still+ # publishes the port recorded at adoption.+ port = settings['compose_web_port'] or app.port+ web = settings['compose_web'] if settings['compose_web'] in config['services'] else None+ if not web:+ web, _target = web_service(config, port)+ if not web:+ return {'reasons': [f'No service in the compose file publishes port {port}, '+ 'so there is no web service to run twice.'],+ 'warnings': [], 'split_needed': False}+ _stateless, stateful = classify(config, web)+ split_needed = bool(stateful) and not settings['compose_data_split']+ if split_needed:+ reasons.append(f"The stack has stateful services ({', '.join(stateful)}) that cannot run "+ f'twice. They move once into a shared {data_network(app)} project first; '+ 'preview the split in Settings.')+ shared = _named_volumes(config['services'][web])+ if shared:+ names = ', '.join(shared)+ if settings['slot_volumes_confirmed']:+ warnings.append(f'Both slots of {web} mount the same volumes ({names}) for a few '+ 'seconds around each switch. You confirmed the app handles it.')+ else:+ reasons.append(f'Both slots of {web} would mount the same volumes ({names}) for a '+ 'few seconds around each switch; confirm the app handles it to enable.')+ return {'reasons': reasons, 'warnings': warnings, 'split_needed': split_needed,+ 'web': web, 'stateful': stateful}+++# ── rendering ────────────────────────────────────────────────────────────────++def _pin_volumes(config: dict, keep: Optional[set] = None) -> dict:+ volumes = {}+ for name, spec in (config.get('volumes') or {}).items():+ if keep is not None and name not in keep:+ continue+ spec = dict(spec or {})+ # compose config already emits the resolved name (<project>_<vol>);+ # keeping it verbatim is exactly the pin. Say so if it ever doesn't.+ if not spec.get('name') and not spec.get('external'):+ logger.warning('volume %s has no resolved name; slots would not share it', name)+ volumes[name] = spec+ return volumes+++def _used_volumes(services: dict) -> set:+ used = set()+ for service in services.values():+ used.update(_named_volumes(service))+ return used+++def render_slot(app, config: dict, web: str, host_port: int, container_port: int,+ image_tags: Dict[str, str], with_data_network: bool) -> dict:+ """The compose project for one slot (see the module docstring)."""+ _stateless, stateful = classify(config, web)+ split = with_data_network+ services = {}+ for name, service in (config.get('services') or {}).items():+ if split and name in stateful:+ continue+ service = copy.deepcopy(service)+ service.pop('container_name', None)+ if name == web:+ service['ports'] = [f'127.0.0.1:{host_port}:{container_port}']+ else:+ service.pop('ports', None)+ if name in image_tags:+ # `build` + `image`: compose builds under this immutable tag (a+ # cache hit after the preflight build), then `up --no-build` runs+ # exactly that image, and a rollback can find it again.+ service['image'] = image_tags[name]+ if split and isinstance(service.get('depends_on'), dict):+ service['depends_on'] = {k: v for k, v in service['depends_on'].items()+ if k not in stateful}+ if not service['depends_on']:+ service.pop('depends_on')+ if split and 'network_mode' not in service:+ networks = service.get('networks')+ if not isinstance(networks, dict):+ networks = {n: None for n in (networks or ['default'])}+ networks.setdefault('default', None)+ networks[data_network(app)] = None+ service['networks'] = networks+ services[name] = service+ rendered = {'services': services}+ volumes = _pin_volumes(config, keep=_used_volumes(services))+ if volumes:+ rendered['volumes'] = volumes+ networks = {}+ for name, spec in (config.get('networks') or {}).items():+ spec = dict(spec or {})+ if not spec.get('external'):+ spec.pop('name', None) # per-slot: never share a default network+ networks[name] = spec+ if split:+ networks[data_network(app)] = {'external': True, 'name': data_network(app)}+ if networks:+ rendered['networks'] = networks+ return rendered+++def render_data(app, config: dict, web: str) -> dict:+ """The shared ``<name>-data`` project: the stateful services, on a named+ network the slots join, with their volumes pinned to today's names."""+ _stateless, stateful = classify(config, web)+ services = {}+ for name in stateful:+ service = copy.deepcopy(config['services'][name])+ service.pop('container_name', None)+ service.pop('depends_on', None)+ services[name] = service+ rendered = {'services': services}+ volumes = _pin_volumes(config, keep=_used_volumes(services))+ if volumes:+ rendered['volumes'] = volumes+ rendered['networks'] = {'default': {'name': data_network(app)}}+ return rendered+++def write(path: str, rendered: dict) -> None:+ os.makedirs(os.path.dirname(path), exist_ok=True)+ with open(path, 'w', encoding='utf-8') as fh:+ fh.write('# Rendered by ServerKit for A/B slot deploys (plan 87). Do not edit:\n'+ '# it is regenerated from the app\'s compose file on every deploy.\n')+ yaml.safe_dump(rendered, fh, sort_keys=False, default_flow_style=False)+++# ── running slot projects ────────────────────────────────────────────────────++def compose(project: str, files: List[str], *args, cwd=None, timeout=None) -> Dict:+ from app.services.docker_service import DockerService+ cmd = DockerService._get_compose_cmd() + ['-p', project]+ for f in files:+ cmd += ['-f', f]+ return DockerService.run_compose(cmd + list(args), cwd=cwd, timeout=timeout)+++def project_containers(project: str, service: Optional[str] = None) -> List[Dict]:+ """Containers of a compose project by label (running or not)."""+ import json+ from app.services.docker_service import DockerService+ args = ['ps', '-a', '--filter', f'label=com.docker.compose.project={project}']+ if service:+ args += ['--filter', f'label=com.docker.compose.service={service}']+ result = DockerService.run(args + ['--format', '{{json .}}'], timeout=30)+ rows = []+ for line in (result.get('output') or '').splitlines() if result.get('success') else []:+ try:+ c = json.loads(line)+ except ValueError:+ continue+ labels = c.get('Labels') or ''+ svc = next((part.split('=', 1)[1] for part in labels.split(',')+ if part.startswith('com.docker.compose.service=')), None)+ rows.append({'id': c.get('ID'), 'name': c.get('Names'), 'service': svc or service,+ 'state': (c.get('State') or '').lower()})+ return rowsdiff --git a/backend/app/services/slot_deploy_service.py b/backend/app/services/slot_deploy_service.pyindex 6e381707..55ca7085 100644--- a/backend/app/services/slot_deploy_service.py+++ b/backend/app/services/slot_deploy_service.py@@ -25,6 +25,7 @@ existing ``serverkit-app-<id>`` container as slot ``a`` until slot ``a`` isnext deployed into."""import logging+import osimport timefrom datetime import datetime, timedeltafrom typing import Callable, Dict, Optional@@ -44,6 +45,7 @@ _now = datetime.utcnowWATCH_INTERVAL = 2.0WATCH_FAILURES = 3 # consecutive failed probes that trigger a revertREVERT_CHECK_TIMEOUT = 20 # the restored slot must answer within this+NGINX_PROBE_URL = 'http://127.0.0.1/' # host nginx, asked with the site's Host headerdef slot_container_name(app, slot: str) -> str:@@ -64,6 +66,47 @@ def live_container_name(app) -> str:return legacy_container_name(app)+def _compose_files(slot: AppSlot):+ """The file(s) a compose slot's project was brought up from."""+ from app.services import slot_compose_service as sc+ app = slot.application+ if slot.project_name == sc.original_project(app):+ return [os.path.join(app.root_path, app.compose_file)]+ return [sc.slot_file(app, slot.slot)]+++def _slot_start(slot: AppSlot) -> bool:+ from app.services.docker_service import DockerService+ if slot.project_name:+ from app.services import slot_compose_service as sc+ return bool(sc.compose(slot.project_name, _compose_files(slot), 'start',+ cwd=slot.application.root_path).get('success'))+ if not slot.container_name:+ return False+ return bool(DockerService.start_container(slot.container_name).get('success'))+++def _slot_stop(slot: AppSlot) -> None:+ from app.services.docker_service import DockerService+ if slot.project_name:+ from app.services import slot_compose_service as sc+ sc.compose(slot.project_name, _compose_files(slot), 'stop', cwd=slot.application.root_path)+ elif slot.container_name:+ DockerService.stop_container(slot.container_name)+++def _slot_remove(slot: AppSlot) -> None:+ """Remove a slot's containers. Never its volumes: those are shared."""+ from app.services.docker_service import DockerService+ if slot.project_name:+ from app.services import slot_compose_service as sc+ sc.compose(slot.project_name, _compose_files(slot), 'down', '--remove-orphans',+ cwd=slot.application.root_path)+ elif slot.container_name:+ DockerService.stop_container(slot.container_name)+ DockerService.remove_container(slot.container_name)++def _restorable(app):from app.services import release_phase_servicereturn release_phase_service.restorable(app)@@ -122,8 +165,15 @@ class SlotDeployService:reasons.append('Not published on a domain or private URL. Slots switch traffic ''in nginx, and an app reached on its raw port would lose it.')- if compose:- reasons.append('A compose app. Slot deploys cover single-container apps so far.')+ split_needed = False+ if compose and not reasons:+ from app.services import slot_compose_service+ verdict = slot_compose_service.eligibility(app)+ reasons.extend(verdict['reasons'])+ warnings.extend(verdict['warnings'])+ split_needed = verdict.get('split_needed', False)+ elif compose:+ passelif app.volumes:settings = deploy_settings.effective(app)names = ', '.join(sorted(v.name for v in app.volumes))@@ -134,7 +184,8 @@ class SlotDeployService:reasons.append(f'Both slots would mount the same volumes ({names}) for a few ''seconds around each switch. Something like SQLite breaks when ''two processes write to it; confirm the app handles it to enable.')- return {'eligible': not reasons, 'mode': mode, 'reasons': reasons, 'warnings': warnings}+ return {'eligible': not reasons, 'mode': mode, 'reasons': reasons, 'warnings': warnings,+ 'split_needed': split_needed}# ------------------------------------------------------------------ ## Slot rows@@ -158,10 +209,13 @@ class SlotDeployService:from app.models.deployment import Deploymentfrom app.services.docker_service import DockerService+ from app.services.deployment_service import DeploymentServicerows = cls.slots(app)if app.active_slot in SLOTS:db.session.commit()return rows+ if DeploymentService._uses_compose(app):+ return cls._adopt_compose(app, rows)a = rows['a']a.host_port = app.porta.container_port = app.port@@ -180,6 +234,37 @@ class SlotDeployService:db.session.commit()return rows+ @classmethod+ def _adopt_compose(cls, app, rows) -> Dict[str, AppSlot]:+ """The compose project running today becomes slot a, untouched."""+ from app.models.deployment import Deployment+ from app.services import slot_compose_service as sc+ config, error = sc.merged_config(app)+ if config is None:+ raise SlotDeployError(f'Could not read the compose file: {error}')+ web, target = sc.web_service(config, app.port)+ if not web:+ raise SlotDeployError(f'No service publishes port {app.port}.')+ deploy_settings.set_internal(app, 'compose_web', web)+ deploy_settings.set_internal(app, 'compose_web_port', app.port)+ a = rows['a']+ a.project_name = sc.original_project(app)+ a.host_port = app.port+ a.container_port = target+ found = sc.project_containers(a.project_name, web)+ a.container_name = found[0]['name'] if found else None+ running = bool(found and found[0]['state'] == 'running')+ a.state = 'live' if running else ('stopped' if found else 'empty')+ a.started_at = _now() if running else None+ current = Deployment.get_current(app.id)+ if current:+ a.deployment_id = current.id+ a.commit_sha = current.commit_hash+ a.image_ref = f'compose:d{current.id}' if current else None+ app.active_slot = 'a'+ db.session.commit()+ return rows+@classmethoddef status(cls, app) -> Dict:rows = {row.slot: row for row in AppSlot.query.filter_by(application_id=app.id).all()}@@ -200,12 +285,19 @@ class SlotDeployService:if not verdict['eligible']:return {'success': False, 'error': verdict['reasons'][0],'eligibility': verdict}- from app.services.deployment_service import DeploymentServiceapp.slot_deploys_enabled = Truedb.session.commit()- if not DeploymentService._uses_compose(app):+ try:cls.adopt(app)+ except SlotDeployError as exc:+ app.slot_deploys_enabled = False+ db.session.commit()+ return {'success': False, 'error': str(exc)}return {'success': True, 'slots': cls.status(app)}+ if deploy_settings.get(app, 'compose_data_split'):+ return {'success': False,+ 'error': 'This app\'s stateful services live in a shared data project now; '+ 'it keeps deploying through slots.'}# Opting out keeps the slots until the next deploy, which goes back to# the in-place path and folds them away (release_slots).app.slot_deploys_enabled = False@@ -312,7 +404,7 @@ class SlotDeployService:_log(log, f'Stopping slot {live.slot.upper()} before the release command, as this ''app is set to: the site is down until the new release passes its ''health check.')- DockerService.stop_container(live.container_name)+ _slot_stop(live)old_stopped = Trueran = release.run_release(app, image, env, volumes, command, log)deployment.update_metadata('release_ran', True)@@ -363,9 +455,19 @@ class SlotDeployService:f'The site is still serving {version}.')connect_shared_network(app, name)+ failed = cls._bring_live(app, live, idle, name, reuse, old_stopped, version, log, stage)+ if failed:+ return failed+ workers = WorkerProcessService.deploy(app, image, env, volumes, log=log)+ return {'success': True, 'slot': idle.slot, 'workers': workers, 'container_id': name}++ @classmethod+ def _bring_live(cls, app, live, idle, container, reuse, old_stopped, version, log, stage):+ """Gate the booted idle slot, switch to it, watch it, retire the old+ one to standby. None on success, else the failure result."""stage('Health gate')try:- result = health_gate.gate_for_app(app, port=idle.host_port, container=name,+ result = health_gate.gate_for_app(app, port=idle.host_port, container=container,log=lambda line: _log(log, line))idle.last_health = f"{result['status']} on {result['url']}"[:255]except HealthGateError as exc:@@ -395,17 +497,254 @@ class SlotDeployService:else 'switching back FAILED — the site may be down.'))}cls._make_standby(app, live, log)- workers = WorkerProcessService.deploy(app, image, env, volumes, log=log)- return {'success': True, 'slot': idle.slot, 'workers': workers, 'container_id': name}+ return None++ # ------------------------------------------------------------------ #+ # Deploy (compose, §C)+ # ------------------------------------------------------------------ #+ @classmethod+ def deploy_compose(cls, app, deployment, log=None, stage=None, reuse_standby=False) -> Dict:+ """The compose project booted as ``<name>-<idle slot>`` beside the live+ one, then the same gate / switch / watch / standby as a container.++ The caller has already run the compose preflight (validate, build,+ pull) against the app's own compose file while the live slot served.+ """+ from app.services import deploy_stages, release_phase_service as release+ from app.services import slot_compose_service as sc++ stage = stage or deploy_stages.mark+ rows = cls.adopt(app) # SlotDeployError: nothing was touched yet+ live = rows[app.active_slot]+ idle = rows[other(app.active_slot)]+ version = f'v{live.deployment.version}' if live.deployment else 'the previous release'++ reuse = bool(reuse_standby and idle.state in ('standby', 'stopped') and idle.project_name+ and sc.project_containers(idle.project_name))+ if not reuse:+ config, error = sc.merged_config(app)+ if config is None:+ return cls._abort(app, live, False, f'Could not read the compose file: {error}. '+ f'The site is still serving {version}.')+ web = deploy_settings.get(app, 'compose_web')+ if web not in (config.get('services') or {}):+ return cls._abort(app, live, False,+ f'The compose file no longer has the web service {web!r}. '+ f'The site is still serving {version}.')+ split = bool(deploy_settings.get(app, 'compose_data_split'))+ if not idle.host_port or idle.host_port == live.host_port:+ idle.host_port = cls._pick_port(app, exclude={live.host_port, app.port})+ container_port = live.container_port+ tags = {name: f'serverkit-app-{app.id}-{name}:d{deployment.id}'+ for name, service in config['services'].items()+ if service.get('build') and not (split and name in sc.classify(config, web)[1])}+ rendered = sc.render_slot(app, config, web, idle.host_port, container_port, tags,+ with_data_network=split)+ project = sc.slot_project(app, idle.slot)+ path = sc.slot_file(app, idle.slot)+ sc.write(path, rendered)+ if tags:+ stage('Build')+ built = sc.compose(project, [path], 'build', cwd=app.root_path)+ if not built.get('success'):+ return cls._abort(app, live, False,+ f"Building the slot images failed: {built.get('error')}. "+ f'The site is still serving {version}.')++ # ---- snapshot + release, once, before the switch+ stage('Snapshot')+ release.snapshot_databases(app, deployment, log)+ stage('Release')+ old_stopped = False+ command, source = release.resolve_release_command(app)+ if command and (reuse or getattr(deployment, 'deploy_trigger', None) == 'rollback'):+ _log(log, f'Rollback: not running the release command ({source}).')+ command = None+ if command:+ if deploy_settings.get(app, 'stop_old_before_release'):+ _log(log, f'Stopping slot {live.slot.upper()} before the release command, as this '+ 'app is set to: the site is down until the new release passes its '+ 'health check.')+ _slot_stop(live)+ old_stopped = True+ _log(log, f'Running release command in a one-off {web} container: {command}')+ ran = sc.compose(project, [path], 'run', '--rm', '--no-deps', web, 'sh', '-c', command,+ cwd=app.root_path, timeout=release.RELEASE_TIMEOUT)+ for line in (ran.get('output') or '').splitlines()[-40:]:+ _log(log, line)+ deployment.update_metadata('release_ran', True)+ db.session.commit()+ if not ran.get('success'):+ return cls._abort(app, live, old_stopped,+ f"The release command ({source}) failed: {ran.get('error')}. "+ f'The new release was not started; the site is still serving {version}.')++ stage(f'Boot slot {idle.slot.upper()}')+ if reuse:+ _log(log, f'Slot {idle.slot.upper()} still holds this release; starting it again...')+ if not _slot_start(idle):+ return cls._abort(app, live, old_stopped,+ f'The standby in slot {idle.slot.upper()} would not start. '+ f'The site is still serving {version}.')+ else:+ # Whatever the idle slot ran before (an older release, or the+ # adopted in-place project) makes way; the live slot is untouched.+ if idle.project_name and idle.project_name != live.project_name:+ _slot_remove(idle)+ _log(log, f'Booting slot {idle.slot.upper()} ({project}) on 127.0.0.1:{idle.host_port} '+ f'while slot {live.slot.upper()} keeps serving...')+ up = sc.compose(project, [path], 'up', '-d', '--no-build', '--remove-orphans',+ cwd=app.root_path)+ idle.project_name = project+ idle.container_port = container_port+ if not up.get('success'):+ idle.state = 'failed'+ idle.last_health = (up.get('error') or 'did not start')[:255]+ db.session.commit()+ _slot_remove(idle)+ return cls._abort(app, live, old_stopped,+ f"The new release did not start: {up.get('error')}. "+ f'The site is still serving {version}.')+ web_rows = sc.project_containers(project, web)+ idle.container_name = web_rows[0]['name'] if web_rows else None+ idle.image_ref = f'compose:d{deployment.id}'+ idle.deployment_id = deployment.id+ idle.commit_sha = getattr(deployment, 'commit_hash', None)+ idle.state = 'booting'+ idle.standby_until = None+ db.session.commit()++ failed = cls._bring_live(app, live, idle, idle.container_name, reuse, old_stopped,+ version, log, stage)+ if failed:+ return failed+ return {'success': True, 'slot': idle.slot, 'container_id': idle.container_name}++ @classmethod+ def rollback_compose(cls, app, target, rollback_deployment, log=None) -> Dict:+ """Roll a compose slot app back: the warm/stopped standby when it holds+ ``target``, otherwise a slot deploy of ``target``'s commit."""+ from app.services.deployment_service import DeploymentService+ idle = AppSlot.query.filter_by(application_id=app.id,+ slot=other(app.active_slot or 'a')).first()+ if idle is not None and idle.deployment_id == target.id:+ return cls.deploy_compose(app, rollback_deployment, log=log, reuse_standby=True)+ if not target.commit_hash:+ return {'success': False, 'still_serving': True,+ 'error': f'v{target.version} is not in the standby and has no commit to '+ 'rebuild from; only the standby can be switched back to.'}+ checkout = DeploymentService._checkout(app, target.commit_hash, log)+ if not checkout.get('success'):+ return {'success': False, 'still_serving': True, 'error': checkout.get('error')}+ from app.services import deploy_preflight_service as preflight+ checks = preflight.preflight_compose_project(app.root_path, app.compose_file, log=log)+ if not checks.ok:+ return {'success': False, 'still_serving': True, 'error': checks.error}+ return cls.deploy_compose(app, rollback_deployment, log=log)++ # ------------------------------------------------------------------ #+ # The one-time data split (§C)+ # ------------------------------------------------------------------ #+ @classmethod+ def compose_split_preview(cls, app) -> Dict:+ from app.services import slot_compose_service as sc+ import yaml+ config, error = sc.merged_config(app)+ if config is None:+ return {'success': False, 'error': error}+ web, target = sc.web_service(config, app.port)+ if not web:+ return {'success': False, 'error': f'No service publishes port {app.port}.'}+ _stateless, stateful = sc.classify(config, web)+ if not stateful:+ return {'success': False, 'error': 'This stack has no stateful services to move.'}+ data = sc.render_data(app, config, web)+ slot = sc.render_slot(app, config, web, 0, target, {}, with_data_network=True)+ return {'success': True, 'web': web, 'stateful': stateful,+ 'data_project': sc.slot_project(app, sc.DATA),+ 'data_compose': yaml.safe_dump(data, sort_keys=False),+ 'slot_compose': yaml.safe_dump(slot, sort_keys=False).replace(+ '127.0.0.1:0:', '127.0.0.1:<slot port>:'),+ 'downtime': ('The app is stopped while its stateful services move to the shared '+ 'data project on the same volumes, then starts again as slot A. '+ 'Expect a short outage, once.')}++ @classmethod+ def compose_split_apply(cls, app, log=None) -> Dict:+ """Move the stateful services into ``<name>-data`` and restart the+ stateless ones as slot a. One announced outage; data stays on the same+ volumes (pinned names)."""+ from app.models.deployment import Deployment+ from app.services import slot_compose_service as sc+ from app.services.deploy_lock import deploy_lock+ from app.services.docker_service import DockerService++ with deploy_lock(app.id, 'compose split', log=log):+ preview = cls.compose_split_preview(app)+ if not preview.get('success'):+ return preview+ config, _ = sc.merged_config(app)+ web, target = sc.web_service(config, app.port)+ base_port = app.port+ data_project = sc.slot_project(app, sc.DATA)+ data_path = sc.slot_file(app, sc.DATA)+ sc.write(data_path, sc.render_data(app, config, web))+ original = sc.original_project(app)+ original_files = [os.path.join(app.root_path, app.compose_file)]++ _log(log, f'Stopping {original} (announced outage)...')+ sc.compose(original, original_files, 'down', '--remove-orphans', cwd=app.root_path)+ DockerService.ensure_network(sc.data_network(app))+ _log(log, f"Starting {data_project}: {', '.join(preview['stateful'])}...")+ up = sc.compose(data_project, [data_path], 'up', '-d', cwd=app.root_path)+ if not up.get('success'):+ DockerService.compose_up(app.root_path, detach=True, compose_file=app.compose_file)+ return {'success': False,+ 'error': f"Starting the data project failed: {up.get('error')}. "+ 'The original stack was started again.'}++ rows = cls.slots(app)+ a = rows['a']+ a.host_port = base_port+ a.container_port = target+ project = sc.slot_project(app, 'a')+ path = sc.slot_file(app, 'a')+ sc.write(path, sc.render_slot(app, config, web, base_port, target, {},+ with_data_network=True))+ _log(log, f'Starting {project} as slot A...')+ up = sc.compose(project, [path], 'up', '-d', '--remove-orphans', cwd=app.root_path)+ if not up.get('success'):+ sc.compose(data_project, [data_path], 'down', cwd=app.root_path)+ DockerService.compose_up(app.root_path, detach=True, compose_file=app.compose_file)+ return {'success': False,+ 'error': f"Starting slot A failed: {up.get('error')}. "+ 'The original stack was started again.'}+ found = sc.project_containers(project, web)+ a.project_name = project+ a.container_name = found[0]['name'] if found else None+ current = Deployment.get_current(app.id)+ a.deployment_id = current.id if current else None+ a.image_ref = f'compose:d{current.id}' if current else None+ a.state = 'live'+ a.started_at = _now()+ app.active_slot = 'a'+ app.slot_deploys_enabled = True+ app.container_id = a.container_name+ deploy_settings.set_internal(app, 'compose_data_split', True)+ deploy_settings.set_internal(app, 'compose_web', web)+ deploy_settings.set_internal(app, 'compose_web_port', base_port)+ db.session.commit()+ from app.services import container_status_service+ container_status_service.invalidate(app.id)+ return {'success': True, 'slots': cls.status(app)}@classmethoddef _abort(cls, app, live: AppSlot, old_stopped: bool, message: str, log=None) -> Dict:"""End a deploy before the switch. The live slot kept serving — unlessthe app stops it for its release, in which case it is started again."""- from app.services.docker_service import DockerServiceserving = Trueif old_stopped:- serving = bool(DockerService.start_container(live.container_name).get('success'))+ serving = _slot_start(live)if not serving:message += ' Starting the previous release again FAILED — the site is down.'_log(log, message)@@ -498,7 +837,7 @@ class SlotDeployService:# Through nginx with the site's Host header: a 502/504 here means# the switch routed traffic somewhere that does not answer. A 3xx# (the HTTPS redirect) or 4xx is nginx and the app talking.- via, error = health_gate.probe('http://127.0.0.1/', host_header=host)+ via, error = health_gate.probe(NGINX_PROBE_URL, host_header=host)if via is None or via >= 500:return False, f'{host} via nginx -> {via or error}'return True, status@@ -513,10 +852,9 @@ class SlotDeployService:def _revert(cls, app, bad: AppSlot, good: AppSlot, reason: str, log=None,remove: bool = True) -> bool:"""Switch back to ``good`` and confirm it answers. True when it does."""- from app.services.docker_service import DockerService_log(log, f'Release failed after the switch ({reason}); switching back to slot 'f'{good.slot.upper()}...')- DockerService.start_container(good.container_name)+ _slot_start(good)switched = cls._switch(app, good, bad, log)answered = Falseif switched['success']:@@ -541,11 +879,10 @@ class SlotDeployService:def _discard(cls, slot: AppSlot, reason: str, remove: bool = True):"""Take a failed slot out of the way. It never served traffic, soremoving it loses nothing; a reused standby is only stopped."""- from app.services.docker_service import DockerService- if slot.container_name:- DockerService.stop_container(slot.container_name)- if remove:- DockerService.remove_container(slot.container_name)+ if remove:+ _slot_remove(slot)+ else:+ _slot_stop(slot)slot.state = 'failed'slot.last_health = (reason or '')[:255]db.session.commit()@@ -564,9 +901,7 @@ class SlotDeployService:@staticmethoddef _stop_standby(slot: AppSlot):- from app.services.docker_service import DockerService- if slot.container_name:- DockerService.stop_container(slot.container_name)+ _slot_stop(slot)slot.state = 'stopped'slot.standby_until = Nonedb.session.commit()@@ -650,6 +985,17 @@ class SlotDeployService:if not rows:return Nonecontainer_port = next((r.container_port for r in rows if r.container_port), None)+ if any(r.project_name for r in rows):+ from app.services import slot_compose_service as sc+ original = sc.original_project(app)+ for row in rows:+ if row.project_name and row.project_name != original:+ _log(log, f'Removing slot project {row.project_name}...')+ _slot_remove(row)+ db.session.delete(row)+ app.active_slot = None+ db.session.commit()+ return deploy_settings.get(app, 'compose_web_port')for row in rows:if row.container_name and row.container_name != legacy_container_name(app):if DockerService.get_container(row.container_name):diff --git a/backend/app/services/template_service.py b/backend/app/services/template_service.pyindex 1aa3a00e..c82d9123 100644--- a/backend/app/services/template_service.py+++ b/backend/app/services/template_service.py@@ -2253,6 +2253,49 @@ class TemplateService:'update_available': latest_version != installed_version}+ @classmethod+ def _update_slot_app(cls, app, template, variables, install_info, info_path,+ compose_path, backup_path, compose_content, config, user_id,+ log_callback):+ """Template update for a slot app (plan 87 §C): write the new compose,+ then deploy it into the idle slot. The live slot keeps serving until+ the new one passes its gate; any failure puts the old files back."""+ from app.services.deployment_service import DeploymentService++ with open(compose_path, 'w') as f:+ f.write(compose_content)+ if 'files' in template:+ files_result = cls._process_template_files(+ template['files'], app.root_path, compose_path, variables)+ if not files_result.get('success'):+ if os.path.exists(backup_path):+ shutil.copy(backup_path, compose_path)+ return files_result++ result = DeploymentService._deploy_locked(app.id, user_id, False, 'template_update',+ f"template-{template.get('version')}",+ log_callback)+ if not result.get('success'):+ # The live slot never stopped; the files on disk must match it again.+ if os.path.exists(backup_path):+ shutil.copy(backup_path, compose_path)+ return result++ install_info['template_version'] = template.get('version')+ install_info['updated_at'] = datetime.now().isoformat()+ install_info['variables'] = variables+ with open(info_path, 'w') as f:+ json.dump(install_info, f, indent=2)+ if 'scripts' in template and 'post_update' in template['scripts']:+ cls._run_script(template['scripts']['post_update'], app.root_path, variables)+ config['installed'][str(app.id)]['template_version'] = template.get('version')+ config['installed'][str(app.id)]['updated_at'] = datetime.now().isoformat()+ cls.save_config(config)+ if os.path.exists(backup_path):+ os.remove(backup_path)+ return {'success': True, 'version': template.get('version'), 'app_id': app.id,+ 'slot': result.get('deployment', {}).get('id')}+@classmethod@with_deploy_lock('template update')def update_app(cls, app_id: int, user_id: int = None,@@ -2357,6 +2400,11 @@ class TemplateService:return {'success': False, 'error': checks.error,'preflight': checks.to_dict()}+ if app.slot_deploys_enabled:+ return cls._update_slot_app(app, template, variables, install_info,+ info_path, compose_path, backup_path,+ compose_content, config, user_id, log_callback)+# ---- switchover: past this line the live stack is down ---------DockerService.compose_down(app_path)diff --git a/backend/app/services/webhook_service.py b/backend/app/services/webhook_service.pyindex a41d46b4..254c3e52 100644--- a/backend/app/services/webhook_service.py+++ b/backend/app/services/webhook_service.py@@ -42,8 +42,7 @@ class WebhookService:source_branch: str = 'main', local_repo_name: str = None,sync_direction: str = 'pull', auto_sync: bool = True,app_id: int = None, deploy_on_push: bool = False,- pre_deploy_script: str = None, post_deploy_script: str = None,- zero_downtime: bool = False) -> Dict:+ pre_deploy_script: str = None, post_deploy_script: str = None) -> Dict:"""Create a new webhook configuration."""from app import dbfrom app.models import GitWebhook, Application@@ -85,7 +84,6 @@ class WebhookService:deploy_on_push=deploy_on_push,pre_deploy_script=pre_deploy_script,post_deploy_script=post_deploy_script,- zero_downtime=zero_downtime)db.session.add(webhook)@@ -136,8 +134,6 @@ class WebhookService:webhook.pre_deploy_script = data['preDeployScript'] if data['preDeployScript'] else Noneif 'postDeployScript' in data:webhook.post_deploy_script = data['postDeployScript'] if data['postDeployScript'] else None- if 'zeroDowntime' in data:- webhook.zero_downtime = data['zeroDowntime']db.session.commit()diff --git a/backend/migrations/versions/108_drop_webhook_zero_downtime.py b/backend/migrations/versions/108_drop_webhook_zero_downtime.pynew file mode 100644index 00000000..5a2026fc--- /dev/null+++ b/backend/migrations/versions/108_drop_webhook_zero_downtime.py@@ -0,0 +1,28 @@+"""Drop git_webhooks.zero_downtime (plan 87 §C).++The flag switched webhook deploys to a `--scale web=2` rolling restart that+assumed a service called `web`, ignored its own health result and always+reported success. No UI ever set it. Slot deploys replace it.+"""+import sqlalchemy as sa+from alembic import op++revision = '108_drop_webhook_zero_downtime'+down_revision = '107_app_slots'+branch_labels = None+depends_on = None+++def _columns():+ return {c['name'] for c in sa.inspect(op.get_bind()).get_columns('git_webhooks')}+++def upgrade():+ if 'zero_downtime' in _columns():+ with op.batch_alter_table('git_webhooks') as batch:+ batch.drop_column('zero_downtime')+++def downgrade():+ if 'zero_downtime' not in _columns():+ op.add_column('git_webhooks', sa.Column('zero_downtime', sa.Boolean, nullable=True))diff --git a/backend/tests/test_manifest_persistence.py b/backend/tests/test_manifest_persistence.pyindex 6ea1a5f6..3b3bf8e8 100644--- a/backend/tests/test_manifest_persistence.py+++ b/backend/tests/test_manifest_persistence.py@@ -98,25 +98,3 @@ def test_apply_import_stores_manifest_when_project_and_v1(client, auth_headers,stored = ApplicationManifest.query.filter_by(project_id=proj.id).first()assert stored is not Noneassert stored.get_normalized()['services'][0]['name'] == 'api'---def test_health_wait_falls_back_without_path(app):- from app.models import Application- a = Application(name='x', app_type='docker', user_id=_owner().id if _owner() else 1)- a.healthcheck_path = None- a.port = None- msg = GitDeployService._wait_for_health(a, timeout=1, fallback=0)- assert 'no port to health-check' in msg---def test_health_wait_times_out_when_unreachable(app):- # Plan 87 §A3: the wait is a gate now — it raises instead of returning a- # "did not pass" line the deploy then ignored.- import pytest- from app.models import Application- from app.services.health_gate import HealthGateError- a = Application(name='x', app_type='docker', user_id=1)- a.healthcheck_path = '/health'- a.port = 1 # nothing listening- with pytest.raises(HealthGateError, match='did not pass'):- GitDeployService._wait_for_health(a, timeout=1)diff --git a/backend/tests/test_plan87_compose_slots.py b/backend/tests/test_plan87_compose_slots.pynew file mode 100644index 00000000..2703485d--- /dev/null+++ b/backend/tests/test_plan87_compose_slots.py@@ -0,0 +1,350 @@+"""Plan 87 §C — slot deploys for compose apps.++Two layers. The renderer is pure and is tested against the exact shape+``docker compose config`` prints (captured from compose v5). The engine runs+against the same fake world as the container slots, extended with compose+projects, and asserts the site served the whole time.+"""+import os++import pytest+import yaml++from factories import make_application++from app import db+from app.models.app_slot import AppSlot+from app.models.deployment import Deployment+from app.models.domain import Domain+from app.services import deploy_preflight_service as preflight+from app.services import deploy_settings+from app.services import slot_compose_service as sc+from app.services.deployment_service import DeploymentService+from app.services.docker_service import DockerService+from app.services.slot_deploy_service import SlotDeployService++from test_plan87_slot_deploys import World, world # noqa: F401 - the shared fake world fixture++CONFIG = yaml.safe_load("""+name: shop+services:+ db:+ container_name: shop-db+ image: postgres:16+ networks: {default: null}+ volumes:+ - {type: volume, source: pgdata, target: /var/lib/postgresql/data, volume: {}}+ web:+ build: {context: /srv/shop, dockerfile: Dockerfile}+ container_name: shop+ depends_on:+ db: {condition: service_started, required: true}+ environment: {DB: postgres://db/x}+ networks: {default: null}+ ports:+ - {mode: ingress, target: 3000, published: "8100", protocol: tcp}+ volumes:+ - {type: bind, source: /srv/shop/conf, target: /conf, bind: {}}+ worker:+ image: shop-worker:latest+ container_name: shop-worker+ networks: {default: null}+ ports:+ - {mode: ingress, target: 9000, published: "9000", protocol: tcp}+networks:+ default: {name: shop_default}+volumes:+ pgdata: {name: shop_pgdata}+""")++STATELESS = yaml.safe_load("""+name: shop+services:+ web:+ image: shop:latest+ container_name: shop+ networks: {default: null}+ ports:+ - {mode: ingress, target: 3000, published: "8100", protocol: tcp}+ volumes:+ - {type: volume, source: uploads, target: /app/uploads, volume: {}}+ cache:+ image: memcached:1.6+ container_name: shop-cache+ networks: {default: null}+networks:+ default: {name: shop_default}+volumes:+ uploads: {name: shop_uploads}+""")+++class _App:+ id = 7+ name = 'Shop'+ root_path = '/srv/shop'+ compose_file = 'docker-compose.yml'+ port = 8100+++# ── the renderer ─────────────────────────────────────────────────────────────++def test_the_web_service_is_the_one_publishing_the_apps_port():+ assert sc.web_service(CONFIG, 8100) == ('web', 3000)+ assert sc.web_service(CONFIG, 1234) == (None, None)+++def test_stateful_means_a_named_volume_on_anything_but_web():+ assert sc.classify(CONFIG, 'web') == (['web', 'worker'], ['db'])+ assert sc.classify(STATELESS, 'web') == (['web', 'cache'], [])+++def test_a_slot_render_strips_names_publishes_only_web_and_pins_volumes():+ rendered = sc.render_slot(_App, STATELESS, 'web', 9401, 3000, {}, with_data_network=False)+ services = rendered['services']+ assert all('container_name' not in s for s in services.values())+ assert services['web']['ports'] == ['127.0.0.1:9401:3000']+ assert 'ports' not in services['cache']+ # Pinned: without this, project shop-b would get an empty shop-b_uploads.+ assert rendered['volumes'] == {'uploads': {'name': 'shop_uploads'}}+ # Per-slot network: two slots never share one DNS namespace.+ assert rendered['networks'] == {'default': {}}+++def test_a_split_render_leaves_the_stateful_services_to_the_data_project():+ tags = {'web': 'serverkit-app-7-web:d12'}+ rendered = sc.render_slot(_App, CONFIG, 'web', 9401, 3000, tags, with_data_network=True)+ services = rendered['services']+ assert set(services) == {'web', 'worker'}+ assert 'depends_on' not in services['web']+ assert services['web']['image'] == 'serverkit-app-7-web:d12'+ assert services['web']['build']['context'] == '/srv/shop', 'built under the immutable tag'+ assert services['web']['networks'] == {'default': None, 'shop-data': None}+ assert rendered['networks']['shop-data'] == {'external': True, 'name': 'shop-data'}+ assert 'volumes' not in rendered, 'pgdata belongs to the data project now'+++def test_the_data_project_keeps_the_same_volume_and_a_named_network():+ rendered = sc.render_data(_App, CONFIG, 'web')+ assert list(rendered['services']) == ['db']+ assert 'container_name' not in rendered['services']['db']+ assert rendered['volumes'] == {'pgdata': {'name': 'shop_pgdata'}}+ assert rendered['networks'] == {'default': {'name': 'shop-data'}}+++def test_project_names():+ assert sc.slot_project(_App, 'a') == 'shop-a'+ assert sc.original_project(_App) == 'shop'+ assert sc.data_network(_App) == 'shop-data'+++# ── the engine, against a fake compose ──────────────────────────────────────++def compose_world(world, monkeypatch, tmp_path): # noqa: F811 - pytest fixture injection+ w = world+ w.config = STATELESS+ w.compose_calls = []+ w.up_fails = False++ def compose(project, files, *args, cwd=None, timeout=None):+ verb = args[0]+ w.compose_calls.append((project, verb))+ members = [n for n in w.containers if n.startswith(f'{project}-')]+ if verb == 'up':+ if w.up_fails:+ return {'success': False, 'error': 'port is already allocated'}+ with open(files[0], encoding='utf-8') as fh:+ rendered = yaml.safe_load(fh.read().split('\n', 2)[2])+ for name, service in rendered['services'].items():+ port = None+ if service.get('ports'):+ port = int(str(service['ports'][0]).split(':')[1])+ w.containers[f'{project}-{name}-1'] = {+ 'image': f'{project}:{name}', 'port': port, 'running': True}+ w.note(f'up {project}')+ elif verb in ('stop', 'start'):+ for n in members:+ w.containers[n]['running'] = verb == 'start'+ w.note(f'{verb} {project}')+ elif verb == 'down':+ for n in members:+ w.containers.pop(n)+ w.note(f'down {project}')+ elif verb == 'restart':+ w.note(f'restart {project}')+ return {'success': True, 'output': ''}++ def project_containers(project, service=None):+ rows = []+ for name, c in w.containers.items():+ if not name.startswith(f'{project}-'):+ continue+ svc = name[len(project) + 1:].rsplit('-', 1)[0]+ if service and svc != service:+ continue+ rows.append({'id': name, 'name': name, 'service': svc,+ 'state': 'running' if c['running'] else 'exited'})+ return rows++ monkeypatch.setattr(sc, 'compose', compose)+ monkeypatch.setattr(sc, 'project_containers', project_containers)+ monkeypatch.setattr(sc, 'merged_config', lambda app: (w.config, None))+ monkeypatch.setattr(DockerService, 'ensure_network', classmethod(lambda cls, name: {'success': True}))+ monkeypatch.setattr(preflight, 'preflight_compose_project',+ lambda *a, **k: preflight.PreflightResult())+ monkeypatch.setattr(DockerService, 'compose_up', classmethod(+ lambda cls, path, **k: (w.note('IN-PLACE up'), {'success': True})[1]))+ monkeypatch.setattr(DockerService, 'compose_down', classmethod(+ lambda cls, path, **k: (w.note('IN-PLACE down'), {'success': True})[1]))+ w.root = tmp_path+ return w+++def _compose_app(w, **kw):+ fields = dict(name='shop', app_type='docker', status='running', root_path=str(w.root),+ docker_image=None, compose_file='docker-compose.yml',+ managed_by='docker_compose', port=8100)+ fields.update(kw)+ row = make_application(db, **fields)+ db.session.add(Domain(name='shop.example.com', application_id=row.id, is_primary=True))+ db.session.add(Deployment(app_id=row.id, version=1, status='live',+ deploy_completed_at=db.func.now()))+ db.session.commit()+ project = sc.original_project(row)+ # The in-place project running today (compose derives its name from the dir).+ w.containers[f'{project}-web-1'] = {'image': 'shop:v1', 'port': 8100, 'running': True}+ w.containers[f'{project}-cache-1'] = {'image': 'memcached', 'port': None, 'running': True}+ w.vhost_port = 8100+ w.note('initial')+ deploy_settings.update(row, {'slot_volumes_confirmed': True})+ db.session.commit()+ return row+++def test_opting_in_adopts_the_running_project_as_slot_a(app, compose_world):+ w = compose_world+ row = _compose_app(w)+ assert SlotDeployService.set_enabled(row, True)['success']+ a = AppSlot.query.filter_by(application_id=row.id, slot='a').one()+ assert a.project_name == sc.original_project(row) and a.state == 'live'+ assert a.container_port == 3000 and a.host_port == 8100+ assert w.compose_calls == [] and w.outage() == []+++def test_a_compose_slot_deploy_boots_a_second_project_then_switches(app, compose_world):+ w = compose_world+ row = _compose_app(w)+ SlotDeployService.set_enabled(row, True)++ result = DeploymentService.deploy(row.id)++ assert result['success'], result+ b = AppSlot.query.filter_by(application_id=row.id, slot='b').one()+ assert b.project_name == 'shop-b' and row.active_slot == 'b' and row.port == b.host_port+ rendered = yaml.safe_load(open(sc.slot_file(row, 'b'), encoding='utf-8').read().split('\n', 2)[2])+ assert rendered['services']['web']['ports'] == [f'127.0.0.1:{b.host_port}:3000']+ assert rendered['volumes']['uploads']['name'] == 'shop_uploads'+ assert 'IN-PLACE down' not in [e for e, _ in w.timeline], 'the live stack was never stopped'+ assert w.outage() == []+ # The standby is the original project, still up.+ assert w.containers[f'{sc.original_project(row)}-web-1']['running']++ # Back to A: the adopted in-place project makes way for shop-a.+ again = DeploymentService.deploy(row.id)+ assert again['success'], again+ assert row.active_slot == 'a'+ assert (sc.original_project(row), 'down') in w.compose_calls+ assert ('shop-a', 'up') in w.compose_calls and w.outage() == []+++def test_a_compose_slot_that_never_gets_healthy_is_removed_and_nothing_switches(app, compose_world):+ w = compose_world+ row = _compose_app(w)+ SlotDeployService.set_enabled(row, True)+ w.unhealthy_images.add('shop-b:web')+ result = DeploymentService.deploy(row.id)+ assert result['success'] is False and result['still_serving'] is True+ assert ('shop-b', 'down') in w.compose_calls+ assert row.active_slot == 'a' and w.outage() == []+++def test_a_stack_with_a_database_needs_the_split_first(app, compose_world):+ w = compose_world+ w.config = CONFIG+ row = _compose_app(w)+ verdict = SlotDeployService.eligibility(row)+ assert verdict['eligible'] is False and verdict['split_needed'] is True+ assert any('db' in r and 'shop-data' in r for r in verdict['reasons'])++ preview = SlotDeployService.compose_split_preview(row)+ assert preview['stateful'] == ['db'] and 'shop_pgdata' in preview['data_compose']++ result = SlotDeployService.compose_split_apply(row)++ assert result['success'], result+ assert [c for c in w.compose_calls] == [(sc.original_project(row), 'down'),+ ('shop-data', 'up'), ('shop-a', 'up')]+ assert row.slot_deploys_enabled and row.active_slot == 'a'+ assert SlotDeployService.eligibility(row)['eligible'] is True+ # From now on deploys touch only the stateless services.+ assert DeploymentService.deploy(row.id)['success']+ rendered = yaml.safe_load(open(sc.slot_file(row, 'b'), encoding='utf-8').read().split('\n', 2)[2])+ assert 'db' not in rendered['services']+ assert ('shop-data', 'down') not in w.compose_calls+++def test_a_split_app_cannot_quietly_leave_slots(app, compose_world):+ w = compose_world+ w.config = CONFIG+ row = _compose_app(w)+ SlotDeployService.compose_split_apply(row)+ assert SlotDeployService.set_enabled(row, False)['success'] is False+++def test_switching_back_restarts_the_standby_project_without_rebuilding(app, compose_world):+ w = compose_world+ row = _compose_app(w)+ SlotDeployService.set_enabled(row, True)+ assert DeploymentService.deploy(row.id)['success']+ ups = [c for c in w.compose_calls if c[1] == 'up']+ assert SlotDeployService.switch_back(row)['success']+ assert [c for c in w.compose_calls if c[1] == 'up'] == ups, 'no new project was brought up'+ assert row.active_slot == 'a' and row.port == 8100 and w.outage() == []+++def test_the_webhook_path_deploys_through_the_slots(app, compose_world, monkeypatch):+ from app.services.git_deploy_service import GitDeployService+ w = compose_world+ row = _compose_app(w)+ SlotDeployService.set_enabled(row, True)+ result = GitDeployService._standard_restart(row)+ assert result['success'], result+ assert row.active_slot == 'b'+ assert 'IN-PLACE down' not in [e for e, _ in w.timeline] and w.outage() == []+++def test_a_template_update_on_slots_never_stops_the_live_stack(app, compose_world, monkeypatch):+ from app.services.template_service import TemplateService+ w = compose_world+ row = _compose_app(w)+ (w.root / 'docker-compose.yml').write_text('services: {web: {image: old}}')+ SlotDeployService.set_enabled(row, True)+ monkeypatch.setattr(TemplateService, 'get_config', classmethod(+ lambda cls: {'installed': {str(row.id): {'template_id': 't'}}}))+ monkeypatch.setattr(TemplateService, 'save_config', classmethod(lambda cls, c: None))+ monkeypatch.setattr(TemplateService, 'get_template', classmethod(+ lambda cls, tid: {'success': True, 'template': {'name': 't', 'version': '2'}}))+ monkeypatch.setattr(TemplateService, 'generate_compose', classmethod(+ lambda cls, template, variables: 'services: {web: {image: new}}'))++ w.up_fails = True+ failed = TemplateService.update_app(row.id)+ assert failed['success'] is False+ assert (w.root / 'docker-compose.yml').read_text() == 'services: {web: {image: old}}'++ w.up_fails = False+ assert TemplateService.update_app(row.id)['success']+ assert (w.root / 'docker-compose.yml').read_text() == 'services: {web: {image: new}}'+ assert 'IN-PLACE down' not in [e for e, _ in w.timeline] and w.outage() == []+diff --git a/backend/tests/test_plan87_deploy_foundations.py b/backend/tests/test_plan87_deploy_foundations.pyindex 2a317652..0811ef6b 100644--- a/backend/tests/test_plan87_deploy_foundations.py+++ b/backend/tests/test_plan87_deploy_foundations.py@@ -91,17 +91,20 @@ def test_old_deploy_images_are_pruned_but_recent_and_live_kept(app, monkeypatch)rows.append(dep)rows[0].status = 'live' # an old deployment that is somehow still livedb.session.commit()- tags = [f'd{d.id}' for d in rows] + ['latest']+ refs = [d.image_tag for d in rows] + [f'serverkit-app-{row.id}:latest',+ f'serverkit-app-{row.id}-web:d{rows[2].id}',+ f'serverkit-app-{row.id}0:d{rows[2].id}'] # another appmonkeypatch.setattr(DockerService, 'run', classmethod(- lambda cls, args, **kw: {'success': True, 'output': '\n'.join(tags)}))+ lambda cls, args, **kw: {'success': True, 'output': '\n'.join(refs)}))removed = []monkeypatch.setattr(DockerService, 'remove_image', staticmethod(lambda ref, force=False: removed.append(ref) or {'success': True}))app_image_retention.prune(row.id, keep=2, protect={rows[1].image_tag})- # newest two + live (rows[0]) + protected (rows[1]) survive; :latest untouched- assert removed == [rows[2].image_tag]+ # newest two + live (rows[0]) + protected (rows[1]) survive; :latest and+ # another app's images untouched; v3's compose service image goes too.+ assert removed == [rows[2].image_tag, f'serverkit-app-{row.id}-web:d{rows[2].id}']# ── A2 commit recorded after the pull ────────────────────────────────────────@@ -206,24 +209,6 @@ def test_the_gate_reads_the_apps_settings(app):assert seen['timeout'] == 7 and seen['allow_4xx'] is True and seen['consecutive'] == 3-def test_a_rolling_restart_whose_new_container_is_unhealthy_fails(app, monkeypatch):- from app.services import git_deploy_service- from app.services.git_deploy_service import GitDeployService- row = _image_app(healthcheck_path='/health')- commands = []- monkeypatch.setattr(git_deploy_service, 'run_checked', lambda cmd, **kw: (- commands.append(cmd) or {'success': True, 'output': '', 'error': None}))-- def unhealthy(app, port=None, **kw):- raise HealthGateError('Health check did not pass within 120s')-- monkeypatch.setattr(health_gate, 'gate_for_app', unhealthy)- result = GitDeployService._zero_downtime_restart(row)- assert result['success'] is False and 'did not pass' in result['error']- # ... and it still scaled back to one copy.- assert commands[-1][-1] == 'web=1'--# ── A4 settings API ──────────────────────────────────────────────────────────def test_health_settings_round_trip_through_the_app_api(app, client, auth_headers):diff --git a/backend/tests/test_real_slot_deploy_docker.py b/backend/tests/test_real_slot_deploy_docker.pynew file mode 100644index 00000000..b2256e95--- /dev/null+++ b/backend/tests/test_real_slot_deploy_docker.py@@ -0,0 +1,261 @@+"""Slot deploys through the REAL docker daemon (plan 87, the real Docker leg).++The fake-world tests prove the ordering; this proves the property against real+containers and a real nginx: two deploys of a tiny HTTP image while a probe+loop hammers the site through nginx, and the loop must record ZERO non-2xx+answers — through the switch, and through the switch back.++nginx runs in a container on a private network and proxies to the live slot+by container name; the switch is still what production does — rewrite the+vhost, `nginx -t`, graceful reload — only the upstream address differs (a+containerised nginx cannot reach the host's loopback ports).++The compose half brings a rendered slot project up for real and checks the+claim the whole compose design rests on: the slot mounts the SAME named+volume as the original project, not an empty project-prefixed one.++ SERVERKIT_DOCKER_BUILDS=1 pytest tests -m docker_builds+"""+import os+import shutil+import socket+import subprocess+import threading+import time+import urllib.error+import urllib.request+import uuid++import pytest+import yaml++from factories import make_application++from app import db+from app.models.deployment import Deployment+from app.models.domain import Domain+++def _docker_ready() -> bool:+ if shutil.which('docker') is None:+ return False+ try:+ return subprocess.run(['docker', 'info'], capture_output=True, timeout=20).returncode == 0+ except (OSError, subprocess.TimeoutExpired):+ return False+++pytestmark = [+ pytest.mark.docker_builds,+ pytest.mark.skipif(os.environ.get('SERVERKIT_DOCKER_BUILDS') != '1',+ reason='docker-builds leg; opt in with SERVERKIT_DOCKER_BUILDS=1'),+ pytest.mark.skipif(not _docker_ready(), reason='docker daemon not reachable'),+]+++def _docker(*args, check=True, timeout=300):+ result = subprocess.run(['docker', *args], capture_output=True, text=True,+ encoding='utf-8', errors='replace', timeout=timeout)+ if check and result.returncode != 0:+ raise AssertionError(f"docker {' '.join(args)} failed:\n{result.stdout}\n{result.stderr}")+ return result+++def _free_port():+ with socket.socket() as s:+ s.bind(('127.0.0.1', 0))+ return s.getsockname()[1]+++def _build_app_image(tmp, tag, port, body):+ """nginx:alpine serving `body` on `port` — the tiny HTTP app."""+ ctx = tmp / tag.replace(':', '_')+ ctx.mkdir()+ (ctx / 'index.html').write_text(body)+ (ctx / 'default.conf').write_text(+ f'server {{ listen {port}; location / {{ root /usr/share/nginx/html; }} }}\n')+ (ctx / 'Dockerfile').write_text(+ 'FROM nginx:alpine\nCOPY default.conf /etc/nginx/conf.d/default.conf\n'+ 'COPY index.html /usr/share/nginx/html/index.html\n')+ _docker('build', '-t', tag, str(ctx), timeout=600)+++class Probe(threading.Thread):+ """GET the site through nginx every few ms; remember every answer."""++ def __init__(self, url):+ super().__init__(daemon=True)+ self.url = url+ self.answers = []+ self.stop = threading.Event()++ def run(self):+ while not self.stop.is_set():+ try:+ with urllib.request.urlopen(self.url, timeout=3) as resp:+ self.answers.append((resp.status, resp.read().decode().strip()))+ except urllib.error.HTTPError as exc:+ self.answers.append((exc.code, ''))+ except Exception as exc: # noqa: BLE001 - a refused connection is a failure too+ self.answers.append((None, str(exc)))+ time.sleep(0.02)++ def bad(self):+ return [a for a in self.answers if a[0] is None or not 200 <= a[0] < 300]+++def real_slots(app, monkeypatch, tmp_path):+ from app.services import slot_deploy_service+ from app.services.site_domain_service import SiteDomainService++ run = uuid.uuid4().hex[:8]+ net = f'sk-slot-net-{run}'+ proxy = f'sk-slot-proxy-{run}'+ conf_dir = tmp_path / 'conf.d'+ conf_dir.mkdir()+ port = _free_port()+ proxy_port = _free_port()+ images = [f'sk-slot-e2e-{run}:v1', f'sk-slot-e2e-{run}:v2']+ for version, tag in enumerate(images, start=1):+ _build_app_image(tmp_path, tag, port, f'v{version}')++ _docker('network', 'create', net)+ row = make_application(db, name=f'e2e{run}', app_type='docker', status='running',+ root_path=None, compose_file=None, docker_image=images[0], port=port)+ db.session.add(Domain(name='e2e.example.test', application_id=row.id, is_primary=True))+ db.session.add(Deployment(app_id=row.id, version=1, status='live', image_tag=images[0],+ deploy_completed_at=db.func.now()))+ db.session.commit()+ # The app as an in-place deploy leaves it (port:port), on the proxy's network.+ legacy = f'serverkit-app-{row.id}'+ _docker('run', '-d', '--name', legacy, '--network', net, '-p', f'127.0.0.1:{port}:{port}',+ images[0])++ def write_vhost(app, force_type=None):+ live = slot_deploy_service.live_container_name(app)+ _docker('network', 'connect', net, live, check=False)+ (conf_dir / 'site.conf').write_text(+ f'server {{ listen 80; server_name e2e.example.test; '+ f'location / {{ proxy_pass http://{live}:{port}; }} }}\n')+ tested = _docker('exec', proxy, 'nginx', '-t', check=False)+ if tested.returncode != 0:+ return {'nginx': {'success': False}, 'warning': tested.stderr}+ _docker('exec', proxy, 'nginx', '-s', 'reload')+ return {'nginx': {'success': True}, 'warning': None}++ (conf_dir / 'site.conf').write_text(+ f'server {{ listen 80; location / {{ proxy_pass http://{legacy}:{port}; }} }}\n')+ _docker('run', '-d', '--name', proxy, '--network', net, '-p', f'127.0.0.1:{proxy_port}:80',+ '-v', f'{conf_dir}:/etc/nginx/conf.d:ro', 'nginx:alpine')+ monkeypatch.setattr(SiteDomainService, 'write_app_vhost', classmethod(+ lambda cls, app, force_type=None: write_vhost(app)))+ monkeypatch.setattr(slot_deploy_service, 'NGINX_PROBE_URL', f'http://127.0.0.1:{proxy_port}/')+ from app.plugins_sdk import notify+ monkeypatch.setattr(notify, 'send', lambda *a, **k: None)+ from app.services import deploy_settings+ deploy_settings.update(row, {'watch_seconds': 3, 'healthcheck_timeout': 60})+ db.session.commit()++ # nginx answering before the probe starts+ deadline = time.time() + 30+ while time.time() < deadline:+ try:+ urllib.request.urlopen(f'http://127.0.0.1:{proxy_port}/', timeout=2)+ break+ except Exception: # noqa: BLE001+ time.sleep(0.3)++ yield row, images, proxy_port+ for name in (proxy, legacy, f'serverkit-slot-{row.id}-a', f'serverkit-slot-{row.id}-b'):+ _docker('rm', '-f', name, check=False)+ _docker('network', 'rm', net, check=False)+ _docker('rmi', '-f', *images, check=False)+++def test_two_slot_deploys_and_a_switch_back_never_drop_a_request(real_slots):+ from app.services.slot_deploy_service import SlotDeployService+ row, images, proxy_port = real_slots+ assert SlotDeployService.set_enabled(row, True)['success']++ probe = Probe(f'http://127.0.0.1:{proxy_port}/')+ probe.start()+ try:+ time.sleep(0.5)+ v2 = Deployment(app_id=row.id, version=2, status='deploying', image_tag=images[1])+ db.session.add(v2)+ db.session.commit()+ result = SlotDeployService.deploy_container(row, v2, images[1], {}, [])+ assert result['success'], result+ assert row.active_slot == 'b'+ time.sleep(0.5)++ # Switch back to the warm standby: a start-and-switch, no new container.+ v3 = Deployment(app_id=row.id, version=3, status='deploying', image_tag=images[0],+ deploy_trigger='rollback')+ db.session.add(v3)+ db.session.commit()+ back = SlotDeployService.deploy_container(row, v3, images[0], {}, [])+ assert back['success'], back+ assert row.active_slot == 'a'+ time.sleep(0.5)+ finally:+ probe.stop.set()+ probe.join(5)++ bodies = [body for status, body in probe.answers if status == 200]+ assert probe.bad() == [], f'{len(probe.bad())} failed requests during the switch: {probe.bad()[:5]}'+ assert 'v1' in bodies and 'v2' in bodies, 'the probe never saw the switch happen'+ assert bodies[-1] == 'v1'+ assert len(probe.answers) > 50+++def test_a_compose_slot_mounts_the_original_projects_volume(tmp_path):+ """The volume-pinning claim, against real compose."""+ from types import SimpleNamespace+ from app.services import slot_compose_service as sc++ run = uuid.uuid4().hex[:8]+ root = tmp_path / f'shop{run}'+ root.mkdir()+ port = _free_port()+ (root / 'docker-compose.yml').write_text(yaml.safe_dump({+ 'services': {'web': {'image': 'nginx:alpine', 'container_name': f'shop{run}',+ 'ports': [f'{port}:80'],+ 'volumes': ['uploads:/usr/share/nginx/html']}},+ 'volumes': {'uploads': {}},+ }))+ original = sc.original_project(SimpleNamespace(root_path=str(root)))+ compose = ['docker', 'compose']+ try:+ subprocess.run(compose + ['-p', original, 'up', '-d'], cwd=root, check=True,+ capture_output=True, timeout=300)+ # Data written by the original project.+ _docker('exec', f'shop{run}', 'sh', '-c', 'echo from-a > /usr/share/nginx/html/index.html')+ config = yaml.safe_load(subprocess.run(compose + ['-p', original, 'config'], cwd=root,+ check=True, capture_output=True, text=True).stdout)+ app = SimpleNamespace(id=1, name=f'shop{run}', root_path=str(root), port=port)+ slot_port = _free_port()+ rendered = sc.render_slot(app, config, 'web', slot_port, 80, {}, with_data_network=False)+ path = root / sc.SLOT_DIR / 'b.yml'+ sc.write(str(path), rendered)+ subprocess.run(compose + ['-p', f'shop{run}-b', '-f', str(path), 'up', '-d'], cwd=root,+ check=True, capture_output=True, timeout=300)++ deadline = time.time() + 30+ body = None+ while time.time() < deadline:+ try:+ body = urllib.request.urlopen(f'http://127.0.0.1:{slot_port}/', timeout=2).read()+ break+ except Exception: # noqa: BLE001+ time.sleep(0.3)+ assert body is not None and body.decode().strip() == 'from-a', \+ 'slot b did not see the original volume — it got an empty project-prefixed one'+ volumes = _docker('volume', 'ls', '--format', '{{.Name}}').stdout.split()+ assert f'shop{run}-b_uploads' not in volumes+ finally:+ subprocess.run(compose + ['-p', f'shop{run}-b', '-f', str(root / sc.SLOT_DIR / 'b.yml'),+ 'down'], cwd=root, capture_output=True, timeout=120)+ subprocess.run(compose + ['-p', original, 'down', '-v'], cwd=root, capture_output=True,+ timeout=120)diff --git a/docs/API_SURFACE.md b/docs/API_SURFACE.mdindex d79885cf..f081db10 100644--- a/docs/API_SURFACE.md+++ b/docs/API_SURFACE.md@@ -187,6 +187,7 @@ Regenerate (backend/):- `GET /apps/{app_id}/resources`- `GET /apps/{app_id}/sleep-policy`- `GET /apps/{app_id}/slots`+- `GET /apps/{app_id}/slots/compose-split`- `GET /apps/{app_id}/snapshots`- `GET /apps/{app_id}/snapshots/{snap_id}`- `GET /apps/{app_id}/snapshots/{snap_id}/diff`@@ -793,6 +794,7 @@ Regenerate (backend/):- `POST /apps/{app_id}/restart`- `POST /apps/{app_id}/rollback`- `POST /apps/{app_id}/sleep`+- `POST /apps/{app_id}/slots/compose-split`- `POST /apps/{app_id}/slots/restore-db`- `POST /apps/{app_id}/slots/switch-back`- `POST /apps/{app_id}/snapshots/{snap_id}/restore`diff --git a/docs/screenshots/service-slot-split.png b/docs/screenshots/service-slot-split.pngnew file mode 100644index 00000000..ca87dffeBinary files /dev/null and b/docs/screenshots/service-slot-split.png differdiff --git a/frontend/src/components/apps/SlotDeploysPanel.jsx b/frontend/src/components/apps/SlotDeploysPanel.jsxindex 4b3b27ca..3798eed4 100644--- a/frontend/src/components/apps/SlotDeploysPanel.jsx+++ b/frontend/src/components/apps/SlotDeploysPanel.jsx@@ -1,4 +1,5 @@import { useEffect, useState } from 'react';+import Modal from '@/components/Modal';import { Layers } from 'lucide-react';import api from '../../services/api';import { useToast } from '../../contexts/useToast.js';@@ -34,6 +35,8 @@ const SlotDeploysPanel = ({ app, onChanged }) => {const [releaseCommand, setReleaseCommand] = useState(settings.release_command || '');const [snapshotDbs, setSnapshotDbs] = useState(settings.snapshot_databases ?? true);const [stopOld, setStopOld] = useState(!!settings.stop_old_before_release);+ const [split, setSplit] = useState(null);+ const [splitting, setSplitting] = useState(false);useEffect(() => {let cancelled = false;@@ -90,6 +93,29 @@ const SlotDeploysPanel = ({ app, onChanged }) => {}}+ async function handlePreviewSplit() {+ try {+ setSplit(await api.previewAppComposeSplit(app.id));+ } catch (err) {+ toast.error(err.message || t('app.slotDeploysPanel.splitPreviewFailed', 'Could not preview the split'));+ }+ }++ async function handleApplySplit() {+ setSplitting(true);+ try {+ const data = await api.applyAppComposeSplit(app.id);+ setStatus(data.slots);+ setSplit(null);+ toast.success(t('app.slotDeploysPanel.splitDone', 'Stateful services moved. The app now deploys through slots.'));+ onChanged?.();+ } catch (err) {+ toast.error(err.message || t('app.slotDeploysPanel.splitFailed', 'The split failed'));+ } finally {+ setSplitting(false);+ }+ }+if (!status) return null;const { eligibility } = status;const hasVolumes = (status.volumes || []).length > 0;@@ -124,6 +150,22 @@ const SlotDeploysPanel = ({ app, onChanged }) => {</div></div>+ {eligibility.split_needed && (+ <div className="settings-row">+ <div className="settings-label">+ <span>{t('app.slotDeploysPanel.split', 'Move the stateful services first')}</span>+ <span className="settings-hint">+ {t('app.slotDeploysPanel.splitHint', 'A database or cache cannot run twice. It moves once into a shared data project that both slots reach by the same name, on the same volumes. Preview exactly what changes before anything happens.')}+ </span>+ </div>+ <div className="settings-control">+ <Button variant="outline" size="sm" onClick={handlePreviewSplit}>+ {t('app.slotDeploysPanel.previewSplit', 'Preview the split')}+ </Button>+ </div>+ </div>+ )}+<div className="settings-row"><div className="settings-label"><label htmlFor={`slot-watch-${app.id}`}>@@ -220,6 +262,35 @@ const SlotDeploysPanel = ({ app, onChanged }) => {</div></div>+ {split && (+ <Modal+ open+ onClose={() => setSplit(null)}+ title={t('app.slotDeploysPanel.splitTitle', 'Move {{services}} to {{project}}', {+ services: split.stateful.join(', '), project: split.data_project,+ })}+ size="lg"+ footer={(+ <>+ <Button variant="outline" onClick={() => setSplit(null)} disabled={splitting}>+ {t('common.actions.cancel', 'Cancel')}+ </Button>+ <Button onClick={handleApplySplit} disabled={splitting}>+ {splitting+ ? t('app.slotDeploysPanel.splitting', 'Moving…')+ : t('app.slotDeploysPanel.applySplit', 'Stop the app once and move them')}+ </Button>+ </>+ )}+ >+ <p className="slot-split__downtime">{split.downtime}</p>+ <h4 className="slot-split__label">{t('app.slotDeploysPanel.dataProject', 'Shared data project')}</h4>+ <pre className="slot-split__yaml">{split.data_compose}</pre>+ <h4 className="slot-split__label">{t('app.slotDeploysPanel.slotProject', 'Each slot')}</h4>+ <pre className="slot-split__yaml">{split.slot_compose}</pre>+ </Modal>+ )}+<p className="app-panel-hint">{t('app.slotDeploysPanel.schemaRule', 'Both releases use the same database during the switch. Keep schema changes backward-compatible for one deploy: add first, deploy, remove later.')}</p>diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.jsonindex 220e3688..c1273ec5 100644--- a/frontend/src/i18n/locales/en.json+++ b/frontend/src/i18n/locales/en.json@@ -5628,19 +5628,30 @@"wildcardDns": "Wildcard DNS"},"slotDeploysPanel": {+ "applySplit": "Stop the app once and move them",+ "dataProject": "Shared data project","disabled": "Slot deploys off.","enable": "Use slot deploys","enableHint": "Turning it on restarts nothing: the running container becomes slot A.","enabled": "Slot deploys on. The running container is now slot A; the next deploy boots slot B.","intro": "Each deploy boots the new release in a second slot on its own port while the live one keeps serving. It switches traffic only after the new release passes its health check, watches it for a while, and switches back on its own if it falls over. The previous release stays warm for an instant manual switch back.",+ "previewSplit": "Preview the split","release": "Release command","releaseHint": "Runs once in a throwaway container of the new release, before traffic switches, for example database migrations. If it fails the deploy stops and the live release is untouched. Empty uses the release: line of the Procfile or serverkit.yaml, if there is one.","releasePlaceholder": "npm run migrate","saveFailed": "Failed to save rollout settings","saved": "Rollout settings saved.","schemaRule": "Both releases use the same database during the switch. Keep schema changes backward-compatible for one deploy: add first, deploy, remove later.",+ "slotProject": "Each slot","snapshot": "Snapshot the databases first","snapshotHint": "Dumps the databases this app owns before the release command runs, so a migration can be undone after a switch back.",+ "split": "Move the stateful services first",+ "splitDone": "Stateful services moved. The app now deploys through slots.",+ "splitFailed": "The split failed",+ "splitHint": "A database or cache cannot run twice. It moves once into a shared data project that both slots reach by the same name, on the same volumes. Preview exactly what changes before anything happens.",+ "splitPreviewFailed": "Could not preview the split",+ "splitTitle": "Move {{services}} to {{project}}",+ "splitting": "Moving…","standby": "Keep the previous release warm (minutes)","standbyHint": "While warm, switching back takes seconds. After that it is stopped: it no longer uses memory, and switching back starts it first.","stopOld": "Stop the live release before the release command",diff --git a/frontend/src/i18n/locales/es.json b/frontend/src/i18n/locales/es.jsonindex 2f67e6ff..b8aa5394 100644--- a/frontend/src/i18n/locales/es.json+++ b/frontend/src/i18n/locales/es.json@@ -6844,7 +6844,18 @@"snapshotHint": "Vuelca las bases de datos de esta app antes de ejecutar el comando de publicación, para poder deshacer una migración después de volver atrás.","stopOld": "Detener la versión en vivo antes del comando de publicación","stopOldHint": "Para cambios de esquema que la versión en marcha no soporta. El sitio queda caído desde el comando de publicación hasta que la versión nueva pasa su comprobación de salud; si algo falla, la versión anterior vuelve a arrancar.",- "releasePlaceholder": "npm run migrate"+ "releasePlaceholder": "npm run migrate",+ "split": "Primero mover los servicios con estado",+ "splitHint": "Una base de datos o una caché no puede ejecutarse dos veces. Se mueve una sola vez a un proyecto de datos compartido al que ambas ranuras llegan con el mismo nombre, sobre los mismos volúmenes. Revisa exactamente qué cambia antes de que pase nada.",+ "previewSplit": "Ver la separación",+ "splitPreviewFailed": "No se pudo mostrar la separación",+ "splitDone": "Servicios con estado movidos. La app ahora se despliega por ranuras.",+ "splitFailed": "La separación falló",+ "splitTitle": "Mover {{services}} a {{project}}",+ "splitting": "Moviendo…",+ "applySplit": "Detener la app una vez y moverlos",+ "dataProject": "Proyecto de datos compartido",+ "slotProject": "Cada ranura"}},"auth": {diff --git a/frontend/src/services/api/apps.js b/frontend/src/services/api/apps.jsindex 75064eea..c8858052 100644--- a/frontend/src/services/api/apps.js+++ b/frontend/src/services/api/apps.js@@ -594,3 +594,12 @@ export async function restoreAppSlotDatabase(appId, deploymentId) {method: 'POST', body: { deployment_id: deploymentId },});}++// Compose slot apps with a database move it once into a shared data project.+export async function previewAppComposeSplit(appId) {+ return this.request(`/apps/${appId}/slots/compose-split`);+}++export async function applyAppComposeSplit(appId) {+ return this.request(`/apps/${appId}/slots/compose-split`, { method: 'POST', body: { confirm: true } });+}diff --git a/frontend/src/styles/pages/_bandwidth.scss b/frontend/src/styles/pages/_bandwidth.scssindex fa24a66e..7af68351 100644--- a/frontend/src/styles/pages/_bandwidth.scss+++ b/frontend/src/styles/pages/_bandwidth.scss@@ -384,3 +384,33 @@&--warn { color: var(--amber); }}}++// Settings: the compose data-split preview (plan 87 §C).+// Component: apps/SlotDeploysPanel.+.slot-split {+ &__downtime {+ margin: 0 0 12px;+ font-size: 13px;+ color: var(--amber);+ }++ &__label {+ margin: 12px 0 6px;+ font-size: 12px;+ text-transform: uppercase;+ letter-spacing: 0.04em;+ color: var(--text-dim);+ }++ &__yaml {+ max-height: 240px;+ margin: 0;+ padding: 10px 12px;+ overflow: auto;+ font-family: var(--mono);+ font-size: 12px;+ background: var(--bg-code);+ border: 1px solid var(--border-soft);+ border-radius: 6px;+ }+}