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(recycle-bin): restore a domain through write_app_vhost, not create_site
Juan Denisa9d72ef7d37f
diff --git a/backend/app/services/domain_restore.py b/backend/app/services/domain_restore.pyindex d26725e6..a4cf9aa5 100644--- a/backend/app/services/domain_restore.py+++ b/backend/app/services/domain_restore.py@@ -9,7 +9,7 @@ import loggingfrom app import dbfrom app.models import Application, Domain-from app.services.nginx_service import NginxService+from app.services.site_domain_service import SiteDomainServicelogger = logging.getLogger(__name__)@@ -40,8 +40,8 @@ def pre_restore_domain(domain):def on_restore_domain(domain):"""Re-serve the domain: rewrite the app's vhost from its LIVE domains.- Mirrors what delete_domain does in reverse — same NginxService.create_site- call over the same live-domain set, which now includes this one again.+ Mirrors what delete_domain does in reverse, over the same live-domain set+ -- which now includes this one again."""app = Application.query.get(domain.application_id)if app is None:@@ -59,31 +59,29 @@ def on_restore_domain(domain):domain.is_primary = Falsedb.session.commit()- # Only Docker apps get a generated vhost (see delete_domain / create_domain);- # anything else has no nginx state to put back.- if app.app_type != 'docker' or not app.port:- return-- domains = [d.name for d in- Domain.query_active().filter_by(application_id=app.id).all()]- if not domains:- return-- result = NginxService.create_site(- name=app.name,- app_type='docker',- domains=domains,- root_path=app.root_path or '',- port=app.port,- )- if not result or not result.get('success'):+ # Rewrite through SiteDomainService, NOT a bare NginxService.create_site.+ # Three reasons, all of which were bugs in the first cut of this hook:+ #+ # 1. ENABLE. When the deleted domain was the app's LAST one, delete_domain+ # ran disable_site() + delete_site() — the sites-enabled symlink is gone,+ # not just the server_name. create_site alone rewrites the file and+ # leaves it unserved. write_app_vhost calls enable_site.+ # 2. SSL / CACHE. app_vhost_kwargs re-attaches a covering wildcard cert and+ # the micro-cache flag. A raw create_site drops both, so a restored site+ # would silently fall back to plain HTTP.+ # 3. EVERY APP TYPE. delete_domain only touches nginx for docker apps, so+ # php/static/flask/wordpress vhosts still list the deleted domain. The+ # rewrite is idempotent, so running it for them repairs that asymmetry+ # instead of preserving it.+ result = SiteDomainService.write_app_vhost(app)+ warning = (result or {}).get('warning')+ if warning:# Surfaced to the caller as a restore warning: the row IS back, it just# is not being served yet.- raise RuntimeError((result or {}).get('error') or 'nginx config could not be written')- NginxService.reload()+ raise RuntimeError(warning)# NOTE: the certificate is deliberately NOT re-issued. ssl_enabled may still# be True with cert files that expired or were cleaned up while the domain# sat in the bin, and silently talking to Let's Encrypt during a restore is- # not something a "put it back" button should do. The domain returns on- # HTTP; re-enable SSL explicitly.+ # not something a "put it back" button should do. A covering wildcard is+ # re-attached above; a per-domain cert is re-enabled explicitly.diff --git a/backend/tests/test_recycle_bin.py b/backend/tests/test_recycle_bin.pyindex 90223c40..543b144d 100644--- a/backend/tests/test_recycle_bin.py+++ b/backend/tests/test_recycle_bin.py@@ -60,8 +60,8 @@ def test_restore_is_idempotent(app, app_with_domain, monkeypatch):# Domain now has an on_restore hook that rewrites the vhost; stub the nginx# side so this test stays about restore semantics.from app.services import domain_restore- monkeypatch.setattr(domain_restore.NginxService, 'create_site', lambda **kw: {'success': True})- monkeypatch.setattr(domain_restore.NginxService, 'reload', lambda: True)+ monkeypatch.setattr(domain_restore.SiteDomainService, 'write_app_vhost',+ classmethod(lambda cls, a: {'nginx': {}, 'warning': None}))with app.app_context():Domain.query.get(app_with_domain['domain_id']).soft_delete()@@ -167,9 +167,8 @@ def test_restoring_a_domain_rewrites_the_vhost(app, app_with_domain, monkeypatchfrom app.services import domain_restorecalls = []- monkeypatch.setattr(domain_restore.NginxService, 'create_site',- lambda **kw: calls.append(kw) or {'success': True})- monkeypatch.setattr(domain_restore.NginxService, 'reload', lambda: True)+ monkeypatch.setattr(domain_restore.SiteDomainService, 'write_app_vhost',+ classmethod(lambda cls, a: calls.append(a) or {'nginx': {}, 'warning': None}))with app.app_context():Domain.query.get(app_with_domain['domain_id']).soft_delete()@@ -178,7 +177,7 @@ def test_restoring_a_domain_rewrites_the_vhost(app, app_with_domain, monkeypatchitem, err = recycle_bin_service.restore('domain', app_with_domain['domain_id'])assert err is None and item is not Noneassert len(calls) == 1- assert 'shop.example.com' in calls[0]['domains']+ assert calls[0].id == app_with_domain['app_id']def test_restore_is_refused_when_the_name_was_taken_again(app, app_with_domain):@@ -211,8 +210,8 @@ def test_restore_is_refused_when_the_app_is_gone(app, app_with_domain):def test_restored_domain_does_not_become_a_second_primary(app, app_with_domain, monkeypatch):from app.services import domain_restore- monkeypatch.setattr(domain_restore.NginxService, 'create_site', lambda **kw: {'success': True})- monkeypatch.setattr(domain_restore.NginxService, 'reload', lambda: True)+ monkeypatch.setattr(domain_restore.SiteDomainService, 'write_app_vhost',+ classmethod(lambda cls, a: {'nginx': {}, 'warning': None}))with app.app_context():first = Domain.query.get(app_with_domain['domain_id'])@@ -230,8 +229,8 @@ def test_restored_domain_does_not_become_a_second_primary(app, app_with_domain,def test_a_failing_vhost_rewrite_warns_but_keeps_the_restore(app, app_with_domain, monkeypatch):from app.services import domain_restore- monkeypatch.setattr(domain_restore.NginxService, 'create_site',- lambda **kw: {'success': False, 'error': 'nginx -t failed'})+ monkeypatch.setattr(domain_restore.SiteDomainService, 'write_app_vhost',+ classmethod(lambda cls, a: {'nginx': None, 'warning': 'nginx -t failed'}))with app.app_context():Domain.query.get(app_with_domain['domain_id']).soft_delete()@@ -242,3 +241,37 @@ def test_a_failing_vhost_rewrite_warns_but_keeps_the_restore(app, app_with_domaiassert item is not Noneassert 'nginx -t failed' in errassert Domain.query.get(app_with_domain['domain_id']).deleted_at is None+++def test_restoring_the_last_domain_re_enables_the_site(app, app_with_domain, monkeypatch):+ """The bug this hook shipped with.++ When the deleted domain was the app's LAST one, delete_domain ran+ disable_site() + delete_site() — the sites-enabled symlink is gone, not just+ the server_name. A bare NginxService.create_site rewrites the file and+ leaves it UNSERVED. Restore has to go through SiteDomainService.write_app_vhost,+ which calls enable_site as well.+ """+ from app.services import domain_restore+ from app.services.nginx_service import NginxService++ seen = {'create': 0, 'enable': 0}+ monkeypatch.setattr(NginxService, 'create_site',+ classmethod(lambda cls, **kw: seen.__setitem__('create', seen['create'] + 1) or {'success': True}))+ monkeypatch.setattr(NginxService, 'enable_site',+ classmethod(lambda cls, name: seen.__setitem__('enable', seen['enable'] + 1) or {'success': True}))++ assert domain_restore is not None # hook module must be importable++ with app.app_context():+ # the app's ONLY domain — this is the disable_site + delete_site branch+ Domain.query.get(app_with_domain['domain_id']).soft_delete()+ db.session.commit()+ assert Domain.query_active().filter_by(+ application_id=app_with_domain['app_id']).count() == 0++ _, err = recycle_bin_service.restore('domain', app_with_domain['domain_id'])+ assert err is None++ assert seen['create'] >= 1, 'vhost was never written'+ assert seen['enable'] >= 1, 'vhost written but never enabled — the site stays unserved'