diff --git a/marvis/node/docker.py b/marvis/node/docker.py index 90df183c..31ae69f1 100644 --- a/marvis/node/docker.py +++ b/marvis/node/docker.py @@ -6,6 +6,7 @@ from nsenter import Namespace import docker +import pyroute2 from ..context import defer from ..command_executor import DockerCommandExecutor @@ -161,7 +162,7 @@ def prepare(self, simulation): else: self.start_docker_container(simulation.log_directory, simulation.hosts) self.setup_host_interfaces() - + def build_docker_image(self): """Build the image for the container.""" client = docker.from_env() @@ -238,6 +239,28 @@ def start_docker_container(self, log_directory, hosts=None): self.command_executor = DockerCommandExecutor(self.name, self.container) + def restart_docker_container(self, simulation): + """This restarts a container and connects it back to the network. """ + logger.info('Restarting node %s', self.name) + self.stop_docker_container() + + self.start_docker_container(simulation.log_directory, simulation.hosts) + for name, interface in self.interfaces.items(): + # The veth pair might still exist but is then connected to the namespace of the old container + try: + interface.remove_veth_pair() + except pyroute2.netlink.exceptions.NetlinkError as exception: + pass + + interface.setup_veth_pair({ + 'ifname': name, + "net_ns_fd": f"/proc/{self.container_pid}/ns/net" + }) + + # Get container's namespace and setup the interface in the container + with Namespace(self.container_pid, 'net'): + interface.setup_veth_container_end(name) + def stop_docker_container(self): """Stop the container.""" if self.container is None: