Skip to content

Commit f8739b8

Browse files
committed
Fix printing stdout of rathole client subprocess
1 parent 01e77b9 commit f8739b8

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

splitgraph/cloud/tunnel_client.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22
import subprocess
3-
import sys
43
from os import path
5-
from typing import IO, Optional, cast
4+
from typing import Optional
5+
6+
from click import echo
67

78
RATHOLE_CLIENT_CONFIG_FILENAME = "rathole-client.toml"
89

@@ -62,8 +63,8 @@ def write_rathole_client_config(
6263
local_address: str,
6364
tls_hostname: Optional[str],
6465
) -> str:
65-
# in production, this will be None, but for dev instances, we need to
66-
# specify rootCA.pem
66+
# If a specific root CA file is used (eg: for self-signed hosts), reference
67+
# it in the rathole client config.
6768
trusted_root = os.environ.get("REQUESTS_CA_BUNDLE") or os.environ.get("SSL_CERT_FILE")
6869
rathole_client_config = get_rathole_client_config(
6970
tunnel_connect_address=f"{tunnel_connect_host}:{tunnel_connect_port}",
@@ -79,13 +80,7 @@ def write_rathole_client_config(
7980
return config_filename
8081

8182

82-
# inspired by https://stackoverflow.com/questions/18421757/live-output-from-subprocess-command
83-
def launch_rathole_client(rathole_client_binary_path: str, rathole_client_config_path: str) -> None:
84-
process = subprocess.Popen(
85-
[rathole_client_binary_path, "--client", rathole_client_config_path],
86-
stdout=subprocess.PIPE,
87-
stderr=subprocess.STDOUT,
88-
)
89-
# pipe rathole process output to stdout
90-
for c in iter(lambda: cast(IO[bytes], process.stdout).read(1), b""): # nomypy
91-
sys.stdout.buffer.write(c)
83+
def launch_rathole_client(rathole_client_config_path: str) -> None:
84+
echo("launching rathole client")
85+
command = [get_rathole_client_binary_path(), "--client", rathole_client_config_path]
86+
subprocess.check_call(command)

splitgraph/commandline/cloud.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from splitgraph.cloud.models import AddExternalRepositoryRequest, IntrospectionMode
1919
from splitgraph.cloud.project.models import Metadata, SplitgraphYAML
2020
from splitgraph.cloud.tunnel_client import (
21-
get_rathole_client_binary_path,
2221
launch_rathole_client,
2322
write_rathole_client_config,
2423
)
@@ -1248,8 +1247,7 @@ def start_repository_tunnel(
12481247
tunnel_connect_host,
12491248
)
12501249

1251-
print("launching rathole client")
1252-
launch_rathole_client(get_rathole_client_binary_path(), rathole_client_config_path)
1250+
launch_rathole_client(rathole_client_config_path)
12531251

12541252

12551253
def start_ephemeral_tunnel(remote: str, local_address: str) -> None:
@@ -1272,11 +1270,10 @@ def start_ephemeral_tunnel(remote: str, local_address: str) -> None:
12721270
local_address,
12731271
tunnel_connect_host,
12741272
)
1275-
print(
1273+
click.echo(
12761274
f"To connect to {local_address} from Splitgraph, use the following connection parameters:\nHost: {private_address_host}\nPort: {private_address_port}"
12771275
)
1278-
print("launching rathole client")
1279-
launch_rathole_client(get_rathole_client_binary_path(), rathole_client_config_path)
1276+
launch_rathole_client(rathole_client_config_path)
12801277

12811278

12821279
@click.command("tunnel")
@@ -1293,17 +1290,17 @@ def tunnel_c(remote: str, repositories_file: List[Path], repository_or_local_add
12931290
external repository specified in the argument.
12941291
"""
12951292

1296-
if repository_or_local_address.find("/") > -1:
1293+
if "/" in repository_or_local_address:
12971294
repository: "CoreRepository" = RepositoryType(exists=False).convert(
12981295
repository_or_local_address, None, None
12991296
)
13001297
external = _get_external_from_yaml(repositories_file, repository)[0]
13011298
start_repository_tunnel(remote, repository, external)
13021299

1303-
elif repository_or_local_address.find(":") > -1:
1300+
elif ":" in repository_or_local_address:
13041301
start_ephemeral_tunnel(remote, repository_or_local_address)
13051302
else:
1306-
raise click.UsageError("argument should be of the form namespace/repository or host:port")
1303+
raise click.UsageError("Argument should be of the form namespace/repository or host:port")
13071304

13081305

13091306
@click.group("cloud")

0 commit comments

Comments
 (0)