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

@ -1,7 +1,7 @@
"""
quagga.py: defines routing services provided by Quagga.
"""
from typing import Optional, Tuple
from typing import Optional
import netaddr
@ -29,15 +29,15 @@ def is_wireless(node: NodeBase) -> bool:
class Zebra(CoreService):
name: str = "zebra"
group: str = "Quagga"
dirs: Tuple[str, ...] = ("/usr/local/etc/quagga", "/var/run/quagga")
configs: Tuple[str, ...] = (
dirs: tuple[str, ...] = ("/usr/local/etc/quagga", "/var/run/quagga")
configs: tuple[str, ...] = (
"/usr/local/etc/quagga/Quagga.conf",
"quaggaboot.sh",
"/usr/local/etc/quagga/vtysh.conf",
)
startup: Tuple[str, ...] = ("bash quaggaboot.sh zebra",)
shutdown: Tuple[str, ...] = ("killall zebra",)
validate: Tuple[str, ...] = ("pidof zebra",)
startup: tuple[str, ...] = ("bash quaggaboot.sh zebra",)
shutdown: tuple[str, ...] = ("killall zebra",)
validate: tuple[str, ...] = ("pidof zebra",)
@classmethod
def generate_config(cls, node: CoreNode, filename: str) -> str:
@ -71,7 +71,7 @@ class Zebra(CoreService):
# we could verify here that filename == Quagga.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 += " "
@ -123,11 +123,11 @@ class Zebra(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_quagga_boot(cls, node: CoreNode) -> str:
@ -140,16 +140,16 @@ class Zebra(CoreService):
quagga_sbin_search = node.session.options.get(
"quagga_sbin_search", '"/usr/local/sbin /usr/sbin /usr/lib/quagga"'
)
return """\
return f"""\
#!/bin/sh
# auto-generated by zebra service (quagga.py)
QUAGGA_CONF=%s
QUAGGA_SBIN_SEARCH=%s
QUAGGA_BIN_SEARCH=%s
QUAGGA_STATE_DIR=%s
QUAGGA_CONF={cls.configs[0]}
QUAGGA_SBIN_SEARCH={quagga_sbin_search}
QUAGGA_BIN_SEARCH={quagga_bin_search}
QUAGGA_STATE_DIR={QUAGGA_STATE_DIR}
searchforprog()
{
{{
prog=$1
searchpath=$@
ret=
@ -160,10 +160,10 @@ searchforprog()
fi
done
echo $ret
}
}}
confcheck()
{
{{
CONF_DIR=`dirname $QUAGGA_CONF`
# if /etc/quagga exists, point /etc/quagga/Quagga.conf -> CONF_DIR
if [ "$CONF_DIR" != "/etc/quagga" ] && [ -d /etc/quagga ] && [ ! -e /etc/quagga/Quagga.conf ]; then
@ -173,10 +173,10 @@ confcheck()
if [ "$CONF_DIR" != "/etc/quagga" ] && [ -d /etc/quagga ] && [ ! -e /etc/quagga/vtysh.conf ]; then
ln -s $CONF_DIR/vtysh.conf /etc/quagga/vtysh.conf
fi
}
}}
bootdaemon()
{
{{
QUAGGA_SBIN_DIR=$(searchforprog $1 $QUAGGA_SBIN_SEARCH)
if [ "z$QUAGGA_SBIN_DIR" = "z" ]; then
echo "ERROR: Quagga's '$1' daemon not found in search path:"
@ -196,10 +196,10 @@ bootdaemon()
echo "ERROR: Quagga's '$1' daemon failed to start!:"
return 1
fi
}
}}
bootquagga()
{
{{
QUAGGA_BIN_DIR=$(searchforprog 'vtysh' $QUAGGA_BIN_SEARCH)
if [ "z$QUAGGA_BIN_DIR" = "z" ]; then
echo "ERROR: Quagga's 'vtysh' program not found in search path:"
@ -215,8 +215,8 @@ bootquagga()
bootdaemon "zebra"
for r in rip ripng ospf6 ospf bgp babel; do
if grep -q "^router \\<${r}\\>" $QUAGGA_CONF; then
bootdaemon "${r}d"
if grep -q "^router \\<${{r}}\\>" $QUAGGA_CONF; then
bootdaemon "${{r}}d"
fi
done
@ -225,7 +225,7 @@ bootquagga()
fi
$QUAGGA_BIN_DIR/vtysh -b
}
}}
if [ "$1" != "zebra" ]; then
echo "WARNING: '$1': all Quagga daemons are launched by the 'zebra' service!"
@ -233,12 +233,7 @@ if [ "$1" != "zebra" ]; then
fi
confcheck
bootquagga
""" % (
cls.configs[0],
quagga_sbin_search,
quagga_bin_search,
QUAGGA_STATE_DIR,
)
"""
class QuaggaService(CoreService):
@ -249,7 +244,7 @@ class QuaggaService(CoreService):
name: Optional[str] = None
group: str = "Quagga"
dependencies: Tuple[str, ...] = (Zebra.name,)
dependencies: tuple[str, ...] = (Zebra.name,)
meta: str = "The config file for this service can be found in the Zebra service."
ipv4_routing: bool = False
ipv6_routing: bool = False
@ -300,8 +295,8 @@ class Ospfv2(QuaggaService):
"""
name: str = "OSPFv2"
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
@ -336,7 +331,7 @@ class Ospfv2(QuaggaService):
def generate_quagga_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:
@ -369,8 +364,8 @@ class Ospfv3(QuaggaService):
"""
name: str = "OSPFv3"
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
@ -397,7 +392,7 @@ class Ospfv3(QuaggaService):
"""
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 ""
@ -416,9 +411,9 @@ class Ospfv3(QuaggaService):
cfg = "router ospf6\n"
rtrid = cls.router_id(node)
cfg += " instance-id 65\n"
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
@ -466,8 +461,8 @@ class Bgp(QuaggaService):
"""
name: str = "BGP"
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
@ -477,9 +472,9 @@ class Bgp(QuaggaService):
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
@ -491,8 +486,8 @@ class Rip(QuaggaService):
"""
name: str = "RIP"
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
@ -514,8 +509,8 @@ class Ripng(QuaggaService):
"""
name: str = "RIPNG"
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
@ -538,15 +533,15 @@ class Babel(QuaggaService):
"""
name: str = "Babel"
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_quagga_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 connected\n"
return cfg
@ -564,8 +559,8 @@ class Xpimd(QuaggaService):
"""
name: str = "Xpimd"
shutdown: Tuple[str, ...] = ("killall xpimd",)
validate: Tuple[str, ...] = ("pidof xpimd",)
shutdown: tuple[str, ...] = ("killall xpimd",)
validate: tuple[str, ...] = ("pidof xpimd",)
ipv4_routing: bool = True
@classmethod
@ -579,8 +574,8 @@ class Xpimd(QuaggaService):
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