Skip to content

Commit 82bd6f1

Browse files
committed
protect users (and admins) from downing nets
1 parent f9eec7c commit 82bd6f1

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
@@ -108,10 +108,15 @@ def stop_all_scenarios(scenarios):
108108
console.print("[bold green]All scenarios have been stopped.[/bold green]")
109109

110110

111+
@click.option(
112+
"--force",
113+
is_flag=True,
114+
default=False,
115+
help="Skip confirmations",
116+
)
111117
@click.command()
112-
def down():
118+
def down(force):
113119
"""Bring down a running warnet quickly"""
114-
console.print("[bold yellow]Bringing down the warnet...[/bold yellow]")
115120

116121
def uninstall_release(namespace, release_name):
117122
cmd = f"helm uninstall {release_name} --namespace {namespace} --wait=false"
@@ -123,19 +128,53 @@ def delete_pod(pod_name, namespace):
123128
subprocess.Popen(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
124129
return f"Initiated deletion of pod: {pod_name} in namespace {namespace}"
125130

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

129173
# Uninstall Helm releases
130-
for namespace in get_namespaces():
131-
command = f"helm list --namespace {namespace.metadata.name} -o json"
132-
result = run_command(command)
133-
if result:
134-
releases = json.loads(result)
135-
for release in releases:
136-
futures.append(
137-
executor.submit(uninstall_release, namespace.metadata.name, release["name"])
138-
)
174+
for release in release_list:
175+
futures.append(
176+
executor.submit(uninstall_release, release["namespace"], release["name"])
177+
)
139178

140179
# Delete remaining pods
141180
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)