|
1 | 1 | import base64
|
2 | 2 | import json
|
3 | 3 | import os
|
| 4 | +import shlex |
4 | 5 | import subprocess
|
5 | 6 | import sys
|
6 | 7 | import time
|
|
15 | 16 | from rich.prompt import Confirm, Prompt
|
16 | 17 | from rich.table import Table
|
17 | 18 |
|
18 |
| -from .constants import COMMANDER_CHART, LOGGING_NAMESPACE |
| 19 | +from .constants import BINARY_CHART, COMMANDER_CHART, LOGGING_NAMESPACE |
19 | 20 | from .deploy import _port_stop_internal
|
20 | 21 | from .k8s import (
|
21 | 22 | get_default_namespace,
|
@@ -257,6 +258,74 @@ def run(scenario_file: str, additional_args: tuple[str]):
|
257 | 258 | print(f"Error: {e.stderr}")
|
258 | 259 |
|
259 | 260 |
|
| 261 | +@click.command(context_settings={"ignore_unknown_options": True}) |
| 262 | +@click.argument("file", type=click.Path(exists=True, file_okay=True, dir_okay=False)) |
| 263 | +@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED) |
| 264 | +def run_binary(file: str, additional_args: tuple[str]): |
| 265 | + """ |
| 266 | + Run a file in warnet |
| 267 | + Pass `-- --help` to get individual scenario help |
| 268 | + """ |
| 269 | + file_path = Path(file).resolve() |
| 270 | + file_name = file_path.stem |
| 271 | + |
| 272 | + name = f"binary-{file_name.replace('_', '')}-{int(time.time())}" |
| 273 | + namespace = get_default_namespace() |
| 274 | + |
| 275 | + try: |
| 276 | + # Construct Helm command |
| 277 | + helm_command = [ |
| 278 | + "helm", |
| 279 | + "upgrade", |
| 280 | + "--install", |
| 281 | + "--namespace", |
| 282 | + namespace, |
| 283 | + "--set", |
| 284 | + f"fullnameOverride={name}", |
| 285 | + "--set", |
| 286 | + f"pod.name={name}", |
| 287 | + ] |
| 288 | + |
| 289 | + # Add additional arguments |
| 290 | + if additional_args: |
| 291 | + helm_command.extend(["--set", f"args={' '.join(additional_args)}"]) |
| 292 | + if "--help" in additional_args or "-h" in additional_args: |
| 293 | + return subprocess.run([sys.executable, file_path, "--help"]) |
| 294 | + |
| 295 | + helm_command.extend([name, BINARY_CHART]) |
| 296 | + |
| 297 | + # Execute Helm command to start the pod |
| 298 | + result = subprocess.run(helm_command, check=True, capture_output=True, text=True) |
| 299 | + |
| 300 | + # Wait for the pod to be ready |
| 301 | + wait_command = [ |
| 302 | + "kubectl", |
| 303 | + "wait", |
| 304 | + "--for=condition=PodReadyToStartContainers", |
| 305 | + "pod", |
| 306 | + "--namespace", |
| 307 | + namespace, |
| 308 | + "--timeout=30s", |
| 309 | + name, |
| 310 | + ] |
| 311 | + subprocess.run(wait_command, check=True) |
| 312 | + |
| 313 | + # Copy the binary into the init container using k8s |
| 314 | + command = f"kubectl cp {file_path} -n {namespace} {name}:/data/binary -c {name}-receiver" |
| 315 | + subprocess.run(shlex.split(command)) |
| 316 | + |
| 317 | + if result.returncode == 0: |
| 318 | + print(f"Successfully started binary: {file_name}") |
| 319 | + print(f"Pod name: {name}") |
| 320 | + else: |
| 321 | + print(f"Failed to start binary: {file_name}") |
| 322 | + print(f"Error: {result.stderr}") |
| 323 | + |
| 324 | + except subprocess.CalledProcessError as e: |
| 325 | + print(f"Failed to start binary: {file_name}") |
| 326 | + print(f"Error: {e.stderr}") |
| 327 | + |
| 328 | + |
260 | 329 | @click.command()
|
261 | 330 | @click.argument("pod_name", type=str, default="")
|
262 | 331 | @click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")
|
|
0 commit comments