From 9b7c30ad75dc2e81082df8eb9a1ff84f4bed38c4 Mon Sep 17 00:00:00 2001 From: willcl-ark Date: Fri, 13 Sep 2024 08:44:17 +0100 Subject: [PATCH] enable stop for binaries --- src/warnet/control.py | 47 +++++++++++++++++++++---------------------- test/test_base.py | 4 ++-- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/warnet/control.py b/src/warnet/control.py index 6522279d0..06f1e11e2 100644 --- a/src/warnet/control.py +++ b/src/warnet/control.py @@ -25,39 +25,38 @@ snapshot_bitcoin_datadir, ) from .process import run_command, stream_command +from .status import _get_active_binaries, _get_active_scenarios console = Console() -def get_active_scenarios(): - """Get list of active scenarios""" - commanders = get_mission("commander") - return [c.metadata.name for c in commanders] - - @click.command() -@click.argument("scenario_name", required=False) -def stop(scenario_name): - """Stop a running scenario or all scenarios""" - active_scenarios = get_active_scenarios() +@click.argument("name", required=False) +def stop(name): + """Stop one or all running scenarios or binaries""" + all_running = [c["name"] for c in _get_active_scenarios()] + [ + b["name"] for b in _get_active_binaries() + ] - if not active_scenarios: - console.print("[bold red]No active scenarios found.[/bold red]") + if not all_running: + console.print("[bold red]No active scenarios or binaries found.[/bold red]") return - if not scenario_name: - table = Table(title="Active Scenarios", show_header=True, header_style="bold magenta") + if not name: + table = Table( + title="Active Scenarios & binaries", show_header=True, header_style="bold magenta" + ) table.add_column("Number", style="cyan", justify="right") - table.add_column("Scenario Name", style="green") + table.add_column("Name", style="green") - for idx, name in enumerate(active_scenarios, 1): + for idx, name in enumerate(all_running, 1): table.add_row(str(idx), name) console.print(table) - choices = [str(i) for i in range(1, len(active_scenarios) + 1)] + ["a", "q"] + choices = [str(i) for i in range(1, len(all_running) + 1)] + ["a", "q"] choice = Prompt.ask( - "[bold yellow]Enter the number of the scenario to stop, 'a' to stop all, or 'q' to quit[/bold yellow]", + "[bold yellow]Enter the number you want to stop, 'a' to stop all, or 'q' to quit[/bold yellow]", choices=choices, show_choices=False, ) @@ -67,18 +66,18 @@ def stop(scenario_name): return elif choice == "a": if Confirm.ask("[bold red]Are you sure you want to stop all scenarios?[/bold red]"): - stop_all_scenarios(active_scenarios) + stop_all_scenarios(all_running) else: console.print("[bold blue]Operation cancelled.[/bold blue]") return - scenario_name = active_scenarios[int(choice) - 1] + name = all_running[int(choice) - 1] - if scenario_name not in active_scenarios: - console.print(f"[bold red]No active scenario found with name: {scenario_name}[/bold red]") + if name not in all_running: + console.print(f"[bold red]No active scenario or binary found with name: {name}[/bold red]") return - stop_scenario(scenario_name) + stop_scenario(name) def stop_scenario(scenario_name): @@ -111,7 +110,7 @@ def stop_all_scenarios(scenarios): def list_active_scenarios(): """List all active scenarios""" - active_scenarios = get_active_scenarios() + active_scenarios = [c["name"] for c in _get_active_scenarios()] if not active_scenarios: print("No active scenarios found.") return diff --git a/test/test_base.py b/test/test_base.py index fbea5e79d..3b00f7a5a 100644 --- a/test/test_base.py +++ b/test/test_base.py @@ -10,9 +10,9 @@ from time import sleep from warnet import SRC_DIR -from warnet.control import get_active_scenarios from warnet.k8s import get_pod_exit_status from warnet.network import _connected as network_connected +from warnet.status import _get_active_scenarios as get_active_scenarios from warnet.status import _get_tank_status as network_status @@ -126,7 +126,7 @@ def wait_for_all_edges(self, timeout=20 * 60, interval=5): def wait_for_all_scenarios(self): def check_scenarios(): - scns = get_active_scenarios() + scns = [c["name"] for c in get_active_scenarios()] if len(scns) == 0: return True for s in scns: