Skip to content

Commit 08b15ed

Browse files
committed
binary file via init
1 parent 867655f commit 08b15ed

File tree

9 files changed

+193
-4
lines changed

9 files changed

+193
-4
lines changed

docs/warnet.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,24 @@ options:
6565

6666
### `warnet run`
6767
Run a scenario from a file.
68-
Pass `-- --help` to get individual scenario help
68+
Pass `-- --help` to get individual scenario help
6969

7070
options:
7171
| name | type | required | default |
7272
|-----------------|--------|------------|-----------|
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

@@ -156,7 +166,7 @@ options:
156166

157167
### `warnet image build`
158168
Build bitcoind and bitcoin-cli from \<repo> at \<commit_sha> with the specified \<tags>.
159-
Optionally deploy to remote registry using --action=push, otherwise image is loaded to local registry.
169+
Optionally deploy to remote registry using --action=push, otherwise image is loaded to local registry.
160170

161171
options:
162172
| name | type | required | default |
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,
@@ -257,6 +258,74 @@ def run(scenario_file: str, additional_args: tuple[str]):
257258
print(f"Error: {e.stderr}")
258259

259260

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 container using k8s
314+
command = f"kubectl cp {file_path} -n {namespace} {name}:/data/binary -c {name}-runner"
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+
260329
@click.command()
261330
@click.argument("pod_name", type=str, default="")
262331
@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)