ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(deploy): let a failed install be retried from the panel
Juan Denis658f51dbbd96
diff --git a/backend/app/services/deployment_job_service.py b/backend/app/services/deployment_job_service.pyindex 0ae937e2..b9fe2d8b 100644--- a/backend/app/services/deployment_job_service.py+++ b/backend/app/services/deployment_job_service.py@@ -66,7 +66,17 @@ class DeploymentJobService:app_path = plan_result['app_path']if not normalized_server_id and os.path.exists(app_path):- return {'success': False, 'error': f"App directory already exists: {app_path}"}+ if not cls._is_abandoned_install_dir(app_path):+ return {+ 'success': False,+ 'error': (f'App directory already exists: {app_path}. '+ 'Remove it, or install under a different name.'),+ }+ # An install that failed part-way leaves its directory behind, and+ # refusing it made the obvious next move — try again — impossible+ # without going to a shell. The plan rewrites every file it owns,+ # so reusing the directory is safe.+ logger.info('Reusing abandoned install directory %s', app_path)job = DeploymentJob(id=str(uuid.uuid4()),@@ -690,6 +700,28 @@ class DeploymentJobService:except Exception:db.session.rollback()+ @staticmethod+ def _is_abandoned_install_dir(app_path: str) -> bool:+ """True if *app_path* is the wreckage of an install that never finished.++ A finished install owns an Application row pointing at the directory,+ and that row is only written once the plan has run — so a directory+ with no owner was left by a failure. Reusing one is safe; overwriting+ somebody's actual files is not, which is why an unowned directory still+ has to look like ours (empty, or carrying the marker the template+ writer drops in) before it can be reused.+ """+ try:+ if Application.query.filter_by(root_path=app_path).first():+ return False+ entries = os.listdir(app_path)+ if not entries:+ return True+ return '.serverkit-template.json' in entries+ except OSError:+ # Unreadable — treat it as occupied and let the caller refuse.+ return False+@staticmethoddef _normalize_server_id(server_id: Optional[str]) -> Optional[str]:if not server_id or server_id == 'local':diff --git a/backend/tests/test_install_retry_after_failure.py b/backend/tests/test_install_retry_after_failure.pynew file mode 100644index 00000000..4247bbac--- /dev/null+++ b/backend/tests/test_install_retry_after_failure.py@@ -0,0 +1,107 @@+"""Retrying an install that failed part-way.++A template install writes its directory before it starts containers, so a+failure at "Start Docker Compose stack" leaves the directory behind. The guard+against clobbering an existing app then refused the retry — "App directory+already exists" — and the only way forward was a shell. That is a dead end in+the one flow where a user is most likely to try again.++A directory with no Application row pointing at it was left by a failure: the+row is written only after the plan completes. Those can be reused. Anything+else is somebody's data and is still refused.+"""++import os++import pytest++from app import db+from app.models.application import Application+from app.services.deployment_job_service import DeploymentJobService+++def app_dir(tmp_path):+ path = tmp_path / 'uptime-kuma'+ path.mkdir()+ return str(path)+++class TestAbandonedDirectoryDetection:+ def test_a_failed_installs_leftovers_are_reusable(self, app, app_dir):+ # What the deploy wrote before it failed.+ open(os.path.join(app_dir, 'docker-compose.yml'), 'w').close()+ open(os.path.join(app_dir, '.serverkit-template.json'), 'w').close()++ assert DeploymentJobService._is_abandoned_install_dir(app_dir) is True++ def test_an_empty_directory_is_reusable(self, app, app_dir):+ assert DeploymentJobService._is_abandoned_install_dir(app_dir) is True++ def test_a_directory_owned_by_an_app_is_not(self, app, app_dir):+ # A finished install: the Application row is written once the plan has+ # run, so its presence means this is a live app, not wreckage.+ open(os.path.join(app_dir, '.serverkit-template.json'), 'w').close()+ db.session.add(Application(name='uptime-kuma', app_type='docker',+ status='running', root_path=app_dir, user_id=1))+ db.session.commit()++ assert DeploymentJobService._is_abandoned_install_dir(app_dir) is False++ def test_someone_elses_files_are_never_reused(self, app, app_dir):+ # No marker and not empty — could be anything. Refuse rather than+ # overwrite it.+ open(os.path.join(app_dir, 'important.db'), 'w').close()++ assert DeploymentJobService._is_abandoned_install_dir(app_dir) is False++ def test_an_unreadable_directory_counts_as_occupied(self, app, monkeypatch):+ def _boom(_path):+ raise PermissionError('nope')++ monkeypatch.setattr(os, 'listdir', _boom)+ assert DeploymentJobService._is_abandoned_install_dir('/root/secret') is False+++class TestInstallGuard:+ def _install(self, monkeypatch, app_path, template_id='actualbudget'):+ """Drive install_template far enough to hit the directory guard."""+ monkeypatch.setattr(+ 'app.services.template_service.TemplateService.build_install_plan',+ classmethod(lambda cls, **kwargs: {+ 'success': True, 'app_path': app_path,+ 'plan': {'steps': [], 'app_name': kwargs.get('app_name')},+ }))+ # Stop before anything is queued; the guard is what is under test.+ monkeypatch.setattr(DeploymentJobService, '_enqueue_install',+ classmethod(lambda cls, job: None))+ return DeploymentJobService.install_template(+ template_id=template_id, app_name='uptime-kuma', user_id=1)++ def test_retry_after_a_failed_install_is_allowed(self, app, app_dir, monkeypatch):+ open(os.path.join(app_dir, '.serverkit-template.json'), 'w').close()++ result = self._install(monkeypatch, app_dir)++ assert result['success'] is True, result.get('error')++ def test_a_real_directory_conflict_still_refuses_and_says_what_to_do(+ self, app, app_dir, monkeypatch):+ open(os.path.join(app_dir, 'important.db'), 'w').close()++ result = self._install(monkeypatch, app_dir)++ assert result['success'] is False+ assert 'already exists' in result['error']+ # The old message was a dead end; this one names a way forward.+ assert 'different name' in result['error']++ def test_the_name_collision_guard_is_untouched(self, app, app_dir, monkeypatch):+ db.session.add(Application(name='uptime-kuma', app_type='docker',+ status='running', root_path='/elsewhere', user_id=1))+ db.session.commit()++ result = self._install(monkeypatch, app_dir)++ assert result['success'] is False+ assert 'already exists on this target server' in result['error']