utp_service/backend/app/plugins/builtin/mikrotik/get_config.py

59 lines
2 KiB
Python

"""
MikrotikGetConfigAction — exports the full RouterOS configuration via SSH and saves
it as a config snapshot.
Uses `/export compact` which produces a minimal, parseable RouterOS script.
"""
from app.models.device import Device
from app.models.object import Object
from app.plugins.base import ActionResult, BaseAction, Emitter
from app.services import snapshot as snapshot_service
from app.services import ssh as ssh_service
class MikrotikGetConfigAction(BaseAction):
name = "mikrotik_get_config"
display_name = "Mikrotik: Export Config"
compatible_categories = ["mikrotik"]
params_schema = []
async def execute(
self, device: Device, obj: Object, params: dict, emit: Emitter, db=None
) -> ActionResult:
await emit(f"[{device.ip}] Running /export compact", "info")
result = await ssh_service.run_command(
device, obj, "/export compact", timeout=60, db=db
)
if not result.success and not result.stdout:
msg = result.error or result.stderr or f"exit code {result.exit_code}"
await emit(f"[{device.ip}] Export failed: {msg}", "error")
return ActionResult(
success=False,
stdout=result.stdout,
stderr=result.stderr,
exit_code=result.exit_code,
error=msg,
)
# RouterOS export always returns exit 0 but may print errors to stdout
content = result.stdout
snap_id, sha256, size = await snapshot_service.save_snapshot(
device_id=device.id,
content=content,
label="routeros-export",
)
await emit(
f"[{device.ip}] Config snapshot saved: {sha256[:12]}… ({size} bytes, id={snap_id})",
"info",
)
return ActionResult(
success=True,
stdout=content,
exit_code=0,
data={"snapshot_id": snap_id, "sha256": sha256, "size": size},
)