Skip to content

Commit

Permalink
munet: run cleanup-cmd's before deleting nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
choppsv1 committed Apr 16, 2024
1 parent b2c517d commit 1642b96
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions munet/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,17 @@ async def _async_cleanup_cmd(self):
This function is called by subclass' async_cleanup_cmd
"""
self.cleanup_called = True

return await self._async_shebang_cmd("cleanup-cmd")

def has_cleanup_cmd(self) -> bool:
return bool(self.config.get("cleanup-cmd", "").strip())

async def async_cleanup_cmd(self):
"""Run the configured cleanup commands for this node."""
if self.cleanup_called:
return
self.cleanup_called = True

return await self._async_cleanup_cmd()

def has_ready_cmd(self) -> bool:
Expand Down Expand Up @@ -615,9 +617,6 @@ def __init__(

self.logger.info("%s: created", self)

def has_ready_cmd(self) -> bool:
return bool(self.config.get("ready-cmd", "").strip())

def _get_pre_cmd(self, use_str, use_pty, ns_only=False, **kwargs):
pre_cmd = []
if self.unet:
Expand Down Expand Up @@ -1522,11 +1521,14 @@ async def run_cmd(self):

async def async_cleanup_cmd(self):
"""Run the configured cleanup commands for this node."""
if self.cleanup_called:
return
self.cleanup_called = True

if "cleanup-cmd" not in self.config:
return

# The opposite of other types, the container needs cmd_p running
if not self.cmd_p:
self.logger.warning("async_cleanup_cmd: container no longer running")
return
Expand Down Expand Up @@ -2460,6 +2462,8 @@ def launch_completed(self, future):

async def async_cleanup_cmd(self):
"""Run the configured cleanup commands for this node."""
if self.cleanup_called:
return
self.cleanup_called = True

if "cleanup-cmd" not in self.config:
Expand Down Expand Up @@ -2890,10 +2894,8 @@ async def run(self):
hosts = self.hosts.values()
launch_nodes = [x for x in hosts if hasattr(x, "launch")]
launch_nodes = [x for x in launch_nodes if x.config.get("qemu")]
run_nodes = [x for x in hosts if hasattr(x, "has_run_cmd") and x.has_run_cmd()]
ready_nodes = [
x for x in hosts if hasattr(x, "has_ready_cmd") and x.has_ready_cmd()
]
run_nodes = [x for x in hosts if x.has_run_cmd()]
ready_nodes = [x for x in hosts if x.has_ready_cmd()]

pcapopt = self.cfgopt.getoption("--pcap")
pcapopt = pcapopt if pcapopt else ""
Expand Down Expand Up @@ -2975,15 +2977,6 @@ async def _async_delete(self):

self.logger.debug("%s: deleting.", self)

if self.cfgopt.getoption("--coverage"):
nodes = (
x for x in self.hosts.values() if hasattr(x, "gather_coverage_data")
)
try:
await asyncio.gather(*(x.gather_coverage_data() for x in nodes))
except Exception as error:
logging.warning("Error gathering coverage data: %s", error)

pause = bool(self.cfgopt.getoption("--pause-at-end"))
pause = pause or bool(self.cfgopt.getoption("--pause"))
if pause:
Expand All @@ -2994,6 +2987,23 @@ async def _async_delete(self):
except Exception as error:
self.logger.error("\n...continuing after error: %s", error)

# Run cleanup-cmd's.
nodes = (x for x in self.hosts.values() if x.has_cleanup_cmd())
try:
await asyncio.gather(*(x.async_cleanup_cmd() for x in nodes))
except Exception as error:
logging.warning("Error running cleanup cmds: %s", error)

# Gather any coverage data
if self.cfgopt.getoption("--coverage"):
nodes = (
x for x in self.hosts.values() if hasattr(x, "gather_coverage_data")
)
try:
await asyncio.gather(*(x.gather_coverage_data() for x in nodes))
except Exception as error:
logging.warning("Error gathering coverage data: %s", error)

# XXX should we cancel launch and run tasks?

try:
Expand Down

0 comments on commit 1642b96

Please sign in to comment.