Skip to content

Commit f1f62bb

Browse files
authored
Merge pull request #450 from willcl-ark/rpc-logs
cli: add 'network logs' command
2 parents d261b7a + 847a9ca commit f1f62bb

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

src/warnet/cli/cluster.py

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import click
77

8+
from .util import run_command
9+
810
MANIFEST_PATH = files("manifests")
911
RPC_PATH = files("images").joinpath("rpc")
1012

@@ -26,46 +28,6 @@ def cluster():
2628
pass
2729

2830

29-
def run_command(command, stream_output=False, env=None):
30-
# Merge the current environment with the provided env
31-
full_env = os.environ.copy()
32-
if env:
33-
# Convert all env values to strings (only a safeguard)
34-
env = {k: str(v) for k, v in env.items()}
35-
full_env.update(env)
36-
37-
if stream_output:
38-
process = subprocess.Popen(
39-
["/bin/bash", "-c", command],
40-
stdout=subprocess.PIPE,
41-
stderr=subprocess.STDOUT,
42-
text=True,
43-
bufsize=1,
44-
universal_newlines=True,
45-
env=full_env,
46-
)
47-
48-
for line in iter(process.stdout.readline, ""):
49-
print(line, end="")
50-
51-
process.stdout.close()
52-
return_code = process.wait()
53-
54-
if return_code != 0:
55-
print(f"Command failed with return code {return_code}")
56-
return False
57-
return True
58-
else:
59-
result = subprocess.run(
60-
command, shell=True, capture_output=True, text=True, executable="/bin/bash"
61-
)
62-
if result.returncode != 0:
63-
print(f"Error: {result.stderr}")
64-
return False
65-
print(result.stdout)
66-
return True
67-
68-
6931
@cluster.command()
7032
@click.option("--clean", is_flag=True, help="Remove configuration files")
7133
def setup_minikube(clean):

src/warnet/cli/network.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from rich.table import Table
1010

1111
from .rpc import rpc_call # noqa: I001
12+
from .util import run_command
1213

1314

1415
DEFAULT_GRAPH_FILE = files("graphs").joinpath("default.graphml")
@@ -158,3 +159,16 @@ def export(network: str, activity: str, exclude: str):
158159
print(
159160
rpc_call("network_export", {"network": network, "activity": activity, "exclude": exclude})
160161
)
162+
163+
164+
@network.command()
165+
@click.option("--follow", "-f", is_flag=True, help="Follow logs")
166+
def logs(follow: bool):
167+
"""Get Kubernetes logs from the RPC server"""
168+
command = "kubectl logs rpc-0"
169+
stream_output = False
170+
if follow:
171+
command += " --follow"
172+
stream_output = True
173+
174+
run_command(command, stream_output=stream_output)

src/warnet/cli/util.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import subprocess
3+
4+
5+
def run_command(command, stream_output=False, env=None):
6+
# Merge the current environment with the provided env
7+
full_env = os.environ.copy()
8+
if env:
9+
# Convert all env values to strings (only a safeguard)
10+
env = {k: str(v) for k, v in env.items()}
11+
full_env.update(env)
12+
13+
if stream_output:
14+
process = subprocess.Popen(
15+
["/bin/bash", "-c", command],
16+
stdout=subprocess.PIPE,
17+
stderr=subprocess.STDOUT,
18+
text=True,
19+
bufsize=1,
20+
universal_newlines=True,
21+
env=full_env,
22+
)
23+
24+
for line in iter(process.stdout.readline, ""):
25+
print(line, end="")
26+
27+
process.stdout.close()
28+
return_code = process.wait()
29+
30+
if return_code != 0:
31+
print(f"Command failed with return code {return_code}")
32+
return False
33+
return True
34+
else:
35+
result = subprocess.run(
36+
command, shell=True, capture_output=True, text=True, executable="/bin/bash"
37+
)
38+
if result.returncode != 0:
39+
print(f"Error: {result.stderr}")
40+
return False
41+
print(result.stdout)
42+
return True

0 commit comments

Comments
 (0)