Skip to content

Commit 04cd6ea

Browse files
committed
refactor
1 parent 62eb06c commit 04cd6ea

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

scripts/hooks/discovery/20.add_routes.py

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,51 @@
33
import subprocess
44

55
def on_discovery_updated(robots, selected_robots, **_):
6-
ip = IPRoute()
6+
ip = IPRoute()
77

8-
routes_to_add = ""
8+
routes_to_add = ""
99

10-
for robot_name, robot in robots.items():
11-
if robot_name not in selected_robots:
12-
continue
10+
for robot_name, robot in robots.items():
11+
if robot_name not in selected_robots:
12+
continue
1313

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))
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))
3232

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
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
4646
ip = IPRoute()
4747
{routes_to_add}
4848
"""
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-
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+
)

tuda_workspace_scripts/robots.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ def _load_pc_from_yaml(
181181
continue
182182
commands[name] = _load_command_from_yaml(name, config["commands"][name])
183183
return RemotePC(
184-
pc_name, address, hostname, user, port=port, netmask=netmask, commands=list(commands.values())
184+
pc_name, address,
185+
hostname,
186+
user,
187+
port=port,
188+
netmask=netmask,
189+
commands=list(commands.values())
185190
)
186191

187192

0 commit comments

Comments
 (0)