Skip to content

Commit 5f46b8a

Browse files
committed
control: add --debug option to run WIP scenarios
1 parent c69f222 commit 5f46b8a

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
@@ -17,11 +17,13 @@
1717

1818
from .constants import COMMANDER_CHART, LOGGING_NAMESPACE
1919
from .k8s import (
20+
delete_pod,
2021
get_default_namespace,
2122
get_mission,
2223
get_pods,
24+
pod_log,
2325
snapshot_bitcoin_datadir,
24-
pod_log
26+
wait_for_pod,
2527
)
2628
from .process import run_command, stream_command
2729

@@ -161,8 +163,14 @@ def get_active_network(namespace):
161163

162164
@click.command(context_settings={"ignore_unknown_options": True})
163165
@click.argument("scenario_file", type=click.Path(exists=True, file_okay=True, dir_okay=False))
166+
@click.option(
167+
"--debug",
168+
is_flag=True,
169+
default=False,
170+
help="Stream scenario output and delete container when stopped",
171+
)
164172
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
165-
def run(scenario_file: str, additional_args: tuple[str]):
173+
def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
166174
"""
167175
Run a scenario from a file.
168176
Pass `-- --help` to get individual scenario help
@@ -230,11 +238,22 @@ def run(scenario_file: str, additional_args: tuple[str]):
230238
print(f"Failed to start scenario: {scenario_name}")
231239
print(f"Error: {e.stderr}")
232240

241+
if debug:
242+
print("Waiting for commander pod to start...")
243+
wait_for_pod(name)
244+
_logs(pod_name=name, follow=True)
245+
print("Deleting pod...")
246+
delete_pod(name)
247+
233248

234249
@click.command()
235250
@click.argument("pod_name", type=str, default="")
236251
@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
237252
def logs(pod_name: str, follow: bool):
253+
return _logs(pod_name, follow)
254+
255+
256+
def _logs(pod_name: str, follow: bool):
238257
"""Show the logs of a pod"""
239258
namespace = get_default_namespace()
240259

@@ -266,9 +285,11 @@ def logs(pod_name: str, follow: bool):
266285
try:
267286
stream = pod_log(pod_name, container_name=None, follow=follow)
268287
for line in stream.stream():
269-
print(line.decode('utf-8'), end=None)
288+
print(line.decode("utf-8"), end=None)
270289
except Exception as e:
271290
print(e)
291+
except KeyboardInterrupt:
292+
print("Interrupted streaming log!")
272293

273294

274295
@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 (
1516
CADDY_INGRESS_NAME,
@@ -293,7 +294,17 @@ def pod_log(pod_name, container_name=None, follow=False):
293294
namespace=get_default_namespace(),
294295
container=container_name,
295296
follow=follow,
296-
_preload_content=False
297+
_preload_content=False,
297298
)
298299
except ApiException as e:
299-
raise Exception(json.loads(e.body.decode('utf-8'))["message"])
300+
raise Exception(json.loads(e.body.decode("utf-8"))["message"]) from None
301+
302+
303+
def wait_for_pod(pod_name, timeout_seconds=10):
304+
sclient = get_static_client()
305+
while timeout_seconds > 0:
306+
pod = sclient.read_namespaced_pod_status(name=pod_name, namespace=get_default_namespace())
307+
if pod.status.phase != "Pending":
308+
return
309+
sleep(1)
310+
timeout_seconds -= 1

0 commit comments

Comments
 (0)