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
refactor(cloud): extract cloud-provision backend + add upgrade re-acquisition
Juan Denis44a73d7796bc
diff --git a/backend/app/__init__.py b/backend/app/__init__.pyindex 9d3a29a4..68748fe8 100644--- a/backend/app/__init__.py+++ b/backend/app/__init__.py@@ -425,9 +425,9 @@ def create_app(config_name=None):from app.api.status_pages import status_pages_bpapp.register_blueprint(status_pages_bp, url_prefix='/api/v1/status')- # Register blueprints - Cloud Provisioning- from app.api.cloud_provisioning import cloud_provisioning_bp- app.register_blueprint(cloud_provisioning_bp, url_prefix='/api/v1/cloud')+ # Cloud Provisioning is an opt-in builtin extension (serverkit-cloud-provision,+ # plan 47) — its blueprint loads from builtin-extensions/ when installed, not+ # from core. The CloudProvider/CloudServer models stay core (G2).# Register blueprints - Remote Access (WireGuard tunnels; imported above)app.register_blueprint(tunnels_bp, url_prefix='/api/v1/tunnels')@@ -529,10 +529,14 @@ def create_app(config_name=None):try:from app.services.dns_provider_service import DNSProviderServicefrom app.services.storage_provider_service import StorageProviderService- from app.services.cloud_provisioning_service import CloudProvisioningServicen_dns = DNSProviderService.encrypt_legacy_secrets()n_store = StorageProviderService.encrypt_legacy_secrets()- n_cloud = CloudProvisioningService.encrypt_legacy_secrets()+ # Cloud-provider legacy-secret encryption moved out with the+ # serverkit-cloud-provision extension (plan 47). It was a one-time+ # migration of pre-encryption rows; any panel reaching this version+ # already ran it in an earlier boot (idempotent), and the extension+ # encrypts on write, so there's nothing left for core to do here.+ n_cloud = 0n_settings = SettingsService.migrate_legacy_secrets()# Migrate DNS zones with an inline Cloudflare token onto the canonical# connection store (idempotent), so every zone resolves creds the same way.@@ -588,6 +592,16 @@ def create_app(config_name=None):import logginglogging.getLogger(__name__).warning(f'Extension auto-install: {e}')+ # One-shot: re-acquire the now-extracted backend for converted builtins+ # that first shipped frontend-only (plan 47 Phase 2), so an upgraded panel+ # that installed them frontend-only doesn't lose the API. Best-effort.+ try:+ from app.services.extension_migration import run_backend_acquisition+ run_backend_acquisition()+ except Exception as e:+ import logging+ logging.getLogger(__name__).warning(f'Extension backend acquisition: {e}')+# Start metrics history collection in backgroundfrom app.services.metrics_history_service import MetricsHistoryServiceif not MetricsHistoryService.is_running():diff --git a/backend/app/services/extension_migration.py b/backend/app/services/extension_migration.pyindex 0efbaa32..021e5335 100644--- a/backend/app/services/extension_migration.py+++ b/backend/app/services/extension_migration.py@@ -68,6 +68,10 @@ GATED_BUILTIN_SLUGS = {}_MARKER_KEY = 'extensions.auto_installed_slugs'+# Slugs whose now-extracted backend has been re-acquired on an upgraded panel+# (plan 47 Phase 2). Separate from _MARKER_KEY so the one-shot backend pickup+# runs even for slugs already auto-installed frontend-only in a prior release.+_BACKEND_ACQUIRED_KEY = 'extensions.backend_acquired_slugs'def _processed_slugs():@@ -153,3 +157,72 @@ def run_auto_install():if changed:_save_processed(processed)+++def _backend_acquired_slugs():+ from app.services.settings_service import SettingsService+ raw = SettingsService.get(_BACKEND_ACQUIRED_KEY, '')+ if not raw:+ return set()+ try:+ return set(json.loads(raw))+ except (ValueError, TypeError):+ return {s.strip() for s in str(raw).split(',') if s.strip()}+++def run_backend_acquisition():+ """Re-acquire the backend for converted builtins that gained one (plan 47).++ Some builtins (ftp, cloud-provision, remote-access, status) first shipped as+ *frontend-only* extensions whose API was still served by CORE. Plan 47 moves+ that backend into the extension and deregisters it from core. On an upgraded+ panel that already installed such an extension frontend-only, the installed+ row has ``has_backend=False`` and no backend on disk — so without this pass+ the API would simply vanish. For each converted slug that is installed but+ missing the now-available backend, force-reinstall from ``builtin-extensions/``+ once (idempotent via a marker). Best-effort; never blocks startup.+ """+ import os++ done = _backend_acquired_slugs()+ try:+ from app.services import plugin_service+ builtins = {e['slug']: e for e in plugin_service.list_builtin_extensions()}+ except Exception as e:+ logger.warning(f'Backend acquisition skipped (builtins unavailable): {e}')+ return++ changed = False+ for slug in CONVERTED_BUILTIN_SLUGS:+ if slug in done:+ continue+ entry = builtins.get(slug)+ # Only slugs that now actually ship a backend/ dir are candidates.+ if not entry or not os.path.isdir(os.path.join(entry['path'], 'backend')):+ continue+ row = InstalledPlugin.query.filter_by(slug=slug).first()+ if not row:+ # Not installed — run_auto_install handles fresh (re-)acquisition,+ # and a fresh panel discovers it in the Marketplace.+ continue+ backend_on_disk = os.path.isdir(+ os.path.join(plugin_service.BACKEND_PLUGINS_DIR, slug))+ if row.has_backend and backend_on_disk:+ done.add(slug)+ changed = True+ continue+ try:+ plugin_service.install_from_path(+ entry['path'], user_id=row.installed_by, force=True,+ source_type='builtin')+ logger.info(f'Acquired extracted backend for {slug} (plan 47)')+ done.add(slug)+ changed = True+ except Exception as e:+ logger.warning(+ f'Backend acquisition for {slug} failed (retry next boot): {e}')+ continue # leave unmarked so we retry++ if changed:+ from app.services.settings_service import SettingsService+ SettingsService.set(_BACKEND_ACQUIRED_KEY, json.dumps(sorted(done)))diff --git a/backend/tests/test_cloud_provision_extraction.py b/backend/tests/test_cloud_provision_extraction.pynew file mode 100644index 00000000..c8704c7b--- /dev/null+++ b/backend/tests/test_cloud_provision_extraction.py@@ -0,0 +1,116 @@+"""Prove the Cloud Provisioning extraction (plan 47 Phase 2).++A fresh panel no longer carries the cloud-provider API in core; installing the+serverkit-cloud-provision builtin registers `/api/v1/cloud`. The CloudProvider+model stays core (G2). Also covers the one-shot backend re-acquisition that keeps+upgraded panels (which installed the extension frontend-only) from losing the API.+"""+import importlib+import sys++import pytest++from app import db+from app.models.plugin import InstalledPlugin+from app.services import plugin_service++SLUG = 'serverkit-cloud-provision'+_PKG = f'app.plugins.{SLUG}'+++def test_core_has_no_cloud_routes(app):+ rules = [r.rule for r in app.url_map.iter_rules()]+ assert [r for r in rules if r.startswith('/api/v1/cloud/')] == []+++def test_core_import_of_cloud_service_is_gone():+ with pytest.raises(ModuleNotFoundError):+ importlib.import_module('app.services.cloud_provisioning_service')+++def test_cloud_in_converted_builtin_slugs():+ from app.services.extension_migration import CONVERTED_BUILTIN_SLUGS+ assert SLUG in CONVERTED_BUILTIN_SLUGS+++def test_cloud_model_stays_core():+ """G2: the CloudProvider/CloudServer models remain importable from core."""+ from app.models.cloud_server import CloudProvider, CloudServer # noqa: F401+++def install_dirs(tmp_path, monkeypatch):+ backend = tmp_path / 'plugins_backend'+ frontend = tmp_path / 'plugins_frontend'+ backend.mkdir()+ frontend.mkdir()+ monkeypatch.setattr(plugin_service, 'BACKEND_PLUGINS_DIR', str(backend))+ monkeypatch.setattr(plugin_service, 'FRONTEND_PLUGINS_DIR', str(frontend))++ added = str(backend)+ app_pkg_plugins = importlib.import_module('app.plugins')+ if added not in app_pkg_plugins.__path__:+ app_pkg_plugins.__path__.append(added)++ yield {'backend': backend, 'frontend': frontend}++ if added in app_pkg_plugins.__path__:+ app_pkg_plugins.__path__.remove(added)+ for name in list(sys.modules):+ if name == _PKG or name.startswith(_PKG + '.'):+ del sys.modules[name]+++def test_install_cloud_extension_registers_routes(app, client, auth_headers, install_dirs):+ available = {e['slug'] for e in plugin_service.list_builtin_extensions()}+ assert SLUG in available++ plugin = plugin_service.install_builtin_extension(SLUG)+ assert plugin.status == InstalledPlugin.STATUS_ACTIVE+ assert plugin.has_backend is True+ assert plugin.url_prefix == '/api/v1/cloud'++ resp = client.get('/api/v1/cloud/providers', headers=auth_headers)+ assert resp.status_code not in (404, 503), resp.status_code+++def test_cloud_provider_secret_encrypted_at_rest(app, install_dirs):+ """The extracted service still encrypts provider secrets at rest (moved here+ from test_provider_secret_encryption.py)."""+ plugin_service.install_builtin_extension(SLUG)+ from app.models.cloud_server import CloudProvider+ from app.utils.crypto import is_encrypted, decrypt_secret_safe++ svc_mod = importlib.import_module(f'{_PKG}.cloud_provisioning_service')+ CloudProvisioningService = svc_mod.CloudProvisioningService++ p = CloudProvisioningService.create_provider(+ {'provider_type': 'digitalocean', 'name': 'do', 'api_key': 'do-token-xyz'})+ row = CloudProvider.query.get(p.id)+ assert row.api_key_encrypted != 'do-token-xyz'+ assert is_encrypted(row.api_key_encrypted) is True+ assert decrypt_secret_safe(row.api_key_encrypted) == 'do-token-xyz'+ assert CloudProvisioningService._auth_headers(row)['Authorization'] == 'Bearer do-token-xyz'+++def test_backend_acquisition_upgrades_frontend_only_install(app, install_dirs):+ """An upgraded panel that installed the extension frontend-only re-acquires+ the now-extracted backend (plan 47 Phase 2 migration)."""+ from app.services import extension_migration++ # Simulate the pre-plan-47 state: a frontend-only install (API came from core)+ row = InstalledPlugin(+ name=SLUG, display_name='Cloud Provisioning', slug=SLUG, version='1.0.0',+ source_type='builtin', status=InstalledPlugin.STATUS_ACTIVE,+ )+ row.has_backend = False+ row.has_frontend = True+ db.session.add(row)+ db.session.commit()++ extension_migration.run_backend_acquisition()++ refreshed = InstalledPlugin.query.filter_by(slug=SLUG).first()+ assert refreshed.has_backend is True+ rules = [r.rule for r in app.url_map.iter_rules()]+ assert any(r.startswith('/api/v1/cloud/') for r in rules)diff --git a/backend/tests/test_provider_secret_encryption.py b/backend/tests/test_provider_secret_encryption.pyindex 7e7f1ea2..9386ee63 100644--- a/backend/tests/test_provider_secret_encryption.py+++ b/backend/tests/test_provider_secret_encryption.py@@ -107,20 +107,10 @@ def test_dns_legacy_plaintext_migration(app):# ── cloud providers ─────────────────────────────────────────────────────────--def test_cloud_provider_secret_encrypted_at_rest(app):- from app.models.cloud_server import CloudProvider- from app.services.cloud_provisioning_service import CloudProvisioningService- from app.utils.crypto import is_encrypted, decrypt_secret_safe-- p = CloudProvisioningService.create_provider(- {'provider_type': 'digitalocean', 'name': 'do', 'api_key': 'do-token-xyz'})- row = CloudProvider.query.get(p.id)- assert row.api_key_encrypted != 'do-token-xyz'- assert is_encrypted(row.api_key_encrypted) is True- assert decrypt_secret_safe(row.api_key_encrypted) == 'do-token-xyz'- # the auth header uses the decrypted token, not the stored blob- assert CloudProvisioningService._auth_headers(row)['Authorization'] == 'Bearer do-token-xyz'+# CloudProvisioningService moved to the serverkit-cloud-provision extension+# (plan 47); its secret-at-rest coverage now lives in+# tests/test_cloud_provision_extraction.py (installs the extension first). The+# CloudProvider model stays core (G2).# ── storage (config-file backed) ────────────────────────────────────────────diff --git a/builtin-extensions/serverkit-cloud-provision/backend/__init__.py b/builtin-extensions/serverkit-cloud-provision/backend/__init__.pynew file mode 100644index 00000000..8d59cc3c--- /dev/null+++ b/builtin-extensions/serverkit-cloud-provision/backend/__init__.py@@ -0,0 +1,11 @@+"""ServerKit Cloud Provisioning extension backend package.++Extracted from core (plan 47): the cloud-provider blueprint + service ship here+so a fresh panel that never provisions cloud servers loads none of it. Mounted+under ``/api/v1/cloud`` via the manifest's ``url_prefix``. The CloudProvider /+CloudServer models stay in core (G2); this package imports them. Upgrades+re-acquire it automatically (``CONVERTED_BUILTIN_SLUGS``).+"""+from .cloud_provisioning import cloud_provisioning_bp++__all__ = ['cloud_provisioning_bp']diff --git a/backend/app/api/cloud_provisioning.py b/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning.pysimilarity index 98%rename from backend/app/api/cloud_provisioning.pyrename to builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning.pyindex c8a806e3..bc3029c6 100644--- a/backend/app/api/cloud_provisioning.py+++ b/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning.py@@ -1,6 +1,6 @@from flask import Blueprint, request, jsonifyfrom flask_jwt_extended import jwt_required-from app.services.cloud_provisioning_service import CloudProvisioningService+from .cloud_provisioning_service import CloudProvisioningServicecloud_provisioning_bp = Blueprint('cloud_provisioning', __name__)diff --git a/backend/app/services/cloud_provisioning_service.py b/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning_service.pysimilarity index 100%rename from backend/app/services/cloud_provisioning_service.pyrename to builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning_service.pydiff --git a/builtin-extensions/serverkit-cloud-provision/plugin.json b/builtin-extensions/serverkit-cloud-provision/plugin.jsonindex 9f8f8c1c..62331332 100644--- a/builtin-extensions/serverkit-cloud-provision/plugin.json+++ b/builtin-extensions/serverkit-cloud-provision/plugin.json@@ -8,6 +8,8 @@"category": "deployment","homepage": "https://github.com/jhd3197/ServerKit","permissions": ["network"],+ "entry_point": "cloud_provisioning:cloud_provisioning_bp",+ "url_prefix": "/api/v1/cloud","icon": "<path d=\"M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z\"/>","contributions": {"tabs": [