|
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, |
@@ -233,6 +234,74 @@ def run(scenario_file: str, additional_args: tuple[str]): |
233 | 234 | print(f"Error: {e.stderr}") |
234 | 235 |
|
235 | 236 |
|
| 237 | +@click.command(context_settings={"ignore_unknown_options": True}) |
| 238 | +@click.argument("file", type=click.Path(exists=True, file_okay=True, dir_okay=False)) |
| 239 | +@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED) |
| 240 | +def run_binary(file: str, additional_args: tuple[str]): |
| 241 | + """ |
| 242 | + Run a file in warnet |
| 243 | + Pass `-- --help` to get individual scenario help |
| 244 | + """ |
| 245 | + file_path = Path(file).resolve() |
| 246 | + file_name = file_path.stem |
| 247 | + |
| 248 | + name = f"binary-{file_name.replace('_', '')}-{int(time.time())}" |
| 249 | + namespace = get_default_namespace() |
| 250 | + |
| 251 | + try: |
| 252 | + # Construct Helm command |
| 253 | + helm_command = [ |
| 254 | + "helm", |
| 255 | + "upgrade", |
| 256 | + "--install", |
| 257 | + "--namespace", |
| 258 | + namespace, |
| 259 | + "--set", |
| 260 | + f"fullnameOverride={name}", |
| 261 | + "--set", |
| 262 | + f"pod.name={name}", |
| 263 | + ] |
| 264 | + |
| 265 | + # Add additional arguments |
| 266 | + if additional_args: |
| 267 | + helm_command.extend(["--set", f"args={' '.join(additional_args)}"]) |
| 268 | + if "--help" in additional_args or "-h" in additional_args: |
| 269 | + return subprocess.run([sys.executable, file_path, "--help"]) |
| 270 | + |
| 271 | + helm_command.extend([name, BINARY_CHART]) |
| 272 | + |
| 273 | + # Execute Helm command to start the pod |
| 274 | + result = subprocess.run(helm_command, check=True, capture_output=True, text=True) |
| 275 | + |
| 276 | + # Wait for the pod to be ready |
| 277 | + wait_command = [ |
| 278 | + "kubectl", |
| 279 | + "wait", |
| 280 | + "--for=condition=PodReadyToStartContainers", |
| 281 | + "pod", |
| 282 | + "--namespace", |
| 283 | + namespace, |
| 284 | + "--timeout=30s", |
| 285 | + name, |
| 286 | + ] |
| 287 | + subprocess.run(wait_command, check=True) |
| 288 | + |
| 289 | + # Copy the binary into the container using k8s |
| 290 | + command = f"kubectl cp {file_path} -n {namespace} {name}:/data/binary -c {name}-runner" |
| 291 | + subprocess.run(shlex.split(command)) |
| 292 | + |
| 293 | + if result.returncode == 0: |
| 294 | + print(f"Successfully started binary: {file_name}") |
| 295 | + print(f"Pod name: {name}") |
| 296 | + else: |
| 297 | + print(f"Failed to start binary: {file_name}") |
| 298 | + print(f"Error: {result.stderr}") |
| 299 | + |
| 300 | + except subprocess.CalledProcessError as e: |
| 301 | + print(f"Failed to start binary: {file_name}") |
| 302 | + print(f"Error: {e.stderr}") |
| 303 | + |
| 304 | + |
236 | 305 | @click.command() |
237 | 306 | @click.argument("pod_name", type=str, default="") |
238 | 307 | @click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs") |
|
0 commit comments