mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-06 07:01:52 +09:00
Foundation for the remote server setup experience: agent gains
auto-sudo, package and systemd handler additions, missing docker
verbs, and a job-channel registry for streaming long-running
operations. Panel exposes the new agent surface as /servers/<id>/
packages, /services, and /refresh-capabilities routes.
Agent:
- New protocol actions: agent:recapabilities, packages:update_cache/
install_async/upgrade/search/info, systemd:daemon_reload/list_units/
logs/logs_follow, docker:container:create/exec, docker:image:build,
docker:volume:create, docker:network:create.
- Auto-sudo helper (sudo -n) probed at boot, surfaced as a capability;
apt invocations get DEBIAN_FRONTEND=noninteractive + dpkg force-confold.
- jobs registry with ring-buffer replay so panels that subscribe to a
job:<id> channel mid-flight still see prior progress events.
- Capability re-probe is mutex-guarded and additive (transient probe
failures don't drop features the panel already saw).
- Existing systemd handlers now sudo and use 2-min timeouts for cold
service starts (postgresql etc.).
Panel:
- remote_packages_service.py and remote_systemd_service.py mirror the
cron service shape (thin send_command dispatcher).
- 13 new routes on servers.py covering packages CRUD + search/info,
systemd unit control + logs + daemon-reload, and capability refresh.
- Long-running install/upgrade return {job_id, channel} for the
frontend to subscribe to via Socket.IO.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
"""
|
|
Remote Systemd Service.
|
|
|
|
Wraps the agent's systemd:* handlers behind a simple Python surface so
|
|
the API layer can call control_unit('nginx', 'restart') instead of
|
|
threading actions and params manually.
|
|
|
|
list_units returns canonical unit rows in either JSON or plain-parsed
|
|
format — the agent flips automatically based on systemd version. The
|
|
panel doesn't need to care which.
|
|
"""
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
from app.services.agent_registry import agent_registry
|
|
|
|
|
|
_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(
|
|
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(
|
|
server_id, 'systemd:status', params={'unit': unit},
|
|
user_id=user_id, timeout=15.0,
|
|
)
|
|
|
|
@staticmethod
|
|
def control(server_id: str, unit: str, action: str,
|
|
user_id: Optional[int] = None) -> Dict[str, Any]:
|
|
if action not in _VALID_ACTIONS:
|
|
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(
|
|
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(
|
|
server_id, 'systemd:daemon_reload',
|
|
user_id=user_id, timeout=310.0,
|
|
)
|
|
|
|
@staticmethod
|
|
def logs(server_id: str, unit: str, lines: int = 200,
|
|
user_id: Optional[int] = None) -> Dict[str, Any]:
|
|
return RemoteSystemdService._send(
|
|
server_id, 'systemd:logs',
|
|
params={'unit': unit, 'lines': int(lines)},
|
|
user_id=user_id, timeout=30.0,
|
|
)
|
|
|
|
@staticmethod
|
|
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(
|
|
server_id, 'systemd:logs_follow',
|
|
params={'unit': unit},
|
|
user_id=user_id, timeout=10.0,
|
|
)
|