Skip to content

Commit

Permalink
enable stop for binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark committed Sep 16, 2024
1 parent 363a94e commit 2ddb7fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
47 changes: 23 additions & 24 deletions src/warnet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2ddb7fa

Please sign in to comment.