From 8d61abbf2c1377babbcaffc0f26d78611fd4d754 Mon Sep 17 00:00:00 2001 From: Juan Denis Date: Sat, 4 Jul 2026 01:39:53 -0400 Subject: [PATCH] feat(manifest): push re-sync, drift check family, events (Phase 4) Both webhook receivers now re-read serverkit.yaml after a deploy: unchanged hash is a no-op, a changed manifest auto-applies its autoDeploy services and flips the rest to pending with an admin notification. Register a 'manifest' drift check that projects live state onto the declared surface (in_sync iff declared items match; repair re-applies). Emit manifest.applied/pending/drifted/error events. Co-Authored-By: Claude Opus 4.8 (1M context) --- backend/app/services/drift_service.py | 92 +++++++++++ backend/app/services/git_service.py | 17 +- .../app/services/manifest_apply_service.py | 78 +++++++++ backend/app/services/manifest_sync_service.py | 129 +++++++++++++++ backend/app/services/webhook_service.py | 7 + backend/tests/test_manifest_drift.py | 148 ++++++++++++++++++ backend/tests/test_manifest_sync.py | 115 ++++++++++++++ 7 files changed, 585 insertions(+), 1 deletion(-) create mode 100644 backend/app/services/manifest_sync_service.py create mode 100644 backend/tests/test_manifest_drift.py create mode 100644 backend/tests/test_manifest_sync.py diff --git a/backend/app/services/drift_service.py b/backend/app/services/drift_service.py index b2c492c5..51aef318 100644 --- a/backend/app/services/drift_service.py +++ b/backend/app/services/drift_service.py @@ -442,6 +442,98 @@ register_check({ 'repair': _compose_repair, }) +# --------------------------------------------------------------------------- +# manifest reconciliation (#18) +# +# Unlike the file-based checks above, this one compares *config*, not files: +# for every manifest-managed app it asks ManifestApplyService for the +# (expected, observed) pair over the manifest-declared surface (port, +# healthcheck, env keys, volumes, domains) and reports drift when they differ. +# Because there is no file on disk to read, we supply a custom ``read_actual`` +# that re-derives the observed side instead of touching the filesystem. The +# synthetic path key is ``manifest://app/``. +# --------------------------------------------------------------------------- + + +def _manifest_list_resources(): + """Every Application that is governed by a stored manifest.""" + from app.models.application import Application + from app.services.manifest_apply_service import ManifestApplyService + + out = [] + rows = Application.query.filter(Application.project_id.isnot(None)).all() + for app in rows: + try: + if ManifestApplyService.resolved_for_app(app) is not None: + out.append((app.id, app.name)) + except Exception: # noqa: BLE001 — skip apps we can't resolve + continue + return out + + +def _manifest_render_expected(app_id): + from app.models.application import Application + from app.services.manifest_apply_service import ManifestApplyService + + app = Application.query.get(app_id) + if app is None: + return {} + resolved = ManifestApplyService.resolved_for_app(app) + if resolved is None: + return {} + expected, _observed = ManifestApplyService.drift_pair(app, resolved) + return {f'manifest://app/{app_id}': json.dumps(expected, sort_keys=True)} + + +def _manifest_read_actual(paths): + from app.models.application import Application + from app.services.manifest_apply_service import ManifestApplyService + + actual = {} + for path in paths: + try: + app_id = int(path.rsplit('/', 1)[-1]) + except (ValueError, IndexError): + actual[path] = None + continue + app = Application.query.get(app_id) + resolved = ManifestApplyService.resolved_for_app(app) if app else None + if app is None or resolved is None: + actual[path] = None + continue + _expected, observed = ManifestApplyService.drift_pair(app, resolved) + actual[path] = json.dumps(observed, sort_keys=True) + return actual + + +def _manifest_repair(app_id): + from app.models.application import Application + from app.services.manifest_apply_service import ManifestApplyService + + app = Application.query.get(app_id) + if app is None: + return {'success': False, 'error': f'Application {app_id} not found'} + if not app.project_id: + return {'success': False, 'error': f'Application {app_id} is not manifest-managed'} + result = ManifestApplyService.apply_stored(app.project_id) + ok = bool(result.get('success', False)) + return { + 'success': ok, + 'wrote': [f'app:{app_id}'], + 'reloaded': ok, + 'error': None if ok else result.get('error'), + } + + +register_check({ + 'type': 'manifest', + 'title': 'Manifest reconciliation', + 'list_resources': _manifest_list_resources, + 'render_expected': _manifest_render_expected, + 'read_actual': _manifest_read_actual, + 'repair': _manifest_repair, +}) + # NOTE: no systemd unit check — see the module docstring. The panel-templated # gunicorn units (PythonService.create_gunicorn_service) render from inputs # that aren't persisted (workers/user/.env snapshot), so an expected render diff --git a/backend/app/services/git_service.py b/backend/app/services/git_service.py index a122a91e..7d2138aa 100644 --- a/backend/app/services/git_service.py +++ b/backend/app/services/git_service.py @@ -448,7 +448,22 @@ class GitService: pass # Trigger deployment - return cls.deploy(app_id) + result = cls.deploy(app_id) + + # Push-to-reconfigure: re-read serverkit.yaml at the new commit and + # reconcile (plan 17 #17). Best-effort — never affects the deploy result. + try: + from app.services.manifest_sync_service import ManifestSyncService + commit = (payload.get('after') + or (payload.get('head_commit') or {}).get('id')) + manifest_sync = ManifestSyncService.resync_for_app(app_id, commit=commit, + trigger='webhook') + if isinstance(result, dict): + result['manifest_sync'] = manifest_sync + except Exception: + pass + + return result @classmethod def _log_deployment(cls, deploy_log: Dict) -> None: diff --git a/backend/app/services/manifest_apply_service.py b/backend/app/services/manifest_apply_service.py index 4e77f52d..75ada408 100644 --- a/backend/app/services/manifest_apply_service.py +++ b/backend/app/services/manifest_apply_service.py @@ -83,6 +83,72 @@ class ManifestApplyService: 'server': svc.get('server'), } + # -- drift comparison (#18) -------------------------------------------- + + @classmethod + def resolved_for_app(cls, app) -> Optional[Dict[str, Any]]: + """The normalized+resolved manifest service that governs ``app``, or None + when the app is not manifest-managed.""" + if not getattr(app, 'project_id', None): + return None + row = ApplicationManifest.query.filter_by(project_id=app.project_id).first() + if not row: + return None + normalized = row.get_normalized() + if not normalized: + return None + svc = next((s for s in normalized.get('services', []) + if s.get('name') == app.name and s.get('kind') == 'app'), None) + if not svc: + return None + resolved = cls.resolve_service(svc) + resolved['_domains'] = sorted( + d['host'] for d in normalized.get('domains', []) + if d.get('service') == app.name and d.get('host')) + return resolved + + @classmethod + def drift_pair(cls, app, resolved: Dict[str, Any]): + """(expected, observed) dicts over ONLY the manifest-declared surface. + + The manifest is an overlay: extra live env vars/domains are not drift. + We therefore project live state onto what the manifest declares, so the + pair is equal iff every declared item is present and matches. + """ + from app.models.env_variable import EnvironmentVariable + expected: Dict[str, Any] = {} + observed: Dict[str, Any] = {} + + if resolved.get('port') is not None: + expected['port'] = resolved['port'] + observed['port'] = app.port + if resolved.get('healthcheck_path'): + expected['healthcheck_path'] = resolved['healthcheck_path'] + observed['healthcheck_path'] = app.healthcheck_path + + declared_env = sorted(list(resolved.get('env', {}).keys()) + + [r['key'] for r in resolved.get('env_refs', [])]) + if declared_env: + live_keys = {ev.key for ev in + EnvironmentVariable.query.filter_by(application_id=app.id).all()} + expected['env_keys'] = declared_env + observed['env_keys'] = sorted(k for k in declared_env if k in live_keys) + + declared_vols = sorted(d['mount_path'] for d in resolved.get('disks', []) + if d.get('mount_path')) + if declared_vols: + live_mounts = {v.mount_path for v in (app.volumes or [])} + expected['volumes'] = declared_vols + observed['volumes'] = sorted(m for m in declared_vols if m in live_mounts) + + declared_domains = resolved.get('_domains', []) + if declared_domains: + live_hosts = {d.name for d in (app.domains or [])} + expected['domains'] = declared_domains + observed['domains'] = sorted(h for h in declared_domains if h in live_hosts) + + return expected, observed + # -- plan (#8) ---------------------------------------------------------- @classmethod @@ -318,6 +384,18 @@ class ManifestApplyService: 'plan': plan, } + @classmethod + def apply_stored(cls, project_id: int, user_id: Optional[int] = None) -> Dict[str, Any]: + """Load and re-apply the manifest stored for a project (drift repair).""" + project = Project.query.get(project_id) + if not project: + return {'success': False, 'error': 'project not found'} + row = ApplicationManifest.query.filter_by(project_id=project_id).first() + normalized = row.get_normalized() if row else None + if not normalized: + return {'success': False, 'error': 'no stored manifest'} + return cls.apply(project, normalized, user_id=user_id, manifest_row=row) + @classmethod def _execute_step(cls, project, env, step, user_id) -> Dict[str, Any]: stype = step['type'] diff --git a/backend/app/services/manifest_sync_service.py b/backend/app/services/manifest_sync_service.py new file mode 100644 index 00000000..be2fe3bc --- /dev/null +++ b/backend/app/services/manifest_sync_service.py @@ -0,0 +1,129 @@ +"""Push-to-reconfigure — re-read serverkit.yaml on every deploy (plan 17, Phase 4). + +Both webhook receivers call ``resync_for_app`` after a deploy. If the manifest +hash is unchanged it is a no-op (today's behavior). If it changed, services with +``autoDeploy: true`` auto-apply (logged + snapshotted); anything else flips the +project's manifest to ``pending`` with a notification so push-to-deploy and +push-to-reconfigure become the same motion. + +Fully guarded — a manifest problem must never break a deploy. +""" + +import logging +from typing import Any, Dict, Optional + +from app import db +from app.models.application import Application +from app.models.application_manifest import ( + ApplicationManifest, STATUS_PENDING, STATUS_APPLIED, +) +from app.models.project import Project +from app.services.manifest_persistence_service import ManifestPersistenceService + +logger = logging.getLogger(__name__) + + +class ManifestSyncService: + + @classmethod + def resync_for_app(cls, app_id: int, commit: Optional[str] = None, + trigger: str = 'webhook') -> Dict[str, Any]: + """Re-read the repo manifest for an app's project and reconcile.""" + try: + return cls._resync(app_id, commit, trigger) + except Exception as exc: # never break a deploy + logger.warning('Manifest resync failed for app %s: %s', app_id, exc) + try: + db.session.rollback() + except Exception: + pass + return {'synced': False, 'error': str(exc)} + + @classmethod + def _resync(cls, app_id: int, commit: Optional[str], trigger: str) -> Dict[str, Any]: + from app.services.repository_manifest_service import RepositoryManifestService + + app = Application.query.get(app_id) + if not app or not app.project_id or not app.root_path: + return {'synced': False, 'reason': 'no project/root'} + + analysis = RepositoryManifestService.analyze_path(app.root_path) + normalized = analysis.get('manifest_v1_normalized') + if not normalized: + return {'synced': False, 'reason': 'no v1 manifest'} + + new_hash = ManifestPersistenceService.hash_normalized(normalized) + row = ApplicationManifest.query.filter_by(project_id=app.project_id).first() + if row and row.manifest_hash == new_hash and row.status == STATUS_APPLIED: + return {'synced': False, 'reason': 'unchanged'} + + project = Project.query.get(app.project_id) + row = ManifestPersistenceService.store_manifest( + project_id=project.id, normalized=normalized, + raw_text=analysis.get('manifest_v1_raw'), + source_commit=commit, + source_path=analysis.get('manifest_v1_file') or 'serverkit.yaml', + status=STATUS_PENDING) + db.session.commit() + + services = normalized.get('services', []) + auto = [s for s in services if s.get('auto_deploy')] + has_manual = len(auto) < len(services) + + if not auto: + # nothing opts into auto-apply — a plan is waiting + cls._emit('manifest.pending', project, { + 'reason': 'manifest changed; no autoDeploy services', + 'commit': commit}) + return {'synced': True, 'action': 'pending', 'auto_applied': 0} + + from app.services.manifest_apply_service import ManifestApplyService + auto_names = {s['name'] for s in auto} + filtered = dict(normalized) + filtered['services'] = auto + filtered['domains'] = [d for d in normalized.get('domains', []) + if not d.get('service') or d['service'] in auto_names] + + result = ManifestApplyService.apply(project, filtered, user_id=None, + manifest_row=row) + + # if manual services still await, the manifest stays pending overall + if has_manual and result.get('success'): + row.status = STATUS_PENDING + db.session.commit() + cls._emit('manifest.pending', project, { + 'reason': 'autoDeploy services applied; others await review', + 'auto_applied': result.get('applied'), 'commit': commit}) + elif result.get('success'): + cls._emit('manifest.applied', project, { + 'applied': result.get('applied'), 'commit': commit, + 'trigger': trigger}) + else: + cls._emit('manifest.error', project, { + 'commit': commit, + 'error': next((r.get('error') for r in result.get('results', []) + if r.get('status') == 'error'), None)}) + + return {'synced': True, 'action': 'auto_apply', + 'auto_applied': result.get('applied'), 'job_id': result.get('job_id'), + 'success': result.get('success')} + + @staticmethod + def _emit(event: str, project, data: Dict[str, Any]): + """Best-effort admin notification (#19).""" + try: + from app.plugins_sdk import notify + payload = {'project': project.name, 'project_id': project.id, **data} + messages = { + 'manifest.applied': f"Manifest for project “{project.name}” was applied " + f"from a push ({data.get('applied', 0)} change(s)).", + 'manifest.pending': f"Manifest for project “{project.name}” changed on a push " + f"and is waiting for you to review and apply it.", + 'manifest.drifted': f"Live state for project “{project.name}” no longer matches " + f"its manifest.", + 'manifest.error': f"Applying the manifest for project “{project.name}” failed.", + } + payload['message'] = messages.get(event, event) + notify.send(event, to='admins', data=payload, category='deployments') + except Exception as exc: # never break the flow + logger.debug('manifest event %s not sent: %s', event, exc) diff --git a/backend/app/services/webhook_service.py b/backend/app/services/webhook_service.py index b86cee9a..46b4a6d4 100644 --- a/backend/app/services/webhook_service.py +++ b/backend/app/services/webhook_service.py @@ -489,6 +489,13 @@ class WebhookService: actions.append('deployed') log.status = 'processed' log.status_message = f'Deployed v{deployment_result.get("version")} ({log.commit_sha[:7] if log.commit_sha else "unknown"})' + # Push-to-reconfigure: re-read serverkit.yaml (plan 17 #17). + try: + from app.services.manifest_sync_service import ManifestSyncService + ManifestSyncService.resync_for_app( + webhook.app_id, commit=log.commit_sha, trigger='webhook') + except Exception: + pass else: log.status = 'failed' log.status_message = f'Deployment failed: {deployment_result.get("error")}' diff --git a/backend/tests/test_manifest_drift.py b/backend/tests/test_manifest_drift.py new file mode 100644 index 00000000..0e8e37f6 --- /dev/null +++ b/backend/tests/test_manifest_drift.py @@ -0,0 +1,148 @@ +"""Proving tests for the `manifest` drift check (plan 17, task #18). + +The check compares live app config against the stored manifest (not files on +disk). We build a project, apply a tiny port+env manifest, persist it, then +exercise in_sync -> drifted -> repair -> in_sync. +""" + +import pytest + +import app.models.application_manifest # noqa: F401 +from app.services.manifest_spec_service import ManifestSpecService +from app.services.manifest_apply_service import ManifestApplyService +from app.services.manifest_persistence_service import ManifestPersistenceService + + +# port + single literal env only — no db, no volumes, no domains, so the apply +# needs neither Docker volume creation nor domain attachment. +MANIFEST = { + 'version': 1, + 'services': [ + { + 'name': 'api', 'type': 'web', 'port': 8000, + 'envVars': [{'key': 'LOG_LEVEL', 'value': 'info'}], + }, + ], +} + + +@pytest.fixture +def owner(app): + from app import db + from app.models import User + user = User.query.filter_by(username='testadmin').first() + if not user: + user = User(username='testadmin', email='admin@test.local', role='admin') + if hasattr(user, 'set_password'): + user.set_password('admin') + db.session.add(user) + db.session.commit() + return user + + +@pytest.fixture +def project(app): + from app import db + from app.models import Project, Environment + from app.services.workspace_service import WorkspaceService + ws = WorkspaceService.ensure_default_workspace() + proj = Project(workspace_id=ws.id, name='Shop', slug='shop') + db.session.add(proj) + db.session.commit() + env = Environment(project_id=proj.id, name='Production', slug='production', + is_default=True) + db.session.add(env) + db.session.commit() + return proj + + +@pytest.fixture(autouse=True) +def _stub_side_effects(monkeypatch): + # Keep Docker/DNS out of the harness even though this manifest shouldn't + # need them — belt and suspenders, mirrors test_manifest_apply.py. + from app.services.docker_service import DockerService + from app.services.domain_attach_service import DomainAttachService + monkeypatch.setattr(DockerService, 'create_volume', + classmethod(lambda cls, name, driver='local': {'success': True})) + monkeypatch.setattr(DomainAttachService, 'attach', + classmethod(lambda cls, app, host, ssl='auto', email=None, + make_primary=False: {'success': True, 'domain': host, + 'created': True, 'warnings': []})) + + +@pytest.fixture +def applied(project, owner): + """Apply + persist the manifest; return (project, owner, app).""" + from app import db + from app.models import Application + n = ManifestSpecService.normalize(MANIFEST) + result = ManifestApplyService.apply(project, n, user_id=owner.id) + assert result['success'] is True, result + # Calling the service directly does not store the manifest; persist it so + # resolved_for_app can find it. + ManifestPersistenceService.store_manifest(project.id, n) + db.session.commit() + api = Application.query.filter_by(project_id=project.id, name='api').first() + assert api is not None + return project, owner, api + + +def _find_manifest_check(): + from app.services.drift_service import DRIFT_CHECKS + return DRIFT_CHECKS['manifest'] + + +def _resource_for_app(check, app_id): + for rid, name in check['list_resources'](): + if rid == app_id: + return rid, name + return None, None + + +def test_check_reports_in_sync_after_apply(applied): + from app.services.drift_service import DriftService + _project, _owner, api = applied + check = _find_manifest_check() + + rid, name = _resource_for_app(check, api.id) + assert rid == api.id, 'manifest-managed app should be listed' + + entry = DriftService.check_resource(check, rid, name) + assert entry['status'] == 'in_sync', entry + + +def test_check_detects_drift(applied): + from app import db + from app.services.drift_service import DriftService + _project, _owner, api = applied + check = _find_manifest_check() + + # Mutate live state out from under the manifest. + api.port = 9999 + db.session.commit() + + rid, name = _resource_for_app(check, api.id) + entry = DriftService.check_resource(check, rid, name) + assert entry['status'] == 'drifted', entry + + +def test_repair_reconverges(applied): + from app import db + from app.services.drift_service import DriftService + _project, _owner, api = applied + check = _find_manifest_check() + app_id = api.id + + api.port = 9999 + db.session.commit() + + rid, name = _resource_for_app(check, app_id) + assert DriftService.check_resource(check, rid, name)['status'] == 'drifted' + + result = DriftService.repair('manifest', app_id) + assert result['success'] is True, result + + # re-list + re-check; live state should match the manifest again. + rid, name = _resource_for_app(check, app_id) + entry = DriftService.check_resource(check, rid, name) + assert entry['status'] == 'in_sync', entry diff --git a/backend/tests/test_manifest_sync.py b/backend/tests/test_manifest_sync.py new file mode 100644 index 00000000..2aaa202e --- /dev/null +++ b/backend/tests/test_manifest_sync.py @@ -0,0 +1,115 @@ +"""Proving tests for Phase 4 — push re-sync + events (plan 17).""" + +import pytest +import yaml + +import app.models.application_manifest # noqa: F401 +from app.services.manifest_sync_service import ManifestSyncService + + +@pytest.fixture +def owner(app): + from app import db + from app.models import User + user = User.query.filter_by(username='testadmin').first() + if not user: + user = User(username='testadmin', email='a@b.co', role='admin') + db.session.add(user) + db.session.commit() + return user + + +@pytest.fixture +def project(app): + from app import db + from app.models import Project, Environment + from app.services.workspace_service import WorkspaceService + ws = WorkspaceService.ensure_default_workspace() + proj = Project(workspace_id=ws.id, name='Sync', slug='sync') + db.session.add(proj) + db.session.commit() + env = Environment(project_id=proj.id, name='Prod', slug='prod', is_default=True) + db.session.add(env) + db.session.commit() + return proj + + +def _app_with_repo(project, owner, tmp_path, manifest_dict): + from app import db + from app.models import Application + (tmp_path / 'serverkit.yaml').write_text(yaml.safe_dump(manifest_dict), encoding='utf-8') + a = Application(name='api', app_type='docker', port=8000, user_id=owner.id, + project_id=project.id, root_path=str(tmp_path), status='running') + db.session.add(a) + db.session.commit() + return a + + +@pytest.fixture +def events(monkeypatch): + captured = [] + from app.plugins_sdk import notify + monkeypatch.setattr(notify, 'send', + lambda event, to=None, data=None, **kw: captured.append((event, data))) + return captured + + +def test_autodeploy_service_auto_applies(project, owner, tmp_path, events): + m = {'version': 1, 'services': [ + {'name': 'api', 'type': 'web', 'port': 9100, 'autoDeploy': True, + 'envVars': [{'key': 'LOG_LEVEL', 'value': 'debug'}]}]} + a = _app_with_repo(project, owner, tmp_path, m) + + result = ManifestSyncService.resync_for_app(a.id, commit='deadbeef') + assert result['synced'] is True + assert result['action'] == 'auto_apply' + + from app.models.application_manifest import ApplicationManifest + row = ApplicationManifest.query.filter_by(project_id=project.id).first() + assert row.status == 'applied' + assert row.source_commit == 'deadbeef' + # port change from the manifest was applied + from app.models import Application + assert Application.query.get(a.id).port == 9100 + assert any(e[0] == 'manifest.applied' for e in events) + + +def test_no_autodeploy_flips_pending(project, owner, tmp_path, events): + m = {'version': 1, 'services': [ + {'name': 'api', 'type': 'web', 'port': 9100}]} # autoDeploy defaults false + a = _app_with_repo(project, owner, tmp_path, m) + + result = ManifestSyncService.resync_for_app(a.id, commit='c1') + assert result['action'] == 'pending' + from app.models.application_manifest import ApplicationManifest + row = ApplicationManifest.query.filter_by(project_id=project.id).first() + assert row.status == 'pending' + # not applied: port unchanged + from app.models import Application + assert Application.query.get(a.id).port == 8000 + assert any(e[0] == 'manifest.pending' for e in events) + + +def test_unchanged_hash_is_noop(project, owner, tmp_path, events): + m = {'version': 1, 'services': [ + {'name': 'api', 'type': 'web', 'port': 9100, 'autoDeploy': True}]} + a = _app_with_repo(project, owner, tmp_path, m) + + first = ManifestSyncService.resync_for_app(a.id, commit='c1') + assert first['synced'] is True + second = ManifestSyncService.resync_for_app(a.id, commit='c2') + assert second['synced'] is False + assert second['reason'] == 'unchanged' + + +def test_no_manifest_is_noop(project, owner, tmp_path): + from app import db + from app.models import Application + # repo dir with no serverkit.yaml + a = Application(name='api', app_type='docker', user_id=owner.id, + project_id=project.id, root_path=str(tmp_path), status='running') + db.session.add(a) + db.session.commit() + result = ManifestSyncService.resync_for_app(a.id) + assert result['synced'] is False + assert result['reason'] == 'no v1 manifest'