added config service manager, added custom loading of subdirs for config based services, added configurations for config services
This commit is contained in:
parent
cf7dda816c
commit
433327c0ae
7 changed files with 194 additions and 52 deletions
|
@ -1,46 +0,0 @@
|
|||
import logging
|
||||
|
||||
import netaddr
|
||||
|
||||
from core.configservice.base import ConfigService, ConfigServiceMode
|
||||
from core.emulator.session import Session
|
||||
from core.nodes.base import CoreNode
|
||||
from core.nodes.interface import Veth
|
||||
|
||||
|
||||
class DefaultRoute(ConfigService):
|
||||
name = "DefaultRoute"
|
||||
group = "Utility"
|
||||
directories = []
|
||||
executables = []
|
||||
dependencies = []
|
||||
startup = []
|
||||
validate = []
|
||||
shutdown = []
|
||||
validation_mode = ConfigServiceMode.BLOCKING
|
||||
|
||||
def create_files(self):
|
||||
self.create_default_route()
|
||||
|
||||
def create_default_route(self):
|
||||
addresses = []
|
||||
for netif in self.node.netifs():
|
||||
if getattr(netif, "control", False):
|
||||
continue
|
||||
for addr in netif.addrlist:
|
||||
net = netaddr.IPNetwork(addr)
|
||||
if net[1] != net[-2]:
|
||||
addresses.append(net[1])
|
||||
data = dict(addresses=addresses)
|
||||
self.render("defaultroute.sh", data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
session = Session(1, mkdir=False)
|
||||
node = CoreNode(session, _id=1, start=False)
|
||||
netif = Veth(session, node, "eth0", "eth0", start=False)
|
||||
netif.addaddr("10.0.0.1/24")
|
||||
node.addnetif(netif, 0)
|
||||
service = DefaultRoute(node)
|
||||
service.start()
|
|
@ -1,5 +1,6 @@
|
|||
#!/bin/sh
|
||||
# auto-generated by DefaultRoute service
|
||||
# config: ${config}
|
||||
% for address in addresses:
|
||||
ip route add default via ${address}
|
||||
% endfor
|
|
@ -0,0 +1,16 @@
|
|||
#!/bin/sh
|
||||
# auto-generated by IPForward service (utility.py)
|
||||
sysctl -w net.ipv4.conf.all.forwarding=1
|
||||
sysctl -w net.ipv4.conf.default.forwarding=1
|
||||
sysctl -w net.ipv6.conf.all.forwarding=1
|
||||
sysctl -w net.ipv6.conf.default.forwarding=1
|
||||
sysctl -w net.ipv4.conf.all.send_redirects=0
|
||||
sysctl -w net.ipv4.conf.default.send_redirects=0
|
||||
sysctl -w net.ipv4.conf.all.rp_filter=0
|
||||
sysctl -w net.ipv4.conf.default.rp_filter=0
|
||||
# setup forwarding for node interfaces
|
||||
% for devname in devnames:
|
||||
sysctl -w net.ipv4.conf.${devname}.forwarding=1
|
||||
sysctl -w net.ipv4.conf.${devname}.send_redirects=0
|
||||
sysctl -w net.ipv4.conf.${devname}.rp_filter=0
|
||||
% endfor
|
109
daemon/core/configservices/serviceutils/utils.py
Normal file
109
daemon/core/configservices/serviceutils/utils.py
Normal file
|
@ -0,0 +1,109 @@
|
|||
import logging
|
||||
import os
|
||||
|
||||
import netaddr
|
||||
|
||||
from core import utils
|
||||
from core.config import Configuration
|
||||
from core.configservice.base import (
|
||||
ConfigService,
|
||||
ConfigServiceManager,
|
||||
ConfigServiceMode,
|
||||
)
|
||||
from core.emulator.coreemu import CoreEmu
|
||||
from core.emulator.emudata import IpPrefixes, NodeOptions
|
||||
from core.emulator.enumerations import ConfigDataTypes, EventTypes, NodeTypes
|
||||
|
||||
GROUP_NAME = "Utility"
|
||||
|
||||
|
||||
class DefaultRoute(ConfigService):
|
||||
name = "DefaultRoute"
|
||||
group = GROUP_NAME
|
||||
directories = []
|
||||
executables = []
|
||||
dependencies = []
|
||||
startup = ["sh defaultroute.sh"]
|
||||
validate = []
|
||||
shutdown = []
|
||||
validation_mode = ConfigServiceMode.BLOCKING
|
||||
default_configs = [
|
||||
Configuration(_id="value1", _type=ConfigDataTypes.STRING, label="Value 1"),
|
||||
Configuration(_id="value2", _type=ConfigDataTypes.STRING, label="Value 2"),
|
||||
Configuration(_id="value3", _type=ConfigDataTypes.STRING, label="Value 3"),
|
||||
]
|
||||
|
||||
def create_files(self):
|
||||
addresses = []
|
||||
for netif in self.node.netifs():
|
||||
if getattr(netif, "control", False):
|
||||
continue
|
||||
for addr in netif.addrlist:
|
||||
net = netaddr.IPNetwork(addr)
|
||||
if net[1] != net[-2]:
|
||||
addresses.append(net[1])
|
||||
data = dict(addresses=addresses)
|
||||
self.render("defaultroute.sh", data)
|
||||
|
||||
|
||||
class IpForwardService(ConfigService):
|
||||
name = "IPForward"
|
||||
group = GROUP_NAME
|
||||
directories = []
|
||||
executables = ["sysctl"]
|
||||
dependencies = []
|
||||
startup = ["sh ipforward.sh"]
|
||||
validate = []
|
||||
shutdown = []
|
||||
validation_mode = ConfigServiceMode.BLOCKING
|
||||
default_configs = []
|
||||
|
||||
def create_files(self) -> None:
|
||||
devnames = []
|
||||
for ifc in self.node.netifs():
|
||||
devname = utils.sysctl_devname(ifc.name)
|
||||
devnames.append(devname)
|
||||
data = dict(devnames=devnames)
|
||||
self.render("ipforward.sh", data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# setup basic network
|
||||
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
|
||||
options = NodeOptions(model="nothing")
|
||||
# options.services = []
|
||||
coreemu = CoreEmu()
|
||||
session = coreemu.create_session()
|
||||
session.set_state(EventTypes.CONFIGURATION_STATE)
|
||||
switch = session.add_node(_type=NodeTypes.SWITCH)
|
||||
|
||||
# node one
|
||||
node_one = session.add_node(options=options)
|
||||
interface = prefixes.create_interface(node_one)
|
||||
session.add_link(node_one.id, switch.id, interface_one=interface)
|
||||
|
||||
# node two
|
||||
node_two = session.add_node(options=options)
|
||||
interface = prefixes.create_interface(node_two)
|
||||
session.add_link(node_two.id, switch.id, interface_one=interface)
|
||||
|
||||
session.instantiate()
|
||||
|
||||
# manager load config services
|
||||
manager = ConfigServiceManager()
|
||||
path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
|
||||
manager.load(path)
|
||||
|
||||
clazz = manager.services["DefaultRoute"]
|
||||
dr_service = clazz(node_one)
|
||||
dr_service.set_config({"value1": "custom"})
|
||||
dr_service.start()
|
||||
|
||||
clazz = manager.services["IPForward"]
|
||||
dr_service = clazz(node_one)
|
||||
dr_service.start()
|
||||
|
||||
input("press enter to exit")
|
||||
session.shutdown()
|
Loading…
Add table
Add a link
Reference in a new issue