Skip to content

Commit

Permalink
server: add exec_run RPC
Browse files Browse the repository at this point in the history
Also expand related K8s backed functions to cover CIRCUITBREAKER
service type
  • Loading branch information
pinheadmz committed Feb 28, 2024
1 parent 878642d commit 3d465af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/backends/kubernetes/kubernetes_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def get_file(self, tank_index: int, service: ServiceType, file_path: str):
return decoded_bytes

def get_pod_name(self, tank_index: int, type: ServiceType) -> str:
if type == ServiceType.LIGHTNING:
if type == ServiceType.LIGHTNING or type == ServiceType.CIRCUITBREAKER:
return f"{self.network_name}-{POD_PREFIX}-ln-{tank_index:06d}"
return f"{self.network_name}-{POD_PREFIX}-{tank_index:06d}"

Expand Down Expand Up @@ -175,18 +175,19 @@ def get_status(self, tank_index: int, service: ServiceType) -> RunningStatus:

def exec_run(self, tank_index: int, service: ServiceType, cmd: str):
pod_name = self.get_pod_name(tank_index, service)
if service == ServiceType.BITCOIN:
exec_cmd = ["/bin/bash", "-c", f"{cmd}"]
elif service == ServiceType.LIGHTNING:
exec_cmd = ["/bin/sh", "-c", f"{cmd}"]
exec_cmd = ["/bin/sh", "-c", f"{cmd}"]
self.log.debug(f"Running {exec_cmd=:} on {tank_index=:}")
if service == ServiceType.BITCOIN:
container = BITCOIN_CONTAINER_NAME
if service == ServiceType.LIGHTNING:
container = LN_CONTAINER_NAME
if service == ServiceType.CIRCUITBREAKER:
container = LN_CB_CONTAINER_NAME
result = stream(
self.client.connect_get_namespaced_pod_exec,
pod_name,
self.namespace,
container=BITCOIN_CONTAINER_NAME
if service == ServiceType.BITCOIN
else LN_CONTAINER_NAME,
container=container,
command=exec_cmd,
stderr=True,
stdin=False,
Expand Down
10 changes: 10 additions & 0 deletions src/warnet/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jsonschema
import networkx as nx
import scenarios
from backends import ServiceType
from flask import Flask, jsonify, request
from flask_jsonrpc.app import JSONRPC
from flask_jsonrpc.exceptions import ServerError
Expand Down Expand Up @@ -175,6 +176,7 @@ def setup_rpc(self):
self.jsonrpc.register(self.graph_validate)
# Debug
self.jsonrpc.register(self.generate_deployment)
self.jsonrpc.register(self.exec_run)
# Server
self.jsonrpc.register(self.server_stop)
# Logs
Expand Down Expand Up @@ -552,6 +554,14 @@ def logs_grep(self, pattern: str, network: str = "warnet") -> str:
self.logger.error(msg)
raise ServerError(message=msg) from e

def exec_run(self, index: int, service_type: int, cmd: str, network: str = "warnet") -> str:
"""
Execute an arbitrary command in an arbitrary container,
identified by tank index and ServiceType
"""
wn = Warnet.from_network(network, self.backend)
return wn.container_interface.exec_run(index, ServiceType(service_type), cmd)


def run_server():
parser = argparse.ArgumentParser(description="Run the server")
Expand Down

0 comments on commit 3d465af

Please sign in to comment.