fix: dial per-node console hosts in MCP device config tools

The device tools (device_config_send / device_show_run / vpcs_config_set)
built their Nornir inventory with only the console port and inherited the
group hostname, which is always the controller host. Console ports of
nodes on remote computes are bound on the compute's own host, so those
nodes were unreachable from the MCP tools - only local-compute nodes
worked.

Pass the per-node console_host through the project inventory and inject
it as a host-level Nornir hostname (which overrides the group's
controller-wide default). Missing or wildcard (0.0.0.0/::) values keep
the group inheritance, so single-machine setups behave exactly as
before. VPCS builds its own host entries, so it carries the hostname
over explicitly.
This commit is contained in:
YueGuobin committed 2026-10-16 10:29:19 +08:00
1 parent b79dcaa49d
commit f47cb56495
6 files changed
+145 -6

No files matched your search

+30
View File
@@ -130,6 +130,36 @@ class TestVPCSNodeTypeGuard:
assert "output" not in result[0]
class TestVPCSConsoleHostPropagation:
"""
VPCS builds its own nornir host entries, so the per-node console
hostname resolved by get_device_ports_from_topology must be carried
over instead of silently falling back to the controller host.
"""
def test_remote_compute_hostname_reaches_nornir_inventory(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
with patch(
f"{VPCS_MOD}.get_device_ports_from_topology",
return_value={
"PC1": {"port": 5000, "node_type": "vpcs", "hostname": "192.168.122.10"},
"PC2": {"port": 5001, "node_type": "vpcs"},
},
):
hosts_data = VPCSCommands()._prepare_device_hosts_data(
[
{"device_name": "PC1", "commands": ["ip 10.0.0.1/24"]},
{"device_name": "PC2", "commands": ["ip 10.0.0.2/24"]},
],
PROJECT_ID,
)
# remote-compute console hostname lands at host level
assert hosts_data["PC1"]["hostname"] == "192.168.122.10"
# local-compute entries keep inheriting the group hostname
assert "hostname" not in hosts_data["PC2"]
class TestDeviceToolErrorContract:
"""
Every in-band error entry carries status "failed" plus an "error"
+86
View File
@@ -64,6 +64,35 @@ def test_nodes_inventory_emits_default_credentials():
assert inventory["R1"]["tags"] == []
def test_nodes_inventory_carries_console_host():
"""
The inventory must keep the per-node console_host: remote computes bind
console ports on their own host, and the Nornir inventory needs it to
dial devices outside the controller machine.
"""
pytest.importorskip("jwt", reason="ai-features extras not installed")
from gns3server.agent.gns3_copilot.gns3_client.project_inventory import (
build_nodes_inventory,
)
nodes = [
{
"name": "R1",
"node_id": "0d15c2e6-8f83-4b79-8875-9dbc3e5f2f1e",
"node_type": "dynamips",
"console": 5000,
"console_host": "192.168.122.10",
"console_type": "telnet",
"status": "started",
},
]
inventory = build_nodes_inventory(nodes, "127.0.0.1")
assert inventory["R1"]["console_host"] == "192.168.122.10"
# the controller-wide "server" stays for backward compatibility
assert inventory["R1"]["server"] == "127.0.0.1"
def test_links_summary_resolves_names_and_ports():
"""
links_summary maps raw link endpoint lists to
@@ -234,3 +263,60 @@ def test_device_ports_inject_default_credentials(monkeypatch):
assert "password" not in hosts["R2"]
assert "username" not in hosts["R3"]
assert "password" not in hosts["R3"]
def test_device_ports_carry_per_node_console_host(monkeypatch):
"""
Remote computes bind console ports on their own host: console_host from
the topology becomes a host-level nornir hostname (overriding the
group's controller-wide default). Missing or wildcard values keep the
group inheritance.
"""
pytest.importorskip("jwt", reason="ai-features extras not installed")
from gns3server.agent.gns3_copilot import gns3_client
from gns3server.agent.gns3_copilot.utils import get_gns3_device_port
class _FakeTopology:
def _run(self, project_id=None, jwt_token=None, url=None):
return {
"nodes": {
# node on a remote compute (another GNS3 VM)
"R1": {
"console_port": 5000,
"console_host": "192.168.122.10",
"tags": [],
"netmiko_device_type": "cisco_ios_telnet",
},
# node on the local compute
"R2": {
"console_port": 5001,
"console_host": "127.0.0.1",
"tags": [],
"netmiko_device_type": "cisco_ios_telnet",
},
# legacy topology payload without console_host
"R3": {
"console_port": 5002,
"tags": [],
"netmiko_device_type": "cisco_ios_telnet",
},
# wildcard host cannot be dialed; keep group inheritance
"R4": {
"console_port": 5003,
"console_host": "0.0.0.0",
"tags": [],
"netmiko_device_type": "cisco_ios_telnet",
},
}
}
# the function does a lazy from-import inside the body
monkeypatch.setattr(gns3_client, "GNS3TopologyTool", _FakeTopology)
hosts = get_gns3_device_port.get_device_ports_from_topology(["R1", "R2", "R3", "R4"])
# remote and local consoles dial their own host
assert hosts["R1"]["hostname"] == "192.168.122.10"
assert hosts["R2"]["hostname"] == "127.0.0.1"
# missing or wildcard console_host keeps inheriting the group hostname
assert "hostname" not in hosts["R3"]
assert "hostname" not in hosts["R4"]