Skip to content

Commit 01e77b9

Browse files
committed
Combine sgr cloud tunnel and ephemeral-tunnel
The two commands are now combined into a single command which can receive either a ns/repo argument (for repository tunnels) or a host:port local address (or ephemeral tunnels).
1 parent a000e1e commit 01e77b9

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

splitgraph/cloud/tunnel_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
import subprocess
33
import sys
44
from os import path
5-
from typing import IO, Any, Dict, Optional, cast
6-
7-
from splitgraph.config import CONFIG
8-
from splitgraph.config.config import get_singleton
9-
from splitgraph.core.repository import Repository
5+
from typing import IO, Optional, cast
106

117
RATHOLE_CLIENT_CONFIG_FILENAME = "rathole-client.toml"
128

@@ -47,9 +43,15 @@ def get_rathole_client_config(
4743
)
4844

4945

46+
def get_config_dir():
47+
from splitgraph.config import CONFIG
48+
from splitgraph.config.config import get_singleton
49+
50+
return os.path.dirname(get_singleton(CONFIG, "SG_CONFIG_FILE"))
51+
52+
5053
def get_rathole_client_binary_path():
51-
config_dir = os.path.dirname(get_singleton(CONFIG, "SG_CONFIG_FILE"))
52-
return os.path.join(config_dir, "rathole")
54+
return os.path.join(get_config_dir(), "rathole")
5355

5456

5557
def write_rathole_client_config(
@@ -62,18 +64,16 @@ def write_rathole_client_config(
6264
) -> str:
6365
# in production, this will be None, but for dev instances, we need to
6466
# specify rootCA.pem
65-
config_dir = os.path.dirname(get_singleton(CONFIG, "SG_CONFIG_FILE"))
6667
trusted_root = os.environ.get("REQUESTS_CA_BUNDLE") or os.environ.get("SSL_CERT_FILE")
6768
rathole_client_config = get_rathole_client_config(
68-
# TODO: replace these stub values with response of provisioning call
6969
tunnel_connect_address=f"{tunnel_connect_host}:{tunnel_connect_port}",
7070
tls_hostname=tls_hostname or tunnel_connect_host,
7171
local_address=local_address,
7272
secret_token=secret_token,
7373
section_id=section_id,
7474
trusted_root=trusted_root,
7575
)
76-
config_filename = path.join(config_dir, RATHOLE_CLIENT_CONFIG_FILENAME)
76+
config_filename = path.join(get_config_dir(), RATHOLE_CLIENT_CONFIG_FILENAME)
7777
with open(config_filename, "w") as f:
7878
f.write(rathole_client_config)
7979
return config_filename

splitgraph/commandline/cloud.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from copy import copy
1010
from glob import glob
1111
from pathlib import Path
12-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, OrderedDict, Tuple, cast
12+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, cast
1313
from urllib.parse import quote, urlparse
1414

1515
import click
@@ -31,8 +31,7 @@
3131
wait_for_job,
3232
)
3333
from splitgraph.commandline.engine import inject_config_into_engines
34-
from splitgraph.config import CONFIG
35-
from splitgraph.config.config import get_from_subsection, get_singleton
34+
from splitgraph.config.config import get_from_subsection
3635
from splitgraph.config.management import patch_and_save_config
3736
from splitgraph.core.output import Color, pluralise
3837

@@ -1219,22 +1218,9 @@ def seed_c(remote, seed, github_repository, directory):
12191218
click.echo(f"Splitgraph project generated in {os.path.abspath(directory)}.")
12201219

12211220

1222-
@click.command("tunnel")
1223-
@click.option("--remote", default="data.splitgraph.com", help="Name of the remote registry to use.")
1224-
@click.option(
1225-
"--repositories-file", "-f", default=["splitgraph.yml"], type=click.Path(), multiple=True
1226-
)
1227-
@click.argument("repository", type=RepositoryType(exists=False))
1228-
def tunnel_c(remote: str, repositories_file: List[Path], repository: "CoreRepository"):
1229-
"""
1230-
Start the tunnel client to make tunneled external repo available.
1231-
1232-
This will load a splitgraph.yml file and tunnel the host:port address of the
1233-
external repository specified in the argument.
1234-
"""
1235-
1236-
external = _get_external_from_yaml(repositories_file, repository)[0]
1237-
1221+
def start_repository_tunnel(
1222+
remote: str, repository: "CoreRepository", external: "External"
1223+
) -> None:
12381224
if not external.tunnel:
12391225
raise click.UsageError(
12401226
f"Repository {repository.namespace}/{repository.repository} is not tunneled"
@@ -1266,15 +1252,7 @@ def tunnel_c(remote: str, repositories_file: List[Path], repository: "CoreReposi
12661252
launch_rathole_client(get_rathole_client_binary_path(), rathole_client_config_path)
12671253

12681254

1269-
@click.command("ephemeral-tunnel")
1270-
@click.option("--remote", default="data.splitgraph.com", help="Name of the remote registry to use.")
1271-
@click.argument("host", type=str)
1272-
@click.argument("port", type=int)
1273-
def ephemeral_tunnel_c(remote: str, host: str, port: int):
1274-
"""
1275-
Start ephemeral tunnel.
1276-
"""
1277-
1255+
def start_ephemeral_tunnel(remote: str, local_address: str) -> None:
12781256
from splitgraph.cloud import GQLAPIClient
12791257

12801258
client = GQLAPIClient(remote)
@@ -1291,16 +1269,43 @@ def ephemeral_tunnel_c(remote: str, host: str, port: int):
12911269
secret_token,
12921270
tunnel_connect_host,
12931271
tunnel_connect_port,
1294-
f"{host}:{port}",
1272+
local_address,
12951273
tunnel_connect_host,
12961274
)
12971275
print(
1298-
f"To connect to {host}:{port} from Splitgraph, use the following connection parameters:\nHost: {private_address_host}\nPort: {private_address_port}"
1276+
f"To connect to {local_address} from Splitgraph, use the following connection parameters:\nHost: {private_address_host}\nPort: {private_address_port}"
12991277
)
13001278
print("launching rathole client")
13011279
launch_rathole_client(get_rathole_client_binary_path(), rathole_client_config_path)
13021280

13031281

1282+
@click.command("tunnel")
1283+
@click.option("--remote", default="data.splitgraph.com", help="Name of the remote registry to use.")
1284+
@click.option(
1285+
"--repositories-file", "-f", default=["splitgraph.yml"], type=click.Path(), multiple=True
1286+
)
1287+
@click.argument("repository_or_local_address", type=str)
1288+
def tunnel_c(remote: str, repositories_file: List[Path], repository_or_local_address: str):
1289+
"""
1290+
Start the tunnel client to make tunneled external repo available.
1291+
1292+
This will load a splitgraph.yml file and tunnel the host:port address of the
1293+
external repository specified in the argument.
1294+
"""
1295+
1296+
if repository_or_local_address.find("/") > -1:
1297+
repository: "CoreRepository" = RepositoryType(exists=False).convert(
1298+
repository_or_local_address, None, None
1299+
)
1300+
external = _get_external_from_yaml(repositories_file, repository)[0]
1301+
start_repository_tunnel(remote, repository, external)
1302+
1303+
elif repository_or_local_address.find(":") > -1:
1304+
start_ephemeral_tunnel(remote, repository_or_local_address)
1305+
else:
1306+
raise click.UsageError("argument should be of the form namespace/repository or host:port")
1307+
1308+
13041309
@click.group("cloud")
13051310
def cloud_c():
13061311
"""Run actions on Splitgraph Cloud."""
@@ -1329,4 +1334,3 @@ def cloud_c():
13291334
cloud_c.add_command(validate_c)
13301335
cloud_c.add_command(seed_c)
13311336
cloud_c.add_command(tunnel_c)
1332-
cloud_c.add_command(ephemeral_tunnel_c)

0 commit comments

Comments
 (0)