|
17 | 17 |
|
18 | 18 | from .constants import COMMANDER_CHART, LOGGING_NAMESPACE
|
19 | 19 | from .k8s import (
|
| 20 | + delete_pod, |
20 | 21 | get_default_namespace,
|
21 | 22 | get_mission,
|
22 | 23 | get_pods,
|
| 24 | + pod_log, |
23 | 25 | snapshot_bitcoin_datadir,
|
| 26 | + wait_for_pod, |
24 | 27 | )
|
25 | 28 | from .process import run_command, stream_command
|
26 | 29 |
|
@@ -160,8 +163,14 @@ def get_active_network(namespace):
|
160 | 163 |
|
161 | 164 | @click.command(context_settings={"ignore_unknown_options": True})
|
162 | 165 | @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 | +) |
163 | 172 | @click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
|
164 |
| -def run(scenario_file: str, additional_args: tuple[str]): |
| 173 | +def run(scenario_file: str, debug: bool, additional_args: tuple[str]): |
165 | 174 | """
|
166 | 175 | Run a scenario from a file.
|
167 | 176 | Pass `-- --help` to get individual scenario help
|
@@ -229,52 +238,58 @@ def run(scenario_file: str, additional_args: tuple[str]):
|
229 | 238 | print(f"Failed to start scenario: {scenario_name}")
|
230 | 239 | print(f"Error: {e.stderr}")
|
231 | 240 |
|
| 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 | + |
232 | 248 |
|
233 | 249 | @click.command()
|
234 | 250 | @click.argument("pod_name", type=str, default="")
|
235 | 251 | @click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
|
236 | 252 | def logs(pod_name: str, follow: bool):
|
| 253 | + return _logs(pod_name, follow) |
| 254 | + |
| 255 | + |
| 256 | +def _logs(pod_name: str, follow: bool): |
237 | 257 | """Show the logs of a pod"""
|
238 |
| - follow_flag = "--follow" if follow else "" |
239 | 258 | namespace = get_default_namespace()
|
240 | 259 |
|
241 |
| - if pod_name: |
| 260 | + if pod_name == "": |
242 | 261 | try:
|
243 |
| - command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}" |
244 |
| - stream_command(command) |
245 |
| - return |
| 262 | + pods = get_pods() |
| 263 | + pod_list = [item.metadata.name for item in pods.items] |
246 | 264 | except Exception as e:
|
247 |
| - print(f"Could not find the pod {pod_name}: {e}") |
| 265 | + print(f"Could not fetch any pods in namespace {namespace}: {e}") |
| 266 | + return |
248 | 267 |
|
249 |
| - try: |
250 |
| - pods = run_command(f"kubectl get pods -n {namespace} -o json") |
251 |
| - pods = json.loads(pods) |
252 |
| - pod_list = [item["metadata"]["name"] for item in pods["items"]] |
253 |
| - except Exception as e: |
254 |
| - print(f"Could not fetch any pods in namespace {namespace}: {e}") |
255 |
| - return |
| 268 | + if not pod_list: |
| 269 | + print(f"Could not fetch any pods in namespace {namespace}") |
| 270 | + return |
256 | 271 |
|
257 |
| - if not pod_list: |
258 |
| - print(f"Could not fetch any pods in namespace {namespace}") |
259 |
| - return |
| 272 | + q = [ |
| 273 | + inquirer.List( |
| 274 | + name="pod", |
| 275 | + message="Please choose a pod", |
| 276 | + choices=pod_list, |
| 277 | + ) |
| 278 | + ] |
| 279 | + selected = inquirer.prompt(q, theme=GreenPassion()) |
| 280 | + if selected: |
| 281 | + pod_name = selected["pod"] |
| 282 | + else: |
| 283 | + return # cancelled by user |
260 | 284 |
|
261 |
| - q = [ |
262 |
| - inquirer.List( |
263 |
| - name="pod", |
264 |
| - message="Please choose a pod", |
265 |
| - choices=pod_list, |
266 |
| - ) |
267 |
| - ] |
268 |
| - selected = inquirer.prompt(q, theme=GreenPassion()) |
269 |
| - if selected: |
270 |
| - pod_name = selected["pod"] |
271 |
| - try: |
272 |
| - command = f"kubectl logs pod/{pod_name} -n {namespace} {follow_flag}" |
273 |
| - stream_command(command) |
274 |
| - except Exception as e: |
275 |
| - print(f"Please consider waiting for the pod to become available. Encountered: {e}") |
276 |
| - else: |
277 |
| - pass # cancelled by user |
| 285 | + try: |
| 286 | + stream = pod_log(pod_name, container_name=None, follow=follow) |
| 287 | + for line in stream.stream(): |
| 288 | + print(line.decode("utf-8"), end=None) |
| 289 | + except Exception as e: |
| 290 | + print(e) |
| 291 | + except KeyboardInterrupt: |
| 292 | + print("Interrupted streaming log!") |
278 | 293 |
|
279 | 294 |
|
280 | 295 | @click.command()
|
|
0 commit comments