Skip to content

Commit effe95d

Browse files
committed
binary file via init
1 parent 67e67f5 commit effe95d

File tree

9 files changed

+191
-2
lines changed

9 files changed

+191
-2
lines changed

docs/warnet.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ options:
7373
| scenario_file | Path | yes | |
7474
| additional_args | String | | |
7575

76+
### `warnet run-binary`
77+
Run a file in warnet
78+
Pass `-- --help` to get individual scenario help
79+
80+
options:
81+
| name | type | required | default |
82+
|-----------------|--------|------------|-----------|
83+
| file | Path | yes | |
84+
| additional_args | String | | |
85+
7686
### `warnet setup`
7787
Setup warnet
7888

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v2
2+
name: bin-runner
3+
description: A Helm chart for the bin-runner Pod
4+
version: 0.1.0
5+
type: application
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
nameOverride: ""
3+
fullnameOverride: ""
4+
5+
pod:
6+
name: bin-runner
7+
namespace: warnet
8+
9+
podLabels:
10+
app: "warnet"
11+
mission: "binary"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Binary executing
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{{/*
2+
Expand the name of the chart.
3+
*/}}
4+
{{- define "binary-runner.name" -}}
5+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Create a default fully qualified app name.
10+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
11+
If release name contains chart name it will be used as a full name.
12+
*/}}
13+
{{- define "binary-runner.fullname" -}}
14+
{{- if .Values.fullnameOverride }}
15+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
16+
{{- else }}
17+
{{- printf "%s" .Release.Name | trunc 63 | trimSuffix "-" }}
18+
{{- end }}
19+
{{- end }}
20+
21+
{{/*
22+
Create chart name and version as used by the chart label.
23+
*/}}
24+
{{- define "binary-runner.chart" -}}
25+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
26+
{{- end }}
27+
28+
{{/*
29+
Common labels
30+
*/}}
31+
{{- define "binary-runner.labels" -}}
32+
helm.sh/chart: {{ include "binary-runner.chart" . }}
33+
{{ include "binary-runner.selectorLabels" . }}
34+
{{- if .Chart.AppVersion }}
35+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
36+
{{- end }}
37+
app.kubernetes.io/managed-by: {{ .Release.Service }}
38+
{{- with .Values.podLabels }}
39+
{{ toYaml . }}
40+
{{- end }}
41+
{{- end }}
42+
43+
{{/*
44+
Selector labels
45+
*/}}
46+
{{- define "binary-runner.selectorLabels" -}}
47+
app.kubernetes.io/name: {{ include "binary-runner.name" . }}
48+
app.kubernetes.io/instance: {{ .Release.Name }}
49+
{{- end }}
50+
51+
{{/*
52+
Create the name of the service account to use
53+
*/}}
54+
{{- define "binary-runner.serviceAccountName" -}}
55+
{{- if .Values.serviceAccount.create }}
56+
{{- default (include "binary-runner.fullname" .) .Values.serviceAccount.name }}
57+
{{- else }}
58+
{{- default "default" .Values.serviceAccount.name }}
59+
{{- end }}
60+
{{- end }}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
apiVersion: v1
3+
kind: Pod
4+
metadata:
5+
name: {{ include "binary-runner.fullname" . }}
6+
labels:
7+
{{- include "binary-runner.labels" . | nindent 4 }}
8+
app: {{ include "binary-runner.name" . }}
9+
mission: binary
10+
spec:
11+
restartPolicy: Never
12+
volumes:
13+
- name: shared-data
14+
emptyDir: {}
15+
containers:
16+
- name: {{ .Values.pod.name }}-runner
17+
image: alpine
18+
command: ["/bin/sh", "-c"]
19+
args:
20+
- |
21+
echo "Waiting for binary file..."
22+
while [ ! -f /data/binary ]; do
23+
sleep 1
24+
done
25+
echo "Binary found!"
26+
chmod +x /data/binary
27+
/data/binary
28+
exit 0
29+
volumeMounts:
30+
- name: shared-data
31+
mountPath: /data

src/warnet/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
BITCOIN_CHART_LOCATION = str(CHARTS_DIR.joinpath("bitcoincore"))
3333
FORK_OBSERVER_CHART = str(CHARTS_DIR.joinpath("fork-observer"))
3434
COMMANDER_CHART = str(CHARTS_DIR.joinpath("commander"))
35+
BINARY_CHART = str(CHARTS_DIR.joinpath("binary-runner"))
3536
NAMESPACES_CHART_LOCATION = CHARTS_DIR.joinpath("namespaces")
3637
FORK_OBSERVER_CHART = str(files("resources.charts").joinpath("fork-observer"))
3738
CADDY_CHART = str(files("resources.charts").joinpath("caddy"))

src/warnet/control.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import json
33
import os
4+
import shlex
45
import subprocess
56
import sys
67
import time
@@ -15,7 +16,7 @@
1516
from rich.prompt import Confirm, Prompt
1617
from rich.table import Table
1718

18-
from .constants import COMMANDER_CHART, LOGGING_NAMESPACE
19+
from .constants import BINARY_CHART, COMMANDER_CHART, LOGGING_NAMESPACE
1920
from .deploy import _port_stop_internal
2021
from .k8s import (
2122
get_default_namespace,
@@ -233,6 +234,74 @@ def run(scenario_file: str, additional_args: tuple[str]):
233234
print(f"Error: {e.stderr}")
234235

235236

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+
236305
@click.command()
237306
@click.argument("pod_name", type=str, default="")
238307
@click.option("--follow", "-f", is_flag=True, default=False, help="Follow logs")

src/warnet/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .admin import admin
44
from .bitcoin import bitcoin
5-
from .control import down, logs, run, snapshot, stop
5+
from .control import down, logs, run, run_binary, snapshot, stop
66
from .dashboard import dashboard
77
from .deploy import deploy
88
from .graph import create, graph
@@ -29,6 +29,7 @@ def cli():
2929
cli.add_command(logs)
3030
cli.add_command(new)
3131
cli.add_command(run)
32+
cli.add_command(run_binary)
3233
cli.add_command(setup)
3334
cli.add_command(snapshot)
3435
cli.add_command(status)

0 commit comments

Comments
 (0)