From 08b15eda8705a65ca02351ddf50c065adb90b746 Mon Sep 17 00:00:00 2001 From: willcl-ark Date: Thu, 12 Sep 2024 19:35:49 +0100 Subject: [PATCH] binary file via init --- docs/warnet.md | 14 +++- resources/charts/binary-runner/Chart.yaml | 5 ++ resources/charts/binary-runner/Values.yaml | 11 +++ .../charts/binary-runner/templates/NOTES.txt | 1 + .../binary-runner/templates/_helpers.tpl | 60 ++++++++++++++++ .../charts/binary-runner/templates/pod.yaml | 31 ++++++++ src/warnet/constants.py | 1 + src/warnet/control.py | 71 ++++++++++++++++++- src/warnet/main.py | 3 +- 9 files changed, 193 insertions(+), 4 deletions(-) create mode 100644 resources/charts/binary-runner/Chart.yaml create mode 100644 resources/charts/binary-runner/Values.yaml create mode 100644 resources/charts/binary-runner/templates/NOTES.txt create mode 100644 resources/charts/binary-runner/templates/_helpers.tpl create mode 100644 resources/charts/binary-runner/templates/pod.yaml diff --git a/docs/warnet.md b/docs/warnet.md index e7c180459..45a4cb38a 100644 --- a/docs/warnet.md +++ b/docs/warnet.md @@ -65,7 +65,7 @@ options: ### `warnet run` Run a scenario from a file. -Pass `-- --help` to get individual scenario help + Pass `-- --help` to get individual scenario help options: | name | type | required | default | @@ -73,6 +73,16 @@ options: | scenario_file | Path | yes | | | additional_args | String | | | +### `warnet run-binary` +Run a file in warnet + Pass `-- --help` to get individual scenario help + +options: +| name | type | required | default | +|-----------------|--------|------------|-----------| +| file | Path | yes | | +| additional_args | String | | | + ### `warnet setup` Setup warnet @@ -156,7 +166,7 @@ options: ### `warnet image build` Build bitcoind and bitcoin-cli from \ at \ with the specified \. -Optionally deploy to remote registry using --action=push, otherwise image is loaded to local registry. + Optionally deploy to remote registry using --action=push, otherwise image is loaded to local registry. options: | name | type | required | default | diff --git a/resources/charts/binary-runner/Chart.yaml b/resources/charts/binary-runner/Chart.yaml new file mode 100644 index 000000000..39997e2dc --- /dev/null +++ b/resources/charts/binary-runner/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v2 +name: bin-runner +description: A Helm chart for the bin-runner Pod +version: 0.1.0 +type: application diff --git a/resources/charts/binary-runner/Values.yaml b/resources/charts/binary-runner/Values.yaml new file mode 100644 index 000000000..89f60b9ef --- /dev/null +++ b/resources/charts/binary-runner/Values.yaml @@ -0,0 +1,11 @@ +--- +nameOverride: "" +fullnameOverride: "" + +pod: + name: bin-runner + namespace: warnet + +podLabels: + app: "warnet" + mission: "binary" diff --git a/resources/charts/binary-runner/templates/NOTES.txt b/resources/charts/binary-runner/templates/NOTES.txt new file mode 100644 index 000000000..4fc607ff7 --- /dev/null +++ b/resources/charts/binary-runner/templates/NOTES.txt @@ -0,0 +1 @@ +Binary executing diff --git a/resources/charts/binary-runner/templates/_helpers.tpl b/resources/charts/binary-runner/templates/_helpers.tpl new file mode 100644 index 000000000..a9360f3b0 --- /dev/null +++ b/resources/charts/binary-runner/templates/_helpers.tpl @@ -0,0 +1,60 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "binary-runner.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "binary-runner.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s" .Release.Name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "binary-runner.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "binary-runner.labels" -}} +helm.sh/chart: {{ include "binary-runner.chart" . }} +{{ include "binary-runner.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- with .Values.podLabels }} +{{ toYaml . }} +{{- end }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "binary-runner.selectorLabels" -}} +app.kubernetes.io/name: {{ include "binary-runner.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "binary-runner.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "binary-runner.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/resources/charts/binary-runner/templates/pod.yaml b/resources/charts/binary-runner/templates/pod.yaml new file mode 100644 index 000000000..7453b48a7 --- /dev/null +++ b/resources/charts/binary-runner/templates/pod.yaml @@ -0,0 +1,31 @@ +--- +apiVersion: v1 +kind: Pod +metadata: + name: {{ include "binary-runner.fullname" . }} + labels: + {{- include "binary-runner.labels" . | nindent 4 }} + app: {{ include "binary-runner.name" . }} + mission: binary +spec: + restartPolicy: Never + volumes: + - name: shared-data + emptyDir: {} + containers: + - name: {{ .Values.pod.name }}-runner + image: alpine + command: ["/bin/sh", "-c"] + args: + - | + echo "Waiting for binary file..." + while [ ! -f /data/binary ]; do + sleep 1 + done + echo "Binary found!" + chmod +x /data/binary + /data/binary + exit 0 + volumeMounts: + - name: shared-data + mountPath: /data diff --git a/src/warnet/constants.py b/src/warnet/constants.py index 3f837328d..db4892fee 100644 --- a/src/warnet/constants.py +++ b/src/warnet/constants.py @@ -32,6 +32,7 @@ BITCOIN_CHART_LOCATION = str(CHARTS_DIR.joinpath("bitcoincore")) FORK_OBSERVER_CHART = str(CHARTS_DIR.joinpath("fork-observer")) COMMANDER_CHART = str(CHARTS_DIR.joinpath("commander")) +BINARY_CHART = str(CHARTS_DIR.joinpath("binary-runner")) NAMESPACES_CHART_LOCATION = CHARTS_DIR.joinpath("namespaces") FORK_OBSERVER_CHART = str(files("resources.charts").joinpath("fork-observer")) CADDY_CHART = str(files("resources.charts").joinpath("caddy")) diff --git a/src/warnet/control.py b/src/warnet/control.py index c2a115fa4..6522279d0 100644 --- a/src/warnet/control.py +++ b/src/warnet/control.py @@ -1,6 +1,7 @@ import base64 import json import os +import shlex import subprocess import sys import time @@ -15,7 +16,7 @@ from rich.prompt import Confirm, Prompt from rich.table import Table -from .constants import COMMANDER_CHART, LOGGING_NAMESPACE +from .constants import BINARY_CHART, COMMANDER_CHART, LOGGING_NAMESPACE from .deploy import _port_stop_internal from .k8s import ( get_default_namespace, @@ -257,6 +258,74 @@ def run(scenario_file: str, additional_args: tuple[str]): print(f"Error: {e.stderr}") +@click.command(context_settings={"ignore_unknown_options": True}) +@click.argument("file", type=click.Path(exists=True, file_okay=True, dir_okay=False)) +@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED) +def run_binary(file: str, additional_args: tuple[str]): + """ + Run a file in warnet + Pass `-- --help` to get individual scenario help + """ + file_path = Path(file).resolve() + file_name = file_path.stem + + name = f"binary-{file_name.replace('_', '')}-{int(time.time())}" + namespace = get_default_namespace() + + try: + # Construct Helm command + helm_command = [ + "helm", + "upgrade", + "--install", + "--namespace", + namespace, + "--set", + f"fullnameOverride={name}", + "--set", + f"pod.name={name}", + ] + + # Add additional arguments + if additional_args: + helm_command.extend(["--set", f"args={' '.join(additional_args)}"]) + if "--help" in additional_args or "-h" in additional_args: + return subprocess.run([sys.executable, file_path, "--help"]) + + helm_command.extend([name, BINARY_CHART]) + + # Execute Helm command to start the pod + result = subprocess.run(helm_command, check=True, capture_output=True, text=True) + + # Wait for the pod to be ready + wait_command = [ + "kubectl", + "wait", + "--for=condition=PodReadyToStartContainers", + "pod", + "--namespace", + namespace, + "--timeout=30s", + name, + ] + subprocess.run(wait_command, check=True) + + # Copy the binary into the container using k8s + command = f"kubectl cp {file_path} -n {namespace} {name}:/data/binary -c {name}-runner" + subprocess.run(shlex.split(command)) + + if result.returncode == 0: + print(f"Successfully started binary: {file_name}") + print(f"Pod name: {name}") + else: + print(f"Failed to start binary: {file_name}") + print(f"Error: {result.stderr}") + + except subprocess.CalledProcessError as e: + print(f"Failed to start binary: {file_name}") + print(f"Error: {e.stderr}") + + @click.command() @click.argument("pod_name", type=str, default="") @click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs") diff --git a/src/warnet/main.py b/src/warnet/main.py index 76893575c..1bf91addb 100644 --- a/src/warnet/main.py +++ b/src/warnet/main.py @@ -2,7 +2,7 @@ from .admin import admin from .bitcoin import bitcoin -from .control import down, logs, run, snapshot, stop +from .control import down, logs, run, run_binary, snapshot, stop from .dashboard import dashboard from .deploy import deploy from .graph import create, graph @@ -29,6 +29,7 @@ def cli(): cli.add_command(logs) cli.add_command(new) cli.add_command(run) +cli.add_command(run_binary) cli.add_command(setup) cli.add_command(snapshot) cli.add_command(status)