daemon: added type hinting to DistributedControll and removed bad logic looking for tunnels during add_link

This commit is contained in:
Blake Harnden 2020-06-09 21:03:19 -07:00
parent 32ad8a9b68
commit 452e0720f2
3 changed files with 18 additions and 70 deletions

View file

@ -37,10 +37,10 @@ class DistributedServer:
:param name: convenience name to associate with host :param name: convenience name to associate with host
:param host: host to connect to :param host: host to connect to
""" """
self.name = name self.name: str = name
self.host = host self.host: str = host
self.conn = Connection(host, user="root") self.conn: Connection = Connection(host, user="root")
self.lock = threading.Lock() self.lock: threading.Lock = threading.Lock()
def remote_cmd( def remote_cmd(
self, cmd: str, env: Dict[str, str] = None, cwd: str = None, wait: bool = True self, cmd: str, env: Dict[str, str] = None, cwd: str = None, wait: bool = True
@ -117,10 +117,10 @@ class DistributedController:
:param session: session :param session: session
""" """
self.session = session self.session: "Session" = session
self.servers = OrderedDict() self.servers: Dict[str, DistributedServer] = OrderedDict()
self.tunnels = {} self.tunnels: Dict[int, Tuple[GreTap, GreTap]] = {}
self.address = self.session.options.get_config( self.address: str = self.session.options.get_config(
"distributed_address", default=None "distributed_address", default=None
) )
@ -178,13 +178,10 @@ class DistributedController:
""" """
for node_id in self.session.nodes: for node_id in self.session.nodes:
node = self.session.nodes[node_id] node = self.session.nodes[node_id]
if not isinstance(node, CoreNetwork): if not isinstance(node, CoreNetwork):
continue continue
if isinstance(node, CtrlNet) and node.serverintf is not None: if isinstance(node, CtrlNet) and node.serverintf is not None:
continue continue
for name in self.servers: for name in self.servers:
server = self.servers[name] server = self.servers[name]
self.create_gre_tunnel(node, server) self.create_gre_tunnel(node, server)
@ -195,7 +192,6 @@ class DistributedController:
""" """
Create gre tunnel using a pair of gre taps between the local and remote server. Create gre tunnel using a pair of gre taps between the local and remote server.
:param node: node to create gre tunnel for :param node: node to create gre tunnel for
:param server: server to create :param server: server to create
tunnel for tunnel for
@ -243,15 +239,3 @@ class DistributedController:
(self.session.id << 16) ^ utils.hashkey(n1_id) ^ (utils.hashkey(n2_id) << 8) (self.session.id << 16) ^ utils.hashkey(n1_id) ^ (utils.hashkey(n2_id) << 8)
) )
return key & 0xFFFFFFFF return key & 0xFFFFFFFF
def get_tunnel(self, n1_id: int, n2_id: int) -> GreTap:
"""
Return the GreTap between two nodes if it exists.
:param n1_id: node one id
:param n2_id: node two id
:return: gre tap between nodes or None
"""
key = self.tunnel_key(n1_id, n2_id)
logging.debug("checking for tunnel key(%s) in: %s", key, self.tunnels)
return self.tunnels.get(key)

View file

@ -42,7 +42,7 @@ from core.location.geo import GeoLocation
from core.location.mobility import BasicRangeModel, MobilityManager from core.location.mobility import BasicRangeModel, MobilityManager
from core.nodes.base import CoreNetworkBase, CoreNode, CoreNodeBase, NodeBase from core.nodes.base import CoreNetworkBase, CoreNode, CoreNodeBase, NodeBase
from core.nodes.docker import DockerNode from core.nodes.docker import DockerNode
from core.nodes.interface import CoreInterface, GreTap from core.nodes.interface import CoreInterface
from core.nodes.lxd import LxcNode from core.nodes.lxd import LxcNode
from core.nodes.network import ( from core.nodes.network import (
CtrlNet, CtrlNet,
@ -200,7 +200,6 @@ class Session:
Optional[CoreNode], Optional[CoreNode],
Optional[CoreNetworkBase], Optional[CoreNetworkBase],
Optional[CoreNetworkBase], Optional[CoreNetworkBase],
GreTap,
]: ]:
""" """
Convenience method for retrieving nodes within link data. Convenience method for retrieving nodes within link data.
@ -221,23 +220,6 @@ class Session:
node_one = self.get_node(node_one_id, NodeBase) node_one = self.get_node(node_one_id, NodeBase)
node_two = self.get_node(node_two_id, NodeBase) node_two = self.get_node(node_two_id, NodeBase)
# both node ids are provided
tunnel = self.distributed.get_tunnel(node_one_id, node_two_id)
logging.debug("tunnel between nodes: %s", tunnel)
if isinstance(tunnel, GreTapBridge):
net_one = tunnel
if tunnel.remotenum == node_one_id:
node_one = None
else:
node_two = None
# physical node connected via gre tap tunnel
# TODO: double check this cases type
elif tunnel:
if tunnel.remotenum == node_one_id:
node_one = None
else:
node_two = None
if isinstance(node_one, CoreNetworkBase): if isinstance(node_one, CoreNetworkBase):
if not net_one: if not net_one:
net_one = node_one net_one = node_one
@ -253,14 +235,13 @@ class Session:
node_two = None node_two = None
logging.debug( logging.debug(
"link node types n1(%s) n2(%s) net1(%s) net2(%s) tunnel(%s)", "link node types n1(%s) n2(%s) net1(%s) net2(%s)",
node_one, node_one,
node_two, node_two,
net_one, net_one,
net_two, net_two,
tunnel,
) )
return node_one, node_two, net_one, net_two, tunnel return node_one, node_two, net_one, net_two
def _link_wireless(self, objects: Iterable[CoreNodeBase], connect: bool) -> None: def _link_wireless(self, objects: Iterable[CoreNodeBase], connect: bool) -> None:
""" """
@ -326,7 +307,7 @@ class Session:
options = LinkOptions() options = LinkOptions()
# get node objects identified by link data # get node objects identified by link data
node_one, node_two, net_one, net_two, tunnel = self._link_nodes( node_one, node_two, net_one, net_two = self._link_nodes(
node_one_id, node_two_id node_one_id, node_two_id
) )
@ -415,23 +396,6 @@ class Session:
net_two.setkey(key) net_two.setkey(key)
if addresses: if addresses:
net_two.addrconfig(addresses) net_two.addrconfig(addresses)
# physical node connected with tunnel
if not net_one and not net_two and (node_one or node_two):
if node_one and isinstance(node_one, PhysicalNode):
logging.info("adding link for physical node: %s", node_one.name)
addresses = interface_one.get_addresses()
node_one.adoptnetif(
tunnel, interface_one.id, interface_one.mac, addresses
)
node_one.linkconfig(tunnel, options)
elif node_two and isinstance(node_two, PhysicalNode):
logging.info("adding link for physical node: %s", node_two.name)
addresses = interface_two.get_addresses()
node_two.adoptnetif(
tunnel, interface_two.id, interface_two.mac, addresses
)
node_two.linkconfig(tunnel, options)
finally: finally:
if node_one: if node_one:
node_one.lock.release() node_one.lock.release()
@ -461,7 +425,7 @@ class Session:
:raises core.CoreError: when no common network is found for link being deleted :raises core.CoreError: when no common network is found for link being deleted
""" """
# get node objects identified by link data # get node objects identified by link data
node_one, node_two, net_one, net_two, _tunnel = self._link_nodes( node_one, node_two, net_one, net_two = self._link_nodes(
node_one_id, node_two_id node_one_id, node_two_id
) )
@ -573,7 +537,7 @@ class Session:
options = LinkOptions() options = LinkOptions()
# get node objects identified by link data # get node objects identified by link data
node_one, node_two, net_one, net_two, _tunnel = self._link_nodes( node_one, node_two, net_one, net_two = self._link_nodes(
node_one_id, node_two_id node_one_id, node_two_id
) )

View file

@ -1,4 +1,4 @@
from typing import Any from typing import Any, List
from core.config import ConfigurableManager, ConfigurableOptions, Configuration from core.config import ConfigurableManager, ConfigurableOptions, Configuration
from core.emulator.enumerations import ConfigDataTypes, RegisterTlvs from core.emulator.enumerations import ConfigDataTypes, RegisterTlvs
@ -10,8 +10,8 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
Provides session configuration. Provides session configuration.
""" """
name = "session" name: str = "session"
options = [ options: List[Configuration] = [
Configuration( Configuration(
_id="controlnet", _type=ConfigDataTypes.STRING, label="Control Network" _id="controlnet", _type=ConfigDataTypes.STRING, label="Control Network"
), ),
@ -57,7 +57,7 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
label="SDT3D URL", label="SDT3D URL",
), ),
] ]
config_type = RegisterTlvs.UTILITY config_type: RegisterTlvs = RegisterTlvs.UTILITY
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()