Skip to content

Commit 6240aad

Browse files
committed
control & test base: prevent hasty downs
This does a check to ensure users don't nuke the network by entering `warnet down`. It will prompt the users if there are multiple namespaces affected. Also adds a `--force` option to bypass this.
1 parent fd282ef commit 6240aad

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/warnet/control.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,15 @@ def stop_all_scenarios(scenarios):
113113
console.print("[bold green]All scenarios have been stopped.[/bold green]")
114114

115115

116+
@click.option(
117+
"--force",
118+
is_flag=True,
119+
default=False,
120+
help="Skip confirmations",
121+
)
116122
@click.command()
117-
def down():
123+
def down(force):
118124
"""Bring down a running warnet quickly"""
119-
console.print("[bold yellow]Bringing down the warnet...[/bold yellow]")
120125

121126
def uninstall_release(namespace, release_name):
122127
cmd = f"helm uninstall {release_name} --namespace {namespace} --wait=false"
@@ -128,19 +133,53 @@ def delete_pod(pod_name, namespace):
128133
subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
129134
return f"Initiated deletion of pod: {pod_name} in namespace {namespace}"
130135

136+
namespaces = get_namespaces()
137+
print(namespaces)
138+
release_list: list[dict[str, str]] = []
139+
for namespace in namespaces:
140+
command = f"helm list --namespace {namespace.metadata.name} -o json"
141+
result = run_command(command)
142+
if result:
143+
releases = json.loads(result)
144+
for release in releases:
145+
release_list.append({"namespace": namespace.metadata.name, "name": release["name"]})
146+
147+
if not force:
148+
affected_namespaces = set([entry["namespace"] for entry in release_list])
149+
namespace_listing = "\n ".join(affected_namespaces)
150+
confirmed = "confirmed"
151+
click.secho("Preparing to bring down the running Warnet...", fg="yellow")
152+
click.secho("The listed namespaces will be affected:", fg="yellow")
153+
click.secho(f" {namespace_listing}", fg="blue")
154+
155+
proj_answers = inquirer.prompt(
156+
[
157+
inquirer.Confirm(
158+
confirmed,
159+
message=click.style(
160+
"Do you want to bring down the running Warnet?", fg="yellow", bold=False
161+
),
162+
default=False,
163+
),
164+
]
165+
)
166+
if not proj_answers:
167+
click.secho("Operation cancelled by user.", fg="yellow")
168+
sys.exit(0)
169+
if proj_answers[confirmed]:
170+
click.secho("Bringing down the warnet...", fg="yellow")
171+
else:
172+
click.secho("Operation cancelled by user", fg="yellow")
173+
sys.exit(0)
174+
131175
with ThreadPoolExecutor(max_workers=10) as executor:
132176
futures = []
133177

134178
# Uninstall Helm releases
135-
for namespace in get_namespaces():
136-
command = f"helm list --namespace {namespace.metadata.name} -o json"
137-
result = run_command(command)
138-
if result:
139-
releases = json.loads(result)
140-
for release in releases:
141-
futures.append(
142-
executor.submit(uninstall_release, namespace.metadata.name, release["name"])
143-
)
179+
for release in release_list:
180+
futures.append(
181+
executor.submit(uninstall_release, release["namespace"], release["name"])
182+
)
144183

145184
# Delete remaining pods
146185
pods = get_pods()

test/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def cleanup(self, signum=None, frame=None):
4444
try:
4545
self.log.info("Stopping network")
4646
if self.network:
47-
self.warnet("down")
47+
self.warnet("down --force")
4848
self.wait_for_all_tanks_status(target="stopped", timeout=60, interval=1)
4949
except Exception as e:
5050
self.log.error(f"Error bringing network down: {e}")

0 commit comments

Comments
 (0)