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(agents): centralize remote command dispatch

Juan Denis899dac99b5b2
diff --git a/backend/app/services/remote_cloudflared_service.py b/backend/app/services/remote_cloudflared_service.py
index 67f08f31..d4ef2d0c 100644
--- a/backend/app/services/remote_cloudflared_service.py
+++ b/backend/app/services/remote_cloudflared_service.py
@@ -14,37 +14,26 @@ instead of letting actions fail with confusing errors.
from typing import Any, Dict, Optional
-from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
class RemoteCloudflaredService:
- @staticmethod
- def _send(server_id: str, action: str, params: Optional[Dict[str, Any]] = None,
- user_id: Optional[int] = None, timeout: float = 30.0) -> Dict[str, Any]:
- return agent_registry.send_command(
- server_id=server_id,
- action=action,
- params=params or {},
- user_id=user_id,
- timeout=timeout,
- )
-
@staticmethod
def status(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCloudflaredService._send(server_id, 'cloudflared:status',
- user_id=user_id, timeout=8.0)
+ return dispatch_agent_command(server_id, 'cloudflared:status',
+ user_id=user_id, timeout=8.0)
@staticmethod
def list_tunnels(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
# Tunnel list hits Cloudflare API server-side, so allow more time
# than for crontab parsing.
- return RemoteCloudflaredService._send(server_id, 'cloudflared:tunnel:list',
- user_id=user_id, timeout=30.0)
+ return dispatch_agent_command(server_id, 'cloudflared:tunnel:list',
+ user_id=user_id, timeout=30.0)
@staticmethod
def create_tunnel(server_id: str, name: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCloudflaredService._send(
+ return dispatch_agent_command(
server_id, 'cloudflared:tunnel:create',
params={'name': name},
user_id=user_id, timeout=30.0,
@@ -53,7 +42,7 @@ class RemoteCloudflaredService:
@staticmethod
def route_tunnel(server_id: str, tunnel_ref: str, hostname: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCloudflaredService._send(
+ return dispatch_agent_command(
server_id, 'cloudflared:tunnel:route',
params={'tunnel_ref': tunnel_ref, 'hostname': hostname},
user_id=user_id, timeout=30.0,
@@ -62,7 +51,7 @@ class RemoteCloudflaredService:
@staticmethod
def delete_tunnel(server_id: str, ref: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCloudflaredService._send(
+ return dispatch_agent_command(
server_id, 'cloudflared:tunnel:delete',
params={'ref': ref},
user_id=user_id, timeout=30.0,
@@ -72,7 +61,7 @@ class RemoteCloudflaredService:
def login(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
# Returns {job_id, channel} immediately; the panel subscribes to
# the matching Socket.IO room for the auth_url + completion event.
- return RemoteCloudflaredService._send(
+ return dispatch_agent_command(
server_id, 'cloudflared:login',
user_id=user_id, timeout=10.0,
)
diff --git a/backend/app/services/remote_command_dispatcher.py b/backend/app/services/remote_command_dispatcher.py
new file mode 100644
index 00000000..b8f6fbca
--- /dev/null
+++ b/backend/app/services/remote_command_dispatcher.py
@@ -0,0 +1,28 @@
+"""Shared command boundary for services backed by a ServerKit agent.
+
+Remote feature services describe domain verbs and their parameters. This
+module owns the transport-level defaults and normalization needed to send
+those verbs to an agent. Keeping that seam here prevents every remote
+service from growing its own subtly different ``agent_registry`` wrapper.
+"""
+
+from typing import Any, Dict, Mapping, Optional
+
+from app.services.agent_registry import agent_registry
+
+
+def dispatch_agent_command(
+ server_id: str,
+ action: str,
+ params: Optional[Mapping[str, Any]] = None,
+ user_id: Optional[int] = None,
+ timeout: float = 30.0,
+) -> Dict[str, Any]:
+ """Dispatch one typed agent verb through the shared registry transport."""
+ return agent_registry.send_command(
+ server_id=server_id,
+ action=action,
+ params=dict(params or {}),
+ user_id=user_id,
+ timeout=timeout,
+ )
diff --git a/backend/app/services/remote_cron_service.py b/backend/app/services/remote_cron_service.py
index cfbd82ea..782d9cfe 100644
--- a/backend/app/services/remote_cron_service.py
+++ b/backend/app/services/remote_cron_service.py
@@ -27,38 +27,25 @@ round-trips so the UI can't display stale state.
from typing import Any, Dict, Optional
from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
class RemoteCronService:
"""Thin dispatcher to agent cron handlers."""
- @staticmethod
- def _send(server_id: str, action: str, params: Optional[Dict[str, Any]] = None,
- user_id: Optional[int] = None, timeout: float = 15.0) -> Dict[str, Any]:
- """Single point for cron:* dispatch. Catches the common offline
- case so the API layer can map it to a 503 without parsing
- free-form error strings."""
- return agent_registry.send_command(
- server_id=server_id,
- action=action,
- params=params or {},
- user_id=user_id,
- timeout=timeout,
- )
-
@staticmethod
def status(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCronService._send(server_id, 'cron:status', user_id=user_id, timeout=8.0)
+ return dispatch_agent_command(server_id, 'cron:status', user_id=user_id, timeout=8.0)
@staticmethod
def list_jobs(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCronService._send(server_id, 'cron:list', user_id=user_id, timeout=8.0)
+ return dispatch_agent_command(server_id, 'cron:list', user_id=user_id, timeout=8.0)
@staticmethod
def add_job(server_id: str, schedule: str, command: str,
name: Optional[str] = None, description: Optional[str] = None,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCronService._send(
+ return dispatch_agent_command(
server_id, 'cron:add',
params={
'schedule': schedule,
@@ -98,7 +85,7 @@ class RemoteCronService:
if name is not None:
params['name'] = name
- return RemoteCronService._send(
+ return dispatch_agent_command(
server_id, 'cron:update',
params=params,
user_id=user_id,
@@ -107,7 +94,7 @@ class RemoteCronService:
@staticmethod
def remove_job(server_id: str, job_id: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCronService._send(
+ return dispatch_agent_command(
server_id, 'cron:remove',
params={'id': job_id},
user_id=user_id,
@@ -116,7 +103,7 @@ class RemoteCronService:
@staticmethod
def toggle_job(server_id: str, job_id: str, enabled: bool,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteCronService._send(
+ return dispatch_agent_command(
server_id, 'cron:toggle',
params={'id': job_id, 'enabled': bool(enabled)},
user_id=user_id,
diff --git a/backend/app/services/remote_docker_service.py b/backend/app/services/remote_docker_service.py
index dfa02107..baa2ed8b 100644
--- a/backend/app/services/remote_docker_service.py
+++ b/backend/app/services/remote_docker_service.py
@@ -9,6 +9,7 @@ and returns the results.
from typing import List, Dict, Any
from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
from app.models.server import Server
from app.utils.formatting import format_bytes
@@ -46,7 +47,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:list',
params={'all': all},
@@ -64,7 +65,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:inspect',
params={'id': container_id},
@@ -82,7 +83,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:start',
params={'id': container_id},
@@ -104,7 +105,7 @@ class RemoteDockerService:
if timeout:
params['timeout'] = timeout
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:stop',
params=params,
@@ -126,7 +127,7 @@ class RemoteDockerService:
if timeout:
params['timeout'] = timeout
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:restart',
params=params,
@@ -150,7 +151,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:remove',
params={
@@ -172,7 +173,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:stats',
params={'id': container_id},
@@ -210,7 +211,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:container:logs',
params={
@@ -236,7 +237,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:image:list',
params={},
@@ -254,7 +255,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:image:pull',
params={'image': image},
@@ -273,7 +274,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:image:remove',
params={'id': image_id, 'force': force},
@@ -293,7 +294,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:volume:list',
params={},
@@ -311,7 +312,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:volume:remove',
params={'name': name, 'force': force},
@@ -331,7 +332,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:network:list',
params={},
@@ -349,7 +350,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:network:remove',
params={'id': network_id},
@@ -458,7 +459,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- result = agent_registry.send_command(
+ result = dispatch_agent_command(
server_id=server_id,
action='system:metrics',
params={},
@@ -480,7 +481,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='system:info',
params={},
@@ -582,7 +583,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:list',
params={},
@@ -610,7 +611,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:ps',
params={'project_path': project_path},
@@ -641,7 +642,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:up',
params={
@@ -677,7 +678,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:down',
params={
@@ -713,7 +714,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:logs',
params={
@@ -748,7 +749,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:restart',
params={
@@ -781,7 +782,7 @@ class RemoteDockerService:
except Exception as e:
return {'success': False, 'error': str(e)}
- return agent_registry.send_command(
+ return dispatch_agent_command(
server_id=server_id,
action='docker:compose:pull',
params={
diff --git a/backend/app/services/remote_file_service.py b/backend/app/services/remote_file_service.py
index f3fba763..71775463 100644
--- a/backend/app/services/remote_file_service.py
+++ b/backend/app/services/remote_file_service.py
@@ -20,26 +20,16 @@ second-guess it.
from typing import Any, Dict, Optional
from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
class RemoteFileService:
"""Thin dispatcher to agent file:* handlers."""
- @staticmethod
- def _send(server_id: str, action: str, params: Optional[Dict[str, Any]] = None,
- user_id: Optional[int] = None, timeout: float = 15.0) -> Dict[str, Any]:
- return agent_registry.send_command(
- server_id=server_id,
- action=action,
- params=params or {},
- user_id=user_id,
- timeout=timeout,
- )
-
@staticmethod
def list_directory(server_id: str, path: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteFileService._send(
+ return dispatch_agent_command(
server_id, 'file:list',
params={'path': path},
user_id=user_id,
@@ -48,7 +38,7 @@ class RemoteFileService:
@staticmethod
def read_file(server_id: str, path: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteFileService._send(
+ return dispatch_agent_command(
server_id, 'file:read',
params={'path': path},
user_id=user_id,
@@ -57,7 +47,7 @@ class RemoteFileService:
@staticmethod
def write_file(server_id: str, path: str, content: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteFileService._send(
+ return dispatch_agent_command(
server_id, 'file:write',
params={'path': path, 'content': content},
user_id=user_id,
diff --git a/backend/app/services/remote_packages_service.py b/backend/app/services/remote_packages_service.py
index 655ce7f7..4fa626ce 100644
--- a/backend/app/services/remote_packages_service.py
+++ b/backend/app/services/remote_packages_service.py
@@ -15,23 +15,12 @@ synchronous round-trips matching the cron service shape.
from typing import Any, Dict, Iterable, List, Optional
-from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
class RemotePackagesService:
"""Thin dispatcher to the agent's packages:* handlers."""
- @staticmethod
- def _send(server_id: str, action: str, params: Optional[Dict[str, Any]] = None,
- user_id: Optional[int] = None, timeout: float = 30.0) -> Dict[str, Any]:
- return agent_registry.send_command(
- server_id=server_id,
- action=action,
- params=params or {},
- user_id=user_id,
- timeout=timeout,
- )
-
# ---- read paths -------------------------------------------------
@staticmethod
@@ -39,14 +28,14 @@ class RemotePackagesService:
# The agent's list_installed shells `apt-get list --installed` /
# `dnf list installed` / etc., which can return ~250KB on a
# populated server. 30s budget is generous; usually subsecond.
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:list_installed', user_id=user_id, timeout=30.0,
)
@staticmethod
def search(server_id: str, query: str, limit: int = 100,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:search',
params={'query': query, 'limit': limit},
user_id=user_id,
@@ -55,7 +44,7 @@ class RemotePackagesService:
@staticmethod
def info(server_id: str, name: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:info',
params={'name': name},
user_id=user_id,
@@ -66,7 +55,7 @@ class RemotePackagesService:
@staticmethod
def update_cache(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:update_cache', user_id=user_id, timeout=180.0,
)
@@ -74,7 +63,7 @@ class RemotePackagesService:
def install(server_id: str, name: str, user_id: Optional[int] = None) -> Dict[str, Any]:
# Synchronous single-name install for callers that want a
# structured result (workflow_engine agent_command nodes).
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:install',
params={'name': name},
user_id=user_id,
@@ -85,7 +74,7 @@ class RemotePackagesService:
def install_async(server_id: str, names: Iterable[str],
user_id: Optional[int] = None) -> Dict[str, Any]:
# Streaming variant: returns {job_id, channel}.
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:install_async',
params={'names': list(names)},
user_id=user_id,
@@ -94,7 +83,7 @@ class RemotePackagesService:
@staticmethod
def remove(server_id: str, name: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:remove',
params={'name': name},
user_id=user_id,
@@ -109,7 +98,7 @@ class RemotePackagesService:
params['all'] = True
if names:
params['names'] = list(names)
- return RemotePackagesService._send(
+ return dispatch_agent_command(
server_id, 'packages:upgrade',
params=params,
user_id=user_id,
diff --git a/backend/app/services/remote_runtimes_service.py b/backend/app/services/remote_runtimes_service.py
index a283c159..c149fdeb 100644
--- a/backend/app/services/remote_runtimes_service.py
+++ b/backend/app/services/remote_runtimes_service.py
@@ -12,48 +12,37 @@ matching Socket.IO room for live build output.
from typing import Any, Dict, Optional
-from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
class RemoteRuntimesService:
"""Thin dispatcher to runtimes:* on a remote agent."""
- @staticmethod
- def _send(server_id: str, action: str, params: Optional[Dict[str, Any]] = None,
- user_id: Optional[int] = None, timeout: float = 30.0) -> Dict[str, Any]:
- return agent_registry.send_command(
- server_id=server_id,
- action=action,
- params=params or {},
- user_id=user_id,
- timeout=timeout,
- )
-
@staticmethod
def list_state(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(server_id, 'runtimes:list', user_id=user_id)
+ return dispatch_agent_command(server_id, 'runtimes:list', user_id=user_id)
@staticmethod
def python_installed(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:installed', user_id=user_id, timeout=15.0,
)
@staticmethod
def python_available(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:available', user_id=user_id, timeout=30.0,
)
@staticmethod
def python_current(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:current', user_id=user_id, timeout=15.0,
)
@staticmethod
def python_install(server_id: str, version: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:install',
params={'version': version},
user_id=user_id, timeout=10.0, # async — returns job_id quickly
@@ -61,7 +50,7 @@ class RemoteRuntimesService:
@staticmethod
def python_uninstall(server_id: str, version: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:uninstall',
params={'version': version},
user_id=user_id, timeout=120.0,
@@ -69,7 +58,7 @@ class RemoteRuntimesService:
@staticmethod
def python_set_global(server_id: str, version: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:set_global',
params={'version': version},
user_id=user_id, timeout=15.0,
@@ -78,7 +67,7 @@ class RemoteRuntimesService:
@staticmethod
def python_set_local(server_id: str, version: str, dir_path: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:python:set_local',
params={'version': version, 'dir': dir_path},
user_id=user_id, timeout=15.0,
@@ -86,7 +75,7 @@ class RemoteRuntimesService:
@staticmethod
def pyenv_bootstrap(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteRuntimesService._send(
+ return dispatch_agent_command(
server_id, 'runtimes:pyenv:bootstrap',
user_id=user_id, timeout=10.0, # async
)
diff --git a/backend/app/services/remote_systemd_service.py b/backend/app/services/remote_systemd_service.py
index 40091565..c58fa2f7 100644
--- a/backend/app/services/remote_systemd_service.py
+++ b/backend/app/services/remote_systemd_service.py
@@ -12,7 +12,7 @@ panel doesn't need to care which.
from typing import Any, Dict, Optional
-from app.services.agent_registry import agent_registry
+from app.services.remote_command_dispatcher import dispatch_agent_command
_VALID_ACTIONS = {'start', 'stop', 'restart', 'enable', 'disable'}
@@ -21,31 +21,20 @@ _VALID_ACTIONS = {'start', 'stop', 'restart', 'enable', 'disable'}
class RemoteSystemdService:
"""Thin dispatcher to systemd:* on a remote agent."""
- @staticmethod
- def _send(server_id: str, action: str, params: Optional[Dict[str, Any]] = None,
- user_id: Optional[int] = None, timeout: float = 30.0) -> Dict[str, Any]:
- return agent_registry.send_command(
- server_id=server_id,
- action=action,
- params=params or {},
- user_id=user_id,
- timeout=timeout,
- )
-
@staticmethod
def list_units(server_id: str, state: Optional[str] = None, type_: str = 'service',
user_id: Optional[int] = None) -> Dict[str, Any]:
params: Dict[str, Any] = {'type': type_}
if state:
params['state'] = state
- return RemoteSystemdService._send(
+ return dispatch_agent_command(
server_id, 'systemd:list_units', params=params,
user_id=user_id, timeout=30.0,
)
@staticmethod
def status(server_id: str, unit: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteSystemdService._send(
+ return dispatch_agent_command(
server_id, 'systemd:status', params={'unit': unit},
user_id=user_id, timeout=15.0,
)
@@ -57,14 +46,14 @@ class RemoteSystemdService:
return {'success': False, 'error': f'invalid action: {action}', 'code': 'INVALID_ACTION'}
# 2-minute timeout matches the agent's per-action ceiling for
# cold-starts on heavyweight units (postgresql etc.).
- return RemoteSystemdService._send(
+ return dispatch_agent_command(
server_id, f'systemd:{action}', params={'unit': unit},
user_id=user_id, timeout=130.0,
)
@staticmethod
def daemon_reload(server_id: str, user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteSystemdService._send(
+ return dispatch_agent_command(
server_id, 'systemd:daemon_reload',
user_id=user_id, timeout=310.0,
)
@@ -72,7 +61,7 @@ class RemoteSystemdService:
@staticmethod
def logs(server_id: str, unit: str, lines: int = 200,
user_id: Optional[int] = None) -> Dict[str, Any]:
- return RemoteSystemdService._send(
+ return dispatch_agent_command(
server_id, 'systemd:logs',
params={'unit': unit, 'lines': int(lines)},
user_id=user_id, timeout=30.0,
@@ -82,7 +71,7 @@ class RemoteSystemdService:
def logs_follow(server_id: str, unit: str,
user_id: Optional[int] = None) -> Dict[str, Any]:
# Returns {job_id, channel}; the panel re-broadcasts on Socket.IO.
- return RemoteSystemdService._send(
+ return dispatch_agent_command(
server_id, 'systemd:logs_follow',
params={'unit': unit},
user_id=user_id, timeout=10.0,
diff --git a/backend/tests/test_remote_command_dispatcher.py b/backend/tests/test_remote_command_dispatcher.py
new file mode 100644
index 00000000..97101d15
--- /dev/null
+++ b/backend/tests/test_remote_command_dispatcher.py
@@ -0,0 +1,84 @@
+"""Conformance tests for the shared remote-agent command boundary."""
+
+import ast
+from pathlib import Path
+
+from app.services import remote_command_dispatcher
+from app.services.remote_command_dispatcher import dispatch_agent_command
+
+
+SERVICES_DIR = Path(__file__).parents[1] / "app" / "services"
+REMOTE_SERVICE_FILES = tuple(sorted(SERVICES_DIR.glob("remote_*_service.py")))
+DISPATCHER_MODULE = "app.services.remote_command_dispatcher"
+
+
+def test_dispatcher_normalizes_transport_arguments(monkeypatch):
+ captured = {}
+
+ def fake_send_command(**kwargs):
+ captured.update(kwargs)
+ return {"success": True, "data": {"accepted": True}}
+
+ monkeypatch.setattr(
+ remote_command_dispatcher.agent_registry,
+ "send_command",
+ fake_send_command,
+ )
+
+ params = {"unit": "nginx.service"}
+ result = dispatch_agent_command(
+ "server-1",
+ "systemd:status",
+ params=params,
+ user_id=42,
+ timeout=12.5,
+ )
+
+ assert result == {"success": True, "data": {"accepted": True}}
+ assert captured == {
+ "server_id": "server-1",
+ "action": "systemd:status",
+ "params": {"unit": "nginx.service"},
+ "user_id": 42,
+ "timeout": 12.5,
+ }
+ assert captured["params"] is not params
+
+
+def test_dispatcher_supplies_stable_defaults(monkeypatch):
+ captured = {}
+ monkeypatch.setattr(
+ remote_command_dispatcher.agent_registry,
+ "send_command",
+ lambda **kwargs: captured.update(kwargs) or {"success": True},
+ )
+
+ dispatch_agent_command("server-2", "cron:list")
+
+ assert captured["params"] == {}
+ assert captured["user_id"] is None
+ assert captured["timeout"] == 30.0
+
+
+def test_remote_feature_services_use_shared_dispatcher():
+ """New transport calls cannot bypass the single remote command seam."""
+ assert REMOTE_SERVICE_FILES, "expected remote feature services"
+
+ for path in REMOTE_SERVICE_FILES:
+ tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
+ imports_dispatcher = any(
+ isinstance(node, ast.ImportFrom)
+ and node.module == DISPATCHER_MODULE
+ and any(alias.name == "dispatch_agent_command" for alias in node.names)
+ for node in tree.body
+ )
+ direct_sends = [
+ node
+ for node in ast.walk(tree)
+ if isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and node.func.attr == "send_command"
+ ]
+
+ assert imports_dispatcher, f"{path.name} must import the shared dispatcher"
+ assert not direct_sends, f"{path.name} bypasses the shared dispatcher"