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: finish the §G1 subprocess migration (census 195 -> 28)

Juan Denis28d26a73a26f
diff --git a/backend/app/api/docker.py b/backend/app/api/docker.py
index e3de5702..da245db2 100644
--- a/backend/app/api/docker.py
+++ b/backend/app/api/docker.py
@@ -476,11 +476,9 @@ def docker_cleanup():
# Get list of protected container IDs (ServerKit)
protected_ids = set()
try:
- list_result = subprocess.run(
- ['docker', 'ps', '-a', '--format', '{{.ID}} {{.Names}}'],
- capture_output=True, text=True
- )
- for line in list_result.stdout.strip().split('\n'):
+ list_result = DockerService.run(['ps', '-a', '--format', '{{.ID}} {{.Names}}'],
+ timeout=None)
+ for line in list_result['output'].strip().split('\n'):
if line:
parts = line.split(' ', 1)
if len(parts) == 2:
@@ -493,20 +491,16 @@ def docker_cleanup():
# Remove stopped containers (except protected ones)
try:
# Get stopped containers
- result = subprocess.run(
- ['docker', 'ps', '-a', '-q', '-f', 'status=exited'],
- capture_output=True, text=True
- )
- stopped_ids = [id for id in result.stdout.strip().split('\n') if id and id not in protected_ids]
+ result = DockerService.run(['ps', '-a', '-q', '-f', 'status=exited'],
+ timeout=None)
+ stopped_ids = [id for id in result['output'].strip().split('\n')
+ if id and id not in protected_ids]
if stopped_ids:
- remove_result = subprocess.run(
- ['docker', 'rm'] + stopped_ids,
- capture_output=True, text=True
- )
+ remove_result = DockerService.run(['rm'] + stopped_ids, timeout=None)
results['containers'] = {
'removed': len(stopped_ids),
- 'success': remove_result.returncode == 0
+ 'success': remove_result['success']
}
else:
results['containers'] = {'removed': 0, 'success': True}
@@ -515,26 +509,20 @@ def docker_cleanup():
# Remove dangling images
try:
- result = subprocess.run(
- ['docker', 'image', 'prune', '-f'],
- capture_output=True, text=True
- )
+ result = DockerService.run(['image', 'prune', '-f'], timeout=None)
results['images'] = {
- 'success': result.returncode == 0,
- 'output': result.stdout.strip()
+ 'success': result['success'],
+ 'output': result['output'].strip()
}
except Exception as e:
results['images'] = {'error': str(e)}
# Remove unused networks
try:
- result = subprocess.run(
- ['docker', 'network', 'prune', '-f'],
- capture_output=True, text=True
- )
+ result = DockerService.run(['network', 'prune', '-f'], timeout=None)
results['networks'] = {
- 'success': result.returncode == 0,
- 'output': result.stdout.strip()
+ 'success': result['success'],
+ 'output': result['output'].strip()
}
except Exception as e:
results['networks'] = {'error': str(e)}
@@ -542,13 +530,10 @@ def docker_cleanup():
# Remove unused volumes (optional)
if include_volumes:
try:
- result = subprocess.run(
- ['docker', 'volume', 'prune', '-f'],
- capture_output=True, text=True
- )
+ result = DockerService.run(['volume', 'prune', '-f'], timeout=None)
results['volumes'] = {
- 'success': result.returncode == 0,
- 'output': result.stdout.strip()
+ 'success': result['success'],
+ 'output': result['output'].strip()
}
except Exception as e:
results['volumes'] = {'error': str(e)}
diff --git a/backend/app/cron_runner.py b/backend/app/cron_runner.py
index fee12b51..b3da0283 100644
--- a/backend/app/cron_runner.py
+++ b/backend/app/cron_runner.py
@@ -13,6 +13,8 @@ failure to record must never change the job's outcome.
"""
import os
import subprocess
+
+from app.utils.system import run_checked
import sys
from datetime import datetime, timezone
@@ -49,8 +51,13 @@ def main(argv=None):
started = datetime.now(timezone.utc)
stdout, stderr, exit_code = '', '', 127
try:
- proc = subprocess.run(command, capture_output=True, text=True)
- stdout, stderr, exit_code = proc.stdout or '', proc.stderr or '', proc.returncode
+ proc = run_checked(command, timeout=None)
+ stdout, stderr = proc['output'], proc['stderr']
+ # 127 (the shell's "command not found") stays the sentinel for a job
+ # that never ran, so a launch failure is not recorded as exit 0.
+ exit_code = proc['returncode'] if proc['returncode'] is not None else 127
+ if proc['returncode'] is None:
+ stderr = stderr or f"serverkit-cron-run: {proc['error']}\n"
except Exception as exc: # noqa: BLE001 - record the failure to launch
stderr = f'serverkit-cron-run: failed to execute: {exc}\n'
finished = datetime.now(timezone.utc)
diff --git a/backend/app/plugins/serverkit-email/dkim_service.py b/backend/app/plugins/serverkit-email/dkim_service.py
index d705a7f8..e79ca0d8 100644
--- a/backend/app/plugins/serverkit-email/dkim_service.py
+++ b/backend/app/plugins/serverkit-email/dkim_service.py
@@ -4,7 +4,8 @@ import re
import subprocess
from typing import Dict
-from app.utils.system import PackageManager, ServiceControl, run_privileged
+from app.utils.system import (PackageManager, ServiceControl, run_checked,
+ run_privileged)
class DKIMService:
@@ -54,15 +55,15 @@ localhost
enabled = False
version = None
try:
- result = subprocess.run(['which', 'opendkim'], capture_output=True, text=True)
- installed = result.returncode == 0
+ result = run_checked(['which', 'opendkim'], timeout=None)
+ installed = result['success']
if not installed:
installed = PackageManager.is_installed('opendkim')
if installed:
running = ServiceControl.is_active('opendkim')
enabled = ServiceControl.is_enabled('opendkim')
- result = subprocess.run(['opendkim', '-V'], capture_output=True, text=True, stderr=subprocess.STDOUT)
- match = re.search(r'OpenDKIM\s+Filter\s+v(\S+)', result.stdout)
+ result = run_checked(['opendkim', '-V'], timeout=None, merge_stderr=True)
+ match = re.search(r'OpenDKIM\s+Filter\s+v(\S+)', result['output'])
if match:
version = match.group(1)
except (subprocess.SubprocessError, FileNotFoundError):
diff --git a/backend/app/plugins/serverkit-email/email_service.py b/backend/app/plugins/serverkit-email/email_service.py
index a05b40b7..fdadf70a 100644
--- a/backend/app/plugins/serverkit-email/email_service.py
+++ b/backend/app/plugins/serverkit-email/email_service.py
@@ -6,7 +6,7 @@ from typing import Dict, List, Optional
from app import db
from app.models.email import EmailDomain, EmailAccount, EmailAlias, EmailForwardingRule
from app.services.postfix_service import PostfixService
-from app.utils.system import PackageManager, ServiceControl
+from app.utils.system import PackageManager, ServiceControl, run_checked
from .dovecot_service import DovecotService
from .dkim_service import DKIMService
from .spamassassin_service import SpamAssassinService
@@ -229,21 +229,21 @@ class EmailService:
# Check MX record
try:
- result = subprocess.run(['dig', '+short', 'MX', name], capture_output=True, text=True, timeout=10)
+ result = run_checked(['dig', '+short', 'MX', name], timeout=10)
results['mx'] = {
- 'found': bool(result.stdout.strip()),
- 'value': result.stdout.strip() or None,
+ 'found': bool(result['output'].strip()),
+ 'value': result['output'].strip() or None,
}
except Exception:
results['mx'] = {'found': False, 'error': 'DNS lookup failed'}
# Check SPF record
try:
- result = subprocess.run(['dig', '+short', 'TXT', name], capture_output=True, text=True, timeout=10)
- spf_found = 'v=spf1' in (result.stdout or '')
+ result = run_checked(['dig', '+short', 'TXT', name], timeout=10)
+ spf_found = 'v=spf1' in result['output']
results['spf'] = {
'found': spf_found,
- 'value': result.stdout.strip() or None,
+ 'value': result['output'].strip() or None,
}
except Exception:
results['spf'] = {'found': False, 'error': 'DNS lookup failed'}
@@ -252,11 +252,11 @@ class EmailService:
selector = domain.dkim_selector or 'default'
try:
dkim_name = f'{selector}._domainkey.{name}'
- result = subprocess.run(['dig', '+short', 'TXT', dkim_name], capture_output=True, text=True, timeout=10)
- dkim_found = 'v=DKIM1' in (result.stdout or '')
+ result = run_checked(['dig', '+short', 'TXT', dkim_name], timeout=10)
+ dkim_found = 'v=DKIM1' in result['output']
results['dkim'] = {
'found': dkim_found,
- 'value': result.stdout.strip() or None,
+ 'value': result['output'].strip() or None,
'name': dkim_name,
}
except Exception:
@@ -265,11 +265,11 @@ class EmailService:
# Check DMARC record
try:
dmarc_name = f'_dmarc.{name}'
- result = subprocess.run(['dig', '+short', 'TXT', dmarc_name], capture_output=True, text=True, timeout=10)
- dmarc_found = 'v=DMARC1' in (result.stdout or '')
+ result = run_checked(['dig', '+short', 'TXT', dmarc_name], timeout=10)
+ dmarc_found = 'v=DMARC1' in result['output']
results['dmarc'] = {
'found': dmarc_found,
- 'value': result.stdout.strip() or None,
+ 'value': result['output'].strip() or None,
}
except Exception:
results['dmarc'] = {'found': False, 'error': 'DNS lookup failed'}
diff --git a/backend/app/plugins/serverkit-email/roundcube_service.py b/backend/app/plugins/serverkit-email/roundcube_service.py
index 85c93253..fb0f2d6f 100644
--- a/backend/app/plugins/serverkit-email/roundcube_service.py
+++ b/backend/app/plugins/serverkit-email/roundcube_service.py
@@ -2,7 +2,7 @@
import subprocess
from typing import Dict
-from app.utils.system import run_privileged, is_command_available
+from app.utils.system import run_checked, run_privileged, is_command_available
class RoundcubeService:
@@ -20,14 +20,13 @@ class RoundcubeService:
return {'installed': False, 'running': False, 'error': 'Docker not available'}
try:
- result = subprocess.run(
+ result = run_checked(
['docker', 'inspect', '--format', '{{.State.Status}}', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode != 0:
+ timeout=None)
+ if not result['success']:
return {'installed': False, 'running': False}
- status = result.stdout.strip()
+ status = result['output'].strip()
return {
'installed': True,
'running': status == 'running',
@@ -47,19 +46,13 @@ class RoundcubeService:
try:
# Remove existing container if any
- subprocess.run(
- ['docker', 'rm', '-f', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
+ run_checked(['docker', 'rm', '-f', cls.CONTAINER_NAME], timeout=None)
# Create volume
- subprocess.run(
- ['docker', 'volume', 'create', cls.VOLUME_NAME],
- capture_output=True, text=True,
- )
+ run_checked(['docker', 'volume', 'create', cls.VOLUME_NAME], timeout=None)
# Run container
- result = subprocess.run([
+ result = run_checked([
'docker', 'run', '-d',
'--name', cls.CONTAINER_NAME,
'--restart', 'unless-stopped',
@@ -73,10 +66,11 @@ class RoundcubeService:
'-e', 'ROUNDCUBEMAIL_SKIN=elastic',
'-v', f'{cls.VOLUME_NAME}:/var/roundcube/db',
cls.IMAGE,
- ], capture_output=True, text=True)
+ ], timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr or 'Failed to start container'}
+ if not result['success']:
+ return {'success': False,
+ 'error': result['error'] or 'Failed to start container'}
# Build a public URL: prefer a supplied domain, then auto-generate
# webmail.<panel_domain>, and only fall back to localhost:port.
@@ -120,7 +114,7 @@ class RoundcubeService:
def uninstall(cls) -> Dict:
"""Stop and remove Roundcube container."""
try:
- subprocess.run(['docker', 'rm', '-f', cls.CONTAINER_NAME], capture_output=True, text=True)
+ run_checked(['docker', 'rm', '-f', cls.CONTAINER_NAME], timeout=None)
return {'success': True, 'message': 'Roundcube uninstalled'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -129,13 +123,10 @@ class RoundcubeService:
def start(cls) -> Dict:
"""Start Roundcube container."""
try:
- result = subprocess.run(
- ['docker', 'start', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'start', cls.CONTAINER_NAME], timeout=None)
+ if result['success']:
return {'success': True, 'message': 'Roundcube started'}
- return {'success': False, 'error': result.stderr or 'Start failed'}
+ return {'success': False, 'error': result['error'] or 'Start failed'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -143,13 +134,10 @@ class RoundcubeService:
def stop(cls) -> Dict:
"""Stop Roundcube container."""
try:
- result = subprocess.run(
- ['docker', 'stop', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'stop', cls.CONTAINER_NAME], timeout=None)
+ if result['success']:
return {'success': True, 'message': 'Roundcube stopped'}
- return {'success': False, 'error': result.stderr or 'Stop failed'}
+ return {'success': False, 'error': result['error'] or 'Stop failed'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -157,13 +145,10 @@ class RoundcubeService:
def restart(cls) -> Dict:
"""Restart Roundcube container."""
try:
- result = subprocess.run(
- ['docker', 'restart', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'restart', cls.CONTAINER_NAME], timeout=None)
+ if result['success']:
return {'success': True, 'message': 'Roundcube restarted'}
- return {'success': False, 'error': result.stderr or 'Restart failed'}
+ return {'success': False, 'error': result['error'] or 'Restart failed'}
except Exception as e:
return {'success': False, 'error': str(e)}
diff --git a/backend/app/plugins/serverkit-email/spamassassin_service.py b/backend/app/plugins/serverkit-email/spamassassin_service.py
index 1418069d..85516e42 100644
--- a/backend/app/plugins/serverkit-email/spamassassin_service.py
+++ b/backend/app/plugins/serverkit-email/spamassassin_service.py
@@ -4,7 +4,8 @@ import re
import subprocess
from typing import Dict
-from app.utils.system import PackageManager, ServiceControl, run_privileged
+from app.utils.system import (PackageManager, ServiceControl, run_checked,
+ run_privileged)
class SpamAssassinService:
@@ -58,8 +59,8 @@ internal_networks 127.0.0.0/8
if installed:
running = ServiceControl.is_active('spamassassin')
enabled = ServiceControl.is_enabled('spamassassin')
- result = subprocess.run(['spamassassin', '--version'], capture_output=True, text=True)
- match = re.search(r'version\s+(\S+)', result.stdout)
+ result = run_checked(['spamassassin', '--version'], timeout=None)
+ match = re.search(r'version\s+(\S+)', result['output'])
if match:
version = match.group(1)
diff --git a/backend/app/services/backup_policy_service.py b/backend/app/services/backup_policy_service.py
index 060097ad..e7c10f9d 100644
--- a/backend/app/services/backup_policy_service.py
+++ b/backend/app/services/backup_policy_service.py
@@ -16,6 +16,7 @@ from datetime import datetime, timedelta
from decimal import Decimal
from app import db
+from app.utils.system import run_checked
from app.utils.formatting import format_bytes
logger = logging.getLogger(__name__)
@@ -332,16 +333,18 @@ class BackupPolicyService:
env['SERVERKIT_TARGET_NAME'] = target['name']
env['SERVERKIT_TARGET_PATH'] = target['root_path'] or ''
try:
- result = subprocess.run(
+ # shell=True stays: this is an operator-authored hook, and running
+ # it through a shell is the documented contract (plan 75 anti-goal).
+ result = run_checked(
hook, shell=True, cwd=target['root_path'] or None, env=env,
- capture_output=True, text=True, timeout=600,
+ timeout=600,
)
except Exception as exc:
raise BackupPolicyError(f'{label} hook error: {exc}')
- if result.returncode != 0:
+ if not result['success']:
raise BackupPolicyError(
- f'{label} hook failed (exit {result.returncode}): '
- f'{(result.stderr or result.stdout or "").strip()[:300]}'
+ f"{label} hook failed (exit {result['returncode']}): "
+ f"{(result['error'] or result['output'])[:300]}"
)
@classmethod
diff --git a/backend/app/services/backup_service.py b/backend/app/services/backup_service.py
index 687888de..1ae4fc8a 100644
--- a/backend/app/services/backup_service.py
+++ b/backend/app/services/backup_service.py
@@ -17,7 +17,7 @@ from app import paths
from app.utils import backup_crypto
from app.utils.config_store import load_json_config, save_json_config
from app.utils.formatting import format_bytes
-from app.utils.system import is_command_available
+from app.utils.system import is_command_available, run_checked
from app.services.telemetry_service import TelemetryService, generate_correlation_id
# Unified job kind for asynchronous scheduled backups (see register_jobs).
@@ -200,10 +200,11 @@ class BackupService:
cmd.append(db_name)
with gzip.open(backup_file, 'wt') as f:
- result = subprocess.run(cmd, stdout=f, stderr=subprocess.PIPE, text=True)
+ result = run_checked(cmd, stdout=f, stderr=subprocess.PIPE,
+ timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
elif db_type == 'postgresql':
if not is_command_available('pg_dump'):
@@ -221,10 +222,11 @@ class BackupService:
cmd.append(db_name)
with gzip.open(backup_file, 'wt') as f:
- result = subprocess.run(cmd, stdout=f, stderr=subprocess.PIPE, text=True, env=env)
+ result = run_checked(cmd, stdout=f, stderr=subprocess.PIPE,
+ env=env, timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
return {'success': True, 'path': backup_file}
@@ -455,7 +457,7 @@ class BackupService:
cmd.append(db_name)
with gzip.open(backup_path, 'rt') as f:
- result = subprocess.run(cmd, stdin=f, capture_output=True, text=True)
+ result = run_checked(cmd, stdin=f, timeout=None)
elif db_type == 'postgresql':
if not is_command_available('psql'):
@@ -473,13 +475,13 @@ class BackupService:
cmd.extend(['-d', db_name])
with gzip.open(backup_path, 'rt') as f:
- result = subprocess.run(cmd, stdin=f, capture_output=True, text=True, env=env)
+ result = run_checked(cmd, stdin=f, env=env, timeout=None)
else:
return {'success': False, 'error': f'Unknown database type: {db_type}'}
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
return {'success': True, 'message': f'Database {db_name} restored'}
@@ -846,9 +848,9 @@ class BackupService:
cmd.append(f'--listed-incremental={snar_path}')
cmd += ['-cf', archive, '-C', os.path.dirname(source_path) or '/', os.path.basename(source_path)]
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=1800)
- if result.returncode != 0:
- raise RuntimeError(f'tar failed: {(result.stderr or "").strip()[:300]}')
+ result = run_checked(cmd, timeout=1800)
+ if not result['success']:
+ raise RuntimeError(f"tar failed: {result['error'][:300]}")
return {'archive': archive, 'size': os.path.getsize(archive),
'compression': program, 'kind': kind, 'incremental': kind == 'incremental', 'ext': ext}
@@ -866,9 +868,9 @@ class BackupService:
if not os.path.exists(archive):
raise RuntimeError(f'Backup archive missing: {archive}')
cmd = ['tar', '--listed-incremental=/dev/null', '-xf', archive, '-C', parent]
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=1800)
- if result.returncode != 0:
- raise RuntimeError(f'tar restore failed: {(result.stderr or "").strip()[:300]}')
+ result = run_checked(cmd, timeout=1800)
+ if not result['success']:
+ raise RuntimeError(f"tar restore failed: {result['error'][:300]}")
return {'success': True, 'restore_path': restore_path}
# --- Scheduler (unified job system) ---
diff --git a/backend/app/services/bootstrap_service.py b/backend/app/services/bootstrap_service.py
index 3e4397d0..b5a24027 100644
--- a/backend/app/services/bootstrap_service.py
+++ b/backend/app/services/bootstrap_service.py
@@ -36,21 +36,17 @@ class BootstrapService:
@staticmethod
def _docker_run(app, service: str, command: str,
timeout_seconds: Optional[int]) -> Dict[str, Any]:
- import subprocess
+ from app.utils.system import run_checked
root = getattr(app, 'root_path', None)
if not root:
return {'success': False,
'error': 'app has no deployed compose project to run bootstrap in'}
cmd = ['docker', 'compose', 'run', '--rm', service] + shlex.split(command)
- try:
- proc = subprocess.run(cmd, cwd=root, capture_output=True, text=True,
- timeout=timeout_seconds or 300)
- except subprocess.TimeoutExpired:
+ proc = run_checked(cmd, cwd=root, timeout=timeout_seconds or 300)
+ if proc['returncode'] is None:
+ # timed out, or docker is absent
+ return {'success': False, 'error': proc['error']}
+ if not proc['success']:
return {'success': False,
- 'error': f'bootstrap timed out after {timeout_seconds or 300}s'}
- except Exception as exc: # docker missing, etc.
- return {'success': False, 'error': str(exc)}
- if proc.returncode != 0:
- return {'success': False,
- 'error': (proc.stderr or proc.stdout or 'bootstrap failed').strip()[:500]}
- return {'success': True, 'output': (proc.stdout or '').strip()[:500]}
+ 'error': (proc['error'] or proc['output'] or 'bootstrap failed')[:500]}
+ return {'success': True, 'output': proc['output'].strip()[:500]}
diff --git a/backend/app/services/build_service.py b/backend/app/services/build_service.py
index 9e6b903f..de869f0e 100644
--- a/backend/app/services/build_service.py
+++ b/backend/app/services/build_service.py
@@ -12,6 +12,8 @@ Supports:
import os
import subprocess
+
+from app.utils.system import run_checked
import json
import shutil
import hashlib
@@ -439,9 +441,7 @@ class BuildService:
image_tag = f"serverkit-app-{app_id}:latest"
# Check if nixpacks is installed
- try:
- subprocess.run(['nixpacks', '--version'], capture_output=True, check=True)
- except (subprocess.CalledProcessError, FileNotFoundError):
+ if not run_checked(['nixpacks', '--version'], timeout=None)['success']:
return {
'success': False,
'error': 'Nixpacks not installed. Install with: curl -sSL https://nixpacks.com/install.sh | bash'
@@ -774,14 +774,11 @@ class BuildService:
def get_nixpacks_plan(cls, app_path: str) -> Dict:
"""Get Nixpacks build plan for an application."""
try:
- result = subprocess.run(
- ['nixpacks', 'plan', app_path, '--format', 'json'],
- capture_output=True,
- text=True
- )
- if result.returncode == 0:
- return {'success': True, 'plan': json.loads(result.stdout)}
- return {'success': False, 'error': result.stderr}
+ result = run_checked(['nixpacks', 'plan', app_path, '--format', 'json'],
+ timeout=None)
+ if result['success']:
+ return {'success': True, 'plan': json.loads(result['output'])}
+ return {'success': False, 'error': result['error']}
except FileNotFoundError:
return {'success': False, 'error': 'Nixpacks not installed'}
except Exception as e:
diff --git a/backend/app/services/container_registry_service.py b/backend/app/services/container_registry_service.py
index c016ba91..fa13be5f 100644
--- a/backend/app/services/container_registry_service.py
+++ b/backend/app/services/container_registry_service.py
@@ -17,6 +17,7 @@ import subprocess
from datetime import datetime
from app import db
+from app.utils.system import run_checked
from app.models.container_registry import ContainerRegistry
from app.utils.crypto import encrypt_secret, decrypt_secret_safe
@@ -114,19 +115,16 @@ class ContainerRegistryService:
host = registry.login_host()
# Secret goes on stdin via --password-stdin, NEVER on argv.
cmd = ['docker', 'login', host, '-u', username, '--password-stdin']
- try:
- result = subprocess.run(cmd, input=password, capture_output=True, text=True)
- except Exception as e:
- return {'success': False, 'error': str(e)}
+ result = run_checked(cmd, input=password, timeout=None)
- if result.returncode == 0:
+ if result['success']:
try:
registry.last_used_at = datetime.utcnow()
db.session.commit()
except Exception:
db.session.rollback()
return {'success': True}
- return {'success': False, 'error': (result.stderr or 'docker login failed').strip()}
+ return {'success': False, 'error': result['error'] or 'docker login failed'}
@staticmethod
def logout(host):
@@ -134,7 +132,7 @@ class ContainerRegistryService:
if not host:
return
try:
- subprocess.run(['docker', 'logout', host], capture_output=True, text=True)
+ run_checked(['docker', 'logout', host], timeout=None)
except Exception:
pass
@@ -175,12 +173,10 @@ class ContainerRegistryService:
cmd = ['aws', 'ecr', 'get-login-password']
if region:
cmd += ['--region', region]
- result = subprocess.run(cmd, capture_output=True, text=True, env=env)
- if result.returncode == 0:
- return result.stdout.strip()
- logger.debug('aws ecr get-login-password failed: %s', (result.stderr or '').strip())
- except FileNotFoundError:
- logger.debug('aws CLI not found; cannot exchange ECR token')
+ result = run_checked(cmd, env=env, timeout=None)
+ if result['success']:
+ return result['output'].strip()
+ logger.debug('aws ecr get-login-password failed: %s', result['error'])
except Exception as e: # pragma: no cover - defensive
logger.debug('ECR password exchange error: %s', e)
return None
diff --git a/backend/app/services/container_scale_service.py b/backend/app/services/container_scale_service.py
index 61f41964..68b79df5 100644
--- a/backend/app/services/container_scale_service.py
+++ b/backend/app/services/container_scale_service.py
@@ -7,6 +7,7 @@ import subprocess
from datetime import datetime, timedelta
from app import db
+from app.utils.system import run_checked
from app.models import Application
from app.models.container_scale_policy import ContainerScalePolicy
@@ -51,14 +52,15 @@ class ContainerScaleService:
ps_cmd = ['docker', 'compose', '-f', _compose_path(app), 'ps', '-q']
if policy.service_name:
ps_cmd.append(policy.service_name)
- ps = subprocess.run(ps_cmd, cwd=app.root_path, capture_output=True, text=True, timeout=15)
- ids = [line.strip() for line in ps.stdout.splitlines() if line.strip()]
+ ps = run_checked(ps_cmd, cwd=app.root_path, timeout=15)
+ ids = [line.strip() for line in ps['output'].splitlines() if line.strip()]
if not ids:
return None
- stats = subprocess.run(['docker', 'stats', '--no-stream', '--format', '{{.CPUPerc}}'] + ids,
- capture_output=True, text=True, timeout=15)
+ stats = run_checked(
+ ['docker', 'stats', '--no-stream', '--format', '{{.CPUPerc}}'] + ids,
+ timeout=15)
values = []
- for line in stats.stdout.splitlines():
+ for line in stats['output'].splitlines():
try:
values.append(float(line.strip().rstrip('%')))
except ValueError:
@@ -74,12 +76,12 @@ class ContainerScaleService:
if not (app.app_type == 'docker' and app.root_path and policy.service_name):
return {'success': False, 'error': 'Scaling requires a docker-compose app with a service_name set'}
try:
- result = subprocess.run(
+ result = run_checked(
['docker', 'compose', '-f', _compose_path(app), 'up', '-d', '--no-recreate',
'--scale', f'{policy.service_name}={replicas}'],
- cwd=app.root_path, capture_output=True, text=True, timeout=120)
- if result.returncode != 0:
- return {'success': False, 'error': (result.stderr or 'scale failed')[:300]}
+ cwd=app.root_path, timeout=120)
+ if not result['success']:
+ return {'success': False, 'error': result['error'][:300]}
return {'success': True}
except Exception as e:
return {'success': False, 'error': str(e)}
diff --git a/backend/app/services/cron_service.py b/backend/app/services/cron_service.py
index 8e75a938..5d162726 100644
--- a/backend/app/services/cron_service.py
+++ b/backend/app/services/cron_service.py
@@ -120,14 +120,9 @@ class CronService:
else:
# Try alternative check
try:
- result = subprocess.run(
- ['pgrep', '-x', 'cron'],
- capture_output=True,
- text=True,
- timeout=5
- )
- cron_active = result.returncode == 0
- except (subprocess.SubprocessError, FileNotFoundError):
+ cron_active = run_checked(['pgrep', '-x', 'cron'],
+ timeout=5)['success']
+ except Exception: # noqa: BLE001 - fallback probe
cron_active = False
return {
@@ -155,15 +150,10 @@ class CronService:
if cls.is_linux():
try:
# Get current user's crontab
- result = subprocess.run(
- ['crontab', '-l'],
- capture_output=True,
- text=True,
- timeout=10
- )
-
- if result.returncode == 0:
- lines = result.stdout.strip().split('\n')
+ current = cls._read_crontab()
+
+ if current is not None:
+ lines = current.strip().split('\n')
for i, line in enumerate(lines):
line = line.strip()
# Skip empty lines and comments
@@ -594,12 +584,7 @@ class CronService:
started = datetime.now()
try:
# Run the command
- result = subprocess.run(
- ['bash', '-c', command],
- capture_output=True,
- text=True,
- timeout=60
- )
+ result = run_checked(['bash', '-c', command], timeout=60)
# Record the run in history too (#18) — a manual "Run now" is a real
# execution, so it shows up alongside cron-triggered runs. Best-effort:
@@ -608,21 +593,25 @@ class CronService:
job_id,
started=started,
finished=datetime.now(),
- exit_code=result.returncode,
- output_tail=(result.stdout or '') + (result.stderr or ''),
+ exit_code=result['returncode'],
+ output_tail=result['output'] + result['stderr'],
)
+ if result['returncode'] is None:
+ # No exit code means the job never ran (no bash, or it outlived
+ # the 60s bound) — reporting exit_code None as a run would put a
+ # fabricated success in the history.
+ return {'success': False, 'error': result['error']}
+
return {
'success': True,
- 'exit_code': result.returncode,
- 'stdout': result.stdout,
- 'stderr': result.stderr,
+ 'exit_code': result['returncode'],
+ 'stdout': result['output'],
+ 'stderr': result['stderr'],
'message': 'Job executed'
}
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Job execution timed out (60s limit)'}
- except subprocess.SubprocessError as e:
+ except Exception as e: # noqa: BLE001
return {'success': False, 'error': str(e)}
@classmethod
diff --git a/backend/app/services/database_service.py b/backend/app/services/database_service.py
index eea63ccd..4e5e4091 100644
--- a/backend/app/services/database_service.py
+++ b/backend/app/services/database_service.py
@@ -7,7 +7,7 @@ import json
from datetime import datetime
from app import paths
-from app.utils.system import unit_is_active
+from app.utils.system import run_checked, unit_is_active
def _validate_identifier(name: str, max_length: int = 64) -> bool:
@@ -31,14 +31,10 @@ class DatabaseService:
@staticmethod
def mysql_is_installed():
"""Check if MySQL/MariaDB is installed."""
- try:
- result = subprocess.run(
- ['mysql', '--version'],
- capture_output=True, text=True
- )
- return result.returncode == 0
- except FileNotFoundError:
- return False
+ # run_checked also resolves argv[0] when $PATH misses it, so a client
+ # installed outside the unit's PATH stops reading as "not installed"
+ # (plan 74's outage class).
+ return run_checked(['mysql', '--version'], timeout=None)['success']
@staticmethod
def mysql_is_running():
@@ -75,14 +71,7 @@ class DatabaseService:
env = DatabaseService._mysql_env(root_password)
- result = subprocess.run(
- cmd, capture_output=True, text=True, env=env
- )
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
- }
+ return run_checked(cmd, env=env, timeout=None)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -130,14 +119,7 @@ class DatabaseService:
cmd.extend(['-e', safe_query])
- result = subprocess.run(
- cmd, capture_output=True, text=True, env=env
- )
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
- }
+ return run_checked(cmd, env=env, timeout=None)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -246,14 +228,7 @@ class DatabaseService:
f"DEALLOCATE PREPARE stmt;\n"
)
- result = subprocess.run(
- cmd, capture_output=True, text=True, input=safe_stmt, env=env
- )
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
- }
+ return run_checked(cmd, input=safe_stmt, env=env, timeout=None)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -372,9 +347,9 @@ class DatabaseService:
return {'success': False, 'error': stderr.decode()}
else:
with open(backup_path, 'r') as f:
- result = subprocess.run(cmd, stdin=f, capture_output=True, text=True, env=env)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ result = run_checked(cmd, stdin=f, env=env, timeout=None)
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
return {'success': True, 'message': 'Database restored successfully'}
except Exception as e:
@@ -419,14 +394,7 @@ class DatabaseService:
@staticmethod
def pg_is_installed():
"""Check if PostgreSQL is installed."""
- try:
- result = subprocess.run(
- ['psql', '--version'],
- capture_output=True, text=True
- )
- return result.returncode == 0
- except FileNotFoundError:
- return False
+ return run_checked(['psql', '--version'], timeout=None)['success']
@staticmethod
def pg_is_running():
@@ -437,14 +405,11 @@ class DatabaseService:
def pg_execute(query, database='postgres', user='postgres'):
"""Execute a PostgreSQL query."""
try:
+ # `sudo -u postgres` is identity switching, not escalation — peer
+ # auth maps the OS user to the DB role, so this cannot go through
+ # run_privileged (which drops the -u when already root).
cmd = ['sudo', '-u', 'postgres', 'psql', '-d', database, '-c', query, '-t', '-A']
-
- result = subprocess.run(cmd, capture_output=True, text=True)
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
- }
+ return run_checked(cmd, timeout=None)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -503,7 +468,7 @@ class DatabaseService:
'-c', "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = :'dbname';",
'-t', '-A'
]
- subprocess.run(cmd, capture_output=True, text=True)
+ run_checked(cmd, timeout=None)
except Exception:
pass
# Name is validated above, safe to use in identifier position
@@ -608,9 +573,9 @@ class DatabaseService:
return {'success': False, 'error': stderr.decode()}
else:
with open(backup_path, 'r') as f:
- result = subprocess.run(cmd, stdin=f, capture_output=True, text=True)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ result = run_checked(cmd, stdin=f, timeout=None)
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
return {'success': True, 'message': 'Database restored successfully'}
except Exception as e:
@@ -702,24 +667,15 @@ class DatabaseService:
# Remove empty strings from cmd
cmd = [c for c in cmd if c]
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=timeout,
- env=env
- )
+ result = run_checked(cmd, timeout=timeout, env=env)
execution_time = time.time() - start_time
- if result.returncode != 0:
- return {
- 'success': False,
- 'error': result.stderr.strip() if result.stderr else 'Query execution failed'
- }
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Parse the output
- lines = result.stdout.strip().split('\n') if result.stdout.strip() else []
+ lines = result['output'].strip().split('\n') if result['output'].strip() else []
if not lines:
return {
@@ -805,24 +761,15 @@ class DatabaseService:
'--pset', 'footer=off' # No row count footer
]
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=timeout
- )
+ result = run_checked(cmd, timeout=timeout)
execution_time = time.time() - start_time
- if result.returncode != 0:
- error_msg = result.stderr.strip() if result.stderr else 'Query execution failed'
- return {
- 'success': False,
- 'error': error_msg
- }
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Parse the output
- lines = result.stdout.strip().split('\n') if result.stdout.strip() else []
+ lines = result['output'].strip().split('\n') if result['output'].strip() else []
if not lines:
return {
@@ -1140,17 +1087,15 @@ class DatabaseService:
"""Find all Docker containers running MySQL/MariaDB."""
try:
# Get all running containers
- result = subprocess.run(
- ['docker', 'ps', '--format', '{{json .}}'],
- capture_output=True, text=True
- )
- if result.returncode != 0:
+ result = run_checked(['docker', 'ps', '--format', '{{json .}}'],
+ timeout=None)
+ if not result['success']:
return []
containers = []
mysql_images = ['mysql', 'mariadb', 'percona']
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
if not line:
continue
container = json.loads(line)
@@ -1185,16 +1130,11 @@ class DatabaseService:
if cached:
return cached
for client in ('mariadb', 'mysql'):
- try:
- probe = subprocess.run(
- ['docker', 'exec', container_name, client, '--version'],
- capture_output=True, text=True, timeout=10,
- )
- if probe.returncode == 0:
- DatabaseService._docker_client_cache[container_name] = client
- return client
- except Exception:
- continue
+ probe = run_checked(['docker', 'exec', container_name, client, '--version'],
+ timeout=10)
+ if probe['success']:
+ DatabaseService._docker_client_cache[container_name] = client
+ return client
# Nothing answered — keep the historic default so the caller's error
# message stays the familiar one.
return 'mysql'
@@ -1228,14 +1168,7 @@ class DatabaseService:
cmd.extend(['-e', query])
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
- }
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Query timed out'}
+ return run_checked(cmd, timeout=timeout)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -1266,14 +1199,7 @@ class DatabaseService:
# 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,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None,
- }
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Query timed out'}
+ return run_checked(cmd, timeout=timeout)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -1376,17 +1302,14 @@ class DatabaseService:
'--batch'
])
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
+ result = run_checked(cmd, timeout=timeout)
execution_time = time.time() - start_time
- if result.returncode != 0:
- return {
- 'success': False,
- 'error': result.stderr.strip() if result.stderr else 'Query execution failed'
- }
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Parse the output
- lines = result.stdout.strip().split('\n') if result.stdout.strip() else []
+ lines = result['output'].strip().split('\n') if result['output'].strip() else []
if not lines:
return {
diff --git a/backend/app/services/db_sync_service.py b/backend/app/services/db_sync_service.py
index 35b3e967..4260ca67 100644
--- a/backend/app/services/db_sync_service.py
+++ b/backend/app/services/db_sync_service.py
@@ -7,6 +7,8 @@ Supports WordPress-specific features like URL search-replace and data anonymizat
import os
import subprocess
+
+from app.utils.system import run_checked
import gzip
import shutil
import json
@@ -43,20 +45,7 @@ class DatabaseSyncService:
cmd.extend(['-D', database])
cmd.extend(['-e', command])
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=300
- )
-
- return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
- }
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Command timed out'}
+ return run_checked(cmd, timeout=300)
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -136,9 +125,11 @@ class DatabaseSyncService:
return {'success': False, 'error': f'mysqldump failed: {error}'}
else:
with open(file_path, 'w') as f:
- result = subprocess.run(cmd, stdout=f, stderr=subprocess.PIPE, text=True)
- if result.returncode != 0:
- return {'success': False, 'error': f'mysqldump failed: {result.stderr}'}
+ result = run_checked(cmd, stdout=f, stderr=subprocess.PIPE,
+ timeout=None)
+ if not result['success']:
+ return {'success': False,
+ 'error': f"mysqldump failed: {result['error']}"}
# Get file size
size_bytes = os.path.getsize(file_path)
@@ -222,23 +213,14 @@ class DatabaseSyncService:
# Handle compressed files
if file_path.endswith('.gz'):
with gzip.open(file_path, 'rb') as f:
- result = subprocess.run(
- cmd,
- stdin=f,
- capture_output=True,
- timeout=1800 # 30 min timeout for large DBs
- )
+ # 30 min bound for large DBs
+ result = run_checked(cmd, stdin=f, timeout=1800)
else:
with open(file_path, 'r') as f:
- result = subprocess.run(
- cmd,
- stdin=f,
- capture_output=True,
- timeout=1800
- )
+ result = run_checked(cmd, stdin=f, timeout=1800)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr.decode() if result.stderr else 'Import failed'}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
return {'success': True, 'message': f'Snapshot restored to {target_db}'}
@@ -732,13 +714,13 @@ class DatabaseSyncService:
return {'success': False, 'error': f'Container mysqldump failed: {error}'}
else:
with open(output_path, 'w') as f:
- result = subprocess.run(
- full_cmd, stdout=f, stderr=subprocess.PIPE, text=True, timeout=600
- )
- if result.returncode != 0:
+ result = run_checked(full_cmd, stdout=f, stderr=subprocess.PIPE,
+ timeout=600)
+ if not result['success']:
if os.path.exists(output_path):
os.remove(output_path)
- return {'success': False, 'error': f'Container mysqldump failed: {result.stderr}'}
+ return {'success': False,
+ 'error': f"Container mysqldump failed: {result['error']}"}
size_bytes = os.path.getsize(output_path)
return {
@@ -791,24 +773,14 @@ class DatabaseSyncService:
if snapshot_path.endswith('.gz'):
with gzip.open(snapshot_path, 'rb') as f:
- result = subprocess.run(
- import_cmd,
- stdin=f,
- capture_output=True,
- timeout=1800
- )
+ result = run_checked(import_cmd, stdin=f, timeout=1800)
else:
with open(snapshot_path, 'r') as f:
- result = subprocess.run(
- import_cmd,
- stdin=f,
- capture_output=True,
- timeout=1800
- )
+ result = run_checked(import_cmd, stdin=f, timeout=1800)
- if result.returncode != 0:
- error = result.stderr.decode() if isinstance(result.stderr, bytes) else result.stderr
- return {'success': False, 'error': f'Container import failed: {error}'}
+ if not result['success']:
+ return {'success': False,
+ 'error': f"Container import failed: {result['error']}"}
return {'success': True, 'message': f'Imported {snapshot_path} into container DB {db_name}'}
diff --git a/backend/app/services/deployment_service.py b/backend/app/services/deployment_service.py
index 73b13f1d..83f75074 100644
--- a/backend/app/services/deployment_service.py
+++ b/backend/app/services/deployment_service.py
@@ -18,7 +18,7 @@ from datetime import datetime
from typing import Dict, List, Optional, Callable
from app import db
-from app.utils.system import ServiceControl
+from app.utils.system import ServiceControl, run_checked
from app.models.deployment import Deployment, DeploymentDiff
from app.models.application import Application
from app.services.build_service import BuildService
@@ -502,14 +502,11 @@ class DeploymentService:
log_callback(f"Checking out commit {target.commit_hash[:8]}...")
# Checkout the specific commit
- result = subprocess.run(
- ['git', '-C', app_path, 'checkout', target.commit_hash],
- capture_output=True,
- text=True
- )
+ result = run_checked(['git', '-C', app_path, 'checkout', target.commit_hash],
+ timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Run post-deploy script if configured
deploy_config = GitService.get_app_config(app.id)
@@ -551,21 +548,18 @@ class DeploymentService:
app_path = app.root_path
# Get git diff
- result = subprocess.run(
+ result = run_checked(
['git', '-C', app_path, 'diff', '--name-status',
- previous.commit_hash, deployment.commit_hash],
- capture_output=True,
- text=True
- )
+ previous.commit_hash, deployment.commit_hash], timeout=None)
- if result.returncode != 0:
+ if not result['success']:
return
files_added = []
files_removed = []
files_modified = []
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
if not line:
continue
parts = line.split('\t')
@@ -579,19 +573,16 @@ class DeploymentService:
files_modified.append(filepath)
# Get diff stats
- stat_result = subprocess.run(
+ stat_result = run_checked(
['git', '-C', app_path, 'diff', '--shortstat',
- previous.commit_hash, deployment.commit_hash],
- capture_output=True,
- text=True
- )
+ previous.commit_hash, deployment.commit_hash], timeout=None)
additions = 0
deletions = 0
- if stat_result.returncode == 0 and stat_result.stdout:
+ if stat_result['success'] and stat_result['output']:
import re
- add_match = re.search(r'(\d+) insertion', stat_result.stdout)
- del_match = re.search(r'(\d+) deletion', stat_result.stdout)
+ add_match = re.search(r'(\d+) insertion', stat_result['output'])
+ del_match = re.search(r'(\d+) deletion', stat_result['output'])
if add_match:
additions = int(add_match.group(1))
if del_match:
diff --git a/backend/app/services/environment_docker_service.py b/backend/app/services/environment_docker_service.py
index e3316b19..d9936f6a 100644
--- a/backend/app/services/environment_docker_service.py
+++ b/backend/app/services/environment_docker_service.py
@@ -10,6 +10,8 @@ import os
import json
import shutil
import subprocess
+
+from app.utils.system import run_checked
from typing import Dict
from app.services.template_service import TemplateService
from app import paths
@@ -164,17 +166,13 @@ networks:
Dict with success status
"""
try:
- result = subprocess.run(
- ['docker', 'compose', '-f', compose_path, 'up', '-d'],
- capture_output=True,
- text=True,
- timeout=120
- )
+ result = run_checked(
+ ['docker', 'compose', '-f', compose_path, 'up', '-d'], timeout=120)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
- return {'success': True, 'output': result.stdout}
+ return {'success': True, 'output': result['output']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Docker compose up timed out'}
@@ -192,17 +190,13 @@ networks:
Dict with success status
"""
try:
- result = subprocess.run(
- ['docker', 'compose', '-f', compose_path, 'down'],
- capture_output=True,
- text=True,
- timeout=60
- )
+ result = run_checked(
+ ['docker', 'compose', '-f', compose_path, 'down'], timeout=60)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
- return {'success': True, 'output': result.stdout}
+ return {'success': True, 'output': result['output']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Docker compose down timed out'}
@@ -241,22 +235,17 @@ networks:
if remove_volumes:
cmd.append('-v')
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=60
- )
+ result = run_checked(cmd, timeout=60)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Remove the environment directory
env_dir = os.path.dirname(compose_path)
if os.path.exists(env_dir):
shutil.rmtree(env_dir)
- return {'success': True, 'output': result.stdout}
+ return {'success': True, 'output': result['output']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Docker compose down timed out'}
@@ -274,20 +263,17 @@ networks:
Dict with running status and container details
"""
try:
- result = subprocess.run(
+ result = run_checked(
['docker', 'compose', '-f', compose_path, 'ps', '--format', 'json'],
- capture_output=True,
- text=True,
- timeout=30
- )
+ timeout=30)
- if result.returncode != 0:
- return {'success': False, 'running': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'running': False, 'error': result['error']}
containers = []
- if result.stdout.strip():
+ if result['output'].strip():
# docker compose ps --format json outputs one JSON object per line
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
line = line.strip()
if line:
try:
@@ -324,17 +310,14 @@ networks:
Dict with success status and logs
"""
try:
- result = subprocess.run(
- ['docker', 'compose', '-f', compose_path, 'logs', '--tail', str(lines), service],
- capture_output=True,
- text=True,
- timeout=30
- )
+ result = run_checked(
+ ['docker', 'compose', '-f', compose_path, 'logs', '--tail', str(lines),
+ service], timeout=30)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
- return {'success': True, 'logs': result.stdout}
+ return {'success': True, 'logs': result['output']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Log retrieval timed out'}
@@ -354,17 +337,15 @@ networks:
Dict with success status and output
"""
try:
- result = subprocess.run(
- ['docker', 'compose', '-f', compose_path, 'exec', '-T', service] + command.split(),
- capture_output=True,
- text=True,
- timeout=120
- )
+ result = run_checked(
+ ['docker', 'compose', '-f', compose_path, 'exec', '-T', service]
+ + command.split(), timeout=120)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr, 'output': result.stdout}
+ if not result['success']:
+ return {'success': False, 'error': result['error'],
+ 'output': result['output']}
- return {'success': True, 'output': result.stdout}
+ return {'success': True, 'output': result['output']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Command execution timed out'}
diff --git a/backend/app/services/environment_domain_service.py b/backend/app/services/environment_domain_service.py
index a2ee0b2f..6ee4496d 100644
--- a/backend/app/services/environment_domain_service.py
+++ b/backend/app/services/environment_domain_service.py
@@ -13,7 +13,7 @@ from typing import Dict
from app.services.nginx_service import NginxService
from app.utils.slug import slugify as _slugify
-from app.utils.system import run_privileged, write_privileged_file
+from app.utils.system import run_checked, run_privileged, write_privileged_file
class EnvironmentDomainService:
@@ -194,14 +194,13 @@ class EnvironmentDomainService:
run_privileged(['mkdir', '-p', cls.HTPASSWD_DIR])
# Generate password hash using openssl
- hash_result = subprocess.run(
- ['openssl', 'passwd', '-apr1', password],
- capture_output=True, text=True, timeout=10
- )
- if hash_result.returncode != 0:
- return {'success': False, 'error': f'Failed to hash password: {hash_result.stderr}'}
+ hash_result = run_checked(['openssl', 'passwd', '-apr1', password],
+ timeout=10)
+ if not hash_result['success']:
+ return {'success': False,
+ 'error': f"Failed to hash password: {hash_result['error']}"}
- password_hash = hash_result.stdout.strip()
+ password_hash = hash_result['output'].strip()
htpasswd_content = f'{username}:{password_hash}\n'
# Write htpasswd file
diff --git a/backend/app/services/environment_health_service.py b/backend/app/services/environment_health_service.py
index 85c9d85a..9c98f36a 100644
--- a/backend/app/services/environment_health_service.py
+++ b/backend/app/services/environment_health_service.py
@@ -12,6 +12,7 @@ from typing import Dict, Optional
from app import db
from app.models.wordpress_site import WordPressSite
+from app.utils.system import run_checked
from app.services.environment_docker_service import EnvironmentDockerService
from app.services.environment_pipeline_service import EnvironmentPipelineService
from app.utils.formatting import format_bytes
@@ -330,15 +331,12 @@ class EnvironmentHealthService:
def _check_wordpress_http(cls, port: int) -> Dict:
"""Check WordPress HTTP response on localhost."""
try:
- result = subprocess.run(
+ result = run_checked(
['curl', '-s', '-o', '/dev/null', '-w', '%{http_code}',
'--max-time', '5', f'http://127.0.0.1:{port}/wp-login.php'],
- capture_output=True,
- text=True,
- timeout=10
- )
+ timeout=10)
- status_code = result.stdout.strip()
+ status_code = result['output'].strip()
if status_code in ('200', '302', '301'):
return {'status': 'healthy', 'message': f'HTTP {status_code}'}
elif status_code == '401':
@@ -357,14 +355,9 @@ class EnvironmentHealthService:
def _get_dir_size(cls, path: str) -> int:
"""Get total size of a directory in bytes."""
try:
- result = subprocess.run(
- ['du', '-sb', path],
- capture_output=True,
- text=True,
- timeout=30
- )
- if result.returncode == 0 and result.stdout.strip():
- return int(result.stdout.split()[0])
+ result = run_checked(['du', '-sb', path], timeout=30)
+ if result['success'] and result['output'].strip():
+ return int(result['output'].split()[0])
except Exception:
pass
diff --git a/backend/app/services/environment_pipeline_service.py b/backend/app/services/environment_pipeline_service.py
index 9115c4f6..8a7ea46a 100644
--- a/backend/app/services/environment_pipeline_service.py
+++ b/backend/app/services/environment_pipeline_service.py
@@ -24,6 +24,7 @@ from datetime import datetime, timedelta
from typing import Dict, Optional
from app import db
+from app.utils.system import run_checked, run_privileged
from app.models.application import Application
from app.models.wordpress_site import WordPressSite, DatabaseSnapshot
from app.models.environment_activity import EnvironmentActivity
@@ -1714,10 +1715,10 @@ class EnvironmentPipelineService:
shutil.copytree(source_dir, target_dir)
# Set permissions
- subprocess.run(
- ['sudo', 'chown', '-R', 'www-data:www-data', target_dir],
- capture_output=True, timeout=60
- )
+ # run_privileged, not a hardcoded sudo: this is escalation, and
+ # _needs_sudo() is the one place that decides it (plan 75 §G2).
+ run_privileged(['chown', '-R', 'www-data:www-data', target_dir],
+ timeout=60)
return {'success': True}
except Exception as e:
@@ -1727,7 +1728,7 @@ class EnvironmentPipelineService:
def _sync_wordpress_files(cls, source_dir: str, target_dir: str) -> Dict:
"""Sync WordPress files using rsync (more efficient than full copy)."""
try:
- result = subprocess.run(
+ result = run_checked(
[
'rsync', '-a', '--delete',
'--exclude', '.git',
@@ -1736,13 +1737,10 @@ class EnvironmentPipelineService:
f'{source_dir}/',
f'{target_dir}/'
],
- capture_output=True,
- text=True,
- timeout=300
- )
+ timeout=300)
- if result.returncode != 0:
- return {'success': False, 'error': f'rsync failed: {result.stderr}'}
+ if not result['success']:
+ return {'success': False, 'error': f"rsync failed: {result['error']}"}
return {'success': True}
except subprocess.TimeoutExpired:
@@ -1756,20 +1754,17 @@ class EnvironmentPipelineService:
try:
os.makedirs(target, exist_ok=True)
- result = subprocess.run(
+ result = run_checked(
[
'rsync', '-a', '--delete',
'--exclude', '.git',
f'{source}',
f'{target}'
],
- capture_output=True,
- text=True,
- timeout=300
- )
+ timeout=300)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
return {'success': True}
except subprocess.TimeoutExpired:
diff --git a/backend/app/services/git_deploy_service.py b/backend/app/services/git_deploy_service.py
index 43991146..7223c825 100644
--- a/backend/app/services/git_deploy_service.py
+++ b/backend/app/services/git_deploy_service.py
@@ -8,6 +8,7 @@ import logging
from datetime import datetime
from typing import Dict, Optional
+from app.utils.system import run_checked
from app.utils.git_security import git_argv, git_env, validate_ref_name
logger = logging.getLogger(__name__)
@@ -357,23 +358,16 @@ class GitDeployService:
output = []
for cmd in commands:
- result = subprocess.run(
- cmd,
- cwd=path,
- capture_output=True,
- text=True,
- timeout=120,
- env=git_env()
- )
+ result = run_checked(cmd, cwd=path, timeout=120, env=git_env())
output.append(f"$ {' '.join(cmd)}")
- output.append(result.stdout)
- if result.stderr:
- output.append(result.stderr)
+ output.append(result['output'])
+ if result['stderr']:
+ output.append(result['stderr'])
- if result.returncode != 0:
+ if not result['success']:
return {
'success': False,
- 'error': result.stderr,
+ 'error': result['error'],
'output': '\n'.join(output)
}
@@ -444,43 +438,30 @@ class GitDeployService:
try:
# Build new images first
- build_result = subprocess.run(
- ['docker', 'compose', 'build'],
- cwd=app.root_path,
- capture_output=True,
- text=True,
- timeout=300
- )
- output.append(f"Build: {build_result.stdout}")
+ build_result = run_checked(['docker', 'compose', 'build'],
+ cwd=app.root_path, timeout=300)
+ output.append(f"Build: {build_result['output']}")
- if build_result.returncode != 0:
+ if not build_result['success']:
return {
'success': False,
- 'error': build_result.stderr,
+ 'error': build_result['error'],
'output': '\n'.join(output)
}
# Rolling update - start new containers before stopping old
- up_result = subprocess.run(
+ up_result = run_checked(
['docker', 'compose', 'up', '-d', '--no-deps', '--scale', 'web=2'],
- cwd=app.root_path,
- capture_output=True,
- text=True,
- timeout=120
- )
+ cwd=app.root_path, timeout=120)
# Wait for the new container to be healthy. When a health-check path
# is declared, poll it instead of a blind fixed wait (plan 17 #4).
output.append(cls._wait_for_health(app))
# Scale back down
- scale_result = subprocess.run(
+ scale_result = run_checked(
['docker', 'compose', 'up', '-d', '--no-deps', '--scale', 'web=1'],
- cwd=app.root_path,
- capture_output=True,
- text=True,
- timeout=60
- )
+ cwd=app.root_path, timeout=60)
output.append(f"Rolling update completed")
@@ -535,19 +516,13 @@ class GitDeployService:
def _run_script(cls, script: str, cwd: str) -> Dict:
"""Run a deployment script."""
try:
- result = subprocess.run(
- ['bash', '-c', script],
- cwd=cwd,
- capture_output=True,
- text=True,
- timeout=300,
- env={**os.environ, 'DEPLOY_DIR': cwd}
- )
+ result = run_checked(['bash', '-c', script], cwd=cwd, timeout=300,
+ env={**os.environ, 'DEPLOY_DIR': cwd})
return {
- 'success': result.returncode == 0,
- 'output': result.stdout + result.stderr,
- 'error': result.stderr if result.returncode != 0 else None
+ 'success': result['success'],
+ 'output': result['output'] + result['stderr'],
+ 'error': result['error'],
}
except subprocess.TimeoutExpired:
@@ -578,15 +553,10 @@ class GitDeployService:
# Capture current git commit
try:
- result = subprocess.run(
- ['git', 'rev-parse', 'HEAD'],
- cwd=app.root_path,
- capture_output=True,
- text=True,
- timeout=10
- )
- if result.returncode == 0:
- snapshot['commit_sha'] = result.stdout.strip()
+ result = run_checked(['git', 'rev-parse', 'HEAD'],
+ cwd=app.root_path, timeout=10)
+ if result['success']:
+ snapshot['commit_sha'] = result['output'].strip()
except Exception:
pass
@@ -607,17 +577,12 @@ class GitDeployService:
# Checkout specific commit if available
if snapshot.get('commit_sha'):
- result = subprocess.run(
- ['git', 'checkout', snapshot['commit_sha']],
- cwd=app.root_path,
- capture_output=True,
- text=True,
- timeout=60
- )
- if result.returncode == 0:
+ result = run_checked(['git', 'checkout', snapshot['commit_sha']],
+ cwd=app.root_path, timeout=60)
+ if result['success']:
output.append(f"Checked out commit {snapshot['commit_sha'][:7]}")
else:
- output.append(f"Warning: Could not checkout commit: {result.stderr}")
+ output.append(f"Warning: Could not checkout commit: {result['error']}")
return {
'success': True,
diff --git a/backend/app/services/git_service.py b/backend/app/services/git_service.py
index 86f3508a..abdfdbcd 100644
--- a/backend/app/services/git_service.py
+++ b/backend/app/services/git_service.py
@@ -10,6 +10,7 @@ from pathlib import Path
from app import paths
from app.utils.config_store import load_json_config, save_json_config
+from app.utils.system import run_checked
from app.utils.git_security import (
git_argv,
git_env,
@@ -96,6 +97,18 @@ class GitService:
del config['apps'][str(app_id)]
return cls.save_config(config)
+ @staticmethod
+ def _git_out(app_path, *args, timeout=None):
+ """stdout of a read-only ``git -C <app_path> <args>``, or ``None``.
+
+ ``None`` means the command did not succeed — not "empty output" (§A).
+ ``get_commit_info`` alone had five copies of
+ ``subprocess.run(...); x = result.stdout.strip() if result.returncode
+ == 0 else None``, which is this function written out by hand each time.
+ """
+ result = run_checked(['git', '-C', app_path, *args], timeout=timeout)
+ return result['output'].strip() if result['success'] else None
+
@classmethod
def clone_repository(cls, app_path: str, repo_url: str, branch: str = 'main') -> Dict:
"""Clone a Git repository."""
@@ -122,16 +135,15 @@ class GitService:
if branch:
cmd.extend(['--branch', branch, '--single-branch'])
cmd.extend(['--', repo_url, app_path])
- result = subprocess.run(cmd, capture_output=True, text=True,
- timeout=300, env=git_env())
+ result = run_checked(cmd, timeout=300, env=git_env())
- if result.returncode == 0:
+ if result['success']:
return {
'success': True,
'message': f'Repository cloned to {app_path}',
'path': app_path
}
- return {'success': False, 'error': result.stderr}
+ return {'success': False, 'error': result['error']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Clone operation timed out'}
@@ -151,21 +163,17 @@ class GitService:
try:
# Fetch first
fetch_cmd = git_argv('-C', app_path, 'fetch', '--all')
- subprocess.run(fetch_cmd, capture_output=True, text=True, timeout=60,
- env=git_env())
+ run_checked(fetch_cmd, timeout=60, env=git_env())
# Get current branch if not specified
if not branch:
- branch_cmd = ['git', '-C', app_path, 'rev-parse', '--abbrev-ref', 'HEAD']
- result = subprocess.run(branch_cmd, capture_output=True, text=True)
- branch = result.stdout.strip() if result.returncode == 0 else 'main'
+ branch = cls._git_out(app_path, 'rev-parse', '--abbrev-ref', 'HEAD') or 'main'
# Reset to remote branch (force pull)
reset_cmd = git_argv('-C', app_path, 'reset', '--hard', f'origin/{branch}')
- result = subprocess.run(reset_cmd, capture_output=True, text=True, timeout=60,
- env=git_env())
+ result = run_checked(reset_cmd, timeout=60, env=git_env())
- if result.returncode == 0:
+ if result['success']:
# Get new commit info
commit_info = cls.get_commit_info(app_path)
return {
@@ -173,7 +181,7 @@ class GitService:
'message': 'Changes pulled successfully',
'commit': commit_info
}
- return {'success': False, 'error': result.stderr}
+ return {'success': False, 'error': result['error']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Pull operation timed out'}
@@ -187,30 +195,11 @@ class GitService:
return None
try:
- # Get commit hash
- cmd = ['git', '-C', app_path, 'rev-parse', 'HEAD']
- result = subprocess.run(cmd, capture_output=True, text=True)
- commit_hash = result.stdout.strip() if result.returncode == 0 else None
-
- # Get commit message
- cmd = ['git', '-C', app_path, 'log', '-1', '--pretty=%B']
- result = subprocess.run(cmd, capture_output=True, text=True)
- commit_message = result.stdout.strip() if result.returncode == 0 else None
-
- # Get author
- cmd = ['git', '-C', app_path, 'log', '-1', '--pretty=%an <%ae>']
- result = subprocess.run(cmd, capture_output=True, text=True)
- author = result.stdout.strip() if result.returncode == 0 else None
-
- # Get timestamp
- cmd = ['git', '-C', app_path, 'log', '-1', '--pretty=%ci']
- result = subprocess.run(cmd, capture_output=True, text=True)
- timestamp = result.stdout.strip() if result.returncode == 0 else None
-
- # Get branch
- cmd = ['git', '-C', app_path, 'rev-parse', '--abbrev-ref', 'HEAD']
- result = subprocess.run(cmd, capture_output=True, text=True)
- branch = result.stdout.strip() if result.returncode == 0 else None
+ commit_hash = cls._git_out(app_path, 'rev-parse', 'HEAD')
+ commit_message = cls._git_out(app_path, 'log', '-1', '--pretty=%B')
+ author = cls._git_out(app_path, 'log', '-1', '--pretty=%an <%ae>')
+ timestamp = cls._git_out(app_path, 'log', '-1', '--pretty=%ci')
+ branch = cls._git_out(app_path, 'rev-parse', '--abbrev-ref', 'HEAD')
return {
'hash': commit_hash,
@@ -304,18 +293,12 @@ class GitService:
def _run_script(cls, script: str, working_dir: str) -> Dict:
"""Run a deployment script."""
try:
- result = subprocess.run(
- ['bash', '-c', script],
- cwd=working_dir,
- capture_output=True,
- text=True,
- timeout=300
- )
+ result = run_checked(['bash', '-c', script], cwd=working_dir, timeout=300)
return {
- 'success': result.returncode == 0,
- 'output': result.stdout,
- 'error': result.stderr if result.returncode != 0 else None
+ 'success': result['success'],
+ 'output': result['output'],
+ 'error': result['error'],
}
except subprocess.TimeoutExpired:
@@ -368,18 +351,18 @@ class GitService:
try:
# Fetch latest from remote
- fetch_cmd = ['git', '-C', app_path, 'fetch', '--all', '--prune']
- subprocess.run(fetch_cmd, capture_output=True, text=True, timeout=60)
+ run_checked(['git', '-C', app_path, 'fetch', '--all', '--prune'], timeout=60)
# Get remote branches
- cmd = ['git', '-C', app_path, 'branch', '-r', '--format=%(refname:short)']
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
+ result = run_checked(
+ ['git', '-C', app_path, 'branch', '-r', '--format=%(refname:short)'],
+ timeout=30)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
branches = []
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
branch = line.strip()
if branch and not branch.endswith('/HEAD'):
# Remove origin/ prefix
@@ -387,10 +370,7 @@ class GitService:
branch = branch[7:]
branches.append(branch)
- # Get current branch
- cmd = ['git', '-C', app_path, 'rev-parse', '--abbrev-ref', 'HEAD']
- result = subprocess.run(cmd, capture_output=True, text=True)
- current_branch = result.stdout.strip() if result.returncode == 0 else None
+ current_branch = cls._git_out(app_path, 'rev-parse', '--abbrev-ref', 'HEAD')
return {
'success': True,
@@ -415,14 +395,13 @@ class GitService:
try:
cmd = git_argv('ls-remote', '--heads', '--', repo_url)
- result = subprocess.run(cmd, capture_output=True, text=True,
- timeout=30, env=git_env())
+ result = run_checked(cmd, timeout=30, env=git_env())
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr or 'Failed to fetch branches'}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
branches = []
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
if line:
# Format: <hash>\trefs/heads/<branch>
parts = line.split('\t')
@@ -539,20 +518,17 @@ class GitService:
try:
# Status
- cmd = ['git', '-C', app_path, 'status', '--porcelain']
- result = subprocess.run(cmd, capture_output=True, text=True)
- changes = result.stdout.strip().split('\n') if result.stdout.strip() else []
+ porcelain = cls._git_out(app_path, 'status', '--porcelain') or ''
+ changes = porcelain.split('\n') if porcelain else []
# Remote URL
- cmd = ['git', '-C', app_path, 'remote', 'get-url', 'origin']
- result = subprocess.run(cmd, capture_output=True, text=True)
- remote_url = result.stdout.strip() if result.returncode == 0 else None
+ remote_url = cls._git_out(app_path, 'remote', 'get-url', 'origin')
# Behind/ahead of remote
- cmd = ['git', '-C', app_path, 'rev-list', '--left-right', '--count', 'HEAD...@{u}']
- result = subprocess.run(cmd, capture_output=True, text=True)
- if result.returncode == 0:
- parts = result.stdout.strip().split()
+ counts = cls._git_out(app_path, 'rev-list', '--left-right', '--count',
+ 'HEAD...@{u}')
+ if counts is not None:
+ parts = counts.split()
ahead = int(parts[0]) if len(parts) > 0 else 0
behind = int(parts[1]) if len(parts) > 1 else 0
else:
@@ -689,13 +665,10 @@ class GitService:
def _is_gitea_running(cls) -> bool:
"""Check if Gitea container is running."""
try:
- result = subprocess.run(
- ['docker', 'ps', '--filter', f'name={cls.GITEA_APP_NAME}', '--format', '{{.Names}}'],
- capture_output=True,
- text=True,
- timeout=10
- )
- return cls.GITEA_APP_NAME in result.stdout
+ result = run_checked(
+ ['docker', 'ps', '--filter', f'name={cls.GITEA_APP_NAME}',
+ '--format', '{{.Names}}'], timeout=10)
+ return cls.GITEA_APP_NAME in result['output']
except Exception:
return False
diff --git a/backend/app/services/image_scanner_service.py b/backend/app/services/image_scanner_service.py
index 2c9215d6..97d24333 100644
--- a/backend/app/services/image_scanner_service.py
+++ b/backend/app/services/image_scanner_service.py
@@ -9,6 +9,7 @@ from datetime import datetime
from typing import Dict, List, Optional
from app import db
+from app.utils.system import run_checked
from app.models import Application, ImageVulnerabilityScan, SbomArtifact
logger = logging.getLogger(__name__)
@@ -57,17 +58,25 @@ class ImageScannerService:
url = f'https://github.com/anchore/grype/releases/download/{version}/grype_{version.lstrip("v")}_linux_{arch}.tar.gz'
tmp_tar = '/tmp/serverkit-grype.tar.gz'
try:
- subprocess.run(['curl', '-fsSL', url, '-o', tmp_tar], check=True, capture_output=True)
- subprocess.run(['tar', '-xzf', tmp_tar, '-C', SCANNERS_DIR, 'grype'], check=True, capture_output=True)
+ download = run_checked(['curl', '-fsSL', url, '-o', tmp_tar],
+ timeout=None)
+ if not download['success']:
+ return {'success': False,
+ 'error': f"Failed to download grype: {download['error'][:200]}"}
+ extract = run_checked(
+ ['tar', '-xzf', tmp_tar, '-C', SCANNERS_DIR, 'grype'],
+ timeout=None)
+ if not extract['success']:
+ return {'success': False,
+ 'error': f"Failed to unpack grype: {extract['error'][:200]}"}
os.chmod(GRYPE_BIN, 0o755)
- version_out = subprocess.run([GRYPE_BIN, 'version'], capture_output=True, text=True)
+ version_out = run_checked([GRYPE_BIN, 'version'], timeout=None)
return {
'success': True,
'message': 'grype installed',
- 'version': version_out.stdout.strip().splitlines()[0] if version_out.returncode == 0 else version
+ 'version': (version_out['output'].strip().splitlines() or [version])[0]
+ if version_out['success'] else version,
}
- except subprocess.CalledProcessError as e:
- return {'success': False, 'error': f'Failed to install grype: {e.stderr.decode(errors="ignore")[:200]}'}
finally:
if os.path.exists(tmp_tar):
os.remove(tmp_tar)
@@ -84,17 +93,25 @@ class ImageScannerService:
url = f'https://github.com/anchore/syft/releases/download/{version}/syft_{version.lstrip("v")}_linux_{arch}.tar.gz'
tmp_tar = '/tmp/serverkit-syft.tar.gz'
try:
- subprocess.run(['curl', '-fsSL', url, '-o', tmp_tar], check=True, capture_output=True)
- subprocess.run(['tar', '-xzf', tmp_tar, '-C', SCANNERS_DIR, 'syft'], check=True, capture_output=True)
+ download = run_checked(['curl', '-fsSL', url, '-o', tmp_tar],
+ timeout=None)
+ if not download['success']:
+ return {'success': False,
+ 'error': f"Failed to download syft: {download['error'][:200]}"}
+ extract = run_checked(
+ ['tar', '-xzf', tmp_tar, '-C', SCANNERS_DIR, 'syft'],
+ timeout=None)
+ if not extract['success']:
+ return {'success': False,
+ 'error': f"Failed to unpack syft: {extract['error'][:200]}"}
os.chmod(SYFT_BIN, 0o755)
- version_out = subprocess.run([SYFT_BIN, 'version'], capture_output=True, text=True)
+ version_out = run_checked([SYFT_BIN, 'version'], timeout=None)
return {
'success': True,
'message': 'syft installed',
- 'version': version_out.stdout.strip().splitlines()[0] if version_out.returncode == 0 else version
+ 'version': (version_out['output'].strip().splitlines() or [version])[0]
+ if version_out['success'] else version,
}
- except subprocess.CalledProcessError as e:
- return {'success': False, 'error': f'Failed to install syft: {e.stderr.decode(errors="ignore")[:200]}'}
finally:
if os.path.exists(tmp_tar):
os.remove(tmp_tar)
@@ -106,19 +123,17 @@ class ImageScannerService:
if not install['success']:
return install
try:
- result = subprocess.run(
+ result = run_checked(
[GRYPE_BIN, image_ref, '-o', 'json', '--quiet'],
- capture_output=True,
- text=True,
timeout=600,
- env={**os.environ, 'GRYPE_DB_CACHE_DIR': os.path.join(SCANNERS_DIR, 'grype-db')}
- )
- if result.returncode not in (0, 1):
- return {'success': False, 'error': result.stderr[:500] or f'grype exited {result.returncode}'}
- data = json.loads(result.stdout)
+ env={**os.environ,
+ 'GRYPE_DB_CACHE_DIR': os.path.join(SCANNERS_DIR, 'grype-db')})
+ # 0 = clean, 1 = vulnerabilities FOUND. Only anything else is a
+ # failed scan — read the code, not result['success'].
+ if result['returncode'] not in (0, 1):
+ return {'success': False, 'error': result['error'][:500]}
+ data = json.loads(result['output'])
return {'success': True, 'data': data}
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'grype scan timed out after 10 minutes'}
except json.JSONDecodeError as e:
return {'success': False, 'error': f'Failed to parse grype output: {e}'}
except Exception as e:
@@ -131,18 +146,11 @@ class ImageScannerService:
if not install['success']:
return install
try:
- result = subprocess.run(
- [SYFT_BIN, image_ref, '-o', 'spdx-json'],
- capture_output=True,
- text=True,
- timeout=300
- )
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr[:500] or f'syft exited {result.returncode}'}
- data = json.loads(result.stdout)
+ result = run_checked([SYFT_BIN, image_ref, '-o', 'spdx-json'], timeout=300)
+ if not result['success']:
+ return {'success': False, 'error': result['error'][:500]}
+ data = json.loads(result['output'])
return {'success': True, 'data': data}
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'syft scan timed out after 5 minutes'}
except json.JSONDecodeError as e:
return {'success': False, 'error': f'Failed to parse syft output: {e}'}
except Exception as e:
diff --git a/backend/app/services/install_profile_service.py b/backend/app/services/install_profile_service.py
index c7b3be59..ed98fa88 100644
--- a/backend/app/services/install_profile_service.py
+++ b/backend/app/services/install_profile_service.py
@@ -26,6 +26,8 @@ import logging
import os
import shutil
import subprocess
+
+from app.utils.system import run_checked
import time
logger = logging.getLogger(__name__)
@@ -186,15 +188,18 @@ def _docker_usable():
if not _binary_present('docker'):
return False
try:
- result = subprocess.run(
- ['docker', 'info', '--format', '{{.ServerVersion}}'],
- capture_output=True,
- timeout=5,
- )
- return result.returncode == 0
- except Exception as e:
+ result = run_checked(['docker', 'info', '--format', '{{.ServerVersion}}'],
+ timeout=5)
+ except Exception as e: # noqa: BLE001 - the probe must never raise
logger.debug(f'Docker probe failed: {e}')
return None
+ if result['returncode'] is None:
+ # The probe never ran (exec failure, timeout). run_checked reports that
+ # as success=False, but False here would mean "docker is not usable" —
+ # a fact nobody established. This is what returncode-is-None is for.
+ logger.debug("Docker probe failed: %s", result['error'])
+ return None
+ return result['success']
def get_capabilities(force_refresh=False):
diff --git a/backend/app/services/log_service.py b/backend/app/services/log_service.py
index 3120bdc3..42e92bda 100644
--- a/backend/app/services/log_service.py
+++ b/backend/app/services/log_service.py
@@ -8,7 +8,8 @@ import queue
from app import paths
from app.utils.formatting import format_bytes
-from app.utils.system import run_privileged, privileged_cmd, is_command_available, sourced_result
+from app.utils.system import (run_privileged, privileged_cmd, is_command_available,
+ run_checked, sourced_result)
class LogService:
@@ -224,16 +225,10 @@ class LogService:
cmd.extend(['-f', compose_file])
cmd.extend(['logs', '--tail', str(lines), '--no-color'])
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=30,
- cwd=app_dir
- )
+ result = run_checked(cmd, timeout=30, cwd=app_dir)
- if result.returncode == 0:
- log_lines = result.stdout.split('\n') if result.stdout else []
+ if result['success']:
+ log_lines = result['output'].split('\n') if result['output'] else []
return {**sourced_result(log_lines, 'docker', 'Docker Compose'), 'app_dir': app_dir}
else:
# Try with docker-compose (older syntax) as fallback
@@ -241,17 +236,11 @@ class LogService:
if compose_file:
cmd.extend(['-f', compose_file])
cmd.extend(['logs', '--tail', str(lines), '--no-color'])
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=30,
- cwd=app_dir
- )
- if result.returncode == 0:
- log_lines = result.stdout.split('\n') if result.stdout else []
+ result = run_checked(cmd, timeout=30, cwd=app_dir)
+ if result['success']:
+ log_lines = result['output'].split('\n') if result['output'] else []
return {**sourced_result(log_lines, 'docker', 'Docker Compose (legacy)'), 'app_dir': app_dir}
- return {'success': False, 'error': result.stderr or 'Failed to get Docker logs'}
+ return {'success': False, 'error': result['error']}
except FileNotFoundError:
return {'success': False, 'error': 'Docker not found'}
@@ -352,18 +341,15 @@ class LogService:
def _read_windows_eventlog(lines: int) -> Dict:
"""Read system logs from Windows Event Log via wevtutil."""
try:
- result = subprocess.run(
+ result = run_checked(
['wevtutil', 'qe', 'System', f'/c:{int(lines)}', '/f:text', '/rd:true'],
- capture_output=True,
- text=True,
- timeout=60,
- )
+ timeout=60)
- if result.returncode == 0:
- log_lines = result.stdout.split('\n') if result.stdout else []
+ if result['success']:
+ log_lines = result['output'].split('\n') if result['output'] else []
return sourced_result(log_lines, 'eventlog', 'Windows Event Log')
else:
- return {'success': False, 'error': result.stderr}
+ return {'success': False, 'error': result['error']}
except FileNotFoundError:
return {'success': False, 'error': 'wevtutil command not found'}
diff --git a/backend/app/services/malware_scan_service.py b/backend/app/services/malware_scan_service.py
index f9a0d3e4..9d0629e2 100644
--- a/backend/app/services/malware_scan_service.py
+++ b/backend/app/services/malware_scan_service.py
@@ -21,6 +21,8 @@ result, never an exception).
import os
import subprocess
+
+from app.utils.system import run_checked
import logging
from datetime import datetime
from typing import Dict, Optional
@@ -165,14 +167,13 @@ class MalwareScanService:
def _clamav_pass(cls, path: str, errors: list) -> list:
findings = []
try:
- result = subprocess.run(
- ['clamscan', '-r', '--infected', '--no-summary', path],
- capture_output=True, text=True, timeout=CLAMAV_TIMEOUT,
- )
- # 0 = clean, 1 = infections found, 2 = error
- if result.returncode == 2:
- errors.append(f"clamav: {(result.stderr or result.stdout or 'scan error').strip()[:300]}")
- for line in (result.stdout or '').strip().split('\n'):
+ result = run_checked(['clamscan', '-r', '--infected', '--no-summary', path],
+ timeout=CLAMAV_TIMEOUT)
+ # 0 = clean, 1 = infections FOUND, anything else (or None) = the
+ # scan itself failed and must be reported, never rendered as clean.
+ if result['returncode'] != 1 and not result['success']:
+ errors.append(f"clamav: {(result['error'] or 'scan error')[:300]}")
+ for line in result['output'].strip().split('\n'):
if ': ' in line and line.endswith('FOUND'):
file_path, rest = line.rsplit(': ', 1)
signature = rest[:-len('FOUND')].strip()
diff --git a/backend/app/services/migration_service.py b/backend/app/services/migration_service.py
index a4ed6eb1..ef086712 100644
--- a/backend/app/services/migration_service.py
+++ b/backend/app/services/migration_service.py
@@ -249,18 +249,16 @@ class MigrationService:
return {'success': True, 'path': backup_path}
elif 'postgresql' in db_url:
- import subprocess
+ from app.utils.system import run_checked
backup_dir = '/var/serverkit/backups/db'
os.makedirs(backup_dir, exist_ok=True)
backup_name = f'serverkit_pre_migration_{timestamp}.sql'
backup_path = os.path.join(backup_dir, backup_name)
- result = subprocess.run(
- ['pg_dump', db_url, '-f', backup_path],
- capture_output=True, text=True, timeout=300
- )
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ result = run_checked(['pg_dump', db_url, '-f', backup_path],
+ timeout=300)
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
logger.info(f'Database backup created: {backup_path}')
return {'success': True, 'path': backup_path}
diff --git a/backend/app/services/php_service.py b/backend/app/services/php_service.py
index 8d1f39e9..9e7291d9 100644
--- a/backend/app/services/php_service.py
+++ b/backend/app/services/php_service.py
@@ -5,6 +5,7 @@ from typing import Dict, List, Optional
from pathlib import Path
from app.utils.system import (PackageManager, ServiceControl, is_command_available,
+ run_checked,
run_privileged, write_privileged_file)
@@ -71,13 +72,9 @@ env[TEMP] = /tmp
if os.path.exists(php_bin):
# Get detailed version
try:
- result = subprocess.run(
- [php_bin, '-v'],
- capture_output=True,
- text=True,
- timeout=10
- )
- full_version = result.stdout.split('\n')[0] if result.returncode == 0 else version
+ result = run_checked([php_bin, '-v'], timeout=10)
+ full_version = (result['output'].split('\n')[0]
+ if result['success'] else version)
except Exception:
full_version = version
@@ -107,14 +104,9 @@ env[TEMP] = /tmp
def get_default_version(cls) -> Optional[str]:
"""Get the default PHP version."""
try:
- result = subprocess.run(
- ['php', '-v'],
- capture_output=True,
- text=True,
- timeout=10
- )
- if result.returncode == 0:
- match = re.search(r'PHP (\d+\.\d+)', result.stdout)
+ result = run_checked(['php', '-v'], timeout=10)
+ if result['success']:
+ match = re.search(r'PHP (\d+\.\d+)', result['output'])
if match:
return match.group(1)
except Exception:
@@ -204,15 +196,10 @@ env[TEMP] = /tmp
return extensions
try:
- result = subprocess.run(
- [php_bin, '-m'],
- capture_output=True,
- text=True,
- timeout=10
- )
+ result = run_checked([php_bin, '-m'], timeout=10)
- if result.returncode == 0:
- for ext in result.stdout.strip().split('\n'):
+ if result['success']:
+ for ext in result['output'].strip().split('\n'):
if ext and not ext.startswith('['):
extensions.append({
'name': ext,
@@ -386,14 +373,9 @@ env[TEMP] = /tmp
service = cls.PHP_FPM_SERVICE.format(version=version)
try:
- result = subprocess.run(
- ['systemctl', 'status', service],
- capture_output=True,
- text=True,
- timeout=10
- )
+ result = run_checked(['systemctl', 'status', service], timeout=10)
- is_running = 'active (running)' in result.stdout
+ is_running = 'active (running)' in result['output']
return {
'version': version,
@@ -413,19 +395,14 @@ env[TEMP] = /tmp
return {'error': f'PHP {version} not found'}
try:
- result = subprocess.run(
- [php_bin, '-i'],
- capture_output=True,
- text=True,
- timeout=30
- )
+ result = run_checked([php_bin, '-i'], timeout=30)
- if result.returncode != 0:
- return {'error': result.stderr}
+ if not result['success']:
+ return {'error': result['error']}
# Parse key configuration values
info = {}
- for line in result.stdout.split('\n'):
+ for line in result['output'].split('\n'):
if '=>' in line:
parts = line.split('=>')
if len(parts) >= 2:
diff --git a/backend/app/services/plugin_service.py b/backend/app/services/plugin_service.py
index ba2e5410..72fb16ff 100644
--- a/backend/app/services/plugin_service.py
+++ b/backend/app/services/plugin_service.py
@@ -12,6 +12,8 @@ import os
import re
import shutil
import subprocess
+
+from app.utils.system import run_checked
import sys
import tempfile
import zipfile
@@ -1199,13 +1201,15 @@ def _install_requirements(req_content, plugin_name):
try:
logger.info(f'Installing requirements for plugin {plugin_name}')
- subprocess.check_call(
+ install = run_checked(
[sys.executable, '-m', 'pip', 'install', '-r', req_path, '--quiet'],
timeout=300,
)
- except subprocess.CalledProcessError as e:
- logger.error(f'Failed to install requirements for {plugin_name}: {e}')
- raise ValueError(f'Failed to install Python dependencies: {e}')
+ if not install['success']:
+ logger.error('Failed to install requirements for %s: %s',
+ plugin_name, install['error'])
+ raise ValueError(
+ f"Failed to install Python dependencies: {install['error']}")
finally:
os.unlink(req_path)
diff --git a/backend/app/services/postfix_service.py b/backend/app/services/postfix_service.py
index 92961ed8..b6fa3051 100644
--- a/backend/app/services/postfix_service.py
+++ b/backend/app/services/postfix_service.py
@@ -4,7 +4,8 @@ import re
import subprocess
from typing import Dict
-from app.utils.system import (PackageManager, ServiceControl, run_privileged,
+from app.utils.system import (PackageManager, ServiceControl, run_checked,
+ run_privileged,
write_privileged_file)
from app import paths
@@ -99,8 +100,8 @@ mailbox_size_limit = 0
hostname = None
try:
- result = subprocess.run(['which', 'postfix'], capture_output=True, text=True)
- installed = result.returncode == 0
+ result = run_checked(['which', 'postfix'], timeout=None)
+ installed = result['success']
if not installed:
installed = PackageManager.is_installed('postfix')
@@ -108,13 +109,13 @@ mailbox_size_limit = 0
running = ServiceControl.is_active('postfix')
enabled = ServiceControl.is_enabled('postfix')
- result = subprocess.run(['postconf', 'mail_version'], capture_output=True, text=True)
- match = re.search(r'mail_version\s*=\s*(\S+)', result.stdout)
+ result = run_checked(['postconf', 'mail_version'], timeout=None)
+ match = re.search(r'mail_version\s*=\s*(\S+)', result['output'])
if match:
version = match.group(1)
- result = subprocess.run(['postconf', 'myhostname'], capture_output=True, text=True)
- match = re.search(r'myhostname\s*=\s*(\S+)', result.stdout)
+ result = run_checked(['postconf', 'myhostname'], timeout=None)
+ match = re.search(r'myhostname\s*=\s*(\S+)', result['output'])
if match:
hostname = match.group(1)
except (subprocess.SubprocessError, FileNotFoundError):
@@ -294,8 +295,8 @@ mailbox_size_limit = 0
def get_queue(cls) -> Dict:
"""Get the Postfix mail queue."""
try:
- result = subprocess.run(['mailq'], capture_output=True, text=True)
- output = result.stdout or ''
+ result = run_checked(['mailq'], timeout=None)
+ output = result['output']
if 'Mail queue is empty' in output:
return {'success': True, 'queue': [], 'total': 0}
diff --git a/backend/app/services/process_service.py b/backend/app/services/process_service.py
index 1bae184a..2006cfb6 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, unit_is_active
+from app.utils.system import run_checked, run_privileged, unit_is_active
class ProcessService:
@@ -110,16 +110,13 @@ class ProcessService:
"""
cmd = ['systemctl', 'show', *units]
cmd += [f'--property={p}' for p in cls._SHOW_PROPERTIES]
- try:
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=15)
- except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
- return {}
- if result.returncode != 0:
+ result = run_checked(cmd, timeout=15)
+ if not result['success']:
return {}
blocks: List[Dict[str, str]] = []
current: Dict[str, str] = {}
- for line in (result.stdout or '').splitlines():
+ for line in result['output'].splitlines():
line = line.strip()
if not line:
if current:
@@ -251,10 +248,10 @@ class ProcessService:
action_map = {'start': 'start', 'stop': 'stop', 'restart': 'restart'}
cmd = ['net', action_map.get(action, action), service_name]
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
+ result = run_checked(cmd, timeout=30)
return {
- 'success': result.returncode == 0,
- 'message': result.stdout if result.returncode == 0 else result.stderr
+ 'success': result['success'],
+ 'message': result['output'] if result['success'] else result['error']
}
else:
diff --git a/backend/app/services/python_service.py b/backend/app/services/python_service.py
index 11b19cce..9c903fff 100644
--- a/backend/app/services/python_service.py
+++ b/backend/app/services/python_service.py
@@ -5,7 +5,7 @@ import json
import secrets
from pathlib import Path
-from app.utils.system import ServiceControl
+from app.utils.system import ServiceControl, run_checked
class PythonService:
@@ -74,12 +74,9 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
python_path = shutil.which(f'python{version}')
if python_path:
try:
- result = subprocess.run(
- [python_path, '--version'],
- capture_output=True, text=True
- )
- if result.returncode == 0:
- full_version = result.stdout.strip().replace('Python ', '')
+ result = run_checked([python_path, '--version'], timeout=None)
+ if result['success']:
+ full_version = result['output'].strip().replace('Python ', '')
versions.append({
'version': version,
'full_version': full_version,
@@ -103,12 +100,9 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
python_path = shutil.which('python3')
if python_path:
try:
- result = subprocess.run(
- [python_path, '--version'],
- capture_output=True, text=True
- )
- if result.returncode == 0:
- return result.stdout.strip().replace('Python ', '')
+ result = run_checked([python_path, '--version'], timeout=None)
+ if result['success']:
+ return result['output'].strip().replace('Python ', '')
except Exception:
pass
return None
@@ -125,20 +119,14 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
try:
# Create venv
- result = subprocess.run(
- [python_bin, '-m', 'venv', venv_path],
- capture_output=True, text=True
- )
+ result = run_checked([python_bin, '-m', 'venv', venv_path], timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Upgrade pip
pip_path = os.path.join(venv_path, 'bin', 'pip')
- subprocess.run(
- [pip_path, 'install', '--upgrade', 'pip'],
- capture_output=True, text=True
- )
+ run_checked([pip_path, 'install', '--upgrade', 'pip'], timeout=None)
return {
'success': True,
@@ -163,24 +151,19 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Install from list
if isinstance(requirements, str):
requirements = [requirements]
- result = subprocess.run(
- [pip_path, 'install'] + requirements,
- capture_output=True, text=True
- )
+ result = run_checked([pip_path, 'install'] + requirements, timeout=None)
else:
# Install from requirements.txt
req_file = os.path.join(app_path, 'requirements.txt')
if not os.path.exists(req_file):
return {'success': False, 'error': 'requirements.txt not found'}
- result = subprocess.run(
- [pip_path, 'install', '-r', req_file],
- capture_output=True, text=True
- )
+ result = run_checked([pip_path, 'install', '-r', req_file], timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr, 'output': result.stdout}
+ if not result['success']:
+ return {'success': False, 'error': result['error'],
+ 'output': result['output']}
- return {'success': True, 'output': result.stdout}
+ return {'success': True, 'output': result['output']}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -194,12 +177,9 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
return []
try:
- result = subprocess.run(
- [pip_path, 'list', '--format=json'],
- capture_output=True, text=True
- )
- if result.returncode == 0:
- return json.loads(result.stdout)
+ result = run_checked([pip_path, 'list', '--format=json'], timeout=None)
+ if result['success']:
+ return json.loads(result['output'])
return []
except Exception:
return []
@@ -214,16 +194,13 @@ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
return {'success': False, 'error': 'Virtual environment not found'}
try:
- result = subprocess.run(
- [pip_path, 'freeze'],
- capture_output=True, text=True
- )
- if result.returncode == 0:
+ result = run_checked([pip_path, 'freeze'], timeout=None)
+ if result['success']:
req_file = os.path.join(app_path, 'requirements.txt')
with open(req_file, 'w') as f:
- f.write(result.stdout)
- return {'success': True, 'requirements': result.stdout}
- return {'success': False, 'error': result.stderr}
+ f.write(result['output'])
+ return {'success': True, 'requirements': result['output']}
+ return {'success': False, 'error': result['error']}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -297,23 +274,18 @@ SECRET_KEY={secrets.token_hex(32)}
python_path = os.path.join(venv_path, 'bin', 'python')
# Install Django and Gunicorn
- subprocess.run(
- [pip_path, 'install', 'django', 'gunicorn', 'python-dotenv'],
- capture_output=True, text=True
- )
+ run_checked([pip_path, 'install', 'django', 'gunicorn', 'python-dotenv'],
+ timeout=None)
# Create Django project
django_admin = os.path.join(venv_path, 'bin', 'django-admin')
project_name = app_name.replace('-', '_').replace(' ', '_').lower()
- result = subprocess.run(
- [django_admin, 'startproject', project_name, '.'],
- cwd=app_path,
- capture_output=True, text=True
- )
+ result = run_checked([django_admin, 'startproject', project_name, '.'],
+ cwd=app_path, timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr}
+ if not result['success']:
+ return {'success': False, 'error': result['error']}
# Build ALLOWED_HOSTS: local addresses plus the panel domain and its
# subdomains so Django accepts requests behind nginx on a real domain.
@@ -353,11 +325,7 @@ application = get_wsgi_application()
f.write(wsgi_content)
# Run migrations
- subprocess.run(
- [python_path, 'manage.py', 'migrate'],
- cwd=app_path,
- capture_output=True, text=True
- )
+ run_checked([python_path, 'manage.py', 'migrate'], cwd=app_path, timeout=None)
# Collect static files directory
os.makedirs(os.path.join(app_path, 'staticfiles'), exist_ok=True)
@@ -466,14 +434,13 @@ application = get_wsgi_application()
status = 'active' if ServiceControl.is_active(service_name) else 'inactive'
# Get more details
- details_result = subprocess.run(
+ details_result = run_checked(
['systemctl', 'show', service_name,
'--property=ActiveState,SubState,MainPID,MemoryCurrent'],
- capture_output=True, text=True
- )
+ timeout=None)
details = {}
- for line in details_result.stdout.strip().split('\n'):
+ for line in details_result['output'].strip().split('\n'):
if '=' in line:
key, value = line.split('=', 1)
details[key] = value
@@ -566,23 +533,20 @@ application = get_wsgi_application()
env_vars = PythonService.get_env_vars(app_path)
env.update(env_vars)
- result = subprocess.run(
- ['bash', '-c', command],
- cwd=app_path,
- env=env,
- capture_output=True,
- text=True,
- timeout=300
- )
+ result = run_checked(['bash', '-c', command], cwd=app_path, env=env,
+ timeout=300)
+
+ if result['returncode'] is None:
+ # Never ran (no bash, or it outlived the 300s bound) — do not
+ # hand back a return_code the command never produced.
+ return {'success': False, 'error': result['error']}
return {
- 'success': result.returncode == 0,
- 'stdout': result.stdout,
- 'stderr': result.stderr,
- 'return_code': result.returncode
+ 'success': result['success'],
+ 'stdout': result['output'],
+ 'stderr': result['stderr'],
+ 'return_code': result['returncode'],
}
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Command timed out'}
except Exception as e:
return {'success': False, 'error': str(e)}
diff --git a/backend/app/services/security_service.py b/backend/app/services/security_service.py
index 35603236..1aaec447 100644
--- a/backend/app/services/security_service.py
+++ b/backend/app/services/security_service.py
@@ -25,6 +25,7 @@ from app.utils.system import (
PackageManager,
ServiceControl,
is_command_available,
+ run_checked,
run_privileged,
)
@@ -104,16 +105,11 @@ class SecurityService:
# Check if ClamAV is installed
try:
- version_output = subprocess.run(
- ['clamscan', '--version'],
- capture_output=True,
- text=True,
- timeout=10
- )
- if version_output.returncode == 0:
+ version_output = run_checked(['clamscan', '--version'], timeout=10)
+ if version_output['success']:
result['installed'] = True
- result['version'] = version_output.stdout.strip()
- except (FileNotFoundError, subprocess.TimeoutExpired):
+ result['version'] = version_output['output'].strip()
+ except Exception: # noqa: BLE001 - probe only; absence is the answer
pass
# Check if clamd service is running
@@ -175,29 +171,26 @@ class SecurityService:
# Stop freshclam if running to avoid conflicts
ServiceControl.stop('clamav-freshclam', timeout=10)
- result = subprocess.run(
- ['freshclam'],
- capture_output=True,
- text=True,
- timeout=300
- )
+ result = run_checked(['freshclam'], timeout=300)
# Restart freshclam
ServiceControl.start('clamav-freshclam', timeout=10)
- if result.returncode == 0:
- return {'success': True, 'message': 'Definitions updated', 'output': result.stdout}
+ if result['success']:
+ return {'success': True, 'message': 'Definitions updated',
+ 'output': result['output']}
+
+ if result['returncode'] is None:
+ # Never ran: no freshclam, or it outlived the timeout. Not the
+ # same as "the update reported a problem".
+ return {'success': False, 'error': result['error']}
# Return code 1 might just mean "already up to date"
- if 'up to date' in result.stdout.lower() or 'up to date' in result.stderr.lower():
+ if ('up to date' in result['output'].lower()
+ or 'up to date' in result['stderr'].lower()):
return {'success': True, 'message': 'Definitions already up to date'}
- return {'success': False, 'error': result.stderr or result.stdout}
-
- except FileNotFoundError:
- return {'success': False, 'error': 'freshclam not found. Is ClamAV installed?'}
- except subprocess.TimeoutExpired:
- return {'success': False, 'error': 'Update timed out'}
+ return {'success': False, 'error': result['stderr'] or result['output']}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -232,19 +225,16 @@ class SecurityService:
return {'success': False, 'error': 'File not found'}
try:
- result = subprocess.run(
- ['clamscan', '--no-summary', file_path],
- capture_output=True,
- text=True,
- timeout=60
- )
-
- infected = result.returncode == 1
+ result = run_checked(['clamscan', '--no-summary', file_path], timeout=60)
+
+ # clamscan exit 1 means INFECTED, not "the command failed" — read
+ # the code, not result['success'].
+ infected = result['returncode'] == 1
scan_result = {
'success': True,
'file': file_path,
'infected': infected,
- 'output': result.stdout.strip(),
+ 'output': result['output'].strip(),
'scanned_at': datetime.now().isoformat()
}
@@ -252,7 +242,7 @@ class SecurityService:
# Log and send notification
cls._log_alert('malware', f'Malware detected in {file_path}', {
'file': file_path,
- 'output': result.stdout.strip()
+ 'output': result['output'].strip()
})
cls._send_security_notification(
'malware_detected',
@@ -320,16 +310,11 @@ class SecurityService:
cmd.append(directory)
- result = subprocess.run(
- cmd,
- capture_output=True,
- text=True,
- timeout=3600 # 1 hour timeout
- )
+ result = run_checked(cmd, timeout=3600) # 1 hour
# Parse output
infected_files = []
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
if ': ' in line and 'FOUND' in line:
file_path = line.split(':')[0]
infected_files.append(file_path)
@@ -337,7 +322,7 @@ class SecurityService:
cls._current_scan['status'] = 'completed'
cls._current_scan['completed_at'] = datetime.now().isoformat()
cls._current_scan['infected_files'] = infected_files
- cls._current_scan['output'] = result.stdout
+ cls._current_scan['output'] = result['output']
# Log scan result
cls._log_scan(cls._current_scan)
@@ -903,16 +888,11 @@ class SecurityService:
}
try:
- version_output = subprocess.run(
- ['fail2ban-client', '--version'],
- capture_output=True,
- text=True,
- timeout=10
- )
- if version_output.returncode == 0:
+ version_output = run_checked(['fail2ban-client', '--version'], timeout=10)
+ if version_output['success']:
result['installed'] = True
- result['version'] = version_output.stdout.strip().split('\n')[0]
- except (FileNotFoundError, subprocess.TimeoutExpired):
+ result['version'] = version_output['output'].strip().split('\n')[0]
+ except Exception: # noqa: BLE001 - probe only; absence is the answer
pass
if result['installed']:
@@ -923,14 +903,9 @@ class SecurityService:
if result['service_running']:
try:
- jails_output = subprocess.run(
- ['fail2ban-client', 'status'],
- capture_output=True,
- text=True,
- timeout=10
- )
- if jails_output.returncode == 0:
- for line in jails_output.stdout.split('\n'):
+ jails_output = run_checked(['fail2ban-client', 'status'], timeout=10)
+ if jails_output['success']:
+ for line in jails_output['output'].split('\n'):
if 'Jail list:' in line:
jails_str = line.split(':')[1].strip()
if jails_str:
@@ -967,14 +942,9 @@ class SecurityService:
def get_fail2ban_jail_status(cls, jail: str) -> Dict:
"""Get status of a specific Fail2ban jail."""
try:
- result = subprocess.run(
- ['fail2ban-client', 'status', jail],
- capture_output=True,
- text=True,
- timeout=10
- )
+ result = run_checked(['fail2ban-client', 'status', jail], timeout=10)
- if result.returncode != 0:
+ if not result['success']:
return {'success': False, 'error': f'Jail {jail} not found'}
status = {
@@ -986,7 +956,7 @@ class SecurityService:
'total_failed': 0
}
- for line in result.stdout.split('\n'):
+ for line in result['output'].split('\n'):
line = line.strip()
if 'Currently banned:' in line:
status['currently_banned'] = int(line.split(':')[1].strip())
@@ -1027,23 +997,14 @@ class SecurityService:
"""Unban an IP from Fail2ban."""
try:
if jail:
- result = subprocess.run(
- ['fail2ban-client', 'set', jail, 'unbanip', ip],
- capture_output=True,
- text=True,
- timeout=10
- )
+ result = run_checked(['fail2ban-client', 'set', jail, 'unbanip', ip],
+ timeout=10)
else:
- result = subprocess.run(
- ['fail2ban-client', 'unban', ip],
- capture_output=True,
- text=True,
- timeout=10
- )
+ result = run_checked(['fail2ban-client', 'unban', ip], timeout=10)
- if result.returncode == 0:
+ if result['success']:
return {'success': True, 'message': f'IP {ip} unbanned'}
- return {'success': False, 'error': result.stderr or 'Failed to unban IP'}
+ return {'success': False, 'error': result['error'] or 'Failed to unban IP'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -1052,16 +1013,11 @@ class SecurityService:
def ban_ip(cls, ip: str, jail: str = 'sshd') -> Dict:
"""Manually ban an IP in Fail2ban."""
try:
- result = subprocess.run(
- ['fail2ban-client', 'set', jail, 'banip', ip],
- capture_output=True,
- text=True,
- timeout=10
- )
+ result = run_checked(['fail2ban-client', 'set', jail, 'banip', ip], timeout=10)
- if result.returncode == 0:
+ if result['success']:
return {'success': True, 'message': f'IP {ip} banned in {jail}'}
- return {'success': False, 'error': result.stderr or 'Failed to ban IP'}
+ return {'success': False, 'error': result['error'] or 'Failed to ban IP'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -1116,15 +1072,9 @@ class SecurityService:
def _get_key_fingerprint(cls, key_line: str) -> str:
"""Get SSH key fingerprint."""
try:
- result = subprocess.run(
- ['ssh-keygen', '-lf', '-'],
- input=key_line,
- capture_output=True,
- text=True,
- timeout=5
- )
- if result.returncode == 0:
- parts = result.stdout.strip().split()
+ result = run_checked(['ssh-keygen', '-lf', '-'], input=key_line, timeout=5)
+ if result['success']:
+ parts = result['output'].strip().split()
if len(parts) >= 2:
return parts[1]
except Exception:
@@ -1319,8 +1269,8 @@ class SecurityService:
passed_checks = 0
try:
- uname = subprocess.run(['uname', '-a'], capture_output=True, text=True, timeout=5)
- audit['system']['kernel'] = uname.stdout.strip() if uname.returncode == 0 else 'Unknown'
+ uname = run_checked(['uname', '-a'], timeout=5)
+ audit['system']['kernel'] = uname['output'].strip() if uname['success'] else 'Unknown'
except Exception:
audit['system']['kernel'] = 'Unknown'
@@ -1455,9 +1405,9 @@ class SecurityService:
checks['total_checks'] += 1
try:
if PackageManager.detect() == 'apt':
- result = subprocess.run(['apt', 'list', '--upgradable'], capture_output=True, text=True, timeout=60)
- if result.returncode == 0:
- lines = [l for l in result.stdout.split('\n') if '/' in l]
+ result = run_checked(['apt', 'list', '--upgradable'], timeout=60)
+ if result['success']:
+ lines = [l for l in result['output'].split('\n') if '/' in l]
if len(lines) == 0:
checks['passed_checks'] += 1
checks['findings'].append({'severity': 'pass', 'message': 'System is up to date'})
@@ -1615,18 +1565,14 @@ class SecurityService:
def _execute_lynis_scan(cls) -> Dict:
"""Execute Lynis and update the compatibility status snapshot."""
try:
- result = subprocess.run(
- ['lynis', 'audit', 'system', '--quick', '--no-colors'],
- capture_output=True,
- text=True,
- timeout=1800
- )
-
- cls._lynis_scan['output'] = result.stdout
+ result = run_checked(['lynis', 'audit', 'system', '--quick', '--no-colors'],
+ timeout=1800)
+
+ cls._lynis_scan['output'] = result['output']
cls._lynis_scan['status'] = 'completed'
cls._lynis_scan['completed_at'] = datetime.now().isoformat()
- for line in result.stdout.split('\n'):
+ for line in result['output'].split('\n'):
if 'Warning:' in line:
cls._lynis_scan['warnings'].append(line.strip())
elif 'Suggestion:' in line:
diff --git a/backend/app/services/site_importers/ssh.py b/backend/app/services/site_importers/ssh.py
index 0d19e808..292d80d3 100644
--- a/backend/app/services/site_importers/ssh.py
+++ b/backend/app/services/site_importers/ssh.py
@@ -24,6 +24,8 @@ import os
import shlex
import subprocess
+from app.utils.system import run_checked
+
from .base import BaseSiteImporter
@@ -141,14 +143,9 @@ class GenericSshImporter(BaseSiteImporter):
ssh_cmd = 'ssh ' + ' '.join(shlex.quote(o) for o in ssh_opts)
src = f"{remote}:{docroot.rstrip('/')}/"
cmd = ['rsync', '-az', '--delete', '-e', ssh_cmd, src, dest + os.sep]
- try:
- subprocess.run(cmd, check=True, capture_output=True,
- timeout=cls._RSYNC_TIMEOUT)
- except subprocess.CalledProcessError as exc:
- stderr = (exc.stderr or b'').decode('utf-8', 'replace').strip()
- raise SshImportError(f'Failed to pull docroot over SSH: {stderr}')
- except (OSError, subprocess.TimeoutExpired) as exc:
- raise SshImportError(f'Failed to pull docroot over SSH: {exc}')
+ result = run_checked(cmd, timeout=cls._RSYNC_TIMEOUT, errors='replace')
+ if not result['success']:
+ raise SshImportError(f"Failed to pull docroot over SSH: {result['error']}")
@classmethod
def _dump_database(cls, remote, source, ssh_opts, out_path):
@@ -165,15 +162,16 @@ class GenericSshImporter(BaseSiteImporter):
f'-h {shlex.quote(db_host)} -u {shlex.quote(db_user)} '
f'{shlex.quote(db_name)}')
cmd = ['ssh'] + ssh_opts + [remote, remote_cmd]
- try:
- with open(out_path, 'wb') as fh:
- proc = subprocess.run(
- cmd, input=(password + '\n').encode(), stdout=fh,
- stderr=subprocess.PIPE, timeout=cls._DUMP_TIMEOUT)
- except (OSError, subprocess.TimeoutExpired) as exc:
- raise SshImportError(f'Failed to dump remote database: {exc}')
- if proc.returncode != 0:
- stderr = (proc.stderr or b'').decode('utf-8', 'replace').strip()
+ with open(out_path, 'wb') as fh:
+ # text=False: the dump itself is written straight to a binary file
+ # handle; only stderr is captured, and it is decoded below.
+ proc = run_checked(
+ cmd, input=(password + '\n').encode(), stdout=fh,
+ stderr=subprocess.PIPE, timeout=cls._DUMP_TIMEOUT, text=False)
+ if proc['returncode'] is None:
+ raise SshImportError(f"Failed to dump remote database: {proc['error']}")
+ if not proc['success']:
+ stderr = (proc['stderr'] or b'').decode('utf-8', 'replace').strip()
raise SshImportError(f'Remote mysqldump failed: {stderr}')
# ── analysis (built from the source + pull result) ──
@@ -260,15 +258,11 @@ class GenericSshImporter(BaseSiteImporter):
f'test -f {shlex.quote(docroot.rstrip("/") + "/wp-config.php")} '
'&& echo __HAS_WP_CONFIG__')
cmd = ['ssh'] + ssh_opts + [remote, remote_cmd]
- try:
- proc = subprocess.run(cmd, capture_output=True,
- timeout=cls._CONNECT_TIMEOUT + 10)
- except (OSError, subprocess.TimeoutExpired) as exc:
- return {'reachable': False, 'error': str(exc)}
- if proc.returncode != 0:
- stderr = (proc.stderr or b'').decode('utf-8', 'replace').strip()
- return {'reachable': False, 'error': stderr or 'SSH connection failed'}
- lines = (proc.stdout or b'').decode('utf-8', 'replace').splitlines()
+ proc = run_checked(cmd, timeout=cls._CONNECT_TIMEOUT + 10, errors='replace')
+ if not proc['success']:
+ return {'reachable': False,
+ 'error': proc['error'] or 'SSH connection failed'}
+ lines = proc['output'].splitlines()
has_wp = '__HAS_WP_CONFIG__' in lines
files = [ln for ln in lines
if ln and ln != '__HAS_WP_CONFIG__' and ln not in ('.', '..')]
diff --git a/backend/app/services/speed_test_service.py b/backend/app/services/speed_test_service.py
index 527cf2eb..441c7f5c 100644
--- a/backend/app/services/speed_test_service.py
+++ b/backend/app/services/speed_test_service.py
@@ -18,6 +18,8 @@ import logging
import os
import socket
import subprocess
+
+from app.utils.system import run_checked
import time
from datetime import datetime
@@ -118,14 +120,12 @@ class SpeedTestService:
return None
try:
- proc = subprocess.run(
- cmd, capture_output=True, text=True, timeout=CLI_TIMEOUT
- )
- if proc.returncode != 0:
- logger.warning('speedtest CLI exited %s: %s',
- proc.returncode, (proc.stderr or '')[:500])
+ proc = run_checked(cmd, timeout=CLI_TIMEOUT)
+ if not proc['success']:
+ logger.warning('speedtest CLI failed (%s): %s',
+ proc['returncode'], proc['error'][:500])
return None # fall through to the pure-Python tier
- data = json.loads(proc.stdout)
+ data = json.loads(proc['output'])
except (subprocess.SubprocessError, OSError, json.JSONDecodeError) as exc:
logger.warning('speedtest CLI failed (%s); using fallback', exc)
return None
diff --git a/backend/app/services/system_service.py b/backend/app/services/system_service.py
index 406d9db2..7170046c 100644
--- a/backend/app/services/system_service.py
+++ b/backend/app/services/system_service.py
@@ -5,7 +5,7 @@ import os
from datetime import datetime
from app.utils.formatting import format_bytes
-from app.utils.system import run_privileged
+from app.utils.system import run_checked, run_privileged
class SystemService:
@@ -333,12 +333,10 @@ class SystemService:
# Fallback: try timedatectl on systemd systems
if not timezone_id:
try:
- result = subprocess.run(
- ['timedatectl', 'show', '--property=Timezone', '--value'],
- capture_output=True, text=True, timeout=5
- )
- if result.returncode == 0:
- timezone_id = result.stdout.strip()
+ result = run_checked(
+ ['timedatectl', 'show', '--property=Timezone', '--value'], timeout=5)
+ if result['success']:
+ timezone_id = result['output'].strip()
except Exception:
pass
@@ -380,12 +378,9 @@ class SystemService:
"""Get list of available timezones."""
try:
# Use timedatectl to list timezones on systemd systems
- result = subprocess.run(
- ['timedatectl', 'list-timezones'],
- capture_output=True, text=True, timeout=10
- )
- if result.returncode == 0:
- return result.stdout.strip().split('\n')
+ result = run_checked(['timedatectl', 'list-timezones'], timeout=10)
+ if result['success']:
+ return result['output'].strip().split('\n')
except Exception:
pass
diff --git a/backend/app/services/template_service.py b/backend/app/services/template_service.py
index 57e399c7..6b0f58c6 100644
--- a/backend/app/services/template_service.py
+++ b/backend/app/services/template_service.py
@@ -28,6 +28,7 @@ from pathlib import Path
import requests
from app import paths
+from app.utils.system import run_checked
class TemplateService:
@@ -1105,16 +1106,11 @@ class TemplateService:
"""Get all ports currently mapped by Docker containers."""
used_ports = set()
try:
- result = subprocess.run(
- ['docker', 'ps', '--format', '{{.Ports}}'],
- capture_output=True,
- text=True,
- timeout=10
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'ps', '--format', '{{.Ports}}'], timeout=10)
+ if result['success']:
# Parse port mappings like "0.0.0.0:8080->80/tcp, 127.0.0.1:3306->3306/tcp"
import re
- for line in result.stdout.strip().split('\n'):
+ for line in result['output'].strip().split('\n'):
if line:
# Find all host ports in the format "host:port->container"
matches = re.findall(r'(?:[\d.]+:)?(\d+)->', line)
@@ -2180,23 +2176,16 @@ class TemplateService:
env = os.environ.copy()
env.update(variables)
- result = subprocess.run(
- ['bash', '-c', script],
- cwd=cwd,
- env=env,
- capture_output=True,
- text=True,
- timeout=300
- )
+ result = run_checked(['bash', '-c', script], cwd=cwd, env=env, timeout=300)
- if result.returncode != 0:
+ if not result['success']:
return {
'success': False,
- 'error': f"Script failed: {result.stderr}",
- 'output': result.stdout
+ 'error': f"Script failed: {result['error']}",
+ 'output': result['output'],
}
- return {'success': True, 'output': result.stdout}
+ return {'success': True, 'output': result['output']}
except subprocess.TimeoutExpired:
return {'success': False, 'error': 'Script timed out'}
diff --git a/backend/app/services/test_sandbox_service.py b/backend/app/services/test_sandbox_service.py
index 29c5b49d..03741371 100644
--- a/backend/app/services/test_sandbox_service.py
+++ b/backend/app/services/test_sandbox_service.py
@@ -28,6 +28,7 @@ from pathlib import Path
from flask import current_app
from app import db
+from app.utils.system import run_checked
from app.models.sandbox_run import SandboxRun
# backend/app/services/test_sandbox_service.py -> repo root
@@ -127,11 +128,9 @@ class TestSandboxService:
@classmethod
def docker_available(cls):
try:
- proc = subprocess.run(
- ['docker', 'info', '--format', '{{.ServerVersion}}'],
- capture_output=True, text=True, timeout=15)
- return proc.returncode == 0
- except (OSError, subprocess.SubprocessError):
+ return run_checked(['docker', 'info', '--format', '{{.ServerVersion}}'],
+ timeout=15)['success']
+ except Exception: # noqa: BLE001 - availability probe; absence is the answer
return False
@classmethod
@@ -190,8 +189,7 @@ class TestSandboxService:
with _active_lock:
names = list(_active_containers.get(run_id, []))
for name in names:
- subprocess.run(['docker', 'rm', '-f', name],
- capture_output=True, timeout=30)
+ run_checked(['docker', 'rm', '-f', name], timeout=30)
run.status = 'cancelled'
run.finished_at = datetime.utcnow()
db.session.commit()
@@ -276,8 +274,7 @@ class TestSandboxService:
except Exception as exc: # noqa: BLE001 — record, don't kill the run
update('failed', f'sandbox error: {exc}')
finally:
- subprocess.run(['docker', 'rm', '-f', container],
- capture_output=True, timeout=30)
+ run_checked(['docker', 'rm', '-f', container], timeout=30)
@classmethod
def _quick_container(cls, container, distro, work_dir, log):
@@ -288,12 +285,12 @@ class TestSandboxService:
'-v', f'{work_dir}:/work',
image, 'sh', '/work/run-quick.sh',
]
- try:
- proc = subprocess.run(cmd, stdout=log, stderr=subprocess.STDOUT,
- timeout=QUICK_TIMEOUT_S)
- except subprocess.TimeoutExpired:
- return False, f'timed out after {QUICK_TIMEOUT_S // 60}m'
- return proc.returncode == 0, f'exit {proc.returncode}'
+ proc = run_checked(cmd, stdout=log, stderr=subprocess.STDOUT,
+ timeout=QUICK_TIMEOUT_S)
+ if proc['returncode'] is None:
+ # Never produced an exit code: timed out, or docker is not there.
+ return False, proc['error']
+ return proc['success'], f"exit {proc['returncode']}"
@classmethod
def _full_container(cls, container, distro, log):
@@ -305,18 +302,17 @@ class TestSandboxService:
'-v', f'{REPO_ROOT}:/src:ro',
image,
]
- proc = subprocess.run(run_cmd, stdout=log, stderr=subprocess.STDOUT,
- timeout=120)
- if proc.returncode != 0:
- return False, f'container failed to start (exit {proc.returncode})'
+ proc = run_checked(run_cmd, stdout=log, stderr=subprocess.STDOUT, timeout=120)
+ if not proc['success']:
+ return False, f"container failed to start ({proc['error']})"
# Wait for systemd to come up before exec'ing into the container.
booted = False
for _ in range(60):
- probe = subprocess.run(
+ probe = run_checked(
['docker', 'exec', container, 'systemctl', 'is-system-running'],
- capture_output=True, text=True, timeout=15)
- state = probe.stdout.strip()
+ timeout=15)
+ state = probe['output'].strip()
if state in ('running', 'degraded'):
booted = True
break
@@ -331,12 +327,12 @@ class TestSandboxService:
# images) — curl is NOT preinstalled there.
net_ready = False
for _ in range(45):
- probe = subprocess.run(
+ probe = run_checked(
['docker', 'exec', container,
'python3', '-c',
"import urllib.request; urllib.request.urlopen('https://github.com', timeout=8)"],
- capture_output=True, timeout=15)
- if probe.returncode == 0:
+ timeout=15)
+ if probe['success']:
net_ready = True
break
time.sleep(2)
@@ -345,21 +341,21 @@ class TestSandboxService:
log.write('\n===== install.sh =====\n')
log.flush()
- install = subprocess.run(
+ install = run_checked(
['docker', 'exec', container, 'bash', '/src/install.sh'],
stdout=log, stderr=subprocess.STDOUT, timeout=FULL_TIMEOUT_S)
- if install.returncode != 0:
- return False, f'install.sh exited {install.returncode}'
+ if not install['success']:
+ return False, f"install.sh failed ({install['error']})"
log.write('\n===== health probe =====\n')
log.flush()
for _ in range(60):
- probe = subprocess.run(
+ probe = run_checked(
['docker', 'exec', container,
'python3', '-c',
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:5000/api/v1/system/health', timeout=8)"],
- capture_output=True, text=True, timeout=15)
- if probe.returncode == 0:
+ timeout=15)
+ if probe['success']:
log.write('healthy\n')
return True, 'install + health OK'
time.sleep(2)
diff --git a/backend/app/services/volume_service.py b/backend/app/services/volume_service.py
index 32cd9f79..3102cbd3 100644
--- a/backend/app/services/volume_service.py
+++ b/backend/app/services/volume_service.py
@@ -23,6 +23,7 @@ import subprocess
from app import db
from app.models.app_volume import AppVolume
from app.services.docker_service import DockerService
+from app.utils.system import run_checked
logger = logging.getLogger(__name__)
@@ -138,13 +139,12 @@ class VolumeService:
if os.name != 'posix':
return None
try:
- result = subprocess.run(
+ result = run_checked(
['docker', 'run', '--rm', '-v', f'{volume.docker_volume_name}:/data:ro',
cls.HELPER_IMAGE, 'du', '-sb', '/data'],
- capture_output=True, text=True, timeout=120,
- )
- if result.returncode == 0 and result.stdout.strip():
- size = int(result.stdout.split()[0])
+ timeout=120)
+ if result['success'] and result['output'].strip():
+ size = int(result['output'].split()[0])
volume.size_bytes = size
db.session.commit()
return size
@@ -165,13 +165,12 @@ class VolumeService:
host_path = os.path.abspath(host_path or '')
if host_path and os.path.isdir(host_path) and os.name == 'posix':
try:
- subprocess.run(
+ run_checked(
['docker', 'run', '--rm',
'-v', f'{host_path}:/src:ro',
'-v', f'{volume.docker_volume_name}:/dst',
cls.HELPER_IMAGE, 'sh', '-c', 'cp -a /src/. /dst/'],
- capture_output=True, text=True, timeout=600,
- )
+ timeout=600)
except Exception as e: # pragma: no cover - best-effort/defensive
logger.warning('Bind-mount data copy failed for %s: %s', volume.docker_volume_name, e)
return volume
diff --git a/backend/app/services/yara_scan_service.py b/backend/app/services/yara_scan_service.py
index 0127e64d..e0064e1c 100644
--- a/backend/app/services/yara_scan_service.py
+++ b/backend/app/services/yara_scan_service.py
@@ -22,6 +22,8 @@ Result shape (both engines):
import os
import re
import subprocess
+
+from app.utils.system import run_checked
import logging
from typing import Dict, List, Optional
@@ -228,18 +230,15 @@ class YaraScanService:
def _scan_with_cli(cls, path: str) -> List[Dict]:
findings = []
for rules_file in cls._rule_files():
- try:
- result = subprocess.run(
- ['yara', '-r', '-s', '-w', rules_file, path],
- capture_output=True, text=True, timeout=1800,
- )
- except (subprocess.TimeoutExpired, OSError) as exc:
- logger.warning('yara run failed for %s: %s', rules_file, exc)
+ result = run_checked(['yara', '-r', '-s', '-w', rules_file, path],
+ timeout=1800)
+ if result['returncode'] is None:
+ logger.warning('yara run failed for %s: %s', rules_file, result['error'])
continue
- if result.returncode not in (0, 1):
- logger.warning('yara error on %s: %s', rules_file, (result.stderr or '')[:300])
+ if result['returncode'] not in (0, 1):
+ logger.warning('yara error on %s: %s', rules_file, result['error'][:300])
continue
- findings.extend(cls._parse_cli_output(result.stdout or ''))
+ findings.extend(cls._parse_cli_output(result['output']))
return findings
@classmethod
diff --git a/backend/app/utils/system.py b/backend/app/utils/system.py
index 9d944198..df07de16 100644
--- a/backend/app/utils/system.py
+++ b/backend/app/utils/system.py
@@ -35,6 +35,24 @@ def privileged_cmd(cmd: Union[List[str], str], *, user: Optional[str] = None) ->
Pass *user* to run the command as a specific user (``sudo -u <user>``).
+ .. warning::
+
+ *user* is only correct for privilege ESCALATION, not for identity
+ SWITCHING. When :func:`_needs_sudo` is False — most importantly when the
+ process is already root — the whole ``sudo -n -u <user>`` prefix is
+ dropped and the command runs as the current user, not as *user*.
+
+ That is right for "give me root, I am not root"; it is wrong for
+ "run this as postgres". ``database_service`` needs the second, because
+ PostgreSQL's local peer auth maps the OS user to the DB role, so a root
+ panel silently connecting as role ``root`` fails authentication. Its
+ five ``psql``/``pg_dump`` sites therefore spell ``sudo -u postgres``
+ out themselves and go through :func:`run_checked` unprivileged.
+
+ *user* currently has no callers. Before giving it one, decide what
+ should happen as root — ``runuser``/``su`` rather than dropping the
+ switch — and test it on a root install.
+
``sudo -n`` (non-interactive) is always used: nothing here runs attached to a
human terminal, and callers capture output, so a password prompt would be
invisible AND unanswerable — sudo would simply block forever, hanging the
@@ -200,8 +218,11 @@ def run_checked(cmd: Union[List[str], str], *, privileged: bool = False,
# capture_output and an explicit stderr= are mutually exclusive.
kwargs.setdefault('stdout', subprocess.PIPE)
kwargs.setdefault('stderr', subprocess.STDOUT)
- else:
+ elif 'stdout' not in kwargs and 'stderr' not in kwargs:
kwargs.setdefault('capture_output', True)
+ # else: the caller is directing the streams itself (writing straight to a
+ # log file, say). Adding capture_output on top would raise ValueError, and
+ # silently overriding their redirect would throw the output away.
kwargs.setdefault('text', True)
try:
diff --git a/backend/tests/BASELINE_COUNT b/backend/tests/BASELINE_COUNT
index 7193414d..31e10a3b 100644
--- a/backend/tests/BASELINE_COUNT
+++ b/backend/tests/BASELINE_COUNT
@@ -1 +1 @@
-4097
+4116
diff --git a/backend/tests/RAW_SUBPROCESS_CEILING b/backend/tests/RAW_SUBPROCESS_CEILING
index dba40afc..9902f178 100644
--- a/backend/tests/RAW_SUBPROCESS_CEILING
+++ b/backend/tests/RAW_SUBPROCESS_CEILING
@@ -1 +1 @@
-211
+28
diff --git a/backend/tests/api_controller_boundary_baseline.json b/backend/tests/api_controller_boundary_baseline.json
index 5eedd10b..316bc933 100644
--- a/backend/tests/api_controller_boundary_baseline.json
+++ b/backend/tests/api_controller_boundary_baseline.json
@@ -3,8 +3,7 @@
"policy": "Exact reviewed inventory of direct persistence, subprocess, and filesystem mutation calls in Flask API controllers. New calls are forbidden; remove entries as controllers delegate to services.",
"counts": {
"filesystem": 11,
- "persistence": 595,
- "subprocess": 6
+ "persistence": 595
},
"owners": {
"api/admin.py": {
@@ -602,12 +601,6 @@
"api/docker.py::cleanup_all_apps::persistence::db.session.rollback()::1",
"api/docker.py::create_docker_app::persistence::db.session.add(app)::1",
"api/docker.py::create_docker_app::persistence::db.session.commit()::1",
- "api/docker.py::docker_cleanup::subprocess::subprocess.run(['docker', 'image', 'prune', '-f'], capture_output=True, text=True)::1",
- "api/docker.py::docker_cleanup::subprocess::subprocess.run(['docker', 'network', 'prune', '-f'], capture_output=True, text=True)::1",
- "api/docker.py::docker_cleanup::subprocess::subprocess.run(['docker', 'ps', '-a', '--format', '{{.ID}} {{.Names}}'], capture_output=True, text=True)::1",
- "api/docker.py::docker_cleanup::subprocess::subprocess.run(['docker', 'ps', '-a', '-q', '-f', 'status=exited'], capture_output=True, text=True)::1",
- "api/docker.py::docker_cleanup::subprocess::subprocess.run(['docker', 'rm'] + stopped_ids, capture_output=True, text=True)::1",
- "api/docker.py::docker_cleanup::subprocess::subprocess.run(['docker', 'volume', 'prune', '-f'], capture_output=True, text=True)::1",
"api/domains.py::create_domain::persistence::Application.query_active().filter_by(id=application_id).first()::1",
"api/domains.py::create_domain::persistence::Domain.query_active().filter_by(application_id=application_id, is_primary=True).update({'is_primary': False}, synchronize_session=False)::1",
"api/domains.py::create_domain::persistence::Domain.query_active().filter_by(name=name).first()::1",
diff --git a/backend/tests/test_database_service_doors.py b/backend/tests/test_database_service_doors.py
new file mode 100644
index 00000000..630de358
--- /dev/null
+++ b/backend/tests/test_database_service_doors.py
@@ -0,0 +1,155 @@
+"""database_service's subprocess surface after the §G1 migration.
+
+24 raw call sites -> 8, all eight of the survivors being Popen PIPELINES
+(mysqldump|gzip, gunzip|mysql, pg_dump|gzip, gunzip|psql). run_checked runs one
+process; a pipe is two.
+
+The assertions that matter here are the ones guarding against a *plausible*
+future edit rather than a past bug — chiefly the `sudo -u postgres` argv, which
+looks exactly like the hardcoded-sudo drift §G2 removed and is not.
+"""
+
+import subprocess
+
+from app.services.database_service import DatabaseService
+
+
+class TestPostgresIdentitySwitching:
+ """`sudo -u postgres` must survive, and it is NOT the §G2 drift pattern.
+
+ §G2 removed hardcoded `sudo` because privilege escalation belongs to
+ _needs_sudo(), which correctly skips sudo when already root. That reasoning
+ does not transfer here: this sudo switches IDENTITY. PostgreSQL's local
+ peer auth maps the OS user to the DB role, so a panel running as root that
+ dropped the `-u postgres` would connect as role "root" and fail auth.
+
+ run_privileged(user='postgres') cannot express it — privileged_cmd drops
+ the -u along with the sudo whenever _needs_sudo() is False.
+ """
+
+ def test_pg_execute_runs_as_the_postgres_user(self, fake_subprocess):
+ fake_subprocess.script(['sudo'], stdout='1\n')
+ DatabaseService.pg_execute('SELECT 1;')
+ argv = fake_subprocess.argv_for(['sudo'])
+ assert argv[:4] == ['sudo', '-u', 'postgres', 'psql']
+
+ def test_pg_execute_query_runs_as_the_postgres_user(self, fake_subprocess):
+ fake_subprocess.script(['sudo'], stdout='c\nv\n')
+ DatabaseService.pg_execute_query('mydb', 'SELECT 1;')
+ assert fake_subprocess.argv_for(['sudo'])[:3] == ['sudo', '-u', 'postgres']
+
+ def test_the_sudo_is_not_stripped_by_the_privileged_helper(self, fake_subprocess):
+ """run_checked must pass this argv through untouched: privileged=False,
+ so nothing re-decides the sudo."""
+ fake_subprocess.script(['sudo'])
+ DatabaseService.pg_execute('SELECT 1;')
+ argv = fake_subprocess.argv_for(['sudo'])
+ assert argv.count('sudo') == 1 # no `sudo sudo`
+ assert '-n' not in argv[:3] # not rewritten into sudo -n
+
+
+class TestSecretsStayOffTheArgv:
+ def test_mysql_password_goes_in_the_environment(self, fake_subprocess):
+ fake_subprocess.script(['mysql'], stdout='')
+ DatabaseService.mysql_execute('SELECT 1;', root_password='s3cret')
+ argv = fake_subprocess.argv_for(['mysql'])
+ assert 's3cret' not in argv
+ env = fake_subprocess.kwargs_for(['mysql'])['env']
+ assert env['MYSQL_PWD'] == 's3cret'
+
+ def test_no_password_means_no_env_override(self, fake_subprocess):
+ """_mysql_env returns None, and run_checked then does not pass `env` at
+ all — so the child inherits the parent environment exactly as it did
+ before the helper existed, rather than a copy."""
+ fake_subprocess.script(['mysql'], stdout='')
+ DatabaseService.mysql_execute('SELECT 1;')
+ assert 'env' not in fake_subprocess.kwargs_for(['mysql'])
+
+
+class TestInstalledProbes:
+ def test_missing_client_reads_as_not_installed(self, fake_subprocess):
+ fake_subprocess.script(['mysql'], raises=FileNotFoundError)
+ assert DatabaseService.mysql_is_installed() is False
+
+ def test_present_client_reads_as_installed(self, fake_subprocess):
+ fake_subprocess.script(['mysql'], stdout='mysql Ver 8.0\n')
+ assert DatabaseService.mysql_is_installed() is True
+
+ def test_a_failing_client_is_not_installed(self, fake_subprocess):
+ fake_subprocess.script(['psql'], returncode=1, stderr='boom')
+ assert DatabaseService.pg_is_installed() is False
+
+
+class TestQueryTimeouts:
+ """The explorer's bound is the one thing here that was already explicit."""
+
+ def test_mysql_query_keeps_its_caller_supplied_timeout(self, fake_subprocess):
+ fake_subprocess.script(['mysql'], stdout='')
+ DatabaseService.mysql_execute_query('db', 'SELECT 1', timeout=7)
+ assert fake_subprocess.kwargs_for(['mysql'])['timeout'] == 7
+
+ def test_docker_exec_keeps_its_timeout(self, fake_subprocess, monkeypatch):
+ # Pin the client so the only docker call is the query itself — the
+ # client probe runs first and carries its own 10s bound.
+ monkeypatch.setattr(DatabaseService, '_docker_db_client',
+ staticmethod(lambda name: 'mysql'))
+ fake_subprocess.script(['docker'], stdout='')
+ DatabaseService.docker_mysql_execute('c1', 'SELECT 1', timeout=11)
+ assert fake_subprocess.kwargs_for(['docker'])['timeout'] == 11
+
+ def test_the_client_probe_carries_its_own_short_bound(self, fake_subprocess,
+ monkeypatch):
+ monkeypatch.setattr(DatabaseService, '_docker_client_cache', {})
+ fake_subprocess.script(['docker'], stdout='mariadb Ver 11.4')
+ DatabaseService._docker_db_client('c1')
+ assert fake_subprocess.kwargs_for(['docker'])['timeout'] == 10
+
+ def test_a_timed_out_query_reports_the_limit(self, fake_subprocess):
+ """The hand-rolled handler said only "Query timed out"; the shared door
+ names the bound, which is the difference between "your query is slow"
+ and "something is wedged"."""
+ fake_subprocess.script(['docker'],
+ raises=subprocess.TimeoutExpired('docker', 11))
+ result = DatabaseService.docker_mysql_execute('c1', 'SELECT 1', timeout=11)
+ assert result['success'] is False
+ assert '11' in result['error']
+
+ def test_unbounded_admin_calls_stay_unbounded(self, fake_subprocess):
+ """mysql_execute has no timeout today; inheriting run_checked's 60s
+ would cap a legitimately slow ALTER TABLE."""
+ fake_subprocess.script(['mysql'], stdout='')
+ DatabaseService.mysql_execute('ALTER TABLE big ADD COLUMN x INT;')
+ assert fake_subprocess.kwargs_for(['mysql'])['timeout'] is None
+
+
+class TestDockerClientProbe:
+ """MariaDB 11 dropped the `mysql` symlink; the probe picks what is there."""
+
+ def test_prefers_mariadb_when_present(self, fake_subprocess, monkeypatch):
+ monkeypatch.setattr(DatabaseService, '_docker_client_cache', {})
+ fake_subprocess.script(['docker'], stdout='mariadb Ver 11.4\n')
+ assert DatabaseService._docker_db_client('c1') == 'mariadb'
+
+ def test_falls_back_to_mysql_when_neither_answers(self, fake_subprocess, monkeypatch):
+ monkeypatch.setattr(DatabaseService, '_docker_client_cache', {})
+ fake_subprocess.script(['docker'], returncode=127, stderr='not found')
+ assert DatabaseService._docker_db_client('c1') == 'mysql'
+
+ def test_a_broken_docker_does_not_raise_out_of_the_probe(self, fake_subprocess,
+ monkeypatch):
+ monkeypatch.setattr(DatabaseService, '_docker_client_cache', {})
+ fake_subprocess.script(['docker'], raises=FileNotFoundError)
+ assert DatabaseService._docker_db_client('c1') == 'mysql'
+
+
+class TestPipelinesStayRaw:
+ def test_the_only_remaining_raw_calls_are_pipelines(self):
+ """A pipe is two processes; run_checked runs one. If this count moves,
+ either a pipeline was migrated (it cannot be) or a new raw call landed.
+ """
+ import sys
+ sys.path.insert(0, 'tests') if 'tests' not in sys.path else None
+ from raw_subprocess_census import count_file
+
+ hits = count_file('app/services/database_service.py')
+ assert [name for _, name in hits] == ['subprocess.Popen'] * 8
diff --git a/backend/tests/test_utils_system.py b/backend/tests/test_utils_system.py
index 7c959937..d7eb5dd2 100644
--- a/backend/tests/test_utils_system.py
+++ b/backend/tests/test_utils_system.py
@@ -766,3 +766,27 @@ class TestRunChecked:
mock_run.return_value = subprocess.CompletedProcess([], 0, stdout='', stderr='')
run_checked(['crontab', '-'], input='* * * * * true\n')
assert mock_run.call_args[1]['input'] == '* * * * * true\n'
+
+
+class TestPrivilegedCmdUserIsEscalationNotIdentity:
+ """The trap documented on privileged_cmd's `user=` parameter.
+
+ Asserting the CURRENT behaviour, not endorsing it: `user=` has no callers,
+ and the next person to reach for it needs to see that it does not do what
+ its name suggests when the process is already root.
+ """
+
+ @patch('app.utils.system.os.name', 'posix')
+ @patch('app.utils.system.shutil.which', return_value='/usr/bin/sudo')
+ @patch('app.utils.system.os.geteuid', return_value=1000, create=True)
+ def test_non_root_switches_user(self, _euid, _which):
+ assert privileged_cmd(['psql', '-c', 'x'], user='postgres') == [
+ 'sudo', '-n', '-u', 'postgres', 'psql', '-c', 'x']
+
+ @patch('app.utils.system.os.geteuid', return_value=0, create=True)
+ def test_as_root_the_user_switch_is_DROPPED(self, _euid):
+ """Not a typo in the test: running as root, `user=` vanishes entirely
+ and the command runs as root. For postgres that means connecting as DB
+ role "root" under peer auth — which is why database_service spells
+ `sudo -u postgres` out itself instead of using this."""
+ assert privileged_cmd(['psql', '-c', 'x'], user='postgres') == ['psql', '-c', 'x']
diff --git a/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning_service.py b/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning_service.py
index 4ab68edb..7db70c55 100644
--- a/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning_service.py
+++ b/builtin-extensions/serverkit-cloud-provision/backend/cloud_provisioning_service.py
@@ -535,14 +535,14 @@ class CloudProvisioningService:
@staticmethod
def _install_agent(server):
"""SSH into server and run the ServerKit agent install script."""
- import subprocess
+ from app.utils.system import run_checked
if not server.ip_address:
raise ValueError('Server has no IP address')
# Use ssh with strict host key checking disabled for fresh servers
install_cmd = 'curl -fsSL https://get.serverkit.dev/agent.sh | bash'
- result = subprocess.run(
+ result = run_checked(
['ssh', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=30',
f'root@{server.ip_address}', install_cmd],
- capture_output=True, text=True, timeout=120)
- if result.returncode != 0:
- raise RuntimeError(f'Agent install failed: {result.stderr}')
+ timeout=120)
+ if not result['success']:
+ raise RuntimeError(f"Agent install failed: {result['error']}")
diff --git a/builtin-extensions/serverkit-email/backend/dkim_service.py b/builtin-extensions/serverkit-email/backend/dkim_service.py
index d705a7f8..e79ca0d8 100644
--- a/builtin-extensions/serverkit-email/backend/dkim_service.py
+++ b/builtin-extensions/serverkit-email/backend/dkim_service.py
@@ -4,7 +4,8 @@ import re
import subprocess
from typing import Dict
-from app.utils.system import PackageManager, ServiceControl, run_privileged
+from app.utils.system import (PackageManager, ServiceControl, run_checked,
+ run_privileged)
class DKIMService:
@@ -54,15 +55,15 @@ localhost
enabled = False
version = None
try:
- result = subprocess.run(['which', 'opendkim'], capture_output=True, text=True)
- installed = result.returncode == 0
+ result = run_checked(['which', 'opendkim'], timeout=None)
+ installed = result['success']
if not installed:
installed = PackageManager.is_installed('opendkim')
if installed:
running = ServiceControl.is_active('opendkim')
enabled = ServiceControl.is_enabled('opendkim')
- result = subprocess.run(['opendkim', '-V'], capture_output=True, text=True, stderr=subprocess.STDOUT)
- match = re.search(r'OpenDKIM\s+Filter\s+v(\S+)', result.stdout)
+ result = run_checked(['opendkim', '-V'], timeout=None, merge_stderr=True)
+ match = re.search(r'OpenDKIM\s+Filter\s+v(\S+)', result['output'])
if match:
version = match.group(1)
except (subprocess.SubprocessError, FileNotFoundError):
diff --git a/builtin-extensions/serverkit-email/backend/email_service.py b/builtin-extensions/serverkit-email/backend/email_service.py
index a05b40b7..fdadf70a 100644
--- a/builtin-extensions/serverkit-email/backend/email_service.py
+++ b/builtin-extensions/serverkit-email/backend/email_service.py
@@ -6,7 +6,7 @@ from typing import Dict, List, Optional
from app import db
from app.models.email import EmailDomain, EmailAccount, EmailAlias, EmailForwardingRule
from app.services.postfix_service import PostfixService
-from app.utils.system import PackageManager, ServiceControl
+from app.utils.system import PackageManager, ServiceControl, run_checked
from .dovecot_service import DovecotService
from .dkim_service import DKIMService
from .spamassassin_service import SpamAssassinService
@@ -229,21 +229,21 @@ class EmailService:
# Check MX record
try:
- result = subprocess.run(['dig', '+short', 'MX', name], capture_output=True, text=True, timeout=10)
+ result = run_checked(['dig', '+short', 'MX', name], timeout=10)
results['mx'] = {
- 'found': bool(result.stdout.strip()),
- 'value': result.stdout.strip() or None,
+ 'found': bool(result['output'].strip()),
+ 'value': result['output'].strip() or None,
}
except Exception:
results['mx'] = {'found': False, 'error': 'DNS lookup failed'}
# Check SPF record
try:
- result = subprocess.run(['dig', '+short', 'TXT', name], capture_output=True, text=True, timeout=10)
- spf_found = 'v=spf1' in (result.stdout or '')
+ result = run_checked(['dig', '+short', 'TXT', name], timeout=10)
+ spf_found = 'v=spf1' in result['output']
results['spf'] = {
'found': spf_found,
- 'value': result.stdout.strip() or None,
+ 'value': result['output'].strip() or None,
}
except Exception:
results['spf'] = {'found': False, 'error': 'DNS lookup failed'}
@@ -252,11 +252,11 @@ class EmailService:
selector = domain.dkim_selector or 'default'
try:
dkim_name = f'{selector}._domainkey.{name}'
- result = subprocess.run(['dig', '+short', 'TXT', dkim_name], capture_output=True, text=True, timeout=10)
- dkim_found = 'v=DKIM1' in (result.stdout or '')
+ result = run_checked(['dig', '+short', 'TXT', dkim_name], timeout=10)
+ dkim_found = 'v=DKIM1' in result['output']
results['dkim'] = {
'found': dkim_found,
- 'value': result.stdout.strip() or None,
+ 'value': result['output'].strip() or None,
'name': dkim_name,
}
except Exception:
@@ -265,11 +265,11 @@ class EmailService:
# Check DMARC record
try:
dmarc_name = f'_dmarc.{name}'
- result = subprocess.run(['dig', '+short', 'TXT', dmarc_name], capture_output=True, text=True, timeout=10)
- dmarc_found = 'v=DMARC1' in (result.stdout or '')
+ result = run_checked(['dig', '+short', 'TXT', dmarc_name], timeout=10)
+ dmarc_found = 'v=DMARC1' in result['output']
results['dmarc'] = {
'found': dmarc_found,
- 'value': result.stdout.strip() or None,
+ 'value': result['output'].strip() or None,
}
except Exception:
results['dmarc'] = {'found': False, 'error': 'DNS lookup failed'}
diff --git a/builtin-extensions/serverkit-email/backend/roundcube_service.py b/builtin-extensions/serverkit-email/backend/roundcube_service.py
index 85c93253..fb0f2d6f 100644
--- a/builtin-extensions/serverkit-email/backend/roundcube_service.py
+++ b/builtin-extensions/serverkit-email/backend/roundcube_service.py
@@ -2,7 +2,7 @@
import subprocess
from typing import Dict
-from app.utils.system import run_privileged, is_command_available
+from app.utils.system import run_checked, run_privileged, is_command_available
class RoundcubeService:
@@ -20,14 +20,13 @@ class RoundcubeService:
return {'installed': False, 'running': False, 'error': 'Docker not available'}
try:
- result = subprocess.run(
+ result = run_checked(
['docker', 'inspect', '--format', '{{.State.Status}}', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode != 0:
+ timeout=None)
+ if not result['success']:
return {'installed': False, 'running': False}
- status = result.stdout.strip()
+ status = result['output'].strip()
return {
'installed': True,
'running': status == 'running',
@@ -47,19 +46,13 @@ class RoundcubeService:
try:
# Remove existing container if any
- subprocess.run(
- ['docker', 'rm', '-f', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
+ run_checked(['docker', 'rm', '-f', cls.CONTAINER_NAME], timeout=None)
# Create volume
- subprocess.run(
- ['docker', 'volume', 'create', cls.VOLUME_NAME],
- capture_output=True, text=True,
- )
+ run_checked(['docker', 'volume', 'create', cls.VOLUME_NAME], timeout=None)
# Run container
- result = subprocess.run([
+ result = run_checked([
'docker', 'run', '-d',
'--name', cls.CONTAINER_NAME,
'--restart', 'unless-stopped',
@@ -73,10 +66,11 @@ class RoundcubeService:
'-e', 'ROUNDCUBEMAIL_SKIN=elastic',
'-v', f'{cls.VOLUME_NAME}:/var/roundcube/db',
cls.IMAGE,
- ], capture_output=True, text=True)
+ ], timeout=None)
- if result.returncode != 0:
- return {'success': False, 'error': result.stderr or 'Failed to start container'}
+ if not result['success']:
+ return {'success': False,
+ 'error': result['error'] or 'Failed to start container'}
# Build a public URL: prefer a supplied domain, then auto-generate
# webmail.<panel_domain>, and only fall back to localhost:port.
@@ -120,7 +114,7 @@ class RoundcubeService:
def uninstall(cls) -> Dict:
"""Stop and remove Roundcube container."""
try:
- subprocess.run(['docker', 'rm', '-f', cls.CONTAINER_NAME], capture_output=True, text=True)
+ run_checked(['docker', 'rm', '-f', cls.CONTAINER_NAME], timeout=None)
return {'success': True, 'message': 'Roundcube uninstalled'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -129,13 +123,10 @@ class RoundcubeService:
def start(cls) -> Dict:
"""Start Roundcube container."""
try:
- result = subprocess.run(
- ['docker', 'start', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'start', cls.CONTAINER_NAME], timeout=None)
+ if result['success']:
return {'success': True, 'message': 'Roundcube started'}
- return {'success': False, 'error': result.stderr or 'Start failed'}
+ return {'success': False, 'error': result['error'] or 'Start failed'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -143,13 +134,10 @@ class RoundcubeService:
def stop(cls) -> Dict:
"""Stop Roundcube container."""
try:
- result = subprocess.run(
- ['docker', 'stop', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'stop', cls.CONTAINER_NAME], timeout=None)
+ if result['success']:
return {'success': True, 'message': 'Roundcube stopped'}
- return {'success': False, 'error': result.stderr or 'Stop failed'}
+ return {'success': False, 'error': result['error'] or 'Stop failed'}
except Exception as e:
return {'success': False, 'error': str(e)}
@@ -157,13 +145,10 @@ class RoundcubeService:
def restart(cls) -> Dict:
"""Restart Roundcube container."""
try:
- result = subprocess.run(
- ['docker', 'restart', cls.CONTAINER_NAME],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
+ result = run_checked(['docker', 'restart', cls.CONTAINER_NAME], timeout=None)
+ if result['success']:
return {'success': True, 'message': 'Roundcube restarted'}
- return {'success': False, 'error': result.stderr or 'Restart failed'}
+ return {'success': False, 'error': result['error'] or 'Restart failed'}
except Exception as e:
return {'success': False, 'error': str(e)}
diff --git a/builtin-extensions/serverkit-email/backend/spamassassin_service.py b/builtin-extensions/serverkit-email/backend/spamassassin_service.py
index 1418069d..85516e42 100644
--- a/builtin-extensions/serverkit-email/backend/spamassassin_service.py
+++ b/builtin-extensions/serverkit-email/backend/spamassassin_service.py
@@ -4,7 +4,8 @@ import re
import subprocess
from typing import Dict
-from app.utils.system import PackageManager, ServiceControl, run_privileged
+from app.utils.system import (PackageManager, ServiceControl, run_checked,
+ run_privileged)
class SpamAssassinService:
@@ -58,8 +59,8 @@ internal_networks 127.0.0.0/8
if installed:
running = ServiceControl.is_active('spamassassin')
enabled = ServiceControl.is_enabled('spamassassin')
- result = subprocess.run(['spamassassin', '--version'], capture_output=True, text=True)
- match = re.search(r'version\s+(\S+)', result.stdout)
+ result = run_checked(['spamassassin', '--version'], timeout=None)
+ match = re.search(r'version\s+(\S+)', result['output'])
if match:
version = match.group(1)
diff --git a/builtin-extensions/serverkit-ftp/backend/ftp_service.py b/builtin-extensions/serverkit-ftp/backend/ftp_service.py
index 3cdac0b1..8770038b 100644
--- a/builtin-extensions/serverkit-ftp/backend/ftp_service.py
+++ b/builtin-extensions/serverkit-ftp/backend/ftp_service.py
@@ -5,7 +5,8 @@ import subprocess
import re
from app.utils.formatting import format_bytes
-from app.utils.system import PackageManager, ServiceControl, run_privileged, privileged_cmd
+from app.utils.system import (PackageManager, ServiceControl, privileged_cmd,
+ run_checked, run_privileged)
try:
import pwd
except ImportError:
@@ -65,11 +66,8 @@ class FTPService:
try:
# Check if installed via which or package manager
- result = subprocess.run(
- ['which', service],
- capture_output=True, text=True
- )
- installed = result.returncode == 0
+ result = run_checked(['which', service], timeout=None)
+ installed = result['success']
if not installed:
installed = PackageManager.is_installed(service)
@@ -80,19 +78,14 @@ class FTPService:
# Get version
if service == 'vsftpd':
- result = subprocess.run(
- ['vsftpd', '-v'],
- capture_output=True, text=True, stderr=subprocess.STDOUT
- )
- version_match = re.search(r'version (\d+\.\d+\.\d+)', result.stdout)
+ result = run_checked(['vsftpd', '-v'], timeout=None,
+ merge_stderr=True)
+ version_match = re.search(r'version (\d+\.\d+\.\d+)', result['output'])
if version_match:
version = version_match.group(1)
elif service == 'proftpd':
- result = subprocess.run(
- ['proftpd', '-v'],
- capture_output=True, text=True
- )
- version_match = re.search(r'ProFTPD Version (\d+\.\d+\.\d+)', result.stdout)
+ result = run_checked(['proftpd', '-v'], timeout=None)
+ version_match = re.search(r'ProFTPD Version (\d+\.\d+\.\d+)', result['output'])
if version_match:
version = version_match.group(1)
@@ -493,12 +486,11 @@ class FTPService:
connections = []
# Check for vsftpd connections
- result = subprocess.run(
+ result = run_checked(
['ss', '-tnp', 'state', 'established', 'sport', '=', ':21'],
- capture_output=True, text=True
- )
+ timeout=None)
- for line in result.stdout.strip().split('\n')[1:]: # Skip header
+ for line in result['output'].strip().split('\n')[1:]: # Skip header
if line:
parts = line.split()
if len(parts) >= 5:
@@ -528,12 +520,10 @@ class FTPService:
for log_file in log_files:
if os.path.exists(log_file):
try:
- result = subprocess.run(
- ['tail', '-n', str(lines), log_file],
- capture_output=True, text=True
- )
+ result = run_checked(['tail', '-n', str(lines), log_file],
+ timeout=None)
# Filter FTP-related entries if using syslog
- log_content = result.stdout
+ log_content = result['output']
if 'syslog' in log_file:
log_content = '\n'.join(
line for line in log_content.split('\n')