33import subprocess
44
55def 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
4646ip = 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+ )
0 commit comments