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
Make redeploys fail safe before slot deploys
Juan Deniscecbb743fdc9
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.pyindex 288ebf58..1be8cc05 100644--- a/backend/app/api/apps.py+++ b/backend/app/api/apps.py@@ -1303,6 +1303,12 @@ def update_app(app_id):if 'healthcheck_path' in data:hc = (data['healthcheck_path'] or '').strip()app.healthcheck_path = hc or None+ if 'deploy_settings' in data:+ from app.services import deploy_settings+ _, error = deploy_settings.update(app, data['deploy_settings'])+ if error:+ db.session.rollback()+ return jsonify({'error': error}), 400if 'root_path' in data:app.root_path = data['root_path']if 'docker_image' in data:diff --git a/backend/app/models/application.py b/backend/app/models/application.pyindex 67072c0b..432cd117 100644--- a/backend/app/models/application.py+++ b/backend/app/models/application.py@@ -54,6 +54,9 @@ class Application(JsonColumnMixin, TimestampMixin, SoftDeleteMixin, db.Model):# Populated from a manifest at import, editable in Settings. NULL => no gate# (the restart falls back to a fixed wait).healthcheck_path = db.Column(db.String(255), nullable=True)+ # JSON: health-gate timeout / 4xx rule and the other deploy knobs+ # (plan 87). NULL = defaults; read through app.services.deploy_settings.+ deploy_settings = db.Column(db.Text, nullable=True)# Docker specificdocker_image = db.Column(db.String(200), nullable=True)@@ -218,6 +221,7 @@ class Application(JsonColumnMixin, TimestampMixin, SoftDeleteMixin, db.Model):'python_version': self.python_version,'port': self.port,'healthcheck_path': self.healthcheck_path,+ 'deploy_settings': self._deploy_settings(),'root_path': self.root_path,'docker_image': self.docker_image,'container_id': self.container_id,@@ -314,6 +318,10 @@ class Application(JsonColumnMixin, TimestampMixin, SoftDeleteMixin, db.Model):}return result+ def _deploy_settings(self):+ from app.services.deploy_settings import effective+ return effective(self)+def __repr__(self):return f'<Application {self.name}>'diff --git a/backend/app/models/deployment.py b/backend/app/models/deployment.pyindex 54aa07aa..4627e819 100644--- a/backend/app/models/deployment.py+++ b/backend/app/models/deployment.py@@ -141,11 +141,18 @@ class Deployment(JsonColumnMixin, db.Model):@classmethoddef get_previous(cls, app_id, before_version):- """Get previous successful deployment before a version."""+ """Get previous successful deployment before a version.++ A deploy marks the release it replaces ``rolled_back``, so "was live+ once" is live-or-rolled_back with a completed deploy. Matching only+ ``live`` found nothing for any app deployed twice: rollback without a+ version always failed and no diff was ever generated (plan 87 §A6).+ """return cls.query.filter(cls.app_id == app_id,cls.version < before_version,- cls.status == 'live'+ cls.status.in_(('live', 'rolled_back')),+ cls.deploy_completed_at.isnot(None),).order_by(cls.version.desc()).first()@classmethoddiff --git a/backend/app/services/app_image_retention.py b/backend/app/services/app_image_retention.pynew file mode 100644index 00000000..02245167--- /dev/null+++ b/backend/app/services/app_image_retention.py@@ -0,0 +1,73 @@+"""Immutable per-deployment image tags and their retention (plan 87 §A1).++Every build used to be tagged ``serverkit-app-<id>:latest``, so "roll back to+v11" 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+ran.++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.+"""+import logging+import re++logger = logging.getLogger(__name__)++DEFAULT_KEEP = 3+_TAG = re.compile(r'^d(\d+)$')+++def repository(app_id) -> str:+ return f'serverkit-app-{app_id}'+++def deploy_image_tag(app_id, deployment_id) -> str:+ return f'{repository(app_id)}:d{deployment_id}'+++def _local_tags(app_id):+ from app.services.docker_service import DockerService+ result = DockerService.run(['images', repository(app_id), '--format', '{{.Tag}}'], timeout=30)+ if not result.get('success'):+ return None+ return [t.strip() for t in (result.get('output') or '').splitlines() if t.strip()]+++def 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.+ """+ from app.models.deployment import Deployment+ from app.services.docker_service import DockerService+ try:+ tags = _local_tags(app_id)+ if not tags:+ 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()]+ 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)+ removed = []+ for tag in tags:+ if not _TAG.match(tag):+ continue # :latest and anything a human tagged stay+ ref = f'{repository(app_id)}:{tag}'+ if ref in keep_refs:+ continue+ if DockerService.remove_image(ref).get('success'):+ removed.append(ref)+ if removed:+ logger.info('Pruned %d old image(s) of app %s: %s', len(removed), app_id, removed)+ return removed+ except Exception as exc: # noqa: BLE001 - retention never fails a deploy+ logger.warning('Image retention for app %s failed: %s', app_id, exc)+ return []diff --git a/backend/app/services/build_service.py b/backend/app/services/build_service.pyindex de869f0e..7de69ec1 100644--- a/backend/app/services/build_service.py+++ b/backend/app/services/build_service.py@@ -609,10 +609,14 @@ class BuildService:@classmethoddef build(cls, app_id: int, no_cache: bool = False,- log_callback: Callable[[str], None] = None) -> Dict:+ log_callback: Callable[[str], None] = None,+ image_tag: str = None) -> Dict:"""Build an application using configured method.- Auto-detects build method if set to 'auto'.+ Auto-detects build method if set to 'auto'. ``image_tag`` names the+ image; a deploy passes an immutable per-deployment tag so a rollback+ redeploys the image that actually ran, not whatever ``:latest`` is now+ (plan 87 §A1)."""build_config = cls.get_app_build_config(app_id)if not build_config:@@ -633,6 +637,7 @@ class BuildService:app_id=app_id,app_path=app_path,dockerfile_path=build_config.get('dockerfile_path', 'Dockerfile'),+ image_tag=image_tag,build_args=build_config.get('build_args'),no_cache=no_cache or not build_config.get('cache_enabled', True),timeout=timeout,@@ -649,6 +654,7 @@ class BuildService:app_path=app_path,plan=build_config.get('buildpack_plan'),overrides=build_config.get('buildpack_overrides'),+ image_tag=image_tag,build_args=build_config.get('build_args'),no_cache=no_cache or not build_config.get('cache_enabled', True),timeout=timeout,@@ -659,6 +665,7 @@ class BuildService:return cls.build_with_nixpacks(app_id=app_id,app_path=app_path,+ image_tag=image_tag,env_vars=build_config.get('env_vars'),build_cmd=build_config.get('custom_build_cmd'),start_cmd=build_config.get('custom_start_cmd'),diff --git a/backend/app/services/deploy_lock.py b/backend/app/services/deploy_lock.pynew file mode 100644index 00000000..2eb52db2--- /dev/null+++ b/backend/app/services/deploy_lock.py@@ -0,0 +1,91 @@+"""Per-app deploy lock (plan 87 §A5).++Two webhook pushes a second apart used to run two deploys of the same app at+once: both stopped the container, both started one, and whichever lost the+race left a half-built release behind. Every path that changes what an app+runs — deploy, rollback, webhook deploy, template update, slot switch — now+takes this lock first, so a second deploy WAITS for the first and then runs+against what the first left live. It never races and is never dropped.++In-process by design: the panel runs as one process (the agent gateway+already requires a single worker, see CLAUDE.md), and the job consumer shares+it. Re-entrant, so a deploy that rolls itself back inside the lock does not+deadlock on its own thread.+"""+import logging+import threading+import time+from contextlib import contextmanager+from typing import Callable, Optional++logger = logging.getLogger(__name__)++_guard = threading.Lock()+_locks = {}+_holders = {}+++class DeployLockTimeout(RuntimeError):+ pass+++def _lock_for(app_id):+ with _guard:+ lock = _locks.get(app_id)+ if lock is None:+ lock = _locks[app_id] = threading.RLock()+ return lock+++def is_locked(app_id) -> bool:+ return app_id in _holders+++def holder(app_id) -> Optional[str]:+ return _holders.get(app_id)+++@contextmanager+def deploy_lock(app_id, label: str = 'deploy', timeout: Optional[float] = 3600,+ log: Optional[Callable[[str], None]] = None):+ """Hold the app's deploy lock; wait (up to ``timeout``) if another has it."""+ lock = _lock_for(app_id)+ if not lock.acquire(blocking=False):+ waiting_on = _holders.get(app_id) or 'another deploy'+ if log:+ log(f'Waiting for {waiting_on} of this app to finish...')+ logger.info('app %s %s waits for %s', app_id, label, waiting_on)+ started = time.monotonic()+ acquired = lock.acquire(timeout=-1 if timeout is None else timeout)+ if not acquired:+ raise DeployLockTimeout(+ f'Timed out after {int(time.monotonic() - started)}s waiting for '+ f'{waiting_on} of this app to finish')+ outer = app_id not in _holders+ if outer:+ _holders[app_id] = label+ try:+ yield+ finally:+ if outer:+ _holders.pop(app_id, None)+ lock.release()+++def with_deploy_lock(label: str):+ """Decorate a service method whose first argument (after cls) is app_id.++ A lock timeout comes back as the usual ``{'success': False, 'error'}``.+ """+ import functools++ def decorate(fn):+ @functools.wraps(fn)+ def wrapper(cls, app_id, *args, **kwargs):+ try:+ with deploy_lock(app_id, label, log=kwargs.get('log_callback')):+ return fn(cls, app_id, *args, **kwargs)+ except DeployLockTimeout as exc:+ return {'success': False, 'error': str(exc)}+ return wrapper+ return decoratediff --git a/backend/app/services/deploy_settings.py b/backend/app/services/deploy_settings.pynew file mode 100644index 00000000..012216cf--- /dev/null+++ b/backend/app/services/deploy_settings.py@@ -0,0 +1,92 @@+"""Per-app deploy settings (plan 87).++Stored as one JSON object on ``Application.deploy_settings``; NULL or a+missing key means the default below. ``effective(app)`` is what every reader+uses, so a default changes in one place.+"""+import json++DEFAULTS = {+ # Health gate (§A3): how long a new release may take to answer, whether a+ # 4xx on the health path still counts as up, and how many answers in a row+ # it takes.+ 'healthcheck_timeout': 120,+ 'healthcheck_allow_4xx': False,+ 'healthcheck_consecutive': 3,+ # How many deployments' images stay on disk for rollback (§A1).+ 'keep_images': 3,+}++# key -> (type, min, max). Anything outside is rejected, not clamped: a typo'd+# timeout of 0 would make every deploy fail.+_RULES = {+ 'healthcheck_timeout': (int, 5, 1800),+ 'healthcheck_allow_4xx': (bool, None, None),+ 'healthcheck_consecutive': (int, 1, 20),+ 'keep_images': (int, 1, 20),+}+++def stored(app) -> dict:+ raw = getattr(app, 'deploy_settings', None)+ if not raw:+ return {}+ try:+ value = json.loads(raw)+ except (TypeError, ValueError):+ return {}+ return value if isinstance(value, dict) else {}+++def effective(app) -> dict:+ merged = dict(DEFAULTS)+ merged.update({k: v for k, v in stored(app).items() if k in DEFAULTS})+ return merged+++def get(app, key):+ return effective(app)[key]+++def validate(changes) -> tuple:+ """``(clean, error)``: the recognised keys coerced to their types."""+ if not isinstance(changes, dict):+ return None, 'deploy_settings must be an object'+ clean = {}+ for key, value in changes.items():+ if key not in _RULES:+ return None, f'Unknown deploy setting: {key}'+ kind, low, high = _RULES[key]+ if value is None:+ clean[key] = None # back to the default+ continue+ if kind is bool:+ if not isinstance(value, bool):+ return None, f'{key} must be true or false'+ clean[key] = value+ continue+ if isinstance(value, bool):+ return None, f'{key} must be a number'+ try:+ number = kind(value)+ except (TypeError, ValueError):+ return None, f'{key} must be a number'+ if (low is not None and number < low) or (high is not None and number > high):+ return None, f'{key} must be between {low} and {high}'+ clean[key] = number+ return clean, None+++def update(app, changes) -> tuple:+ """Merge validated ``changes`` into the app's stored settings (no commit)."""+ clean, error = validate(changes)+ if error:+ return None, error+ current = stored(app)+ for key, value in clean.items():+ if value is None:+ current.pop(key, None)+ else:+ current[key] = value+ app.deploy_settings = json.dumps(current) if current else None+ return effective(app), Nonediff --git a/backend/app/services/deployment_service.py b/backend/app/services/deployment_service.pyindex 6628af66..6df5d182 100644--- a/backend/app/services/deployment_service.py+++ b/backend/app/services/deployment_service.py@@ -92,9 +92,23 @@ class DeploymentService:log_callback: Callable[[str], None] = None) -> Dict:"""Execute a full deployment (build + deploy).- This is the main entry point for deployments.+ This is the main entry point for deployments. It holds the app's deploy+ lock for the whole run, so a second deploy of the same app waits for+ this one instead of racing it (plan 87 §A5)."""- # Create deployment record+ from app.services.deploy_lock import deploy_lock, DeployLockTimeout+ try:+ with deploy_lock(app_id, 'deploy', log=log_callback):+ return cls._deploy_locked(app_id, user_id, no_cache, trigger,+ version_tag, log_callback)+ except DeployLockTimeout as exc:+ return {'success': False, 'error': str(exc)}++ @classmethod+ def _deploy_locked(cls, app_id, user_id, no_cache, trigger, version_tag,+ log_callback) -> Dict:+ # Create deployment record (inside the lock: the version number is+ # read-then-written, so two unlocked deploys could both claim vN).result = cls.create_deployment(app_id, user_id, trigger, version_tag)if not result.get('success'):return result@@ -126,6 +140,9 @@ class DeploymentService:pull_result = cls._pull_latest(app, log_callback)if not pull_result.get('success'):return cls._fail(app, deployment, pull_result.get('error') or 'Git pull failed')+ # create_deployment read HEAD before this pull, so the row named+ # the commit that was live, not the one being deployed (§A2).+ cls._record_commit(app, deployment)if cls._uses_compose(app):# The whole compose project: build, validate and pull while@@ -146,10 +163,14 @@ class DeploymentService:if log_callback:log_callback(f"Starting build for {app.name}...")+ # An immutable tag per deployment: a rollback then redeploys+ # the image that actually ran, not whatever :latest became (§A1).+ from app.services.app_image_retention import deploy_image_tagbuild_result = BuildService.build(app_id,no_cache=no_cache,- log_callback=log_callback+ log_callback=log_callback,+ image_tag=deploy_image_tag(app_id, deployment.id),)deployment.build_completed_at = datetime.utcnow()@@ -215,6 +236,13 @@ class DeploymentService:keep_count = build_config.get('keep_deployments', 5)Deployment.cleanup_old_deployments(app_id, keep_count)+ # One image per deploy is a disk leak unless something prunes it+ # (plan 85). Keeps the newest few for rollback.+ if deployment.image_tag:+ from app.services import app_image_retention, deploy_settings+ app_image_retention.prune(app_id, keep=deploy_settings.get(app, 'keep_images'),+ protect=cls._protected_images(app))+if log_callback:log_callback(f"Deployment successful! Version {deployment.version} is now live.")@@ -257,6 +285,22 @@ class DeploymentService:db.session.commit()return {'success': False, 'error': error, 'deployment': deployment.to_dict()}+ @staticmethod+ def _record_commit(app: Application, deployment: Deployment) -> None:+ """Stamp the deployment with the commit now checked out (after a pull)."""+ if not app.root_path:+ return+ info = GitService.get_commit_info(app.root_path)+ if info and info.get('hash'):+ deployment.commit_hash = info.get('hash')+ deployment.commit_message = info.get('message')+ db.session.commit()++ @staticmethod+ def _protected_images(app: Application) -> set:+ """Image refs a slot still points at (never pruned)."""+ return set()+@staticmethoddef _uses_compose(app: Application) -> bool:"""A local app whose runtime is its own compose project."""@@ -427,6 +471,7 @@ class DeploymentService:if not pull_result.get('success'):return pull_result+ cls._record_commit(app, deployment)# Run post-deploy scriptsif deploy_config.get('post_deploy_script'):@@ -462,6 +507,15 @@ class DeploymentService:If target_version is not specified, rolls back to the previous successful deployment."""+ from app.services.deploy_lock import deploy_lock, DeployLockTimeout+ try:+ with deploy_lock(app_id, 'rollback', log=log_callback):+ return cls._rollback_locked(app_id, target_version, user_id, log_callback)+ except DeployLockTimeout as exc:+ return {'success': False, 'error': str(exc)}++ @classmethod+ def _rollback_locked(cls, app_id, target_version, user_id, log_callback) -> Dict:# query_active: a rollback re-deploys code and restarts the app.app = Application.query_active().filter_by(id=app_id).first()if not app:@@ -604,6 +658,25 @@ class DeploymentService:if not script_result.get('success'):return script_result+ # A compose app's code lives in its images: checking out the old+ # commit changed files and nothing else, so the "rollback"+ # reported success while the new release kept serving (§A6).+ # Rebuild and recreate from the checked-out tree, validated first.+ if cls._uses_compose(app):+ from app.services import deploy_preflight_service as preflight+ checks = preflight.preflight_compose_project(+ app.root_path, app.compose_file, log=log_callback)+ if not checks.ok:+ return {'success': False, 'error': checks.error,+ 'preflight': checks.to_dict()}+ if log_callback:+ log_callback(f"Starting compose project {app.compose_file}...")+ up = DockerService.compose_up(app.root_path, detach=True, build=True,+ compose_file=app.compose_file)+ if not up.get('success'):+ return {'success': False,+ 'error': up.get('error') or 'docker compose up failed'}+# Restart serviceif app.app_type in ['flask', 'django']:service_name = f"serverkit-{app.name}"diff --git a/backend/app/services/git_deploy_service.py b/backend/app/services/git_deploy_service.pyindex 7223c825..70146ed5 100644--- a/backend/app/services/git_deploy_service.py+++ b/backend/app/services/git_deploy_service.py@@ -10,6 +10,9 @@ 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 HealthGateError+from app.services.deploy_lock import with_deploy_locklogger = logging.getLogger(__name__)@@ -18,6 +21,7 @@ class GitDeployService:"""Service for managing git-based deployments."""@classmethod+ @with_deploy_lock('webhook deploy')def deploy(cls, app_id: int, webhook_id: int = None, commit_sha: str = None,commit_message: str = None, branch: str = None,triggered_by: str = 'webhook') -> Dict:@@ -102,6 +106,12 @@ class GitDeployService:if not pull_result.get('success'):raise Exception(f"Git pull failed: {pull_result.get('error')}")+ if not deployment.commit_sha:+ # A manual deploy names no commit: record what the pull+ # checked out, never what was live before it (plan 87 §A2).+ head = run_checked(['git', 'rev-parse', 'HEAD'], cwd=app.root_path, timeout=10)+ if head['success']:+ deployment.commit_sha = head['output'].strip()[:40] or None# Step 3: Restart applicationlogs.append("\n=== Restarting application ===")@@ -164,6 +174,7 @@ class GitDeployService:}@classmethod+ @with_deploy_lock('rollback')def rollback(cls, app_id: int, target_version: int = None) -> Dict:"""Rollback to a previous deployment version.@@ -453,17 +464,24 @@ class GitDeployService: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)}- # Wait for the new container to be healthy. When a health-check path- # is declared, poll it instead of a blind fixed wait (plan 17 #4).- output.append(cls._wait_for_health(app))-- # Scale back down- scale_result = run_checked(- ['docker', 'compose', 'up', '-d', '--no-deps', '--scale', 'web=1'],- cwd=app.root_path, timeout=60)-- output.append(f"Rolling update completed")+ # 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,@@ -476,41 +494,21 @@ class GitDeployService:return {'success': False, 'error': str(e), 'output': '\n'.join(output)}@classmethod- def _wait_for_health(cls, app, timeout: int = 30, fallback: int = 5) -> str:- """Block until the app answers its health-check path, or time out.+ def _wait_for_health(cls, app, fallback: int = 5, **overrides) -> str:+ """Block until the app passes the health gate; raise if it never does.- Falls back to a fixed wait when no path/port is declared, preserving the- previous behavior. Best-effort — never raises.+ 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- path = getattr(app, 'healthcheck_path', None)- port = getattr(app, 'port', None)- if not path or not port:+ if not getattr(app, 'port', None):time.sleep(fallback)- return f"Waited {fallback}s (no health check configured)"-- import urllib.request- import urllib.error-- url = f"http://127.0.0.1:{port}{path if path.startswith('/') else '/' + path}"- deadline = time.time() + timeout- last = None- while time.time() < deadline:- try:- with urllib.request.urlopen(url, timeout=3) as resp:- if 200 <= resp.status < 400:- return f"Health check passed ({url} -> {resp.status})"- last = f"status {resp.status}"- except urllib.error.HTTPError as exc:- if 200 <= exc.code < 500:- # a 4xx still means the app is up and routing- return f"Health check reachable ({url} -> {exc.code})"- last = f"HTTP {exc.code}"- except Exception as exc: # not up yet- last = str(exc)- time.sleep(1)- return f"Health check did not pass within {timeout}s ({url}; last: {last})"+ 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:diff --git a/backend/app/services/health_gate.py b/backend/app/services/health_gate.pynew file mode 100644index 00000000..8b36c6a1--- /dev/null+++ b/backend/app/services/health_gate.py@@ -0,0 +1,135 @@+"""The deploy health gate (plan 87 §A3).++``_wait_for_health`` used to be advisory: it logged "did not pass" and the+deploy carried on as a success, and it counted a 4xx on the health path as+healthy. This gate is the one every deploy path now waits on, and it RAISES:++* the app must answer ``GET <path>`` with 2xx/3xx — a 4xx fails unless the+ app's settings explicitly allow it (a health path behind auth, say);+* it must do so ``consecutive`` times in a row, so a process that answers once+ and then crashes on its first real request does not pass;+* a container that has a Docker HEALTHCHECK must also report ``healthy``, and+ one that has exited fails at once instead of burning the whole timeout.+"""+import logging+import time+import urllib.error+import urllib.request+from typing import Callable, Optional++logger = logging.getLogger(__name__)++# Seams the tests replace so a gate does not sleep for real.+sleep = time.sleep+clock = time.monotonic+++class HealthGateError(RuntimeError):+ """The release never became healthy; ``last`` is the final probe result."""++ def __init__(self, message, last=None):+ super().__init__(message)+ self.last = last+++def normalize_path(path: Optional[str]) -> str:+ path = (path or '/').strip() or '/'+ return path if path.startswith('/') else '/' + path+++def probe(url: str, host_header: Optional[str] = None, timeout: float = 3.0):+ """One request: ``(status, error)`` — ``status`` is None when nothing answered."""+ request = urllib.request.Request(url, method='GET')+ request.add_header('User-Agent', 'ServerKit-HealthGate/1.0')+ if host_header:+ request.add_header('Host', host_header)+ try:+ with urllib.request.urlopen(request, timeout=timeout) as response:+ return response.status, None+ except urllib.error.HTTPError as exc:+ return exc.code, None+ except Exception as exc: # noqa: BLE001 - not listening yet, reset, timeout+ return None, str(exc) or exc.__class__.__name__+++def status_passes(status, allow_4xx: bool = False) -> bool:+ if status is None:+ return False+ if 200 <= status < 400:+ return True+ return allow_4xx and 400 <= status < 500+++def container_health(container: str):+ """``(running, health)`` from ``docker inspect``; health is '' without a HEALTHCHECK."""+ from app.services.docker_service import DockerService+ result = DockerService.run(+ ['inspect', '--format',+ '{{.State.Status}}|{{if .State.Health}}{{.State.Health.Status}}{{end}}', container],+ timeout=15)+ if not result.get('success'):+ return None, ''+ status, _, health = (result.get('output') or '').strip().partition('|')+ return status, health+++def wait_healthy(port: int, path: Optional[str] = None, *, timeout: float = 120,+ consecutive: int = 3, allow_4xx: bool = False,+ host: str = '127.0.0.1', host_header: Optional[str] = None,+ container: Optional[str] = None, interval: float = 1.0,+ log: Optional[Callable[[str], None]] = None,+ _probe=None, _container_health=None,+ _sleep=None, _clock=None) -> dict:+ """Block until the release passes, or raise :class:`HealthGateError`."""+ do_probe = _probe or probe+ do_health = _container_health or container_health+ _sleep = _sleep or sleep+ _clock = _clock or clock+ url = f'http://{host}:{port}{normalize_path(path)}'+ deadline = _clock() + timeout+ streak = 0+ last = None+ attempts = 0+ while True:+ attempts += 1+ docker_ok = True+ if container:+ state, health = do_health(container)+ if state in ('exited', 'dead'):+ raise HealthGateError(f'Container {container} {state} before it became healthy',+ last=state)+ if health == 'unhealthy':+ raise HealthGateError(f'Container {container} reported unhealthy', last=health)+ # A HEALTHCHECK that is still 'starting' is not a pass yet.+ docker_ok = health in ('', 'healthy')++ status, error = do_probe(url, host_header)+ last = status if status is not None else error+ if docker_ok and status_passes(status, allow_4xx):+ streak += 1+ if streak >= consecutive:+ message = f'Health check passed ({url} -> {status}, {streak} in a row)'+ if log:+ log(message)+ return {'url': url, 'status': status, 'attempts': attempts}+ else:+ streak = 0++ if _clock() >= deadline:+ raise HealthGateError(+ f'Health check did not pass within {int(timeout)}s ({url}; last: {last})',+ last=last)+ _sleep(interval)+++def gate_for_app(app, port: Optional[int] = None, **overrides) -> dict:+ """:func:`wait_healthy` with the app's own path and settings."""+ from app.services import deploy_settings+ settings = deploy_settings.effective(app)+ kwargs = {+ 'timeout': settings['healthcheck_timeout'],+ 'consecutive': settings['healthcheck_consecutive'],+ 'allow_4xx': settings['healthcheck_allow_4xx'],+ }+ kwargs.update(overrides)+ return wait_healthy(port or app.port, getattr(app, 'healthcheck_path', None), **kwargs)diff --git a/backend/app/services/template_service.py b/backend/app/services/template_service.pyindex c9ae250b..bada8a07 100644--- a/backend/app/services/template_service.py+++ b/backend/app/services/template_service.py@@ -29,6 +29,7 @@ import requestsfrom app import pathsfrom app.utils.system import run_checked+from app.services.deploy_lock import with_deploy_lockclass TemplateService:@@ -2248,6 +2249,7 @@ class TemplateService:}@classmethod+ @with_deploy_lock('template update')def update_app(cls, app_id: int, user_id: int = None,log_callback: Callable[[str], None] = None) -> Dict:"""Update an installed app to the latest template version.@@ -2386,10 +2388,18 @@ class TemplateService:compose_result = DockerService.compose_up(app_path, detach=True, build=True)if not compose_result.get('success'):- # Rollback+ # Rollback — and say whether it worked: an unchecked `up` here+ # reported a plain failure while the app sat stopped (§A6).if os.path.exists(backup_path):shutil.copy(backup_path, compose_path)- DockerService.compose_up(app_path, detach=True)+ restored = DockerService.compose_up(app_path, detach=True)+ compose_result = dict(compose_result)+ compose_result['restored'] = bool(restored.get('success'))+ if not restored.get('success'):+ compose_result['error'] = (+ f"{compose_result.get('error') or 'docker compose up failed'}; "+ f"restoring the previous version also failed: "+ f"{restored.get('error') or 'unknown error'} — the app is down")return compose_result# Run post-update scriptdiff --git a/backend/migrations/versions/106_app_deploy_settings.py b/backend/migrations/versions/106_app_deploy_settings.pynew file mode 100644index 00000000..10c3375d--- /dev/null+++ b/backend/migrations/versions/106_app_deploy_settings.py@@ -0,0 +1,23 @@+"""Per-app deploy settings: health-gate timeout and 4xx rule (plan 87 §A4).++One JSON column rather than a column per knob: the slot engine (plan 87 §B)+adds watch-window and standby settings to the same object. NULL = defaults.+"""+from alembic import op+import sqlalchemy as sa++revision = '106_app_deploy_settings'+down_revision = '105_drop_container_scale_policies'+branch_labels = None+depends_on = None+++def upgrade():+ inspector = sa.inspect(op.get_bind())+ # Add-if-missing: the pre-alembic schema sync may already have added it.+ if 'deploy_settings' not in {c['name'] for c in inspector.get_columns('applications')}:+ op.add_column('applications', sa.Column('deploy_settings', sa.Text, nullable=True))+++def downgrade():+ op.drop_column('applications', 'deploy_settings')diff --git a/backend/tests/test_manifest_persistence.py b/backend/tests/test_manifest_persistence.pyindex 67478de1..6ea1a5f6 100644--- a/backend/tests/test_manifest_persistence.py+++ b/backend/tests/test_manifest_persistence.py@@ -106,13 +106,17 @@ def test_health_wait_falls_back_without_path(app):a.healthcheck_path = Nonea.port = Nonemsg = GitDeployService._wait_for_health(a, timeout=1, fallback=0)- assert 'no health check' in msg+ assert 'no port to health-check' in msgdef 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 pytestfrom app.models import Application+ from app.services.health_gate import HealthGateErrora = Application(name='x', app_type='docker', user_id=1)a.healthcheck_path = '/health'a.port = 1 # nothing listening- msg = GitDeployService._wait_for_health(a, timeout=1)- assert 'did not pass' in msg+ with pytest.raises(HealthGateError, match='did not pass'):+ GitDeployService._wait_for_health(a, timeout=1)diff --git a/backend/tests/test_plan87_deploy_foundations.py b/backend/tests/test_plan87_deploy_foundations.pynew file mode 100644index 00000000..2a317652--- /dev/null+++ b/backend/tests/test_plan87_deploy_foundations.py@@ -0,0 +1,382 @@+"""Plan 87 §A — deploy foundations. One regression test per verified bug; each+fails on the code before the fix.++- A1 every build was tagged :latest, so a rollback redeployed the NEW image+- A2 commit_hash was read before the pull (named the old commit)+- A3 the health wait never failed a deploy, and counted a 4xx as healthy+- A4 health path + timeout are editable settings+- A5 two deploys of one app raced+- A6 a compose rollback changed files and restarted nothing; rollback with no+ version found no target at all+"""+import threading+import time+import types++import pytest++from factories import make_application++from app import db+from app.models.deployment import Deployment+from app.services import deploy_lock, deploy_settings, health_gate+from app.services import deploy_preflight_service as preflight+from app.services.build_service import BuildService+from app.services.deployment_service import DeploymentService+from app.services.docker_service import DockerService+from app.services.git_service import GitService+from app.services.health_gate import HealthGateError+++def _image_app(**kw):+ fields = dict(name='imgapp', app_type='docker', status='running', root_path='/srv/img',+ docker_image=None, compose_file=None, managed_by=None, port=9300)+ fields.update(kw)+ return make_application(db, **fields)+++def image_deploy(monkeypatch):+ """Stub build + run so deploy()/rollback() touch no Docker or Git."""+ seen = {'built': [], 'ran': []}+ monkeypatch.setattr(GitService, 'get_commit_info', classmethod(lambda cls, *a: None))+ monkeypatch.setattr(GitService, 'get_app_config', classmethod(lambda cls, app_id: None))+ monkeypatch.setattr(BuildService, 'get_app_build_config', classmethod(lambda cls, app_id: None))++ def build(cls, app_id, no_cache=False, log_callback=None, image_tag=None):+ tag = image_tag or f'serverkit-app-{app_id}:latest'+ seen['built'].append(tag)+ return {'success': True, 'image_tag': tag}++ monkeypatch.setattr(BuildService, 'build', classmethod(build))+ monkeypatch.setattr(DeploymentService, '_deploy_docker', classmethod(+ lambda cls, app, deployment, log_callback=None:+ seen['ran'].append(deployment.image_tag) or {'success': True, 'container_id': 'c1'}))+ monkeypatch.setattr(DockerService, 'run', classmethod(+ lambda cls, args, **kw: {'success': True, 'output': ''}))+ return seen+++# ── A1 immutable image tags ──────────────────────────────────────────────────++def test_each_deploy_builds_its_own_immutable_tag(app, image_deploy):+ row = _image_app()+ first = DeploymentService.deploy(row.id)['deployment']+ second = DeploymentService.deploy(row.id)['deployment']+ assert first['image_tag'] == f'serverkit-app-{row.id}:d{first["id"]}'+ assert second['image_tag'] == f'serverkit-app-{row.id}:d{second["id"]}'+ assert first['image_tag'] != second['image_tag']+++def test_rollback_redeploys_the_image_that_actually_ran(app, image_deploy):+ row = _image_app()+ first = DeploymentService.deploy(row.id)['deployment']+ second = DeploymentService.deploy(row.id)['deployment']+ result = DeploymentService.rollback(row.id, target_version=first['version'])+ assert result['success'], result+ # Rolled back to v1's own image — not the tag v2's build overwrote.+ assert image_deploy['ran'][-1] == first['image_tag']+ assert image_deploy['ran'][-1] != second['image_tag']+++def test_old_deploy_images_are_pruned_but_recent_and_live_kept(app, monkeypatch):+ from app.services import app_image_retention+ row = _image_app()+ rows = []+ for version in range(1, 6):+ dep = Deployment(app_id=row.id, version=version, status='rolled_back')+ db.session.add(dep)+ db.session.flush()+ dep.image_tag = app_image_retention.deploy_image_tag(row.id, dep.id)+ rows.append(dep)+ rows[0].status = 'live' # an old deployment that is somehow still live+ db.session.commit()+ tags = [f'd{d.id}' for d in rows] + ['latest']+ monkeypatch.setattr(DockerService, 'run', classmethod(+ lambda cls, args, **kw: {'success': True, 'output': '\n'.join(tags)}))+ 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]+++# ── A2 commit recorded after the pull ────────────────────────────────────────++def test_the_deployment_records_the_commit_the_pull_checked_out(app, image_deploy, monkeypatch):+ head = {'hash': 'a' * 40, 'message': 'old'}+ monkeypatch.setattr(GitService, 'get_commit_info', classmethod(lambda cls, *a: dict(head)))+ monkeypatch.setattr(GitService, 'get_app_config', classmethod(lambda cls, app_id: {'branch': 'main'}))++ def pull(cls, path, branch=None):+ head.update(hash='b' * 40, message='new')+ return {'success': True}++ monkeypatch.setattr(GitService, 'pull_changes', classmethod(pull))+ row = _image_app()+ result = DeploymentService.deploy(row.id)+ assert result['deployment']['commit_hash'] == 'b' * 40+ assert result['deployment']['commit_message'] == 'new'+++# ── A3 the health gate ───────────────────────────────────────────────────────++class _Clock:+ def __init__(self):+ self.now = 0.0++ def __call__(self):+ return self.now++ def sleep(self, seconds):+ self.now += seconds+++def _gate(statuses, **kw):+ clock = _Clock()+ answers = iter(statuses)+ calls = []++ def probe(url, host_header=None):+ calls.append(url)+ status = next(answers, statuses[-1])+ return (status, None) if isinstance(status, int) else (None, status)++ kw.setdefault('timeout', 10)+ result = health_gate.wait_healthy(9300, kw.pop('path', '/health'), _probe=probe,+ _sleep=clock.sleep, _clock=clock, **kw)+ return result, calls+++def test_a_4xx_on_the_health_path_fails_the_gate():+ with pytest.raises(HealthGateError) as info:+ _gate([404])+ assert info.value.last == 404+++def test_a_4xx_passes_only_when_explicitly_allowed():+ result, _ = _gate([401], allow_4xx=True)+ assert result['status'] == 401+++def test_a_release_that_never_answers_fails_after_the_timeout():+ with pytest.raises(HealthGateError) as info:+ _gate(['Connection refused'], timeout=5)+ assert 'did not pass within 5s' in str(info.value)+++def test_it_takes_consecutive_passes_a_blip_resets_the_streak():+ result, calls = _gate([200, 500, 200, 200, 200], consecutive=3)+ assert result['status'] == 200 and len(calls) == 5+++def test_an_exited_container_fails_at_once_not_after_the_timeout():+ with pytest.raises(HealthGateError) as info:+ _gate([200], container='c', timeout=600,+ _container_health=lambda name: ('exited', ''))+ assert 'exited' in str(info.value)+++def test_a_docker_healthcheck_still_starting_is_not_a_pass():+ states = iter([('running', 'starting')] * 2 + [('running', 'healthy')] * 3)+ result, calls = _gate([200], container='c', consecutive=3,+ _container_health=lambda name: next(states))+ assert len(calls) == 5+++def test_the_gate_reads_the_apps_settings(app):+ row = _image_app(healthcheck_path='ready')+ deploy_settings.update(row, {'healthcheck_timeout': 7, 'healthcheck_allow_4xx': True})+ seen = {}++ def fake(port, path, **kw):+ seen.update(port=port, path=path, **kw)+ return {'url': 'u', 'status': 200}++ original = health_gate.wait_healthy+ health_gate.wait_healthy = fake+ try:+ health_gate.gate_for_app(row)+ finally:+ health_gate.wait_healthy = original+ assert seen['port'] == 9300 and seen['path'] == 'ready'+ 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):+ row = _image_app()+ resp = client.put(f'/api/v1/apps/{row.id}', headers=auth_headers, json={+ 'healthcheck_path': '/ready',+ 'deploy_settings': {'healthcheck_timeout': 45, 'healthcheck_allow_4xx': True},+ })+ assert resp.status_code == 200, resp.get_json()+ body = resp.get_json()['app']+ assert body['healthcheck_path'] == '/ready'+ assert body['deploy_settings']['healthcheck_timeout'] == 45+ assert body['deploy_settings']['healthcheck_allow_4xx'] is True+ assert body['deploy_settings']['healthcheck_consecutive'] == 3++[email protected]('bad', [{'healthcheck_timeout': 0}, {'healthcheck_timeout': 'x'},+ {'nope': 1}, {'healthcheck_allow_4xx': 'yes'}])+def test_bad_deploy_settings_are_rejected(app, client, auth_headers, bad):+ row = _image_app()+ resp = client.put(f'/api/v1/apps/{row.id}', headers=auth_headers,+ json={'deploy_settings': bad})+ assert resp.status_code == 400+++# ── A5 per-app deploy lock ───────────────────────────────────────────────────++def test_a_second_deploy_of_the_same_app_waits_for_the_first():+ order = []+ release_first = threading.Event()++ def first():+ with deploy_lock.deploy_lock(41, 'first'):+ order.append('first-start')+ release_first.wait(5)+ order.append('first-end')++ def second():+ with deploy_lock.deploy_lock(41, 'second'):+ order.append('second-start')++ t1 = threading.Thread(target=first)+ t1.start()+ while 'first-start' not in order:+ time.sleep(0.01)+ t2 = threading.Thread(target=second)+ t2.start()+ time.sleep(0.1)+ assert order == ['first-start'], 'the second deploy ran while the first held the app'+ release_first.set()+ t1.join(5)+ t2.join(5)+ assert order == ['first-start', 'first-end', 'second-start']+++def test_other_apps_do_not_wait():+ with deploy_lock.deploy_lock(51, 'a'):+ done = []+ t = threading.Thread(target=lambda: deploy_lock.deploy_lock(52, 'b').__enter__() or done.append(1))+ t.start()+ t.join(2)+ assert done == [1]+++def test_the_lock_is_reentrant_for_a_deploy_that_rolls_itself_back():+ with deploy_lock.deploy_lock(61, 'deploy'):+ with deploy_lock.deploy_lock(61, 'auto-revert'):+ assert deploy_lock.holder(61) == 'deploy'+ assert not deploy_lock.is_locked(61)+++def test_every_redeploy_path_holds_the_lock(app, image_deploy, monkeypatch):+ from app.services.git_deploy_service import GitDeployService+ from app.services.template_service import TemplateService+ row = _image_app()+ held = {}++ def spy(label):+ def record(*a, **k):+ held[label] = deploy_lock.is_locked(row.id)+ raise RuntimeError('stop here')+ return record++ monkeypatch.setattr(BuildService, 'get_app_build_config', classmethod(spy('deploy')))+ with pytest.raises(RuntimeError):+ DeploymentService.deploy(row.id)+ monkeypatch.setattr(GitDeployService, '_create_snapshot', classmethod(spy('webhook')))+ GitDeployService.deploy(row.id)+ monkeypatch.setattr(TemplateService, 'get_config', classmethod(spy('template')))+ with pytest.raises(RuntimeError):+ TemplateService.update_app(row.id)+ monkeypatch.setattr(Deployment, 'get_current', classmethod(spy('rollback')))+ with pytest.raises(RuntimeError):+ DeploymentService.rollback(row.id)+ assert held == {'deploy': True, 'webhook': True, 'template': True, 'rollback': True}+ assert not deploy_lock.is_locked(row.id)+++# ── A6 rollback actually restarts ────────────────────────────────────────────++def _compose_history(row):+ for version, status, sha in ((1, 'rolled_back', 'a' * 40), (2, 'live', 'b' * 40)):+ db.session.add(Deployment(app_id=row.id, version=version, status=status,+ commit_hash=sha, deploy_completed_at=db.func.now()))+ db.session.commit()+++def test_rollback_without_a_version_finds_the_release_before(app, monkeypatch):+ row = _image_app(compose_file='docker-compose.yml')+ _compose_history(row)+ target = Deployment.get_previous(row.id, 2)+ assert target is not None and target.version == 1+++def test_a_compose_rollback_recreates_the_containers(app, monkeypatch):+ from app.services import deployment_service+ row = _image_app(compose_file='docker-compose.yml')+ _compose_history(row)+ steps = []+ monkeypatch.setattr(GitService, 'get_app_config', classmethod(lambda cls, app_id: None))+ monkeypatch.setattr(deployment_service, 'run_checked', lambda cmd, **kw: (+ steps.append(('git', cmd[-1])) or {'success': True, 'output': '', 'error': None}))+ monkeypatch.setattr(preflight, 'preflight_compose_project',+ lambda path, compose_file=None, **k: steps.append(('preflight',))+ or preflight.PreflightResult())+ monkeypatch.setattr(DockerService, 'compose_up', classmethod(+ lambda cls, path, detach=True, build=False, compose_file=None:+ steps.append(('up', build)) or {'success': True}))++ result = DeploymentService.rollback(row.id)++ assert result['success'], result+ assert steps == [('git', 'a' * 40), ('preflight',), ('up', True)]+++def test_a_failed_template_update_says_when_the_restore_failed_too(app, monkeypatch, tmp_path):+ from app.services.template_service import TemplateService+ row = _image_app(root_path=str(tmp_path))+ (tmp_path / 'docker-compose.yml').write_text('services: {web: {image: old}}')+ monkeypatch.setattr(TemplateService, 'get_config', classmethod(+ lambda cls: {'installed': {str(row.id): {'template_id': 't'}}}))+ 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}}'))+ monkeypatch.setattr(preflight, 'preflight_compose_project',+ lambda *a, **k: preflight.PreflightResult())+ monkeypatch.setattr(DockerService, 'compose_down', classmethod(lambda cls, path, **k: {'success': True}))+ monkeypatch.setattr(DockerService, 'compose_pull', classmethod(lambda cls, path, **k: {'success': True}))+ monkeypatch.setattr(DockerService, 'compose_up', classmethod(+ lambda cls, path, **k: {'success': False, 'error': 'port is already allocated'}))++ result = TemplateService.update_app(row.id)++ assert result['success'] is False and result['restored'] is False+ assert 'the app is down' in result['error']diff --git a/frontend/src/components/apps/DeploySafetyPanel.jsx b/frontend/src/components/apps/DeploySafetyPanel.jsxnew file mode 100644index 00000000..13ebb480--- /dev/null+++ b/frontend/src/components/apps/DeploySafetyPanel.jsx@@ -0,0 +1,136 @@+import { useState } from 'react';+import { HeartPulse } from 'lucide-react';+import api from '../../services/api';+import { useToast } from '../../contexts/useToast.js';+import { Button } from '@/components/ui/button';+import { Input } from '@/components/ui/input';+import { Switch } from '@/components/ui/switch';+import { useTranslation } from 'react-i18next';++// Must match app/services/deploy_settings.py DEFAULTS / _RULES.+const TIMEOUT_DEFAULT = 120;+const TIMEOUT_MIN = 5;+const TIMEOUT_MAX = 1800;++// Health gate settings (plan 87 §A4). A deploy waits for the new release to+// answer its health path before it counts as live; these say where to ask and+// how long to wait.+const DeploySafetyPanel = ({ app, onChanged }) => {+ const { t } = useTranslation();+ const toast = useToast();+ const settings = app.deploy_settings || {};+ const [path, setPath] = useState(app.healthcheck_path || '');+ const [timeout, setTimeoutValue] = useState(String(settings.healthcheck_timeout ?? TIMEOUT_DEFAULT));+ const [allow4xx, setAllow4xx] = useState(!!settings.healthcheck_allow_4xx);+ const [saving, setSaving] = useState(false);++ const timeoutNumber = Number(timeout);+ const timeoutValid = Number.isInteger(timeoutNumber)+ && timeoutNumber >= TIMEOUT_MIN && timeoutNumber <= TIMEOUT_MAX;+ const dirty = path.trim() !== (app.healthcheck_path || '')+ || (timeoutValid && timeoutNumber !== (settings.healthcheck_timeout ?? TIMEOUT_DEFAULT))+ || allow4xx !== !!settings.healthcheck_allow_4xx;++ async function handleSave() {+ setSaving(true);+ try {+ await api.updateApp(app.id, {+ healthcheck_path: path.trim(),+ deploy_settings: {+ healthcheck_timeout: timeoutNumber,+ healthcheck_allow_4xx: allow4xx,+ },+ });+ toast.success(t('app.deploySafetyPanel.saved', 'Health check settings saved.'));+ onChanged?.();+ } catch (err) {+ toast.error(err.message || t('app.deploySafetyPanel.saveFailed', 'Failed to save health check settings'));+ } finally {+ setSaving(false);+ }+ }++ return (+ <div className="app-panel">+ <div className="app-panel-header">+ <HeartPulse />+ <span>{t('app.deploySafetyPanel.healthCheck', 'Health check')}</span>+ </div>+ <div className="app-panel-body">+ <p className="app-panel-hint">+ {t('app.deploySafetyPanel.intro', 'Before a new release counts as live, ServerKit asks it for this path and waits for a 2xx or 3xx answer three times in a row. A release that never answers fails the deploy.')}+ </p>++ <div className="settings-row">+ <div className="settings-label">+ <label htmlFor={`health-path-${app.id}`}>+ {t('app.deploySafetyPanel.path', 'Health check path')}+ </label>+ <span className="settings-hint">+ {t('app.deploySafetyPanel.pathHint', 'Leave empty to use /. Point it at a page that answers without a login.')}+ </span>+ </div>+ <div className="settings-control">+ <Input+ id={`health-path-${app.id}`}+ placeholder="/"+ value={path}+ onChange={(e) => setPath(e.target.value)}+ disabled={saving}+ />+ </div>+ </div>++ <div className="settings-row">+ <div className="settings-label">+ <label htmlFor={`health-timeout-${app.id}`}>+ {t('app.deploySafetyPanel.timeout', 'Timeout (seconds)')}+ </label>+ <span className="settings-hint">+ {t('app.deploySafetyPanel.timeoutHint', 'How long a new release may take to start answering ({{min}}–{{max}}).', { min: TIMEOUT_MIN, max: TIMEOUT_MAX })}+ </span>+ </div>+ <div className="settings-control">+ <Input+ id={`health-timeout-${app.id}`}+ type="number"+ min={TIMEOUT_MIN}+ max={TIMEOUT_MAX}+ value={timeout}+ onChange={(e) => setTimeoutValue(e.target.value)}+ disabled={saving}+ />+ </div>+ </div>++ <div className="settings-row">+ <div className="settings-label">+ <span>{t('app.deploySafetyPanel.allow4xx', 'Accept a 4xx answer')}</span>+ <span className="settings-hint">+ {t('app.deploySafetyPanel.allow4xxHint', 'Only for a health path that sits behind a login and answers 401 or 403 when the app is up. Otherwise a 4xx means the release is broken.')}+ </span>+ </div>+ <div className="settings-control">+ <Switch+ checked={allow4xx}+ onCheckedChange={setAllow4xx}+ disabled={saving}+ aria-label={t('app.deploySafetyPanel.allow4xx', 'Accept a 4xx answer')}+ />+ </div>+ </div>++ <div className="settings-row">+ <div className="settings-label" />+ <div className="settings-control">+ <Button size="sm" onClick={handleSave} disabled={saving || !dirty || !timeoutValid}>+ {saving ? t('common.editing.saving', 'Saving…') : t('common.actions.save', 'Save')}+ </Button>+ </div>+ </div>+ </div>+ </div>+ );+};++export default DeploySafetyPanel;diff --git a/frontend/src/components/service-detail/SettingsTab.jsx b/frontend/src/components/service-detail/SettingsTab.jsxindex 33b5d151..8df5431b 100644--- a/frontend/src/components/service-detail/SettingsTab.jsx+++ b/frontend/src/components/service-detail/SettingsTab.jsx@@ -17,6 +17,7 @@ import {CircleX,Sparkles,Zap,+ HeartPulse,} from 'lucide-react';import api from '../../services/api';import { useToast } from '../../contexts/useToast.js';@@ -28,6 +29,7 @@ import ContainerOpsPanel from '../apps/ContainerOpsPanel';import VolumesPanel from '../apps/VolumesPanel';import ResourceLimitsPanel from '../apps/ResourceLimitsPanel';import MicroCachePanel from '../apps/MicroCachePanel';+import DeploySafetyPanel from '../apps/DeploySafetyPanel';import AppWafPanel from '../apps/AppWafPanel';import BuildTab from '../appdetail/BuildTab';import DeployTab from '../appdetail/DeployTab';@@ -75,6 +77,7 @@ function buildSettingsGroups(app) {items: [{ id: 'git', labelKey: 'app.settingsTab.gitDeploy', label: 'Git & Deploy', icon: GitBranch },{ id: 'build', labelKey: 'app.settingsTab.build', label: 'Build', icon: Hammer },+ ...(isDocker ? [{ id: 'health', labelKey: 'app.settingsTab.healthRollout', label: 'Health & Rollout', icon: HeartPulse }] : []),{ id: 'manifest', labelKey: 'app.settingsTab.manifest', label: 'Manifest', icon: Zap },],},@@ -347,6 +350,15 @@ const SettingsTab = ({ app, deployConfig, domains, primaryDomain, onUpdate }) =></div>)}+ {/* Health & Rollout (Docker only) — the deploy health gate+ (plan 87): where a new release is asked, and for how long. */}+ {section === 'health' && (+ <div className="svc-settings__section">+ <h3 className="svc-settings__section-title">{t('app.settingsTab.healthRollout', 'Health & Rollout')}</h3>+ <DeploySafetyPanel app={app} onChanged={onUpdate} />+ </div>+ )}+{/* Storage (Docker only) — first-class managed volumes thatsurvive redeploys, replacing fragile relative bind mounts. */}{section === 'storage' && (diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.jsonindex aca4666e..558d7f26 100644--- a/frontend/src/i18n/locales/en.json+++ b/frontend/src/i18n/locales/en.json@@ -1713,6 +1713,18 @@"deployPill": {"openTheDeployConsole": "Open the Deploy Console"},+ "deploySafetyPanel": {+ "allow4xx": "Accept a 4xx answer",+ "allow4xxHint": "Only for a health path that sits behind a login and answers 401 or 403 when the app is up. Otherwise a 4xx means the release is broken.",+ "healthCheck": "Health check",+ "intro": "Before a new release counts as live, ServerKit asks it for this path and waits for a 2xx or 3xx answer three times in a row. A release that never answers fails the deploy.",+ "path": "Health check path",+ "pathHint": "Leave empty to use /. Point it at a page that answers without a login.",+ "saveFailed": "Failed to save health check settings",+ "saved": "Health check settings saved.",+ "timeout": "Timeout (seconds)",+ "timeoutHint": "How long a new release may take to start answering ({{min}}–{{max}})."+ },"deployTab": {"anImmutableConfigCheckpointEnvKeys": "An immutable config checkpoint (env keys, domains, image, build method, volumes) is captured before each deployment. Secret values are masked. Open a checkpoint to diff it against the previous one or restore it.","autoDeploy": "Auto Deploy",@@ -5362,6 +5374,7 @@"general": "General","gitDeploy": "Git & Deploy","giveItASubdomain": "Give it a subdomain",+ "healthRollout": "Health & Rollout","issuer": "Issuer","learnAboutServerkitYaml": "Learn about serverkit.yaml","linkARepoSoServerkitCan": "Link a repo so ServerKit can pull your code and redeploy on every push.",diff --git a/frontend/src/i18n/locales/es.json b/frontend/src/i18n/locales/es.jsonindex 8b7704dc..a4318415 100644--- a/frontend/src/i18n/locales/es.json+++ b/frontend/src/i18n/locales/es.json@@ -5093,7 +5093,8 @@"unlinkFromItsLinkedApplication": "¿Desvincular {{name}} de su aplicación vinculada?","unlinkingWillResetBothAppsTo": "Al desvincularlas, ambas apps volverán al modo independiente.","usedForCertificateExpiryRemindersFrom": "Se usa para los recordatorios de vencimiento de certificados de Let's Encrypt.",- "whichRegisteredBaseDomainToPublish": "Bajo qué dominio base registrado se publicará este servicio."+ "whichRegisteredBaseDomainToPublish": "Bajo qué dominio base registrado se publicará este servicio.",+ "healthRollout": "Salud y despliegue"},"setupHealthWidget": {"allSet": "Todo listo —",@@ -6782,6 +6783,18 @@"crashLoopFailure": "Una política de reinicio oculta un fallo: la app parece en marcha entre fallos.","crashLoopSignal": "{{count}} reinicios en los últimos 10 minutos.","openLogs": "Abrir registros"+ },+ "deploySafetyPanel": {+ "allow4xx": "Aceptar una respuesta 4xx",+ "allow4xxHint": "Solo para una ruta de salud que está detrás de un inicio de sesión y responde 401 o 403 cuando la app funciona. En otro caso, un 4xx significa que la versión está rota.",+ "healthCheck": "Comprobación de salud",+ "intro": "Antes de que una versión nueva se considere activa, ServerKit le pide esta ruta y espera una respuesta 2xx o 3xx tres veces seguidas. Una versión que nunca responde hace fallar el despliegue.",+ "path": "Ruta de comprobación de salud",+ "pathHint": "Déjala vacía para usar /. Apúntala a una página que responda sin iniciar sesión.",+ "saveFailed": "No se pudo guardar la comprobación de salud",+ "saved": "Comprobación de salud guardada.",+ "timeout": "Tiempo límite (segundos)",+ "timeoutHint": "Cuánto puede tardar una versión nueva en empezar a responder ({{min}}–{{max}})."}},"auth": {