feat(fleet): preserve operator VESC integration before final driver merge
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import secrets
|
||||
import sqlite3
|
||||
import threading
|
||||
@@ -36,6 +37,8 @@ class FleetRegistry:
|
||||
from .monitor import MonitorReceiver
|
||||
|
||||
self.monitor = MonitorReceiver(root)
|
||||
from .rover_control import RoverControl
|
||||
self.rover_control = RoverControl()
|
||||
self.trust = CoreTrust(root)
|
||||
path = root / "fleet.sqlite3"
|
||||
if path.is_symlink():
|
||||
@@ -50,6 +53,16 @@ class FleetRegistry:
|
||||
"node_id TEXT UNIQUE NOT NULL, body TEXT NOT NULL)"
|
||||
)
|
||||
self.db.commit()
|
||||
from k1link.device_plugins.vesc.archive import Archive
|
||||
|
||||
self.vesc_archive = Archive(root / "vesc-configurations")
|
||||
# Preserve pre-archive releases' completed backups before the ordinary
|
||||
# short-lived operation receipts are pruned.
|
||||
for row in self.rows():
|
||||
for receipt in row.get("sensor_commands", {}).values():
|
||||
if receipt.get("state") == "complete" and receipt.get("command", {}).get("action_id") == "vesc.config.backup":
|
||||
with suppress(ValueError, KeyError, TypeError):
|
||||
self.vesc_archive.add(row["node_id"], receipt["result"])
|
||||
self.previews: dict[str, dict] = {}
|
||||
self.listeners: dict[str, object] = {}
|
||||
self.stop = threading.Event()
|
||||
@@ -359,7 +372,11 @@ class FleetRegistry:
|
||||
if not isinstance(key, Ed25519PublicKey):
|
||||
return 403, {"error": "Unauthorized Node"}
|
||||
node_id = public_id("node_", key)
|
||||
entered = time.monotonic()
|
||||
with self.lock:
|
||||
waited = time.monotonic() - entered
|
||||
if waited > .025:
|
||||
logging.getLogger(__name__).warning("fleet timing path=%s lock_ms=%.1f", path, waited*1000)
|
||||
row = next((item for item in self.rows() if item["node_id"] == node_id), None)
|
||||
if (
|
||||
row is None
|
||||
@@ -390,7 +407,7 @@ class FleetRegistry:
|
||||
)
|
||||
self.save(row)
|
||||
return 200, {"ok": True}
|
||||
if path != "/v1/node/heartbeat":
|
||||
if path not in ("/v1/node/heartbeat", "/v1/node/rover", "/v1/node/rover-stream"):
|
||||
return 404, {"error": "Unknown operation"}
|
||||
# Endpoint revisions prevent an old in-flight heartbeat or lost ack
|
||||
# from reversing a migration. Use the actual listener, not a claimed IP.
|
||||
@@ -411,11 +428,33 @@ class FleetRegistry:
|
||||
row["core_address"] = address
|
||||
row["binding"]["endpoint"] = value["core_endpoint"]
|
||||
row["binding"]["endpoint_revision"] = revision
|
||||
if path in ("/v1/node/rover", "/v1/node/rover-stream"):
|
||||
if row["enrollment"] != "paired":
|
||||
return 410, {"error": "Pairing incomplete"}
|
||||
if path.endswith("-stream"):
|
||||
relay = value.get("relay_id")
|
||||
if not isinstance(relay, str) or len(relay) != 32:
|
||||
return 400, {"error": "Invalid relay"}
|
||||
return 200, self.rover_control.stream(node_id, relay)
|
||||
return 200, self.rover_control.exchange(node_id, value)
|
||||
binding = ExecutionBinding.model_validate(value["execution_binding"])
|
||||
if binding.node_id != node_id or binding.platform.value != "linux":
|
||||
return 400, {"error": "Invalid Node inventory"}
|
||||
sensor_state = sensors.validate_inventory(value, node_id)
|
||||
sensor_response = sensors.heartbeat(row, value)
|
||||
archive_ack = None
|
||||
archive_started = time.monotonic()
|
||||
try:
|
||||
if "vesc_configurations" in value:
|
||||
archive_ack = self.vesc_archive.receive(node_id, value["vesc_configurations"])
|
||||
for receipt in row.get("sensor_commands", {}).values():
|
||||
if receipt.get("state") == "complete" and receipt.get("command", {}).get("action_id") == "vesc.config.backup":
|
||||
self.vesc_archive.add(node_id, receipt["result"])
|
||||
except (ValueError, KeyError, TypeError, sqlite3.Error, OSError):
|
||||
# No archive acknowledgement until durable storage succeeds.
|
||||
# Archive failure must never delay a control/stop response.
|
||||
archive_ack = None
|
||||
archive_elapsed = time.monotonic() - archive_started
|
||||
enrollment_response = self.device_enrollment.heartbeat(row, value)
|
||||
host = value.get("host")
|
||||
if (
|
||||
@@ -441,7 +480,12 @@ class FleetRegistry:
|
||||
"until": time.time() + 3600,
|
||||
}
|
||||
row["binding"]["client_pem"] = self.trust.leaf(node_id, key)
|
||||
save_started = time.monotonic()
|
||||
self.save(row)
|
||||
save_elapsed = time.monotonic() - save_started
|
||||
if archive_elapsed > .025 or save_elapsed > .025:
|
||||
logging.getLogger(__name__).warning(
|
||||
"fleet heartbeat timing archive_ms=%.1f save_ms=%.1f", archive_elapsed*1000, save_elapsed*1000)
|
||||
monitor_ack = None
|
||||
try:
|
||||
monitor_ack = self.monitor.submit(node_id, value.get("monitor"))
|
||||
@@ -449,6 +493,7 @@ class FleetRegistry:
|
||||
# Telemetry persistence failure must not block device control.
|
||||
pass
|
||||
return 200, {
|
||||
"vesc_configurations_ack": archive_ack,
|
||||
"monitor_ack": monitor_ack,
|
||||
"ok": True,
|
||||
"client_pem": row["binding"]["client_pem"],
|
||||
|
||||
Reference in New Issue
Block a user