utp_service/backend/app/plugins/builtin/mikrotik/set_ntp.py
2026-04-07 12:49:59 +03:00

60 lines
1.9 KiB
Python

"""
MikrotikSetNTPAction — configures NTP server on RouterOS via SSH.
"""
from app.models.device import Device
from app.models.object import Object
from app.plugins.base import ActionResult, BaseAction, Emitter
from app.services import ssh as ssh_service
class MikrotikSetNTPAction(BaseAction):
name = "mikrotik_set_ntp"
display_name = "Mikrotik: Set NTP Server"
compatible_categories = ["mikrotik"]
params_schema = [
{
"name": "server",
"type": "string",
"label": "NTP Server",
"required": True,
"placeholder": "pool.ntp.org",
},
]
async def execute(
self, device: Device, obj: Object, params: dict, emit: Emitter
) -> ActionResult:
server = params.get("server", "").strip()
if not server:
return ActionResult(success=False, error="No NTP server specified")
# Enable NTP client and set server
commands = [
"/system ntp client set enabled=yes",
f"/system ntp client set servers={server}",
]
for cmd in commands:
await emit(f"[{device.ip}] {cmd}", "info")
result = await ssh_service.run_command(device, obj, cmd)
if result.error:
await emit(f"[{device.ip}] Error: {result.error}", "error")
return ActionResult(
success=False,
stdout=result.stdout,
stderr=result.stderr,
exit_code=result.exit_code,
error=result.error,
)
# Verify
verify = await ssh_service.run_command(device, obj, "/system ntp client print")
await emit(f"[{device.ip}] Verify: {verify.stdout[:200]}", "info")
return ActionResult(
success=True,
stdout=verify.stdout,
exit_code=0,
data={"server": server},
)