daemon: updated core.services to avoid using deprecated type hinting, also updated string formatting to f strings

This commit is contained in:
Blake Harnden 2023-04-13 15:39:40 -07:00
parent 7f58224f43
commit 921bfdf527
11 changed files with 435 additions and 500 deletions

View file

@ -2,7 +2,7 @@
frr.py: defines routing services provided by FRRouting.
Assumes installation of FRR via https://deb.frrouting.org/
"""
from typing import Optional, Tuple
from typing import Optional
import netaddr
@ -30,16 +30,16 @@ def is_wireless(node: NodeBase) -> bool:
class FRRZebra(CoreService):
name: str = "FRRzebra"
group: str = "FRR"
dirs: Tuple[str, ...] = ("/usr/local/etc/frr", "/var/run/frr", "/var/log/frr")
configs: Tuple[str, ...] = (
dirs: tuple[str, ...] = ("/usr/local/etc/frr", "/var/run/frr", "/var/log/frr")
configs: tuple[str, ...] = (
"/usr/local/etc/frr/frr.conf",
"frrboot.sh",
"/usr/local/etc/frr/vtysh.conf",
"/usr/local/etc/frr/daemons",
)
startup: Tuple[str, ...] = ("bash frrboot.sh zebra",)
shutdown: Tuple[str, ...] = ("killall zebra",)
validate: Tuple[str, ...] = ("pidof zebra",)
startup: tuple[str, ...] = ("bash frrboot.sh zebra",)
shutdown: tuple[str, ...] = ("killall zebra",)
validate: tuple[str, ...] = ("pidof zebra",)
@classmethod
def generate_config(cls, node: CoreNode, filename: str) -> str:
@ -75,7 +75,7 @@ class FRRZebra(CoreService):
# we could verify here that filename == frr.conf
cfg = ""
for iface in node.get_ifaces():
cfg += "interface %s\n" % iface.name
cfg += f"interface {iface.name}\n"
# include control interfaces in addressing but not routing daemons
if iface.control:
cfg += " "
@ -127,11 +127,11 @@ class FRRZebra(CoreService):
"""
address = str(ip.ip)
if netaddr.valid_ipv4(address):
return "ip address %s" % ip
return f"ip address {ip}"
elif netaddr.valid_ipv6(address):
return "ipv6 address %s" % ip
return f"ipv6 address {ip}"
else:
raise ValueError("invalid address: %s", ip)
raise ValueError(f"invalid address: {ip}")
@classmethod
def generate_frr_boot(cls, node: CoreNode) -> str:
@ -144,16 +144,16 @@ class FRRZebra(CoreService):
frr_sbin_search = node.session.options.get(
"frr_sbin_search", '"/usr/local/sbin /usr/sbin /usr/lib/frr"'
)
cfg = """\
cfg = f"""\
#!/bin/sh
# auto-generated by zebra service (frr.py)
FRR_CONF=%s
FRR_SBIN_SEARCH=%s
FRR_BIN_SEARCH=%s
FRR_STATE_DIR=%s
FRR_CONF={cls.configs[0]}
FRR_SBIN_SEARCH={frr_sbin_search}
FRR_BIN_SEARCH={frr_bin_search}
FRR_STATE_DIR={FRR_STATE_DIR}
searchforprog()
{
{{
prog=$1
searchpath=$@
ret=
@ -164,10 +164,10 @@ searchforprog()
fi
done
echo $ret
}
}}
confcheck()
{
{{
CONF_DIR=`dirname $FRR_CONF`
# if /etc/frr exists, point /etc/frr/frr.conf -> CONF_DIR
if [ "$CONF_DIR" != "/etc/frr" ] && [ -d /etc/frr ] && [ ! -e /etc/frr/frr.conf ]; then
@ -177,10 +177,10 @@ confcheck()
if [ "$CONF_DIR" != "/etc/frr" ] && [ -d /etc/frr ] && [ ! -e /etc/frr/vtysh.conf ]; then
ln -s $CONF_DIR/vtysh.conf /etc/frr/vtysh.conf
fi
}
}}
bootdaemon()
{
{{
FRR_SBIN_DIR=$(searchforprog $1 $FRR_SBIN_SEARCH)
if [ "z$FRR_SBIN_DIR" = "z" ]; then
echo "ERROR: FRR's '$1' daemon not found in search path:"
@ -207,10 +207,10 @@ bootdaemon()
echo "ERROR: FRR's '$1' daemon failed to start!:"
return 1
fi
}
}}
bootfrr()
{
{{
FRR_BIN_DIR=$(searchforprog 'vtysh' $FRR_BIN_SEARCH)
if [ "z$FRR_BIN_DIR" = "z" ]; then
echo "ERROR: FRR's 'vtysh' program not found in search path:"
@ -229,8 +229,8 @@ bootfrr()
bootdaemon "staticd"
fi
for r in rip ripng ospf6 ospf bgp babel isis; do
if grep -q "^router \\<${r}\\>" $FRR_CONF; then
bootdaemon "${r}d"
if grep -q "^router \\<${{r}}\\>" $FRR_CONF; then
bootdaemon "${{r}}d"
fi
done
@ -239,7 +239,7 @@ bootfrr()
fi
$FRR_BIN_DIR/vtysh -b
}
}}
if [ "$1" != "zebra" ]; then
echo "WARNING: '$1': all FRR daemons are launched by the 'zebra' service!"
@ -248,12 +248,7 @@ fi
confcheck
bootfrr
""" % (
cls.configs[0],
frr_sbin_search,
frr_bin_search,
FRR_STATE_DIR,
)
"""
for iface in node.get_ifaces():
cfg += f"ip link set dev {iface.name} down\n"
cfg += "sleep 1\n"
@ -337,7 +332,7 @@ class FrrService(CoreService):
name: Optional[str] = None
group: str = "FRR"
dependencies: Tuple[str, ...] = ("FRRzebra",)
dependencies: tuple[str, ...] = ("FRRzebra",)
meta: str = "The config file for this service can be found in the Zebra service."
ipv4_routing: bool = False
ipv6_routing: bool = False
@ -388,8 +383,8 @@ class FRROspfv2(FrrService):
"""
name: str = "FRROSPFv2"
shutdown: Tuple[str, ...] = ("killall ospfd",)
validate: Tuple[str, ...] = ("pidof ospfd",)
shutdown: tuple[str, ...] = ("killall ospfd",)
validate: tuple[str, ...] = ("pidof ospfd",)
ipv4_routing: bool = True
@staticmethod
@ -424,7 +419,7 @@ class FRROspfv2(FrrService):
def generate_frr_config(cls, node: CoreNode) -> str:
cfg = "router ospf\n"
rtrid = cls.router_id(node)
cfg += " router-id %s\n" % rtrid
cfg += f" router-id {rtrid}\n"
# network 10.0.0.0/24 area 0
for iface in node.get_ifaces(control=False):
for ip4 in iface.ip4s:
@ -458,8 +453,8 @@ class FRROspfv3(FrrService):
"""
name: str = "FRROSPFv3"
shutdown: Tuple[str, ...] = ("killall ospf6d",)
validate: Tuple[str, ...] = ("pidof ospf6d",)
shutdown: tuple[str, ...] = ("killall ospf6d",)
validate: tuple[str, ...] = ("pidof ospf6d",)
ipv4_routing: bool = True
ipv6_routing: bool = True
@ -486,7 +481,7 @@ class FRROspfv3(FrrService):
"""
minmtu = cls.min_mtu(iface)
if minmtu < iface.mtu:
return " ipv6 ospf6 ifmtu %d\n" % minmtu
return f" ipv6 ospf6 ifmtu {minmtu:d}\n"
else:
return ""
@ -504,9 +499,9 @@ class FRROspfv3(FrrService):
def generate_frr_config(cls, node: CoreNode) -> str:
cfg = "router ospf6\n"
rtrid = cls.router_id(node)
cfg += " router-id %s\n" % rtrid
cfg += f" router-id {rtrid}\n"
for iface in node.get_ifaces(control=False):
cfg += " interface %s area 0.0.0.0\n" % iface.name
cfg += f" interface {iface.name} area 0.0.0.0\n"
cfg += "!\n"
return cfg
@ -523,8 +518,8 @@ class FRRBgp(FrrService):
"""
name: str = "FRRBGP"
shutdown: Tuple[str, ...] = ("killall bgpd",)
validate: Tuple[str, ...] = ("pidof bgpd",)
shutdown: tuple[str, ...] = ("killall bgpd",)
validate: tuple[str, ...] = ("pidof bgpd",)
custom_needed: bool = True
ipv4_routing: bool = True
ipv6_routing: bool = True
@ -534,9 +529,9 @@ class FRRBgp(FrrService):
cfg = "!\n! BGP configuration\n!\n"
cfg += "! You should configure the AS number below,\n"
cfg += "! along with this router's peers.\n!\n"
cfg += "router bgp %s\n" % node.id
cfg += f"router bgp {node.id}\n"
rtrid = cls.router_id(node)
cfg += " bgp router-id %s\n" % rtrid
cfg += f" bgp router-id {rtrid}\n"
cfg += " redistribute connected\n"
cfg += "! neighbor 1.2.3.4 remote-as 555\n!\n"
return cfg
@ -548,8 +543,8 @@ class FRRRip(FrrService):
"""
name: str = "FRRRIP"
shutdown: Tuple[str, ...] = ("killall ripd",)
validate: Tuple[str, ...] = ("pidof ripd",)
shutdown: tuple[str, ...] = ("killall ripd",)
validate: tuple[str, ...] = ("pidof ripd",)
ipv4_routing: bool = True
@classmethod
@ -571,8 +566,8 @@ class FRRRipng(FrrService):
"""
name: str = "FRRRIPNG"
shutdown: Tuple[str, ...] = ("killall ripngd",)
validate: Tuple[str, ...] = ("pidof ripngd",)
shutdown: tuple[str, ...] = ("killall ripngd",)
validate: tuple[str, ...] = ("pidof ripngd",)
ipv6_routing: bool = True
@classmethod
@ -595,15 +590,15 @@ class FRRBabel(FrrService):
"""
name: str = "FRRBabel"
shutdown: Tuple[str, ...] = ("killall babeld",)
validate: Tuple[str, ...] = ("pidof babeld",)
shutdown: tuple[str, ...] = ("killall babeld",)
validate: tuple[str, ...] = ("pidof babeld",)
ipv6_routing: bool = True
@classmethod
def generate_frr_config(cls, node: CoreNode) -> str:
cfg = "router babel\n"
for iface in node.get_ifaces(control=False):
cfg += " network %s\n" % iface.name
cfg += f" network {iface.name}\n"
cfg += " redistribute static\n redistribute ipv4 connected\n"
return cfg
@ -621,8 +616,8 @@ class FRRpimd(FrrService):
"""
name: str = "FRRpimd"
shutdown: Tuple[str, ...] = ("killall pimd",)
validate: Tuple[str, ...] = ("pidof pimd",)
shutdown: tuple[str, ...] = ("killall pimd",)
validate: tuple[str, ...] = ("pidof pimd",)
ipv4_routing: bool = True
@classmethod
@ -636,8 +631,8 @@ class FRRpimd(FrrService):
cfg += "router igmp\n!\n"
cfg += "router pim\n"
cfg += " !ip pim rp-address 10.0.0.1\n"
cfg += " ip pim bsr-candidate %s\n" % ifname
cfg += " ip pim rp-candidate %s\n" % ifname
cfg += f" ip pim bsr-candidate {ifname}\n"
cfg += f" ip pim rp-candidate {ifname}\n"
cfg += " !ip pim spt-threshold interval 10 bytes 80000\n"
return cfg
@ -654,8 +649,8 @@ class FRRIsis(FrrService):
"""
name: str = "FRRISIS"
shutdown: Tuple[str, ...] = ("killall isisd",)
validate: Tuple[str, ...] = ("pidof isisd",)
shutdown: tuple[str, ...] = ("killall isisd",)
validate: tuple[str, ...] = ("pidof isisd",)
ipv4_routing: bool = True
ipv6_routing: bool = True
@ -672,7 +667,7 @@ class FRRIsis(FrrService):
@classmethod
def generate_frr_config(cls, node: CoreNode) -> str:
cfg = "router isis DEFAULT\n"
cfg += " net 47.0001.0000.1900.%04x.00\n" % node.id
cfg += f" net 47.0001.0000.1900.{node.id:04x}.00\n"
cfg += " metric-style wide\n"
cfg += " is-type level-2-only\n"
cfg += "!\n"