Skip to content

Commit 0f1e3a1

Browse files
committed
rpc: remove kubectl
Had to do an annoying dance because it seems like stream() wants to convert any json it sees into python primitives. There's gotta be a better way to deal with that, but I came up with crudely checking if we are getting a list or dict and literal_eval-ing it. Maybe I'm missing something. Otherwise, seems to work.
1 parent 9e4c096 commit 0f1e3a1

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/warnet/bitcoin.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def bitcoin():
2323
@click.argument("tank", type=str)
2424
@click.argument("method", type=str)
2525
@click.argument("params", type=str, nargs=-1) # this will capture all remaining arguments
26-
def rpc(tank: str, method: str, params: str):
26+
def rpc(tank: str, method: str, params: tuple[str, ...]):
2727
"""
2828
Call bitcoin-cli <method> [params] on <tank pod name>
2929
"""
@@ -35,15 +35,37 @@ def rpc(tank: str, method: str, params: str):
3535
print(result)
3636

3737

38-
def _rpc(tank: str, method: str, params: str):
38+
def _rpc(tank: str, method: str, params: tuple[str, ...]) -> str:
3939
# bitcoin-cli should be able to read bitcoin.conf inside the container
4040
# so no extra args like port, chain, username or password are needed
4141
namespace = get_default_namespace()
42+
43+
sclient = get_static_client()
4244
if params:
43-
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method} {' '.join(map(str, params))}"
45+
cmd = ["bitcoin-cli", method]
46+
cmd.extend(params)
47+
else:
48+
cmd = ["bitcoin-cli", method]
49+
resp = stream(
50+
sclient.connect_get_namespaced_pod_exec,
51+
tank,
52+
namespace,
53+
container="bitcoincore",
54+
command=cmd,
55+
stderr=True,
56+
stdin=False,
57+
stdout=True,
58+
tty=False,
59+
)
60+
# The k8s lib seems to convert json into its python representation. The following dance seems to
61+
# avoid the worst of it.
62+
if resp.startswith("{") or resp.startswith("["):
63+
literal = ast.literal_eval(resp)
64+
json_string = json.dumps(literal)
65+
return json_string
4466
else:
45-
cmd = f"kubectl -n {namespace} exec {tank} -- bitcoin-cli {method}"
46-
return run_command(cmd)
67+
# When bitcoin-cli responds with a txid or similar, don't try to `literal_eval` it.
68+
return resp
4769

4870

4971
@bitcoin.command()

0 commit comments

Comments
 (0)