ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
refactor(services): one CompletedProcess -> dict translation (F5)
Juan Denis775bf4bd29dc
diff --git a/backend/app/services/nginx_service.py b/backend/app/services/nginx_service.pyindex b9d0006a..55d58274 100644--- a/backend/app/services/nginx_service.py+++ b/backend/app/services/nginx_service.py@@ -424,10 +424,8 @@ location /p/ {{try:result = ServiceControl.restart('nginx', timeout=30)- return {- 'success': result.returncode == 0,- 'message': 'Nginx restarted successfully' if result.returncode == 0 else result.stderr- }+ return ServiceControl.result_dict(+ result, 'Nginx restarted successfully', error_key='message')except Exception as e:return {'success': False, 'error': str(e)}diff --git a/backend/app/services/php_service.py b/backend/app/services/php_service.pyindex 2351a30b..15bebb5e 100644--- a/backend/app/services/php_service.py+++ b/backend/app/services/php_service.py@@ -363,11 +363,8 @@ env[TEMP] = /tmptry:result = ServiceControl.restart(service, timeout=30)-- return {- 'success': result.returncode == 0,- 'message': f'{service} restarted' if result.returncode == 0 else result.stderr- }+ return ServiceControl.result_dict(result, f'{service} restarted',+ error_key='message')except Exception as e:return {'success': False, 'error': str(e)}@@ -378,11 +375,8 @@ env[TEMP] = /tmptry:result = ServiceControl.reload(service, timeout=30)-- return {- 'success': result.returncode == 0,- 'message': f'{service} reloaded' if result.returncode == 0 else result.stderr- }+ return ServiceControl.result_dict(result, f'{service} reloaded',+ error_key='message')except Exception as e:return {'success': False, 'error': str(e)}diff --git a/backend/app/services/postfix_service.py b/backend/app/services/postfix_service.pyindex a90c9fe9..4bce68d5 100644--- a/backend/app/services/postfix_service.py+++ b/backend/app/services/postfix_service.py@@ -389,9 +389,8 @@ mailbox_size_limit = 0"""Restart Postfix."""try:result = ServiceControl.restart('postfix', timeout=30)- if result.returncode == 0:- return {'success': True, 'message': 'Postfix restarted'}- return {'success': False, 'error': result.stderr or 'Restart failed'}+ return ServiceControl.result_dict(result, 'Postfix restarted',+ fallback='Restart failed')except Exception as e:return {'success': False, 'error': str(e)}diff --git a/backend/app/utils/system.py b/backend/app/utils/system.pyindex 581afb3e..903c5080 100644--- a/backend/app/utils/system.py+++ b/backend/app/utils/system.py@@ -319,6 +319,20 @@ class ServiceControl:def daemon_reload(**kwargs) -> subprocess.CompletedProcess:return run_privileged(['systemctl', 'daemon-reload'], **kwargs)+ @staticmethod+ def result_dict(proc, ok_message, error_key='error',+ fallback='Operation failed'):+ """CompletedProcess → the service-dict shape every *Service returns.++ The translation was re-implemented per service with subtly divergent+ failure shapes ('error' vs 'message' carrying stderr); keep the key+ and fallback parametrized so converting a site is parity, not a+ contract change (plan 75 §F5).+ """+ if proc.returncode == 0:+ return {'success': True, 'message': ok_message}+ return {'success': False, error_key: proc.stderr or fallback}+@staticmethoddef is_active(service: str) -> bool:"""Return ``True`` when the service is active. No sudo needed.diff --git a/backend/tests/test_nginx_health_and_service_status.py b/backend/tests/test_nginx_health_and_service_status.pyindex af00db08..c84dca14 100644--- a/backend/tests/test_nginx_health_and_service_status.py+++ b/backend/tests/test_nginx_health_and_service_status.py@@ -91,6 +91,11 @@ class TestRestartTestsConfigFirst:patch('app.services.nginx_service.ServiceControl') as control:control.restart.return_value = subprocess.CompletedProcess([], 0, stdout='', stderr='')+ # The whole class is mocked, so the result translation is too —+ # give it the real one's behavior (tested in test_utils_system).+ control.result_dict.side_effect = (+ lambda proc, msg, **kw: {'success': proc.returncode == 0,+ 'message': msg})result = NginxService.restart()assert result['success'] is Truediff --git a/backend/tests/test_utils_system.py b/backend/tests/test_utils_system.pyindex 85729e49..bbd69d80 100644--- a/backend/tests/test_utils_system.py+++ b/backend/tests/test_utils_system.py@@ -430,3 +430,27 @@ class TestServiceControl:@patch('app.utils.system.subprocess.run', side_effect=FileNotFoundError)def test_is_enabled_missing_systemctl(self, _run):assert ServiceControl.is_enabled('nginx') is False+++class TestResultDict:+ """The one CompletedProcess -> service-dict translation (plan 75 §F5)."""++ def test_success_shape(self):+ proc = subprocess.CompletedProcess([], 0, stdout='', stderr='')+ assert ServiceControl.result_dict(proc, 'Postfix restarted') == {+ 'success': True, 'message': 'Postfix restarted'}++ def test_failure_carries_stderr_under_the_error_key_by_default(self):+ proc = subprocess.CompletedProcess([], 1, stdout='', stderr='boom')+ assert ServiceControl.result_dict(proc, 'ok') == {+ 'success': False, 'error': 'boom'}++ def test_failure_key_is_parametrized_for_message_shaped_services(self):+ proc = subprocess.CompletedProcess([], 1, stdout='', stderr='boom')+ assert ServiceControl.result_dict(proc, 'ok', error_key='message') == {+ 'success': False, 'message': 'boom'}++ def test_empty_stderr_uses_the_fallback(self):+ proc = subprocess.CompletedProcess([], 1, stdout='', stderr='')+ assert ServiceControl.result_dict(proc, 'ok', fallback='Restart failed') == {+ 'success': False, 'error': 'Restart failed'}