Skip to content

Commit 62eb06c

Browse files
committed
added feature which automatically sets routes with hector discovery
1 parent 97765de commit 62eb06c

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

scripts/discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def main():
115115
f"Hook {hook} does not contain a valid on_discovery_updated method."
116116
)
117117
continue
118-
on_discovery_updated()
118+
on_discovery_updated(robots=robots, selected_robots=selected_robots)
119119
elif hook.endswith(".bash") or hook.endswith(".sh"):
120120
executable = "bash" if hook.endswith(".bash") else "sh"
121121
subprocess.run([executable, hook], cwd=get_workspace_root())
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
from pyroute2 import IPRoute
3+
import subprocess
4+
5+
def on_discovery_updated(robots, selected_robots, **_):
6+
ip = IPRoute()
7+
8+
routes_to_add = ""
9+
10+
for robot_name, robot in robots.items():
11+
if robot_name not in selected_robots:
12+
continue
13+
14+
for pc_name in robot.remote_pcs:
15+
pc = robot.remote_pcs[pc_name]
16+
if not pc.address:
17+
print(f"Robot {pc.name} has no address configured for its , skipping pc")
18+
continue
19+
else:
20+
pc_ip = pc.address
21+
netmask = pc.netmask
22+
break
23+
24+
if not pc_ip:
25+
print(f"Robot {robot_name} has no address configured for its pcs, skipping robot")
26+
continue
27+
parts = pc_ip.split('.')[:-1]
28+
net_ip = '.'.join(parts + ['0'])
29+
host_ip = '.'.join(parts[:2] + ['0', parts[2]])
30+
31+
existing_routes = ip.get_routes(dst=net_ip + '/' + str(netmask))
32+
33+
valid_route = False
34+
if existing_routes:
35+
gateway = next((value for key, value in existing_routes[0]['attrs'] if key == 'RTA_GATEWAY'), None)
36+
if gateway == host_ip:
37+
valid_route = True
38+
39+
if not valid_route:
40+
print(f"Adding route to {net_ip}/{netmask} via host pc")
41+
command = f"ip.route('add', dst='{net_ip}/{netmask}', gateway='{host_ip}')\n"
42+
routes_to_add += command
43+
44+
if routes_to_add != "":
45+
ip_route_code = f"""from pyroute2 import IPRoute
46+
ip = IPRoute()
47+
{routes_to_add}
48+
"""
49+
print("Privilege escalation required to add routes, running commands with sudo...")
50+
subprocess.run(
51+
["sudo", "python3", "-c", ip_route_code],
52+
check=True
53+
)
54+
55+

scripts/hooks/discovery/90.restart_daemon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from tuda_workspace_scripts.print import *
44

55

6-
def on_discovery_updated():
6+
def on_discovery_updated(**_):
77
print_header("Restarting ROS2 daemon")
88
if is_daemon_running(args=[]):
99
if not shutdown_daemon(args=[], timeout=10):

tuda_workspace_scripts/robots.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ class RemotePC:
3737
def __init__(
3838
self,
3939
name: str,
40+
address: str,
4041
hostname: str,
4142
user: str,
4243
commands: list[Command],
4344
port: int = 22,
45+
netmask: int = 24,
4446
):
4547
self.name = name
48+
self.address = address
49+
self.netmask = netmask
4650
self.hostname = hostname
4751
self.user = user
4852
self.port = port
@@ -166,6 +170,8 @@ def _load_pc_from_yaml(
166170
raise ValueError(f"User not specified for remote PC {pc_name}")
167171
user = config["user"]
168172
hostname = config["hostname"] if "hostname" in config else pc_name
173+
address = config["address"] if "address" in config else None
174+
netmask = config["netmask"] if "netmask" in config else 24
169175
port = config["port"] if "port" in config else 22
170176
commands = dict(shared_commands)
171177
if "commands" in config:
@@ -175,7 +181,7 @@ def _load_pc_from_yaml(
175181
continue
176182
commands[name] = _load_command_from_yaml(name, config["commands"][name])
177183
return RemotePC(
178-
pc_name, hostname, user, port=port, commands=list(commands.values())
184+
pc_name, address, hostname, user, port=port, netmask=netmask, commands=list(commands.values())
179185
)
180186

181187

0 commit comments

Comments
 (0)