diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index 26a8efdf..9525f7ca 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -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: """ diff --git a/daemon/core/location/mobility.py b/daemon/core/location/mobility.py index 7662b765..31127346 100644 --- a/daemon/core/location/mobility.py +++ b/daemon/core/location/mobility.py @@ -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]: """ diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index 845eafc1..d7c2c02f 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -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) diff --git a/daemon/core/nodes/docker.py b/daemon/core/nodes/docker.py index d5e928de..0a0690b1 100644 --- a/daemon/core/nodes/docker.py +++ b/daemon/core/nodes/docker.py @@ -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() diff --git a/daemon/core/nodes/interface.py b/daemon/core/nodes/interface.py index f1857950..bb90653f 100644 --- a/daemon/core/nodes/interface.py +++ b/daemon/core/nodes/interface.py @@ -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 diff --git a/daemon/core/nodes/lxd.py b/daemon/core/nodes/lxd.py index 54fc8341..e755e9c9 100644 --- a/daemon/core/nodes/lxd.py +++ b/daemon/core/nodes/lxd.py @@ -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