Skip to content

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.

fix: purge reclaims every ServerKit-managed app directory, not just uploads

Juan Denisd65a6ae5b74d
diff --git a/backend/app/services/application_restore.py b/backend/app/services/application_restore.py
index 3dde6310..a3e19edb 100644
--- a/backend/app/services/application_restore.py
+++ b/backend/app/services/application_restore.py
@@ -137,7 +137,6 @@ def on_purge_application(app):
"""
from app import paths
from app.services.docker_service import DockerService
- from app.services.upload_service import get_app_storage_dir
# A purge normally reclaims the volumes -- that is most of what it is for.
# `_purge_remove_data` is the one exception: DELETE ?purge=true skips the
@@ -154,16 +153,23 @@ def on_purge_application(app):
except Exception as exc: # noqa: BLE001
logger.warning('Purge %s: compose down failed: %s', app.name, exc)
- # Only ServerKit-managed uploads. A `manual` app points at a directory the
- # operator already had, and removing that would destroy data ServerKit never
- # created — the same guard delete_app has always carried.
+ # Reclaim the app's directory whenever ServerKit created it — anything
+ # whose root_path lives under APPS_DIR: a git clone, an uploaded source
+ # tree, a template render. A purge that leaves the directory behind blocks
+ # the name forever ("App directory already exists" on the next create)
+ # while the Recycle Bin shows nothing to explain it. The one exception is
+ # a `manual` app: its root_path points at a directory the operator already
+ # had, and removing that would destroy data ServerKit never created — the
+ # same guard delete_app has always carried.
try:
- if app.source == 'upload' and app.root_path and app.root_path.startswith(paths.APPS_DIR):
- storage = get_app_storage_dir(app.name)
- if os.path.exists(storage):
- shutil.rmtree(storage)
+ base = os.path.abspath(paths.APPS_DIR)
+ root = os.path.abspath(app.root_path) if app.root_path else None
+ if (app.source != 'manual' and root
+ and root.startswith(base + os.sep)
+ and os.path.exists(root)):
+ shutil.rmtree(root)
except Exception as exc: # noqa: BLE001
- logger.warning('Purge %s: storage removal failed: %s', app.name, exc)
+ logger.warning('Purge %s: root path removal failed: %s', app.name, exc)
# Now that the app is going for good, its cron jobs fall back to the System
# bucket rather than pointing at an id that no longer resolves.
diff --git a/backend/tests/test_application_soft_delete.py b/backend/tests/test_application_soft_delete.py
index 4094f107..a0ddb6db 100644
--- a/backend/tests/test_application_soft_delete.py
+++ b/backend/tests/test_application_soft_delete.py
@@ -358,6 +358,74 @@ def test_purge_honours_an_explicit_remove_data_false(app, dead_app, monkeypatch)
assert calls and calls[0].get('volumes') is False
+# ------------------------------------------------------------- purge cleanup
+
+def _stub_purge_side_effects(monkeypatch):
+ """compose_down and cron detach are not what these tests are about."""
+ from app.services import docker_service
+ monkeypatch.setattr(docker_service.DockerService, 'compose_down',
+ classmethod(lambda cls, path, **kw: {'success': True}))
+ monkeypatch.setattr(application_restore.CronService, 'clear_application',
+ classmethod(lambda cls, _id: 0))
+
+
+def test_purge_removes_a_managed_clone_regardless_of_source(app, monkeypatch, tmp_path):
+ """A purged git app must take its clone with it. Leaving the directory
+ blocks the name on the next create ("App directory already exists") while
+ the Recycle Bin shows nothing to explain it — the field failure this
+ pins."""
+ from app import paths
+ _stub_purge_side_effects(monkeypatch)
+ monkeypatch.setattr(paths, 'APPS_DIR', str(tmp_path))
+ clone = tmp_path / 'repo-app'
+ clone.mkdir()
+ (clone / 'docker-compose.yml').write_text('services: {}')
+
+ with app.app_context():
+ row = Application(name='repo-app', app_type='docker', user_id=1,
+ root_path=str(clone), source='github')
+ application_restore.on_purge_application(row)
+
+ assert not clone.exists()
+
+
+def test_purge_never_removes_a_manual_apps_directory(app, monkeypatch, tmp_path):
+ """A manual app's root_path is a directory the operator already had —
+ removing it would destroy data ServerKit never created, even when it
+ happens to sit under APPS_DIR."""
+ from app import paths
+ _stub_purge_side_effects(monkeypatch)
+ monkeypatch.setattr(paths, 'APPS_DIR', str(tmp_path))
+ existing = tmp_path / 'my-site'
+ existing.mkdir()
+ (existing / 'index.php').write_text('<?php')
+
+ with app.app_context():
+ row = Application(name='my-site', app_type='php', user_id=1,
+ root_path=str(existing), source='manual')
+ application_restore.on_purge_application(row)
+
+ assert (existing / 'index.php').is_file()
+
+
+def test_purge_leaves_directories_outside_apps_dir_alone(app, monkeypatch, tmp_path):
+ """Only ServerKit-managed paths are reclaimed; anything else is the
+ operator's."""
+ from app import paths
+ _stub_purge_side_effects(monkeypatch)
+ monkeypatch.setattr(paths, 'APPS_DIR', str(tmp_path / 'managed'))
+ elsewhere = tmp_path / 'elsewhere'
+ elsewhere.mkdir()
+ (elsewhere / 'app.py').write_text('# not ours')
+
+ with app.app_context():
+ row = Application(name='elsewhere', app_type='flask', user_id=1,
+ root_path=str(elsewhere), source='github')
+ application_restore.on_purge_application(row)
+
+ assert (elsewhere / 'app.py').is_file()
+
+
def test_a_failing_purge_hook_still_purges(app, dead_app):
"""The caller has said twice that they want the row gone; a directory that
will not delete is not a reason to keep the record."""