Skip to content

Commit d90c47a

Browse files
committed
control: add --debug option to run WIP scenarios
1 parent edbaa30 commit d90c47a

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/warnet/control.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
from .constants import COMMANDER_CHART, LOGGING_NAMESPACE
1919
from .deploy import _port_stop_internal
2020
from .k8s import (
21+
delete_pod,
2122
get_default_namespace,
2223
get_mission,
2324
get_pods,
25+
pod_log,
2426
snapshot_bitcoin_datadir,
25-
pod_log
27+
wait_for_pod,
2628
)
2729
from .process import run_command, stream_command
2830

@@ -164,8 +166,14 @@ def get_active_network(namespace):
164166

165167
@click.command(context_settings={"ignore_unknown_options": True})
166168
@click.argument("scenario_file", type=click.Path(exists=True, file_okay=True, dir_okay=False))
169+
@click.option(
170+
"--debug",
171+
is_flag=True,
172+
default=False,
173+
help="Stream scenario output and delete container when stopped",
174+
)
167175
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
168-
def run(scenario_file: str, additional_args: tuple[str]):
176+
def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
169177
"""
170178
Run a scenario from a file.
171179
Pass `-- --help` to get individual scenario help
@@ -233,11 +241,22 @@ def run(scenario_file: str, additional_args: tuple[str]):
233241
print(f"Failed to start scenario: {scenario_name}")
234242
print(f"Error: {e.stderr}")
235243

244+
if debug:
245+
print("Waiting for commander pod to start...")
246+
wait_for_pod(name)
247+
_logs(pod_name=name, follow=True)
248+
print("Deleting pod...")
249+
delete_pod(name)
250+
236251

237252
@click.command()
238253
@click.argument("pod_name", type=str, default="")
239254
@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
240255
def logs(pod_name: str, follow: bool):
256+
return _logs(pod_name, follow)
257+
258+
259+
def _logs(pod_name: str, follow: bool):
241260
"""Show the logs of a pod"""
242261
namespace = get_default_namespace()
243262

@@ -269,9 +288,11 @@ def logs(pod_name: str, follow: bool):
269288
try:
270289
stream = pod_log(pod_name, container_name=None, follow=follow)
271290
for line in stream.stream():
272-
print(line.decode('utf-8'), end=None)
291+
print(line.decode("utf-8"), end=None)
273292
except Exception as e:
274293
print(e)
294+
except KeyboardInterrupt:
295+
print("Interrupted streaming log!")
275296

276297

277298
@click.command()

src/warnet/k8s.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import sys
44
import tempfile
55
from pathlib import Path
6+
from time import sleep
67

78
import yaml
89
from kubernetes import client, config, watch
910
from kubernetes.client.models import CoreV1Event, V1PodList
11+
from kubernetes.client.rest import ApiException
1012
from kubernetes.dynamic import DynamicClient
1113
from kubernetes.stream import stream
12-
from kubernetes.client.rest import ApiException
1314

1415
from .constants import DEFAULT_NAMESPACE, KUBECONFIG
1516
from .process import run_command, stream_command
@@ -266,7 +267,17 @@ def pod_log(pod_name, container_name=None, follow=False):
266267
namespace=get_default_namespace(),
267268
container=container_name,
268269
follow=follow,
269-
_preload_content=False
270+
_preload_content=False,
270271
)
271272
except ApiException as e:
272-
raise Exception(json.loads(e.body.decode('utf-8'))["message"])
273+
raise Exception(json.loads(e.body.decode("utf-8"))["message"]) from None
274+
275+
276+
def wait_for_pod(pod_name, timeout_seconds=10):
277+
sclient = get_static_client()
278+
while timeout_seconds > 0:
279+
pod = sclient.read_namespaced_pod_status(name=pod_name, namespace=get_default_namespace())
280+
if pod.status.phase != "Pending":
281+
return
282+
sleep(1)
283+
timeout_seconds -= 1

0 commit comments

Comments
 (0)