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
Bound panel logs, old installs and small-disk data
Juan Denis9500ae5d2836
diff --git a/backend/app/__init__.py b/backend/app/__init__.pyindex d72e2725..6c09f0b0 100644--- a/backend/app/__init__.py+++ b/backend/app/__init__.py@@ -259,6 +259,14 @@ def create_app(config_name=None):from app.services.settings_service import SettingsServiceSettingsService.initialize_defaults()SettingsService.migrate_legacy_roles()+ if not app.config.get('TESTING'):+ # Size-aware retention defaults, once per install (plan 85 §C).+ # Skipped under test: the result would depend on the test host's disk.+ try:+ from app.services import storage_profile_service+ storage_profile_service.apply_to_existing()+ except Exception as e:+ app.logger.warning(f'Storage profile not applied: {e}')# Encrypt any legacy plaintext provider secrets at rest (idempotent —# DNS-provider api keys and storage credentials predate encryption).@@ -445,6 +453,9 @@ def create_app(config_name=None):MetadataGuardService.register_jobs()if not app.config.get('TESTING'):MetadataGuardService.ensure() # converge the metadata egress rule (no-op when unsupported)+ # Bound the log files the panel writes or causes (plan 85 A5).+ from app.services import log_limits_service+ log_limits_service.converge()from app.services.speed_test_service import SpeedTestServiceSpeedTestService.register_jobs()from app.services import login_link_servicediff --git a/backend/app/jobs/builtin_handlers.py b/backend/app/jobs/builtin_handlers.pyindex 78f88099..07271e7d 100644--- a/backend/app/jobs/builtin_handlers.py+++ b/backend/app/jobs/builtin_handlers.py@@ -12,6 +12,7 @@ behavior — cadence handling, per-task de-dup, idempotency — is unchanged; onthe trigger moved from a bare thread to the unified job system."""import logging+import osfrom app.jobs.registry import register@@ -371,9 +372,20 @@ def run_telemetry_retention():from app.services import history_retention_serviceby_table.update(history_retention_service.prune())deleted = sum(by_table.values())- if deleted:- logger.info('Retention pruned %s row(s): %s', deleted, by_table)- return {'deleted': deleted, 'by_table': by_table}+ result = {'deleted': deleted, 'by_table': by_table} if deleted else {}+ if os.name != 'nt':+ # Files on the same tick: old update/build logs, and the previous+ # version's install once the current one has been live a day.+ from app.services import log_limits_service, slot_retention_service+ files = {k: v for k, v in log_limits_service.prune_files().items() if v}+ if files:+ result['files'] = files+ slot = slot_retention_service.remove_previous_slot()+ if slot:+ result['previous_slot'] = slot+ if result:+ logger.info('Retention pruned: %s', result)+ return resultreturn Nonediff --git a/backend/app/services/log_limits_service.py b/backend/app/services/log_limits_service.pynew file mode 100644index 00000000..5478bd53--- /dev/null+++ b/backend/app/services/log_limits_service.py@@ -0,0 +1,160 @@+"""Bound the log files ServerKit writes or causes (plan 85 A5).++Four files grew without limit:++- the panel's own ``access.log`` / ``error.log`` / ``alerts.log`` /+ ``security_alerts.log`` (appended forever — no logrotate file existed);+- one ``update-<ts>.log`` per update, never pruned;+- per-app build logs under ``builds/<app>/``, never pruned;+- managed containers' json-file logs (no size cap anywhere).++``converge()`` runs at panel start on a Linux host; the prunes also run on the+6-hour retention tick. Everything is best-effort: a host without logrotate or+Docker simply skips that part.+"""+import glob+import json+import logging+import os+import posixpath++logger = logging.getLogger(__name__)++LOGROTATE_PATH = '/etc/logrotate.d/serverkit'+DOCKER_DAEMON_JSON = '/etc/docker/daemon.json'+ROTATED_LOGS = ('access.log', 'error.log', 'alerts.log', 'security_alerts.log')+KEEP_UPDATE_LOGS = 10+KEEP_BUILD_LOGS_PER_APP = 10+DOCKER_LOG_MAX_FILE = '3'+++def logrotate_config(log_dir):+ # Always a Linux path: this file is only ever read by logrotate.+ paths = ' '.join(posixpath.join(log_dir, name) for name in ROTATED_LOGS)+ # copytruncate: gunicorn holds access/error.log open and is never told to+ # reopen them, so moving the file away would keep it writing to the old one.+ return (+ '# Managed by ServerKit (plan 85 A5) - rewritten at panel start.\n'+ f'{paths} {{\n'+ ' weekly\n'+ ' maxsize 50M\n'+ ' rotate 4\n'+ ' missingok\n'+ ' notifempty\n'+ ' compress\n'+ ' delaycompress\n'+ ' copytruncate\n'+ '}\n'+ )+++def ensure_logrotate(log_dir, path=LOGROTATE_PATH):+ """Write the logrotate file when logrotate is installed. Returns True when+ the file was written or changed."""+ if not os.path.isdir(os.path.dirname(path)):+ return False+ content = logrotate_config(log_dir)+ try:+ with open(path, encoding='utf-8') as f:+ if f.read() == content:+ return False+ except OSError:+ pass+ with open(path, 'w', encoding='utf-8') as f:+ f.write(content)+ return True+++def ensure_docker_log_opts(max_size, path=DOCKER_DAEMON_JSON):+ """Give Docker a default json-file size cap, merged into an existing+ daemon.json. Never restarts Docker (that would restart every app), so it+ applies to containers created after Docker's next restart.++ Left alone: no Docker on the host, a daemon.json that does not parse (we+ will not rewrite a file we cannot read), a non-json-file log driver, or a+ max-size the operator already chose. Returns True when the file changed.+ """+ if not os.path.isdir(os.path.dirname(path)):+ return False+ config = {}+ if os.path.exists(path):+ try:+ with open(path, encoding='utf-8') as f:+ config = json.load(f)+ except (OSError, ValueError):+ logger.warning('Not setting Docker log limits: %s does not parse', path)+ return False+ if not isinstance(config, dict):+ return False+ if config.get('log-driver', 'json-file') != 'json-file':+ return False+ opts = config.get('log-opts') or {}+ if not isinstance(opts, dict) or 'max-size' in opts:+ return False+ opts = {**opts, 'max-size': str(max_size)}+ opts.setdefault('max-file', DOCKER_LOG_MAX_FILE)+ config['log-opts'] = opts+ tmp = path + '.serverkit-tmp'+ with open(tmp, 'w', encoding='utf-8') as f:+ json.dump(config, f, indent=2)+ f.write('\n')+ os.replace(tmp, path)+ return True+++def _remove_all_but_newest(paths, keep):+ doomed = sorted(paths, key=lambda p: os.path.getmtime(p), reverse=True)[keep:]+ removed = 0+ for path in doomed:+ try:+ os.remove(path)+ removed += 1+ except OSError:+ pass+ return removed+++def prune_update_logs(log_dir, keep=KEEP_UPDATE_LOGS):+ return _remove_all_but_newest(glob.glob(os.path.join(log_dir, 'update-*.log')), keep)+++def prune_build_logs(build_log_dir, keep=KEEP_BUILD_LOGS_PER_APP):+ removed = 0+ for app_dir in glob.glob(os.path.join(build_log_dir, '*')):+ if os.path.isdir(app_dir):+ removed += _remove_all_but_newest(+ glob.glob(os.path.join(app_dir, 'build-*.json')), keep)+ return removed+++def prune_files():+ """The retention-tick half: old update and build logs."""+ from app import paths+ return {+ 'update_logs': prune_update_logs(paths.SERVERKIT_LOG_DIR),+ 'build_logs': prune_build_logs(paths.BUILD_LOG_DIR),+ }+++def converge():+ """Panel-start half: logrotate + Docker defaults, then the file prunes.+ Linux hosts only; every step is independent and best-effort."""+ if os.name == 'nt':+ return {}+ from app import paths+ from app.services import storage_profile_service+ from app.services.settings_service import SettingsService+ result = {}+ steps = (+ ('logrotate', lambda: ensure_logrotate(paths.SERVERKIT_LOG_DIR)),+ ('docker_log_opts', lambda: ensure_docker_log_opts(+ SettingsService.get('storage.docker_log_max_size')+ or storage_profile_service.default_for('storage.docker_log_max_size'))),+ ('files', prune_files),+ )+ for name, step in steps:+ try:+ result[name] = step()+ except Exception as e:+ logger.warning('Log limits: %s skipped: %s', name, e)+ return resultdiff --git a/backend/app/services/settings_service.py b/backend/app/services/settings_service.pyindex dd369ff8..7150e238 100644--- a/backend/app/services/settings_service.py+++ b/backend/app/services/settings_service.py@@ -64,6 +64,26 @@ class SettingsService:'type': 'integer','description': 'Days to retain telemetry rows (queue messages, system events, API usage logs) before pruning. Without this the database grows ~11 MB/day forever. 0 disables pruning.'},+ 'history.retention_days': {+ 'value': 90,+ 'type': 'integer',+ 'description': 'Days to retain finished history (error logs, notifications, deployment jobs, cron runs, webhook and event deliveries, sandbox runs) before pruning. 0 disables pruning.'+ },+ 'storage.docker_log_max_size': {+ 'value': '50m',+ 'type': 'string',+ 'description': 'Default size cap for each container log file (Docker json-file max-size, 3 files kept). Takes effect for containers created after Docker restarts.'+ },+ 'storage.previous_slot_hours': {+ 'value': 24,+ 'type': 'integer',+ 'description': 'Hours after a healthy update before the previous version\'s install is removed. Rollback within that window stays instant. 0 keeps it until the next update.'+ },+ 'storage.size_profile_applied': {+ 'value': False,+ 'type': 'boolean',+ 'description': 'Set once the disk-size defaults have been applied to this install.'+ },'onboarding_use_cases': {'value': [],'type': 'json',diff --git a/backend/app/services/slot_retention_service.py b/backend/app/services/slot_retention_service.pynew file mode 100644index 00000000..b5bcaf76--- /dev/null+++ b/backend/app/services/slot_retention_service.py@@ -0,0 +1,89 @@+"""Remove the previous version's install once the update has proven itself+(plan 85 §B1).++Updates install into the idle blue/green slot (``/opt/serverkit-a`` or+``-b``) and flip the ``/opt/serverkit`` symlink, so the version that was+running stays on disk — ~1.2 GB — as an instant rollback. That is worth+keeping for a day; weeks later nobody rolls back to it, and the next update+overwrites that slot anyway.++``storage.previous_slot_hours`` (default 24, ``0`` keeps the slot until the+next update) sets how long after the switch the idle slot is removed. The+switch time is the symlink's own mtime: the updater recreates it on every+switch. While an update holds its lock nothing is touched.+"""+import logging+import os+import shutil+import time++logger = logging.getLogger(__name__)++INSTALL_DIR = os.environ.get('SERVERKIT_INSTALL_DIR', '/opt/serverkit')+UPDATE_LOCK = os.environ.get('SERVERKIT_LOCK_FILE', '/var/lock/serverkit-update.lock')+DEFAULT_HOURS = 24+++def _update_running(lock_path):+ """True while ``serverkit update`` holds its flock."""+ try:+ import fcntl+ except ImportError: # not a POSIX host+ return False+ try:+ fd = os.open(lock_path, os.O_RDONLY)+ except OSError:+ return False # no lock file: no update has ever run+ try:+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ except OSError:+ return True+ else:+ fcntl.flock(fd, fcntl.LOCK_UN)+ return False+ finally:+ os.close(fd)+++def previous_slot(install_dir=INSTALL_DIR):+ """The idle slot directory, or None when this is not a blue/green install+ or there is no idle slot on disk."""+ if not os.path.islink(install_dir):+ return None+ active = os.path.realpath(install_dir)+ for suffix in ('-a', '-b'):+ slot = install_dir + suffix+ if os.path.isdir(slot) and not os.path.islink(slot) \+ and os.path.realpath(slot) != active:+ return slot+ return None+++def remove_previous_slot(hours=None, install_dir=INSTALL_DIR, lock_path=UPDATE_LOCK,+ now=None):+ """Delete the idle slot once the active one has been live ``hours``.+ Returns ``{'removed': path, 'bytes': n}`` or None when nothing was due."""+ if hours is None:+ from app.services.settings_service import SettingsService+ try:+ hours = int(SettingsService.get('storage.previous_slot_hours', DEFAULT_HOURS))+ except (TypeError, ValueError):+ hours = DEFAULT_HOURS+ if hours <= 0:+ return None+ slot = previous_slot(install_dir)+ if slot is None:+ return None+ live_for = (now or time.time()) - os.lstat(install_dir).st_mtime+ if live_for < hours * 3600 or _update_running(lock_path):+ return None++ from app.services.disk_reclaim_service import _path_size+ size = _path_size(slot)+ shutil.rmtree(slot, ignore_errors=True)+ if os.path.exists(slot):+ logger.warning('Could not fully remove the previous install at %s', slot)+ return None+ logger.info('Removed the previous install %s (%d bytes), live %.0f h',+ slot, size, live_for / 3600)+ return {'removed': slot, 'bytes': size}diff --git a/backend/app/services/storage_profile_service.py b/backend/app/services/storage_profile_service.pynew file mode 100644index 00000000..31885fdc--- /dev/null+++ b/backend/app/services/storage_profile_service.py@@ -0,0 +1,84 @@+"""Size-aware storage defaults (plan 85 §C).++A 25 GB VPS and a 500 GB server should not keep the same 30 days of+telemetry: on the small box ServerKit's own history competes with the apps+for the disk. This picks retention and log-size defaults from the size of the+filesystem the panel lives on.++These are defaults, never limits: every value is an ordinary setting the+operator can change, and nothing here refuses to run on small hardware.+"""+import logging+import os+import shutil++logger = logging.getLogger(__name__)++SMALL_DISK_BYTES = 50 * 1024 ** 3++# Values per profile. ``standard`` is what every install shipped with before+# profiles existed, so an existing row still holding it was never chosen.+PROFILES = {+ 'small': {+ 'telemetry.retention_days': 7,+ 'jobs.retention_days': 7,+ 'history.retention_days': 30,+ 'storage.docker_log_max_size': '10m',+ },+ 'standard': {+ 'telemetry.retention_days': 30,+ 'jobs.retention_days': 14,+ 'history.retention_days': 90,+ 'storage.docker_log_max_size': '50m',+ },+}++# Marks that the one-time adjustment below already ran on this install.+APPLIED_SETTING = 'storage.size_profile_applied'+++def _probe_path():+ from app.paths import SERVERKIT_DIR+ return SERVERKIT_DIR if os.path.isdir(SERVERKIT_DIR) else os.path.abspath(os.sep)+++def disk_total_bytes(path=None):+ try:+ return shutil.disk_usage(path or _probe_path()).total+ except OSError:+ return None+++def profile_name(total_bytes=None):+ """``small`` below 50 GB, else ``standard``. An unreadable disk size falls+ back to ``standard`` — the defaults that were always there."""+ if total_bytes is None:+ total_bytes = disk_total_bytes()+ if total_bytes is not None and total_bytes < SMALL_DISK_BYTES:+ return 'small'+ return 'standard'+++def default_for(key, total_bytes=None):+ return PROFILES[profile_name(total_bytes)].get(key)+++def apply_to_existing(total_bytes=None):+ """Once per install: on a small disk, move settings that still hold the+ old standard value to the small-disk value. A value the operator changed+ to anything else is left alone. Returns the keys that changed."""+ from app.services.settings_service import SettingsService++ if SettingsService.get(APPLIED_SETTING):+ return []+ changed = []+ if profile_name(total_bytes) == 'small':+ for key, value in PROFILES['small'].items():+ current = SettingsService.get(key)+ if current is None or str(current) == str(PROFILES['standard'][key]):+ SettingsService.set(key, value)+ changed.append(key)+ SettingsService.set(APPLIED_SETTING, True)+ if changed:+ logger.info('Small disk: set %s to the small-disk defaults', ', '.join(changed))+ return changeddiff --git a/backend/tests/test_storage_guardrails.py b/backend/tests/test_storage_guardrails.pynew file mode 100644index 00000000..df9663e0--- /dev/null+++ b/backend/tests/test_storage_guardrails.py@@ -0,0 +1,171 @@+"""Plan 85 A5 / §B1 / §C: log files are bounded, the previous install goes+once the update has proven itself, and small disks get small defaults."""+import json+import os+import time++import pytest++from app.services import log_limits_service as logs+from app.services import slot_retention_service as slots+from app.services import storage_profile_service as profile+from app.services.settings_service import SettingsService++GB = 1024 ** 3+++# ── A5: log files ──────────────────────────────────────────────────────────++def test_logrotate_file_covers_the_panel_logs_and_is_idempotent(tmp_path):+ target = tmp_path / 'logrotate.d' / 'serverkit'+ target.parent.mkdir()+ assert logs.ensure_logrotate('/var/log/serverkit', path=str(target)) is True+ body = target.read_text()+ for name in logs.ROTATED_LOGS:+ assert f'/var/log/serverkit/{name}' in body+ assert 'copytruncate' in body and 'maxsize 50M' in body+ assert logs.ensure_logrotate('/var/log/serverkit', path=str(target)) is False+++def test_no_logrotate_dir_means_nothing_is_written(tmp_path):+ assert logs.ensure_logrotate('/x', path=str(tmp_path / 'missing' / 'serverkit')) is False+++def test_docker_log_cap_is_merged_into_an_existing_daemon_json(tmp_path):+ daemon = tmp_path / 'daemon.json'+ daemon.write_text(json.dumps({'data-root': '/srv/docker', 'log-opts': {'labels': 'a'}}))+ assert logs.ensure_docker_log_opts('10m', path=str(daemon)) is True+ config = json.loads(daemon.read_text())+ assert config['data-root'] == '/srv/docker'+ assert config['log-opts'] == {'labels': 'a', 'max-size': '10m', 'max-file': '3'}++[email protected]('existing', [+ '{not json', # never rewrite what we cannot read+ json.dumps({'log-driver': 'journald'}), # another driver has its own limits+ json.dumps({'log-opts': {'max-size': '200m'}}), # the operator already chose+])+def test_docker_daemon_json_left_alone(tmp_path, existing):+ daemon = tmp_path / 'daemon.json'+ daemon.write_text(existing)+ assert logs.ensure_docker_log_opts('10m', path=str(daemon)) is False+ assert daemon.read_text() == existing+++def test_no_docker_dir_means_no_daemon_json(tmp_path):+ assert logs.ensure_docker_log_opts('10m', path=str(tmp_path / 'docker' / 'daemon.json')) is False+++def _files(directory, names):+ directory.mkdir(parents=True, exist_ok=True)+ now = time.time()+ for age, name in enumerate(names):+ path = directory / name+ path.write_text('x')+ os.utime(path, (now - age * 60, now - age * 60)) # first name = newest+++def test_only_the_newest_update_logs_are_kept(tmp_path):+ names = [f'update-2026090{i}-000000.log' for i in range(9)] + \+ [f'update-2026080{i}-000000.log' for i in range(5)]+ _files(tmp_path, names + ['access.log'])+ assert logs.prune_update_logs(str(tmp_path), keep=10) == 4+ left = sorted(p.name for p in tmp_path.iterdir())+ assert 'access.log' in left and len(left) == 11+ assert set(names[:10]) <= set(left)+++def test_build_logs_keep_the_newest_per_app(tmp_path):+ _files(tmp_path / '1', [f'build-{i}.json' for i in range(12)])+ _files(tmp_path / '2', ['build-0.json'])+ assert logs.prune_build_logs(str(tmp_path), keep=10) == 2+ assert len(list((tmp_path / '1').iterdir())) == 10+ assert len(list((tmp_path / '2').iterdir())) == 1+++# ── §C: size-aware defaults ────────────────────────────────────────────────++def test_profile_follows_disk_size():+ assert profile.profile_name(25 * GB) == 'small'+ assert profile.profile_name(200 * GB) == 'standard'+ assert profile.profile_name(None) in ('small', 'standard')+++def test_small_disk_moves_untouched_defaults_once(app):+ SettingsService.set('telemetry.retention_days', 30) # the old default+ SettingsService.set('jobs.retention_days', 21) # the operator's choice+ changed = profile.apply_to_existing(total_bytes=25 * GB)++ assert 'telemetry.retention_days' in changed+ assert int(SettingsService.get('telemetry.retention_days')) == 7+ assert int(SettingsService.get('jobs.retention_days')) == 21+ # Once only: a later change back to 30 is the operator's and stays.+ SettingsService.set('telemetry.retention_days', 30)+ assert profile.apply_to_existing(total_bytes=25 * GB) == []+ assert int(SettingsService.get('telemetry.retention_days')) == 30+++def test_a_large_disk_keeps_the_standard_values(app):+ SettingsService.set('telemetry.retention_days', 30)+ assert profile.apply_to_existing(total_bytes=500 * GB) == []+ assert int(SettingsService.get('telemetry.retention_days')) == 30+++# ── §B1: previous slot ─────────────────────────────────────────────────────++posix_only = pytest.mark.skipif(os.name == 'nt', reason='symlinks + flock (runs on Linux CI)')+++def _bluegreen(tmp_path, switched_hours_ago):+ install = tmp_path / 'serverkit'+ for slot in ('serverkit-a', 'serverkit-b'):+ (tmp_path / slot / 'backend').mkdir(parents=True)+ (tmp_path / slot / 'backend' / 'app.py').write_text('x' * 100)+ os.symlink(tmp_path / 'serverkit-b', install)+ when = time.time() - switched_hours_ago * 3600+ os.utime(install, (when, when), follow_symlinks=False)+ return str(install)+++@posix_only+def test_the_previous_slot_goes_a_day_after_the_switch(tmp_path):+ install = _bluegreen(tmp_path, switched_hours_ago=30)+ result = slots.remove_previous_slot(hours=24, install_dir=install,+ lock_path=str(tmp_path / 'no.lock'))+ assert result and result['removed'] == str(tmp_path / 'serverkit-a')+ assert not (tmp_path / 'serverkit-a').exists()+ assert (tmp_path / 'serverkit-b' / 'backend' / 'app.py').exists()+++@posix_only+def test_a_fresh_update_keeps_its_rollback(tmp_path):+ install = _bluegreen(tmp_path, switched_hours_ago=2)+ assert slots.remove_previous_slot(hours=24, install_dir=install,+ lock_path=str(tmp_path / 'no.lock')) is None+ assert (tmp_path / 'serverkit-a').exists()+++@posix_only+def test_nothing_is_removed_while_an_update_holds_the_lock(tmp_path):+ import fcntl+ install = _bluegreen(tmp_path, switched_hours_ago=30)+ lock = tmp_path / 'update.lock'+ lock.write_text('')+ with open(lock) as held:+ fcntl.flock(held, fcntl.LOCK_EX)+ assert slots.remove_previous_slot(hours=24, install_dir=install,+ lock_path=str(lock)) is None+ assert (tmp_path / 'serverkit-a').exists()+++@posix_only+def test_zero_hours_keeps_the_slot(tmp_path):+ install = _bluegreen(tmp_path, switched_hours_ago=300)+ assert slots.remove_previous_slot(hours=0, install_dir=install) is None+ assert (tmp_path / 'serverkit-a').exists()+++def test_a_plain_directory_install_has_no_previous_slot(tmp_path):+ (tmp_path / 'serverkit').mkdir()+ (tmp_path / 'serverkit-a').mkdir()+ assert slots.previous_slot(str(tmp_path / 'serverkit')) is None