Commit 216af09b authored by Simon Redman's avatar Simon Redman

Generate and push zebra confs

parent aa7c0af7
......@@ -41,10 +41,28 @@ router ospf6
{router_info}
"""
ZEBRA_CONF_TEMPLATE = """#
# zebra.conf generated by frr_configurator on {date}
#
hostname {hostname}
password zebra
log file /var/log/frr/zebra.log
{interfaces}
ip forwarding
line vty
"""
# interface should be replaced with the Linux interface to speak OSPF on
# One of these per interface should be put in the interfaces section of the OSPF_CONF_TEMPLATE
INTERFACE_LINE_TEMPLATE = "interface {interface}"
# interface should be replaced with the Linux interface to run FRR on
# address_prefix should be replaced with an IPv6 interface address in prefix notation (::1/128, for example)
# One of these per interface should be put in the interfaces section of the ZEBRA_CONF_TEMPLATE
ZEBRA_INTERFACE_LINE_TEMPLATE = INTERFACE_LINE_TEMPLATE + "\n ipv6 address {address_prefix}"
# AREA is an OSPF concept, but it looks like FRR only implements AREA as 0
# range should be replaced with the prefix of the interface, for instance 'fd00:10:10:2:54:28ff:fe8b:1395/64'
# interface should be replaced with the interface OSPF is being used on
......@@ -52,6 +70,7 @@ INTERFACE_LINE_TEMPLATE = "interface {interface}"
ROUTER_LINE_TEMPLATE = " area 0.0.0.0 range {range}\n interface {interface} area 0.0.0.0"
OSPF_CONF_FILENAME = "/etc/frr/ospf6d.conf"
ZEBRA_CONF_FILENAME = "/etc/frr/zebra.conf"
CONFIG_PUSH_COMMAND_TEMPLATE = "echo '{config}' | sudo tee {filename}"
ENABLE_DAEMON_COMMAND_TEMPLATE = "sudo sed -i 's/{daemon}=no/{daemon}=yes/' /etc/frr/daemons"
......@@ -92,6 +111,24 @@ def build_ospf_conf_for_host(host, hostname, interfaces):
return config
def build_zebra_conf_for_host(hostname, interfaces):
interfaces_strs = []
for interface in interfaces.keys():
interfaces_strs.append(ZEBRA_INTERFACE_LINE_TEMPLATE.format(
interface=interface,
address_prefix=interfaces[interface]
))
config = ZEBRA_CONF_TEMPLATE.format(
date=datetime.datetime.today(),
hostname=hostname,
interfaces=str.join('\n', interfaces_strs)
)
return config
def build_ospf_confs_for_network(netgraph):
"""
Build the ospf6d conf file for all nodes in the network
......@@ -109,6 +146,17 @@ def build_ospf_confs_for_network(netgraph):
return ospf_confs
def build_zebra_confs_for_network(netgraph):
zebra_confs = {}
for host in netgraph.nodes:
interfaces = netgraph._node[host]['interfaces']
hostname = netgraph._node[host]['label']
zebra_confs[host] = build_zebra_conf_for_host(hostname, interfaces)
return zebra_confs
def configure_nodes(netgraph):
"""
Create ospf6d and zebra conf files, push them to the relevant nodes, and enable the daemons
......@@ -117,11 +165,15 @@ def configure_nodes(netgraph):
:return: None
"""
ospf_push_commands = []
zebra_push_commands = []
ospf_enable_commands = []
zebra_enable_commands = []
hosts = [host for host in netgraph.nodes]
sessions = [session for session in map(lambda host : netgraph._node[host]['session'], hosts)]
ospf_confs = build_ospf_confs_for_network(netgraph)
zebra_confs = build_zebra_confs_for_network(netgraph)
# Push ospf6d configurations to all nodes
for host in hosts:
......@@ -138,8 +190,26 @@ def configure_nodes(netgraph):
daemon="ospf6d"
)
ospf_enable_commands.append(enable_command)
outputs = ssh_helper.run_commands_on_many_hosts(sessions, ospf_enable_commands)
# Push zebra configurations to all nodes
for host in hosts:
push_command = CONFIG_PUSH_COMMAND_TEMPLATE.format(
config=zebra_confs[host],
filename=ZEBRA_CONF_FILENAME,
)
zebra_push_commands.append(push_command)
outputs = ssh_helper.run_commands_on_many_hosts(sessions, zebra_push_commands)
# Enable zebra
for host in hosts:
enable_command = ENABLE_DAEMON_COMMAND_TEMPLATE.format(
daemon="zebra"
)
zebra_enable_commands.append(enable_command)
outputs = ssh_helper.run_commands_on_many_hosts(sessions, zebra_enable_commands)
ssh_helper.run_commands_on_many_hosts(sessions, ospf_enable_commands)
pass
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment