improve default route service to detect connected routers and use the addresses of the first one found
This commit is contained in:
parent
72189a5c28
commit
7d392c43ac
3 changed files with 34 additions and 30 deletions
|
@ -1,13 +1,13 @@
|
|||
"""
|
||||
utility.py: defines miscellaneous utility services.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import constants, utils
|
||||
from core.errors import CoreCommandError
|
||||
from core.nodes.base import CoreNode
|
||||
from core.services.coreservices import CoreService, ServiceMode
|
||||
|
||||
|
||||
|
@ -77,27 +77,26 @@ class DefaultRouteService(UtilService):
|
|||
|
||||
@classmethod
|
||||
def generate_config(cls, node, filename):
|
||||
# only add default routes for linked routing nodes
|
||||
routes = []
|
||||
for other_node in node.session.nodes.values():
|
||||
if not isinstance(other_node, CoreNode):
|
||||
continue
|
||||
if other_node.type not in ["router", "mdr"]:
|
||||
continue
|
||||
commonnets = node.commonnets(other_node)
|
||||
if commonnets:
|
||||
_, _, router_eth = commonnets[0]
|
||||
for x in router_eth.addrlist:
|
||||
addr, prefix = x.split("/")
|
||||
routes.append(addr)
|
||||
break
|
||||
cfg = "#!/bin/sh\n"
|
||||
cfg += "# auto-generated by DefaultRoute service (utility.py)\n"
|
||||
for ifc in node.netifs():
|
||||
if hasattr(ifc, "control") and ifc.control is True:
|
||||
continue
|
||||
cfg += "\n".join(map(cls.addrstr, ifc.addrlist))
|
||||
cfg += "\n"
|
||||
for route in routes:
|
||||
cfg += f"ip route add default via {route}\n"
|
||||
return cfg
|
||||
|
||||
@staticmethod
|
||||
def addrstr(x):
|
||||
net = netaddr.IPNetwork(x)
|
||||
if net[1] == net[-2]:
|
||||
return ""
|
||||
else:
|
||||
if os.uname()[0] == "Linux":
|
||||
rtcmd = "ip route add default via"
|
||||
else:
|
||||
raise Exception("unknown platform")
|
||||
return "%s %s" % (rtcmd, net[1])
|
||||
|
||||
|
||||
class DefaultMulticastRouteService(UtilService):
|
||||
name = "DefaultMulticastRoute"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue