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:
iface1.options.update(options)
node1_input = node1 if isinstance(node1, CoreNode) else None
iface1.set_config(node1_input)
iface1.set_config()
if iface2 and not options.unidirectional:
iface2.options.update(options)
node2_input = node2 if isinstance(node2, CoreNode) else None
iface2.set_config(node2_input)
iface2.set_config()
def next_node_id(self) -> int:
"""

View file

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

View file

@ -133,6 +133,19 @@ class NodeBase(abc.ABC):
else:
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:
"""
Set the (x,y,z) position of the object.
@ -330,19 +343,6 @@ class CoreNodeBase(NodeBase):
"""
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
def termcmdstring(self, sh: str) -> str:
"""
@ -786,7 +786,7 @@ class CoreNode(CoreNodeBase):
broadcast = "+"
self.node_net_client.create_address(iface.name, str(ip), broadcast)
# configure iface options
iface.set_config(self)
iface.set_config()
# set iface up
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
:param image: image to start container with
"""
if image is None:
image = "ubuntu"
self.image: str = image
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:
"""
@ -141,7 +140,6 @@ class DockerNode(CoreNode):
# nothing to do if node is not up
if not self.up:
return
with self.lock:
self.ifaces.clear()
self.client.stop_container()

View file

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

View file

@ -6,6 +6,7 @@ from tempfile import NamedTemporaryFile
from typing import TYPE_CHECKING, Callable, Dict, Optional
from core import utils
from core.emulator.data import InterfaceData, LinkOptions
from core.emulator.distributed import DistributedServer
from core.emulator.enumerations import NodeTypes
from core.errors import CoreCommandError
@ -89,10 +90,9 @@ class LxcNode(CoreNode):
will run on, default is None for localhost
:param image: image to start container with
"""
if image is None:
image = "ubuntu"
self.image: str = image
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:
"""
@ -125,7 +125,6 @@ class LxcNode(CoreNode):
# nothing to do if node is not up
if not self.up:
return
with self.lock:
self.ifaces.clear()
self.client.stop_container()
@ -212,7 +211,10 @@ class LxcNode(CoreNode):
if mode is not None:
self.cmd(f"chmod {mode:o} {dst_path}")
def add_iface(self, iface: CoreInterface, iface_id: int) -> None:
super().add_iface(iface, iface_id)
def create_iface(
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
time.sleep(0.5)
return iface