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
feat: inject shared variables into compose apps too
Juan Denis574de6af8a53
diff --git a/backend/app/services/compose_env_service.py b/backend/app/services/compose_env_service.pynew file mode 100644index 00000000..eda6d2f9--- /dev/null+++ b/backend/app/services/compose_env_service.py@@ -0,0 +1,151 @@+"""Inject a managed app's effective environment into its Compose services.++Single-container apps get their env via ``docker run -e`` (see+``EnvService.get_effective_env`` + ``DeploymentService._deploy_docker``). Compose+apps instead get container env from the compose file's ``environment:`` block, so+to make shared variable groups + the app's own local env vars reach a compose+app's containers we write a **managed override compose file**+(``docker-compose.serverkit.yml``) next to the app's base compose. The override+adds an ``environment:`` overlay to every service; Docker Compose merges it on top+of the base (override wins on key collisions), so the effective env is+authoritative — matching the single-container behaviour.++This is non-destructive: ServerKit owns only the override file and never edits the+user's / template's base compose. The override is regenerated on every+``compose up`` / ``compose restart`` and removed when the app has no effective env,+so it never goes stale. Everything here is best-effort and guarded — a failure to+build the overlay must never block a deploy; the caller simply falls back to the+plain base compose.+"""+import logging+import os++import yaml++logger = logging.getLogger(__name__)+++class ComposeEnvService:+ """Writes/owns the ServerKit env override for a managed compose app."""++ OVERRIDE_NAME = 'docker-compose.serverkit.yml'+ # Conventional base compose filenames, in the order Docker Compose itself+ # prefers them during auto-discovery.+ BASE_CANDIDATES = (+ 'docker-compose.yml', 'docker-compose.yaml', 'compose.yml', 'compose.yaml',+ )++ @classmethod+ def find_base_compose(cls, project_path, compose_file=None):+ """Return the base compose file to use (a name/path), or None.++ When ``compose_file`` is given it's authoritative; otherwise discover a+ conventional base file in ``project_path``.+ """+ if compose_file:+ return compose_file+ if not project_path:+ return None+ for name in cls.BASE_CANDIDATES:+ if os.path.exists(os.path.join(project_path, name)):+ return name+ return None++ @classmethod+ def override_path(cls, project_path):+ return os.path.join(project_path, cls.OVERRIDE_NAME)++ @staticmethod+ def _app_for_project(project_path):+ """The managed Application whose root_path is this project dir, if any."""+ try:+ from app.models.application import Application+ return Application.query.filter_by(root_path=project_path).first()+ except Exception as e: # pragma: no cover - DB guard+ logger.debug('compose overlay app lookup failed for %s: %s', project_path, e)+ return None++ @staticmethod+ def _service_names(base_compose_path):+ """Service names declared in the base compose (empty list on any error)."""+ try:+ with open(base_compose_path, 'r', encoding='utf-8') as f:+ data = yaml.safe_load(f) or {}+ services = data.get('services') or {}+ if isinstance(services, dict):+ return list(services.keys())+ except Exception as e:+ logger.debug('could not read services from %s: %s', base_compose_path, e)+ return []++ @staticmethod+ def _escape(value):+ """Escape a value for a compose ``environment:`` entry.++ Compose performs ``${VAR}`` interpolation on environment values, so a+ literal ``$`` must be doubled to survive intact.+ """+ return str(value).replace('$', '$$')++ @classmethod+ def refresh_for_project(cls, project_path, compose_file=None):+ """Regenerate (or remove) the env override for a project dir.++ Returns the absolute override path when one was written, else None.+ Best-effort and fully guarded — never raises.+ """+ try:+ if not project_path or not os.path.isdir(project_path):+ return None++ app = cls._app_for_project(project_path)+ if app is None:+ # Not a managed app dir (e.g. a proxy stack) — leave it alone.+ return None++ base = cls.find_base_compose(project_path, compose_file)+ if not base:+ return None+ base_path = base if os.path.isabs(base) else os.path.join(project_path, base)+ service_names = cls._service_names(base_path)+ if not service_names:+ cls._remove_override(project_path)+ return None++ from app.services.env_service import EnvService+ env = EnvService.get_effective_env(app.id)+ if not env:+ # No effective env → make sure no stale override lingers.+ cls._remove_override(project_path)+ return None++ environment = {k: cls._escape(v) for k, v in env.items() if k}+ override = {+ 'services': {+ name: {'environment': dict(environment)} for name in service_names+ }+ }++ override_path = cls.override_path(project_path)+ header = (+ '# Managed by ServerKit — do not edit.\n'+ '# Injects the app\'s effective environment (shared variable groups\n'+ '# under the app\'s own local env vars) into every compose service.\n'+ '# Regenerated on every deploy; delete it and it will be recreated.\n'+ )+ with open(override_path, 'w', encoding='utf-8') as f:+ f.write(header)+ yaml.safe_dump(override, f, default_flow_style=False, sort_keys=True)+ return override_path+ except Exception as e: # pragma: no cover - defensive+ logger.warning('compose env overlay refresh failed for %s: %s', project_path, e)+ return None++ @classmethod+ def _remove_override(cls, project_path):+ try:+ path = cls.override_path(project_path)+ if os.path.exists(path):+ os.remove(path)+ except OSError as e: # pragma: no cover - defensive+ logger.debug('could not remove stale compose override: %s', e)diff --git a/backend/app/services/docker_service.py b/backend/app/services/docker_service.pyindex 7684349a..d8d5ac4d 100644--- a/backend/app/services/docker_service.py+++ b/backend/app/services/docker_service.py@@ -767,6 +767,28 @@ class DockerService:cmd = cmd + ['-f', compose_file]return cmd+ @classmethod+ def _compose_cmd_with_overlay(cls, project_path, compose_file=None):+ """Base compose command, including the ServerKit env override when one+ applies.++ For a managed compose app this regenerates ``docker-compose.serverkit.yml``+ (the app's effective env: shared variable groups under its own local env+ vars) and adds it as a second ``-f`` so those values reach the containers.+ For non-app dirs (e.g. a proxy stack) or apps with no effective env it+ falls back to the plain base command unchanged. Best-effort — never raises.+ """+ try:+ from app.services.compose_env_service import ComposeEnvService+ override = ComposeEnvService.refresh_for_project(project_path, compose_file)+ if override:+ base = ComposeEnvService.find_base_compose(project_path, compose_file)+ if base:+ return cls._get_compose_cmd() + ['-f', base, '-f', override]+ except Exception as e: # pragma: no cover - defensive+ logger.debug('compose overlay command build failed for %s: %s', project_path, e)+ return cls._compose_base_cmd(compose_file)+@classmethoddef compose_list(cls):"""List Docker Compose projects known to the Docker CLI."""@@ -859,9 +881,15 @@ class DockerService:@classmethoddef compose_up(cls, project_path, detach=True, build=False, compose_file=None):- """Start Docker Compose services."""+ """Start Docker Compose services.++ Injects the managed env override (shared variable groups + the app's own+ local env vars) for managed compose apps via ``_compose_cmd_with_overlay``.+ ``up -d`` recreates containers whose merged config changed, so updated env+ takes effect on the next deploy.+ """try:- cmd = cls._compose_base_cmd(compose_file) + ['up']+ cmd = cls._compose_cmd_with_overlay(project_path, compose_file) + ['up']if detach:cmd.append('-d')if build:diff --git a/backend/tests/test_compose_env_overlay.py b/backend/tests/test_compose_env_overlay.pynew file mode 100644index 00000000..45504a17--- /dev/null+++ b/backend/tests/test_compose_env_overlay.py@@ -0,0 +1,145 @@+"""Compose env overlay: the managed override that injects an app's effective+environment (shared variable groups under local env vars) into every compose+service, and that compose_up includes it."""+import os+import yaml+++BASE_COMPOSE = """\+services:+ web:+ image: nginx:latest+ ports:+ - "8080:80"+ db:+ image: postgres:16+"""+++def _make_compose_app(tmp_path, workspace_id=99):+ from app import db+ from app.models.application import Application+ root = str(tmp_path)+ with open(os.path.join(root, 'docker-compose.yml'), 'w', encoding='utf-8') as f:+ f.write(BASE_COMPOSE)+ a = Application(+ name='svc', app_type='docker', status='running', user_id=1,+ root_path=root, managed_by='docker_compose', workspace_id=workspace_id,+ )+ db.session.add(a)+ db.session.commit()+ return a+++def _set_local(app_id, key, value, is_secret=False):+ from app import db+ from app.models import EnvironmentVariable+ ev = EnvironmentVariable(application_id=app_id, key=key, is_secret=is_secret)+ ev.value = value+ db.session.add(ev)+ db.session.commit()+++def _make_scoped_group(scope_type, scope_id, key, value):+ from app import db+ from app.models.shared_resource import SharedVariableGroup, SharedVariable+ g = SharedVariableGroup(scope_type=scope_type, scope_id=str(scope_id), name='g')+ db.session.add(g)+ db.session.commit()+ v = SharedVariable(group_id=g.id, key=key, is_secret=False)+ v.value = value+ db.session.add(v)+ db.session.commit()+++def test_find_base_compose(app, tmp_path):+ from app.services.compose_env_service import ComposeEnvService+ assert ComposeEnvService.find_base_compose(str(tmp_path)) is None+ open(os.path.join(str(tmp_path), 'docker-compose.yml'), 'w').close()+ assert ComposeEnvService.find_base_compose(str(tmp_path)) == 'docker-compose.yml'+ # explicit compose_file is authoritative+ assert ComposeEnvService.find_base_compose(str(tmp_path), 'custom.yml') == 'custom.yml'+++def test_refresh_writes_overlay_for_every_service(app, tmp_path):+ from app.services.compose_env_service import ComposeEnvService+ with app.app_context():+ a = _make_compose_app(tmp_path, workspace_id=99)+ _make_scoped_group('workspace', 99, 'SHARED_KEY', 'shared_val')+ _set_local(a.id, 'LOCAL_KEY', 'local_val')+ _set_local(a.id, 'OVERRIDE_ME', 'local_wins')+ _make_scoped_group('workspace', 99, 'OVERRIDE_ME', 'group_loses')++ path = ComposeEnvService.refresh_for_project(str(tmp_path))+ assert path and os.path.exists(path)+ with open(path, encoding='utf-8') as f:+ data = yaml.safe_load(f)++ for svc in ('web', 'db'):+ env = data['services'][svc]['environment']+ assert env['SHARED_KEY'] == 'shared_val' # shared group injected+ assert env['LOCAL_KEY'] == 'local_val'+ assert env['OVERRIDE_ME'] == 'local_wins' # local beat the shared group+++def test_dollar_signs_are_escaped(app, tmp_path):+ from app.services.compose_env_service import ComposeEnvService+ with app.app_context():+ a = _make_compose_app(tmp_path, workspace_id=5)+ _set_local(a.id, 'PASS', 'a$b${X}')+ path = ComposeEnvService.refresh_for_project(str(tmp_path))+ with open(path, encoding='utf-8') as f:+ data = yaml.safe_load(f)+ # '$' doubled so compose interpolation leaves the literal value intact.+ assert data['services']['web']['environment']['PASS'] == 'a$$b$${X}'+++def test_no_env_removes_stale_override(app, tmp_path):+ from app.services.compose_env_service import ComposeEnvService+ with app.app_context():+ _make_compose_app(tmp_path, workspace_id=1) # app with no env vars+ stale = ComposeEnvService.override_path(str(tmp_path))+ with open(stale, 'w', encoding='utf-8') as f:+ f.write('services: {}\n')+ path = ComposeEnvService.refresh_for_project(str(tmp_path))+ assert path is None+ assert not os.path.exists(stale) # stale override cleaned up+++def test_non_app_dir_is_untouched(app, tmp_path):+ from app.services.compose_env_service import ComposeEnvService+ with app.app_context():+ open(os.path.join(str(tmp_path), 'docker-compose.yml'), 'w').close()+ # No Application has this root_path → leave it alone.+ assert ComposeEnvService.refresh_for_project(str(tmp_path)) is None+ assert not os.path.exists(ComposeEnvService.override_path(str(tmp_path)))+++def test_compose_up_includes_override(app, tmp_path, monkeypatch):+ from app.services import docker_service as ds_mod+ from app.services.docker_service import DockerService+ with app.app_context():+ a = _make_compose_app(tmp_path, workspace_id=3)+ _set_local(a.id, 'FOO', 'bar')++ # Pin the compose binary so detection doesn't shell out.+ monkeypatch.setattr(DockerService, '_compose_cmd', ['docker', 'compose'], raising=False)++ captured = {}++ class _Result:+ returncode = 0+ stdout = ''+ stderr = ''++ def _fake_run(cmd, **kwargs):+ captured['cmd'] = cmd+ return _Result()++ monkeypatch.setattr(ds_mod.subprocess, 'run', _fake_run)++ DockerService.compose_up(str(tmp_path))+ cmd = captured['cmd']+ assert cmd.count('-f') == 2+ assert any('docker-compose.serverkit.yml' in str(c) for c in cmd)+ assert cmd[-1] == 'up' or 'up' in cmd