daemon: moved node cmd to base, allowing interface config to not need a node parameter

This commit is contained in:
Blake Harnden 2022-03-17 16:31:03 -07:00
parent cd7f1a641e
commit 7b16f9cb74
6 changed files with 32 additions and 35 deletions

View file

@ -408,12 +408,10 @@ class Session:
) )
if iface1: if iface1:
iface1.options.update(options) iface1.options.update(options)
node1_input = node1 if isinstance(node1, CoreNode) else None iface1.set_config()
iface1.set_config(node1_input)
if iface2 and not options.unidirectional: if iface2 and not options.unidirectional:
iface2.options.update(options) iface2.options.update(options)
node2_input = node2 if isinstance(node2, CoreNode) else None iface2.set_config()
iface2.set_config(node2_input)
def next_node_id(self) -> int: def next_node_id(self) -> int:
""" """

View file

@ -322,8 +322,7 @@ class BasicRangeModel(WirelessModel):
jitter=self.jitter, jitter=self.jitter,
) )
iface.options.update(options) iface.options.update(options)
if isinstance(iface.node, CoreNode): iface.set_config()
iface.set_config(iface.node)
def get_position(self, iface: CoreInterface) -> Tuple[float, float, float]: def get_position(self, iface: CoreInterface) -> Tuple[float, float, float]:
""" """

View file

@ -133,6 +133,19 @@ class NodeBase(abc.ABC):
else: else:
return self.server.remote_cmd(args, env, cwd, wait) return self.server.remote_cmd(args, env, cwd, wait)
def cmd(self, args: str, wait: bool = True, shell: bool = False) -> str:
"""
Runs a command that is in the context of a node, default is to run a standard
host command.
:param args: command to run
:param wait: True to wait for status, False otherwise
:param shell: True to use shell, False otherwise
:return: combined stdout and stderr
:raises CoreCommandError: when a non-zero exit status occurs
"""
return self.host_cmd(args, wait=wait, shell=shell)
def setposition(self, x: float = None, y: float = None, z: float = None) -> bool: def setposition(self, x: float = None, y: float = None, z: float = None) -> bool:
""" """
Set the (x,y,z) position of the object. Set the (x,y,z) position of the object.
@ -330,19 +343,6 @@ class CoreNodeBase(NodeBase):
""" """
raise NotImplementedError raise NotImplementedError
@abc.abstractmethod
def cmd(self, args: str, wait: bool = True, shell: bool = False) -> str:
"""
Runs a command within a node container.
:param args: command to run
:param wait: True to wait for status, False otherwise
:param shell: True to use shell, False otherwise
:return: combined stdout and stderr
:raises CoreCommandError: when a non-zero exit status occurs
"""
raise NotImplementedError
@abc.abstractmethod @abc.abstractmethod
def termcmdstring(self, sh: str) -> str: def termcmdstring(self, sh: str) -> str:
""" """
@ -786,7 +786,7 @@ class CoreNode(CoreNodeBase):
broadcast = "+" broadcast = "+"
self.node_net_client.create_address(iface.name, str(ip), broadcast) self.node_net_client.create_address(iface.name, str(ip), broadcast)
# configure iface options # configure iface options
iface.set_config(self) iface.set_config()
# set iface up # set iface up
self.node_net_client.device_up(iface.name) self.node_net_client.device_up(iface.name)

View file

@ -93,10 +93,9 @@ class DockerNode(CoreNode):
will run on, default is None for localhost will run on, default is None for localhost
:param image: image to start container with :param image: image to start container with
""" """
if image is None:
image = "ubuntu"
self.image: str = image
super().__init__(session, _id, name, directory, server) super().__init__(session, _id, name, directory, server)
self.image = image if image is not None else "ubuntu"
self.client: Optional[DockerClient] = None
def create_node_net_client(self, use_ovs: bool) -> LinuxNetClient: def create_node_net_client(self, use_ovs: bool) -> LinuxNetClient:
""" """
@ -141,7 +140,6 @@ class DockerNode(CoreNode):
# nothing to do if node is not up # nothing to do if node is not up
if not self.up: if not self.up:
return return
with self.lock: with self.lock:
self.ifaces.clear() self.ifaces.clear()
self.client.stop_container() self.client.stop_container()

View file

@ -293,21 +293,21 @@ class CoreInterface:
""" """
return self.transport_type == TransportType.VIRTUAL return self.transport_type == TransportType.VIRTUAL
def set_config(self, node: "CoreNode" = None) -> None: def set_config(self) -> None:
# clear current settings # clear current settings
if self.options.is_clear(): if self.options.is_clear():
if self.has_netem: if self.has_netem:
cmd = tc_clear_cmd(self.name) cmd = tc_clear_cmd(self.name)
if node: if self.node:
node.cmd(cmd) self.node.cmd(cmd)
else: else:
self.host_cmd(cmd) self.host_cmd(cmd)
self.has_netem = False self.has_netem = False
# set updated settings # set updated settings
else: else:
cmd = tc_cmd(self.name, self.options, self.mtu) cmd = tc_cmd(self.name, self.options, self.mtu)
if node: if self.node:
node.cmd(cmd) self.node.cmd(cmd)
else: else:
self.host_cmd(cmd) self.host_cmd(cmd)
self.has_netem = True self.has_netem = True

View file

@ -6,6 +6,7 @@ from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Callable, Dict, Optional from typing import TYPE_CHECKING, Callable, Dict, Optional
from core import utils from core import utils
from core.emulator.data import InterfaceData, LinkOptions
from core.emulator.distributed import DistributedServer from core.emulator.distributed import DistributedServer
from core.emulator.enumerations import NodeTypes from core.emulator.enumerations import NodeTypes
from core.errors import CoreCommandError from core.errors import CoreCommandError
@ -89,10 +90,9 @@ class LxcNode(CoreNode):
will run on, default is None for localhost will run on, default is None for localhost
:param image: image to start container with :param image: image to start container with
""" """
if image is None:
image = "ubuntu"
self.image: str = image
super().__init__(session, _id, name, directory, server) super().__init__(session, _id, name, directory, server)
self.image: str = image if image is not None else "ubuntu"
self.client: Optional[LxdClient] = None
def alive(self) -> bool: def alive(self) -> bool:
""" """
@ -125,7 +125,6 @@ class LxcNode(CoreNode):
# nothing to do if node is not up # nothing to do if node is not up
if not self.up: if not self.up:
return return
with self.lock: with self.lock:
self.ifaces.clear() self.ifaces.clear()
self.client.stop_container() self.client.stop_container()
@ -212,7 +211,10 @@ class LxcNode(CoreNode):
if mode is not None: if mode is not None:
self.cmd(f"chmod {mode:o} {dst_path}") self.cmd(f"chmod {mode:o} {dst_path}")
def add_iface(self, iface: CoreInterface, iface_id: int) -> None: def create_iface(
super().add_iface(iface, iface_id) self, iface_data: InterfaceData = None, options: LinkOptions = None
) -> CoreInterface:
iface = super().create_iface(iface_data, options)
# adding small delay to allow time for adding addresses to work correctly # adding small delay to allow time for adding addresses to work correctly
time.sleep(0.5) time.sleep(0.5)
return iface