Skip to content

Commit

Permalink
binary file via init
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark committed Sep 16, 2024
1 parent 867655f commit 08b15ed
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 4 deletions.
14 changes: 12 additions & 2 deletions docs/warnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,24 @@ 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 |
|-----------------|--------|------------|-----------|
| 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

Expand Down Expand Up @@ -156,7 +166,7 @@ options:

### `warnet image build`
Build bitcoind and bitcoin-cli from \<repo> at \<commit_sha> with the specified \<tags>.
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 |
Expand Down
5 changes: 5 additions & 0 deletions resources/charts/binary-runner/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v2
name: bin-runner
description: A Helm chart for the bin-runner Pod
version: 0.1.0
type: application
11 changes: 11 additions & 0 deletions resources/charts/binary-runner/Values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
nameOverride: ""
fullnameOverride: ""

pod:
name: bin-runner
namespace: warnet

podLabels:
app: "warnet"
mission: "binary"
1 change: 1 addition & 0 deletions resources/charts/binary-runner/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary executing
60 changes: 60 additions & 0 deletions resources/charts/binary-runner/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -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 }}
31 changes: 31 additions & 0 deletions resources/charts/binary-runner/templates/pod.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/warnet/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
71 changes: 70 additions & 1 deletion src/warnet/control.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import json
import os
import shlex
import subprocess
import sys
import time
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion src/warnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 08b15ed

Please sign in to comment.