Skip to content

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.

refactor(db): one SQL-exec dispatcher, and adopt the doors already built

Juan Denisb768fef72864
diff --git a/backend/app/services/cron_service.py b/backend/app/services/cron_service.py
index 33e7a40c..3d682b38 100644
--- a/backend/app/services/cron_service.py
+++ b/backend/app/services/cron_service.py
@@ -15,6 +15,8 @@ from typing import Dict, List, Optional
from datetime import datetime
import json
+from app.utils.system import unit_is_active
+
# Path for storing job metadata
JOBS_FILE = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', 'cron_jobs.json')
@@ -81,15 +83,10 @@ class CronService:
"""Get cron service status."""
if cls.is_linux():
# Check if cron daemon is running
- try:
- result = subprocess.run(
- ['systemctl', 'is-active', 'cron'],
- capture_output=True,
- text=True,
- timeout=5
- )
- cron_active = result.stdout.strip() == 'active'
- except (subprocess.SubprocessError, FileNotFoundError):
+ active = unit_is_active('cron', timeout=5)
+ if active is not None:
+ cron_active = active
+ else:
# Try alternative check
try:
result = subprocess.run(
diff --git a/backend/app/services/database_service.py b/backend/app/services/database_service.py
index 136e0d2f..eea63ccd 100644
--- a/backend/app/services/database_service.py
+++ b/backend/app/services/database_service.py
@@ -7,6 +7,7 @@ import json
from datetime import datetime
from app import paths
+from app.utils.system import unit_is_active
def _validate_identifier(name: str, max_length: int = 64) -> bool:
@@ -41,22 +42,27 @@ class DatabaseService:
@staticmethod
def mysql_is_running():
- """Check if MySQL/MariaDB is running."""
- try:
- result = subprocess.run(
- ['systemctl', 'is-active', 'mysql'],
- capture_output=True, text=True
- )
- if result.returncode == 0:
- return True
- # Try mariadb service name
- result = subprocess.run(
- ['systemctl', 'is-active', 'mariadb'],
- capture_output=True, text=True
- )
- return result.returncode == 0
- except Exception:
- return False
+ """Check if MySQL/MariaDB is running (either unit name)."""
+ return any(unit_is_active(unit) is True for unit in ('mysql', 'mariadb'))
+
+ @staticmethod
+ def _mysql_env(root_password):
+ """Process env with ``MYSQL_PWD`` set, or ``None`` when there is no password.
+
+ Pasted verbatim eight times in this file (plan 75 §G5). It carries a
+ real rule — the password goes in the environment, never on the argv,
+ where ``ps`` and the shell history would see it — and a rule that lives
+ in eight copies is a rule that only holds in the copies someone
+ remembered to update.
+
+ ``None`` (rather than ``os.environ.copy()``) when there is no password,
+ so ``subprocess`` inherits the parent environment exactly as before.
+ """
+ if not root_password:
+ return None
+ env = os.environ.copy()
+ env['MYSQL_PWD'] = root_password
+ return env
@staticmethod
def mysql_execute(query, database=None, root_password=None):
@@ -67,11 +73,7 @@ class DatabaseService:
cmd.extend(['-D', database])
cmd.extend(['-e', query])
- # Use MYSQL_PWD env var to avoid passing password on CLI
- env = None
- if root_password:
- env = os.environ.copy()
- env['MYSQL_PWD'] = root_password
+ env = DatabaseService._mysql_env(root_password)
result = subprocess.run(
cmd, capture_output=True, text=True, env=env
@@ -104,11 +106,7 @@ class DatabaseService:
if database:
cmd.extend(['-D', database])
- # Use MYSQL_PWD env var to avoid passing password on CLI
- env = None
- if root_password:
- env = os.environ.copy()
- env['MYSQL_PWD'] = root_password
+ env = DatabaseService._mysql_env(root_password)
# Build a safe query using user-defined variables and EXECUTE
# For simple single-param queries, we use a quoted literal approach
@@ -233,10 +231,7 @@ class DatabaseService:
try:
cmd = ['mysql', '-u', 'root']
- env = None
- if root_password:
- env = os.environ.copy()
- env['MYSQL_PWD'] = root_password
+ env = DatabaseService._mysql_env(root_password)
# Hex-encode the password so it never appears as a raw string in SQL.
# UNHEX converts it back to bytes, CAST converts to string, QUOTE wraps
@@ -334,11 +329,7 @@ class DatabaseService:
cmd = ['mysqldump', '-u', 'root']
cmd.append(database)
- # Use MYSQL_PWD env var to avoid passing password on CLI
- env = None
- if root_password:
- env = os.environ.copy()
- env['MYSQL_PWD'] = root_password
+ env = DatabaseService._mysql_env(root_password)
# Pipe through gzip
with open(output_path, 'wb') as f:
@@ -368,11 +359,7 @@ class DatabaseService:
cmd = ['mysql', '-u', 'root']
cmd.append(database)
- # Use MYSQL_PWD env var to avoid passing password on CLI
- env = None
- if root_password:
- env = os.environ.copy()
- env['MYSQL_PWD'] = root_password
+ env = DatabaseService._mysql_env(root_password)
if backup_path.endswith('.gz'):
# Decompress and restore
@@ -444,14 +431,7 @@ class DatabaseService:
@staticmethod
def pg_is_running():
"""Check if PostgreSQL is running."""
- try:
- result = subprocess.run(
- ['systemctl', 'is-active', 'postgresql'],
- capture_output=True, text=True
- )
- return result.returncode == 0
- except Exception:
- return False
+ return unit_is_active('postgresql') is True
@staticmethod
def pg_execute(query, database='postgres', user='postgres'):
@@ -712,11 +692,7 @@ class DatabaseService:
# Build mysql command with JSON output format
cmd = ['mysql', '-u', 'root']
- # Use MYSQL_PWD env var to avoid passing password on CLI
- env = None
- if root_password:
- env = os.environ.copy()
- env['MYSQL_PWD'] = root_password
+ env = DatabaseService._mysql_env(root_password)
cmd.extend([
'-D', database,
'-e', query,
@@ -1224,8 +1200,17 @@ class DatabaseService:
return 'mysql'
@staticmethod
- def docker_mysql_execute(container_name, query, database=None, user='root', password=None):
- """Execute a MySQL query inside a Docker container."""
+ def docker_mysql_execute(container_name, query, database=None, user='root',
+ password=None, machine_readable=False, timeout=30):
+ """Execute a MySQL query inside a Docker container.
+
+ ``machine_readable`` adds ``-N -B``: no column headers, tab-separated,
+ no box drawing. Callers that parse the output need it; callers that
+ show the output to a human do not. It is a flag rather than a second
+ method because ``db_config_tuner_service`` used to keep a private copy
+ of this whole function purely to add those two characters
+ (plan 75 §G5).
+ """
try:
cmd = ['docker', 'exec']
@@ -1238,9 +1223,12 @@ class DatabaseService:
if database:
cmd.extend(['-D', database])
+ if machine_readable:
+ cmd.extend(['-N', '-B'])
+
cmd.extend(['-e', query])
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
return {
'success': result.returncode == 0,
'output': result.stdout,
@@ -1253,7 +1241,7 @@ class DatabaseService:
@staticmethod
def docker_pg_execute(container_name, query, database='postgres', user='postgres',
- password=None, timeout=30):
+ password=None, timeout=30, machine_readable=False):
"""Execute a PostgreSQL statement inside a Docker container.
The psql twin of :meth:`docker_mysql_execute`, so anything that has to
@@ -1274,6 +1262,10 @@ class DatabaseService:
'-d', database or 'postgres',
'-v', 'ON_ERROR_STOP=1',
'-c', query, '-t', '-A'])
+ if machine_readable:
+ # tab, not the '|' default: the callers that parse this split
+ # on tabs, and a '|' is legal inside a value.
+ cmd.extend(['-F', '\t'])
result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
return {
'success': result.returncode == 0,
diff --git a/backend/app/services/db_config_tuner_service.py b/backend/app/services/db_config_tuner_service.py
index 2705f665..3c1f3b4b 100644
--- a/backend/app/services/db_config_tuner_service.py
+++ b/backend/app/services/db_config_tuner_service.py
@@ -29,6 +29,7 @@ import time
from datetime import datetime
from app import paths
+from app.services import db_exec
logger = logging.getLogger(__name__)
@@ -160,22 +161,14 @@ class DbConfigTunerService:
@classmethod
def _exec_sql(cls, target, sql):
- """Run SQL inside the target's container. Single SQL choke point.
+ """Run SQL against the target's engine — via ``db_exec`` (§G5).
``target``: {'container', 'engine', 'user', 'password'}.
Returns {'success', 'output', 'error'} with machine-readable output
- (tab-separated, no headers/decorations).
+ (tab-separated, no headers/decorations) — the reason this used to be a
+ private copy of the whole dispatcher rather than a call into it.
"""
- args = ['exec']
- if target['engine'] == 'mysql':
- if target.get('password'):
- args += ['-e', 'MYSQL_PWD=%s' % target['password']]
- args += [target['container'], 'mysql', '-u', target.get('user') or 'root',
- '-N', '-B', '-e', sql]
- else:
- args += [target['container'], 'psql', '-U', target.get('user') or 'postgres',
- '-t', '-A', '-F', '\t', '-c', sql]
- return cls._docker(args, timeout=30)
+ return db_exec.exec_sql(target, sql, machine_readable=True, timeout=30)
@classmethod
def _restart(cls, container):
diff --git a/backend/app/services/db_exec.py b/backend/app/services/db_exec.py
new file mode 100644
index 00000000..c6ee4f4a
--- /dev/null
+++ b/backend/app/services/db_exec.py
@@ -0,0 +1,94 @@
+"""One SQL-exec dispatcher (plan 75 §G5).
+
+`engine × host` dispatch — mysql/postgres crossed with native/docker — was
+written three times: `db_config_tuner_service`, `db_process_service`, and
+`managed_db_user_service`. Only the last delegated fully to `DatabaseService`'s
+four executors; the other two grew private copies that had already drifted (one
+never learned about `ON_ERROR_STOP=1`, so psql reported success for statements
+the server rejected).
+
+This module is the dispatch, and nothing else. The four executors stay in
+`DatabaseService` — this does not become a second place where SQL is run, only
+the single place where "which executor" is decided.
+
+Every executor returns the same shape, and so does this::
+
+ {'success': bool, 'output': str, 'error': str|None}
+
+A target is a plain dict so callers can build one from a `ManagedDatabase` row,
+a container name, or a tuner probe without a common base class:
+
+ {'engine': 'mysql'|'postgresql', 'container': str|None,
+ 'user': str|None, 'password': str|None, 'database': str|None}
+
+`container` is what decides native-vs-docker. `None`/absent means native.
+"""
+
+from typing import Optional
+
+from app.services.database_service import DatabaseService
+
+# The names the rest of the codebase spells these engines with. Kept here so an
+# unknown engine is *reported*, never silently routed to mysql (§A).
+MYSQL_ALIASES = ('mysql', 'mariadb')
+POSTGRES_ALIASES = ('postgresql', 'postgres', 'pgsql')
+
+
+def target(engine: str, *, container: Optional[str] = None,
+ user: Optional[str] = None, password: Optional[str] = None,
+ database: Optional[str] = None) -> dict:
+ """Build a target dict. Keyword-only so a call site reads at a glance."""
+ return {'engine': engine, 'container': container, 'user': user,
+ 'password': password, 'database': database}
+
+
+def target_from_managed(managed, *, password: Optional[str] = None) -> dict:
+ """A target for a ``ManagedDatabase`` row.
+
+ The admin secret is passed in rather than decrypted here: decryption needs
+ app context, and this module deliberately does not reach for it.
+ """
+ return target(
+ managed.engine,
+ container=managed.container_ref if managed.host_kind == 'docker' else None,
+ user=managed.admin_username or None,
+ password=password,
+ )
+
+
+def exec_sql(tgt: dict, sql: str, *, machine_readable: bool = False,
+ timeout: int = 30) -> dict:
+ """Run *sql* against *tgt*'s engine, native or containerised.
+
+ ``machine_readable`` asks the client for parse-friendly output (no headers,
+ tab-separated). Pass it when the caller splits the output; leave it off
+ when the output is shown to a person.
+ """
+ engine = (tgt.get('engine') or '').strip().lower()
+ container = tgt.get('container')
+ user = tgt.get('user')
+ password = tgt.get('password')
+ database = tgt.get('database')
+
+ if engine in MYSQL_ALIASES:
+ if container:
+ return DatabaseService.docker_mysql_execute(
+ container, sql, database=database, user=user or 'root',
+ password=password, machine_readable=machine_readable,
+ timeout=timeout)
+ return DatabaseService.mysql_execute(
+ sql, database=database, root_password=password)
+
+ if engine in POSTGRES_ALIASES:
+ if container:
+ return DatabaseService.docker_pg_execute(
+ container, sql, database=database or 'postgres',
+ user=user or 'postgres', password=password,
+ timeout=timeout, machine_readable=machine_readable)
+ return DatabaseService.pg_execute(
+ sql, database=database or 'postgres', user=user or 'postgres')
+
+ # Not a default. An unrecognised engine that quietly ran as mysql would
+ # produce a syntax error blamed on the caller's SQL.
+ return {'success': False, 'output': '',
+ 'error': f"unsupported engine: {tgt.get('engine')!r}"}
diff --git a/backend/app/services/db_process_service.py b/backend/app/services/db_process_service.py
index f59cc323..a410f03b 100644
--- a/backend/app/services/db_process_service.py
+++ b/backend/app/services/db_process_service.py
@@ -14,9 +14,7 @@ Every statement flows through the single :meth:`DbProcessService._exec_sql`
choke-point (which reuses the exact exec pathways the explorer already uses in
``DatabaseService``), so tests can stub one method to cover everything.
"""
-import subprocess
-
-from app.services.database_service import DatabaseService
+from app.services import db_exec
SUPPORTED_ENGINES = ('mysql', 'postgresql')
@@ -43,44 +41,14 @@ class DbProcessService:
# ------------------------------------------------------------------
@staticmethod
def _exec_sql(target, sql):
- """Run ``sql`` against the target's engine.
+ """Run ``sql`` against the target's engine — via ``db_exec`` (§G5).
- Returns ``{'success': bool, 'output': str, 'error': str|None}`` —
- the same shape the underlying ``DatabaseService`` helpers return.
+ The named seam stays (tests stub it); the engine × host dispatch it used
+ to re-implement does not. The private docker-postgres branch here never
+ set ``ON_ERROR_STOP=1``, so psql reported success for statements the
+ server had rejected — the drift that made three copies worth one.
"""
- engine = target.get('engine')
- container = target.get('container')
- try:
- if container:
- if engine == 'mysql':
- return DatabaseService.docker_mysql_execute(
- container, sql,
- user=target.get('user') or 'root',
- password=target.get('password'),
- )
- # Docker PostgreSQL — same docker-exec pathway, psql client.
- cmd = ['docker', 'exec']
- if target.get('password'):
- cmd.extend(['-e', f"PGPASSWORD={target['password']}"])
- cmd.extend([
- container, 'psql',
- '-U', target.get('user') or 'postgres',
- '-d', target.get('database') or 'postgres',
- '-t', '-A', '-c', sql,
- ])
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None,
- }
- if engine == 'mysql':
- return DatabaseService.mysql_execute(sql, root_password=target.get('password'))
- return DatabaseService.pg_execute(sql)
- except subprocess.TimeoutExpired:
- return {'success': False, 'output': '', 'error': 'Query timed out'}
- except Exception as e: # docker/client missing (e.g. Windows dev box)
- return {'success': False, 'output': '', 'error': str(e)}
+ return db_exec.exec_sql(target, sql)
# ------------------------------------------------------------------
# Public API
diff --git a/backend/app/services/managed_db_user_service.py b/backend/app/services/managed_db_user_service.py
index 437eaf9c..3c3a1264 100644
--- a/backend/app/services/managed_db_user_service.py
+++ b/backend/app/services/managed_db_user_service.py
@@ -17,7 +17,7 @@ from datetime import datetime
from app import db
from app.models.managed_database_user import ManagedDatabaseUser # explicit import registers the table
-from app.services.database_service import DatabaseService
+from app.services import db_exec
from app.utils.crypto import decrypt_secret_safe
logger = logging.getLogger(__name__)
@@ -43,30 +43,7 @@ class ManagedDbUserService:
shape of ``DatabaseService`` executors.
"""
secret = decrypt_secret_safe(managed.admin_secret_encrypted or '') or None
- if managed.engine == 'mysql':
- if managed.host_kind == 'docker' and managed.container_ref:
- return DatabaseService.docker_mysql_execute(
- managed.container_ref, sql,
- user=managed.admin_username or 'root', password=secret)
- return DatabaseService.mysql_execute(sql, root_password=secret)
- if managed.engine == 'postgresql':
- if managed.host_kind == 'docker' and managed.container_ref:
- return cls._docker_pg_execute(
- managed.container_ref, sql,
- user=managed.admin_username or 'postgres', password=secret)
- return DatabaseService.pg_execute(sql)
- return {'success': False, 'error': 'unsupported engine'}
-
- @staticmethod
- def _docker_pg_execute(container_name, sql, user='postgres', password=None):
- """psql inside a Docker container.
-
- ``database_service`` grew the pg twin of ``docker_mysql_execute``; this
- stays as the named seam these callers use, with one implementation
- underneath.
- """
- return DatabaseService.docker_pg_execute(
- container_name, sql, database='postgres', user=user, password=password)
+ return db_exec.exec_sql(db_exec.target_from_managed(managed, password=secret), sql)
# ── validation / quoting helpers ──
@staticmethod
diff --git a/backend/app/services/preview_service.py b/backend/app/services/preview_service.py
index 06302a85..543f6841 100644
--- a/backend/app/services/preview_service.py
+++ b/backend/app/services/preview_service.py
@@ -21,6 +21,8 @@ import logging
import re
from datetime import datetime, timedelta
+from app.utils.slug import slugify
+
logger = logging.getLogger(__name__)
# Job kinds — also registered in :meth:`PreviewService.register_jobs`.
@@ -37,10 +39,7 @@ def _slugify_branch(branch):
non-alphanumerics collapses to a single dash, leading/trailing dashes
trimmed. Empty input yields ``branch``.
"""
- if not branch:
- return 'branch'
- label = re.sub(r'[^a-z0-9]+', '-', str(branch).lower()).strip('-')
- return label or 'branch'
+ return slugify(branch) or 'branch'
class PreviewService:
diff --git a/backend/app/services/process_service.py b/backend/app/services/process_service.py
index a340cdd9..1bae184a 100644
--- a/backend/app/services/process_service.py
+++ b/backend/app/services/process_service.py
@@ -3,7 +3,7 @@ import subprocess
import platform
from typing import List, Dict, Optional
-from app.utils.system import run_privileged
+from app.utils.system import run_privileged, unit_is_active
class ProcessService:
@@ -275,25 +275,20 @@ class ProcessService:
@classmethod
def get_systemd_unit_status(cls, unit_name: str) -> Dict:
- """Check whether a systemd unit is active using systemctl."""
- system = platform.system()
- if system != 'Linux':
+ """Check whether a systemd unit is active — via ``unit_is_active`` (§G6).
+
+ The probe itself was re-inlined in four other services; this keeps the
+ dict shape its own callers read and lets one implementation answer.
+ """
+ if platform.system() != 'Linux':
return {'success': True, 'status': 'unknown', 'active': False}
- try:
- result = subprocess.run(
- ['systemctl', 'is-active', unit_name],
- capture_output=True, text=True, timeout=10
- )
- active = result.returncode == 0 and result.stdout.strip() == 'active'
- status = result.stdout.strip() if result.stdout else 'unknown'
- return {'success': True, 'status': status, 'active': active}
- except FileNotFoundError:
- return {'success': False, 'error': 'systemctl not found'}
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Command timed out'}
- except Exception as e:
- return {'success': False, 'error': str(e)}
+ active = unit_is_active(unit_name)
+ if active is None:
+ return {'success': False, 'error': 'could not query systemd'}
+ return {'success': True,
+ 'status': 'active' if active else 'inactive',
+ 'active': active}
@classmethod
def get_compose_project_status(cls, project_path: str, compose_file: str = None) -> Dict:
diff --git a/backend/app/services/saved_view_service.py b/backend/app/services/saved_view_service.py
index 0d0f2d9a..2c3ae234 100644
--- a/backend/app/services/saved_view_service.py
+++ b/backend/app/services/saved_view_service.py
@@ -1,8 +1,8 @@
import logging
-import re
from app import db
from app.models.saved_view import SavedView
+from app.utils.slug import slugify as _slugify, unique_slug
logger = logging.getLogger(__name__)
@@ -10,9 +10,12 @@ MAX_VIEWS_PER_PAGE = 50
def slugify(value):
- """URL handle for a view name: 'SSL expiring ≤ 30d' -> 'ssl-expiring-30d'."""
- slug = re.sub(r'[^a-z0-9]+', '-', (value or '').lower()).strip('-')
- return slug[:140] or 'view'
+ """URL handle for a view name: 'SSL expiring ≤ 30d' -> 'ssl-expiring-30d'.
+
+ The regex lives in ``app.utils.slug``; the 140-char cap is this caller's
+ own rule (the column width) and stays here.
+ """
+ return _slugify(value)[:140] or 'view'
def _unique_slug(user_id, page, name, exclude_id=None):
@@ -21,17 +24,15 @@ def _unique_slug(user_id, page, name, exclude_id=None):
Scoped to live rows only: a tombstoned view must not reserve its handle
forever, or deleting a view would burn the link name with it.
"""
- base = slugify(name)
- slug, n = base, 2
- while True:
+ def taken(candidate):
q = (SavedView.query_active()
- .filter_by(user_id=user_id, page=page, slug=slug))
+ .filter_by(user_id=user_id, page=page, slug=candidate))
if exclude_id is not None:
q = q.filter(SavedView.id != exclude_id)
- if not q.first():
- return slug
- slug = f'{base}-{n}'
- n += 1
+ return q.first() is not None
+
+ # start=2 keeps the handles this service has always generated.
+ return unique_slug(slugify(name), taken, default='view', start=2)
def get_by_slug(user_id, page, slug):
diff --git a/backend/app/services/uptime_service.py b/backend/app/services/uptime_service.py
index 34dd6f43..6a992146 100644
--- a/backend/app/services/uptime_service.py
+++ b/backend/app/services/uptime_service.py
@@ -5,13 +5,17 @@ Tracks server uptime history and provides uptime statistics.
"""
import os
-import json
import time
import psutil
from datetime import datetime, timedelta
from typing import Dict, List, Optional
from pathlib import Path
import threading
+import logging
+
+from app.utils.config_store import load_json_config, save_json_config
+
+logger = logging.getLogger(__name__)
class UptimeService:
@@ -41,32 +45,30 @@ class UptimeService:
@classmethod
def _load_history(cls) -> Dict:
- """Load uptime history from file."""
+ """Load uptime history from file, or a fresh one if unreadable."""
cls._ensure_data_dir()
-
- if os.path.exists(cls.UPTIME_FILE):
- try:
- with open(cls.UPTIME_FILE, 'r') as f:
- return json.load(f)
- except Exception:
- pass
-
- return {
+ return load_json_config(cls.UPTIME_FILE, {
'start_time': datetime.now().isoformat(),
'checks': [],
- 'incidents': []
- }
+ 'incidents': [],
+ })
@classmethod
- def _save_history(cls, history: Dict) -> None:
- """Save uptime history to file."""
+ def _save_history(cls, history: Dict) -> bool:
+ """Save uptime history to file. ``True`` when it was actually written.
+
+ Was ``except Exception: pass`` over a truncating write (plan 75 §G6):
+ a full disk both destroyed the existing history and reported nothing,
+ so the panel went on rendering an uptime figure computed from a file
+ that had stopped being updated. Now the write is atomic and the failure
+ is logged and returned, so a caller can tell "saved" from "could not".
+ """
cls._ensure_data_dir()
-
- try:
- with open(cls.UPTIME_FILE, 'w') as f:
- json.dump(history, f)
- except Exception:
- pass
+ result = save_json_config(cls.UPTIME_FILE, history)
+ if not result['success']:
+ logger.warning('Could not save uptime history to %s: %s',
+ cls.UPTIME_FILE, result['error'])
+ return result['success']
@classmethod
def get_boot_time(cls) -> datetime:
diff --git a/backend/app/utils/config_store.py b/backend/app/utils/config_store.py
index 4106d2c0..de5fa43d 100644
--- a/backend/app/utils/config_store.py
+++ b/backend/app/utils/config_store.py
@@ -8,10 +8,17 @@ the per-service parts stay per-service: the path, the default, and any extra
behaviour around the write (cache invalidation, secret merging — those keep
their own methods and may call these helpers inside).
-Deliberate parity with the copies they replace: plain ``open(..., 'w')``, no
-locking, no atomic rename — none of the originals had them, and adding them
-here would be a behaviour change smuggled in with a consolidation. If atomic
-writes are ever wanted, they land here, once.
+The write is atomic (plan 75 §G6). The consolidation that created this module
+deliberately kept the copies' plain ``open(..., 'w')`` so that no behaviour
+change rode along with it, and noted that atomic writes, if ever wanted, would
+land here once. This is that once: a truncating write that dies mid-``json.dump``
+— a full disk, a killed process — leaves a truncated file where a config used
+to be, and every caller's load path treats a corrupt config as "use the
+default". That is a config silently reset to defaults, which is precisely the
+class of false fact this plan exists to remove.
+
+``file_integrity_service._save_state`` already used tmp + ``os.replace`` and is
+the pattern followed here.
"""
import copy
import json
@@ -37,11 +44,25 @@ def load_json_config(path, default):
def save_json_config(path, config):
"""dict → JSON file (indented), creating the parent dir. Returns the
- service-dict shape every caller already returns."""
+ service-dict shape every caller already returns.
+
+ Written to a sibling temp file and ``os.replace``d into position, so a
+ reader never sees a half-written config and a failed write leaves the
+ previous one intact. ``os.replace`` is atomic on POSIX and on Windows.
+ """
+ directory = os.path.dirname(path) or '.'
+ tmp = f'{path}.tmp'
try:
- os.makedirs(os.path.dirname(path) or '.', exist_ok=True)
- with open(path, 'w') as f:
+ os.makedirs(directory, exist_ok=True)
+ with open(tmp, 'w') as f:
json.dump(config, f, indent=2)
+ os.replace(tmp, path)
return {'success': True, 'message': 'Configuration saved'}
except Exception as e: # noqa: BLE001
+ # The old file is still whole; drop the partial temp so a later write
+ # is not confused by it. Cleanup failure must not mask the real error.
+ try:
+ os.remove(tmp)
+ except OSError:
+ pass
return {'success': False, 'error': str(e)}
diff --git a/backend/app/utils/slug.py b/backend/app/utils/slug.py
index 95601354..68c28c92 100644
--- a/backend/app/utils/slug.py
+++ b/backend/app/utils/slug.py
@@ -23,7 +23,7 @@ def slugify(text):
return text
-def unique_slug(base, exists, default='untitled'):
+def unique_slug(base, exists, default='untitled', start=1):
"""Return a unique slug based on `base`.
`exists` is a callable that takes a slug and returns True if it is already
@@ -34,6 +34,11 @@ def unique_slug(base, exists, default='untitled'):
base: The text or slug to start from.
exists: Callable(slug: str) -> bool.
default: Fallback string when `base` slugifies to something empty.
+ start: First suffix to try. `saved_view_service` numbered its
+ duplicates from 2 and kept a private copy of this loop for that one
+ difference (plan 75 §G6); a parameter is cheaper than a second
+ implementation, and changing its numbering would have renamed
+ handles users already had.
Returns:
A unique slug string.
@@ -42,7 +47,7 @@ def unique_slug(base, exists, default='untitled'):
if not exists(base_slug):
return base_slug
- counter = 1
+ counter = start
while True:
candidate = f'{base_slug}-{counter}'
if not exists(candidate):
diff --git a/backend/tests/test_config_store.py b/backend/tests/test_config_store.py
index 0c0611da..7049159d 100644
--- a/backend/tests/test_config_store.py
+++ b/backend/tests/test_config_store.py
@@ -83,3 +83,66 @@ def test_converted_services_use_the_store(tmp_path, monkeypatch):
assert 'webhook_secret' in default and default['apps'] == {}
assert GitService.save_config({'apps': {'1': {}}})['success'] is True
assert GitService.get_config() == {'apps': {'1': {}}}
+
+
+# ── atomic write (plan 75 §G6) ──────────────────────────────────────────────
+# The §F4 consolidation deliberately kept the copies' truncating write so no
+# behaviour change rode along with it. This is the follow-up it pointed at.
+
+def test_a_failed_write_leaves_the_previous_config_intact(tmp_path, monkeypatch):
+ """The reason atomicity is worth the temp file.
+
+ A truncating write that dies mid-dump leaves a truncated file, and
+ load_json_config treats a corrupt file as "use the default" — so a full
+ disk silently resets a config to defaults instead of failing.
+ """
+ path = str(tmp_path / 'cfg.json')
+ assert save_json_config(path, {'retention_days': 30})['success'] is True
+
+ def boom(*args, **kwargs):
+ raise OSError(28, 'No space left on device')
+
+ monkeypatch.setattr(json, 'dump', boom)
+ result = save_json_config(path, {'retention_days': 7})
+
+ assert result['success'] is False
+ assert 'No space left' in result['error']
+ assert load_json_config(path, {}) == {'retention_days': 30}
+
+
+def test_a_failed_write_leaves_no_temp_file_behind(tmp_path, monkeypatch):
+ path = str(tmp_path / 'cfg.json')
+ save_json_config(path, {'a': 1})
+
+ monkeypatch.setattr(json, 'dump', lambda *a, **k: (_ for _ in ()).throw(OSError('nope')))
+ save_json_config(path, {'a': 2})
+
+ assert os.listdir(tmp_path) == ['cfg.json']
+
+
+def test_the_write_goes_through_a_temp_file_then_replace(tmp_path, monkeypatch):
+ """Named so a future refactor cannot quietly drop the rename."""
+ path = str(tmp_path / 'cfg.json')
+ seen = {}
+ real_replace = os.replace
+
+ def spy(src, dst):
+ seen['src'], seen['dst'] = src, dst
+ return real_replace(src, dst)
+
+ monkeypatch.setattr(os, 'replace', spy)
+ assert save_json_config(path, {'a': 1})['success'] is True
+ assert seen['dst'] == path
+ assert seen['src'] != path and seen['src'].startswith(path)
+
+
+def test_uptime_history_reports_a_failed_save(tmp_path, monkeypatch):
+ """uptime_service was `except Exception: pass` over a truncating write."""
+ from app.services.uptime_service import UptimeService
+
+ monkeypatch.setattr(UptimeService, 'DATA_DIR', str(tmp_path))
+ monkeypatch.setattr(UptimeService, 'UPTIME_FILE', str(tmp_path / 'uptime.json'))
+ assert UptimeService._save_history({'checks': []}) is True
+
+ monkeypatch.setattr(json, 'dump', lambda *a, **k: (_ for _ in ()).throw(OSError('disk full')))
+ assert UptimeService._save_history({'checks': [1]}) is False
diff --git a/backend/tests/test_db_processes.py b/backend/tests/test_db_processes.py
index 29377a46..51c1a08a 100644
--- a/backend/tests/test_db_processes.py
+++ b/backend/tests/test_db_processes.py
@@ -162,6 +162,9 @@ class TestKillProcess:
# ---------------------------------------------------------------------------
class TestExecSqlRouting:
+ # The engine x host dispatch itself now lives in db_exec (plan 75 §G5), so
+ # these patch the executors where db_exec reaches them. _exec_sql stays the
+ # seam this service's own tests stub.
def test_host_mysql_routes_to_mysql_execute(self, monkeypatch):
seen = {}
@@ -169,8 +172,8 @@ class TestExecSqlRouting:
seen.update({'query': query, 'root_password': root_password})
return {'success': True, 'output': '', 'error': None}
- from app.services import db_process_service
- monkeypatch.setattr(db_process_service.DatabaseService, 'mysql_execute', staticmethod(fake))
+ from app.services import db_exec
+ monkeypatch.setattr(db_exec.DatabaseService, 'mysql_execute', staticmethod(fake))
result = DbProcessService._exec_sql({'engine': 'mysql', 'password': 's3c'}, 'SELECT 1;')
assert result['success'] is True
assert seen == {'query': 'SELECT 1;', 'root_password': 's3c'}
@@ -182,8 +185,8 @@ class TestExecSqlRouting:
seen['query'] = query
return {'success': True, 'output': '', 'error': None}
- from app.services import db_process_service
- monkeypatch.setattr(db_process_service.DatabaseService, 'pg_execute', staticmethod(fake))
+ from app.services import db_exec
+ monkeypatch.setattr(db_exec.DatabaseService, 'pg_execute', staticmethod(fake))
result = DbProcessService._exec_sql({'engine': 'postgresql'}, 'SELECT 2;')
assert result['success'] is True
assert seen['query'] == 'SELECT 2;'
@@ -191,27 +194,42 @@ class TestExecSqlRouting:
def test_docker_mysql_routes_to_docker_execute(self, monkeypatch):
seen = {}
- def fake(container_name, query, database=None, user='root', password=None):
- seen.update({'container': container_name, 'query': query, 'user': user, 'password': password})
+ def fake(container_name, query, **kwargs):
+ seen.update({'container': container_name, 'query': query,
+ 'user': kwargs.get('user'), 'password': kwargs.get('password')})
return {'success': True, 'output': '', 'error': None}
- from app.services import db_process_service
- monkeypatch.setattr(db_process_service.DatabaseService, 'docker_mysql_execute', staticmethod(fake))
+ from app.services import db_exec
+ monkeypatch.setattr(db_exec.DatabaseService, 'docker_mysql_execute', staticmethod(fake))
result = DbProcessService._exec_sql(
{'engine': 'mysql', 'container': 'wp-db', 'user': 'wp', 'password': 'pw'}, 'SHOW FULL PROCESSLIST;')
assert result['success'] is True
assert seen == {'container': 'wp-db', 'query': 'SHOW FULL PROCESSLIST;', 'user': 'wp', 'password': 'pw'}
- def test_docker_pg_clean_error_when_docker_missing(self, monkeypatch):
- def boom(*args, **kwargs):
- raise FileNotFoundError('docker not found')
-
- from app.services import db_process_service
- monkeypatch.setattr(db_process_service.subprocess, 'run', boom)
+ def test_docker_pg_clean_error_when_docker_missing(self, fake_subprocess):
+ fake_subprocess.script(['docker'], raises=FileNotFoundError('docker not found'))
result = DbProcessService._exec_sql({'engine': 'postgresql', 'container': 'pg1'}, 'SELECT 1;')
assert result['success'] is False
assert 'docker not found' in result['error']
+ def test_docker_pg_sets_on_error_stop(self, fake_subprocess):
+ """The drift that made three dispatchers worth collapsing into one.
+
+ This service's private docker-postgres branch built its own psql argv
+ and never passed ``-v ON_ERROR_STOP=1``, so psql exited 0 for a
+ statement the server had rejected and the caller was told it worked.
+ """
+ fake_subprocess.script(['docker'])
+ DbProcessService._exec_sql({'engine': 'postgresql', 'container': 'pg1'}, 'SELECT 1;')
+ argv = fake_subprocess.argv_for(['docker'])
+ assert 'ON_ERROR_STOP=1' in argv
+
+ def test_an_unknown_engine_is_reported_not_guessed(self, fake_subprocess):
+ result = DbProcessService._exec_sql({'engine': 'oracle'}, 'SELECT 1;')
+ assert result['success'] is False
+ assert 'oracle' in result['error']
+ assert fake_subprocess.commands() == [] # nothing was run on a guess
+
# ---------------------------------------------------------------------------
# API endpoints — auth + wiring (service stubbed)