Skip to content

Commit 0a6d7da

Browse files
tchatonlantiga
authored andcommitted
[App] Enable to register data connections (#16670)
Co-authored-by: thomas <[email protected]> (cherry picked from commit 88e089e)
1 parent 1cca1df commit 0a6d7da

File tree

14 files changed

+226
-51
lines changed

14 files changed

+226
-51
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ module = [
106106
"lightning_app.api.http_methods",
107107
"lightning_app.api.request_types",
108108
"lightning_app.cli.commands.app_commands",
109-
"lightning_app.cli.commands.connection",
110109
"lightning_app.cli.commands.lightning_cli",
111110
"lightning_app.cli.commands.cmd_install",
111+
"lightning_app.cli.connect.app",
112112
"lightning_app.cli.commands.pwd",
113113
"lightning_app.cli.commands.ls",
114114
"lightning_app.cli.commands.cp",
115115
"lightning_app.cli.commands.cd",
116+
"lightning_app.cli.connect.data",
116117
"lightning_app.cli.cmd_install",
117118
"lightning_app.components.database.client",
118119
"lightning_app.components.database.server",

requirements/app/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lightning-cloud>=0.5.22
1+
lightning-cloud>=0.5.24
22
packaging
33
typing-extensions>=4.0.0, <=4.4.0
44
deepdiff>=5.7.0, <6.2.4

src/lightning_app/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2222
* `pwd`: Return the current folder in your Cloud Platform Filesystem
2323
* `cp`: Copy files between your Cloud Platform Filesystem and local filesystem
2424

25+
- Added `lightning connect data` to register data connection to s3 buckets ([#16670](https://github.com/Lightning-AI/lightning/pull/16670))
26+
27+
2528
### Changed
2629

2730
- Changed the default `LightningClient(retry=False)` to `retry=True` ([#16382](https://github.com/Lightning-AI/lightning/pull/16382))
2831

2932
- Add support for async predict method in PythonServer and remove torch context ([#16453](https://github.com/Lightning-AI/lightning/pull/16453))
3033

3134

35+
- Renamed `lightning.app.components.LiteMultiNode` to `lightning.app.components.FabricMultiNode` ([#16505](https://github.com/Lightning-AI/lightning/pull/16505))
36+
37+
38+
- Changed the command `lightning connect` to `lightning connect app` for consistency ([#16670](https://github.com/Lightning-AI/lightning/pull/16670))
39+
40+
3241
### Deprecated
3342

3443
-

src/lightning_app/cli/commands/app_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import requests
2020

21-
from lightning_app.cli.commands.connection import (
21+
from lightning_app.cli.connect.app import (
2222
_clean_lightning_connection,
2323
_install_missing_requirements,
2424
_resolve_command_path,

src/lightning_app/cli/commands/cd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from rich.text import Text
2222

2323
from lightning_app.cli.commands import ls
24-
from lightning_app.cli.commands.connection import _LIGHTNING_CONNECTION_FOLDER
24+
from lightning_app.cli.connect.app import _LIGHTNING_CONNECTION_FOLDER
2525
from lightning_app.utilities.app_helpers import Logger
2626
from lightning_app.utilities.cli_helpers import _error_and_exit
2727

src/lightning_app/cli/commands/ls.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from rich.spinner import Spinner
2727
from rich.text import Text
2828

29-
from lightning_app.cli.commands.connection import _LIGHTNING_CONNECTION_FOLDER
29+
from lightning_app.cli.connect.app import _LIGHTNING_CONNECTION_FOLDER
3030
from lightning_app.utilities.app_helpers import Logger
3131
from lightning_app.utilities.cli_helpers import _error_and_exit
3232
from lightning_app.utilities.network import LightningClient
@@ -41,11 +41,10 @@
4141
def ls(path: Optional[str] = None, print: bool = True, use_live: bool = True) -> List[str]:
4242
"""List the contents of a folder in the Lightning Cloud Filesystem."""
4343

44-
from lightning.app.cli.commands.cd import _CD_FILE
44+
from lightning_app.cli.commands.cd import _CD_FILE
4545

4646
if sys.platform == "win32":
47-
print("`ls` isn't supported on windows. Open an issue on Github.")
48-
sys.exit(0)
47+
_error_and_exit("`ls` isn't supported on windows. Open an issue on Github.")
4948

5049
root = "/"
5150

src/lightning_app/cli/connect/__init__.py

Whitespace-only changes.

src/lightning_app/cli/commands/connection.py renamed to src/lightning_app/cli/connect/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838

3939
@click.argument("app_name_or_id", required=True)
40-
def connect(app_name_or_id: str):
40+
def connect_app(app_name_or_id: str):
4141
"""Connect your local terminal to a running lightning app.
4242
4343
After connecting, the lightning CLI will respond to commands exposed by the app.
@@ -79,8 +79,8 @@ def connect(app_name_or_id: str):
7979
else:
8080
click.echo(f"You are already connected to the cloud Lightning App: {app_name_or_id}.")
8181
else:
82-
disconnect()
83-
connect(app_name_or_id)
82+
disconnect_app()
83+
connect_app(app_name_or_id)
8484

8585
elif app_name_or_id.startswith("localhost"):
8686

@@ -217,7 +217,7 @@ def connect(app_name_or_id: str):
217217
).wait()
218218

219219

220-
def disconnect(logout: bool = False):
220+
def disconnect_app(logout: bool = False):
221221
"""Disconnect from an App."""
222222
_clean_lightning_connection()
223223

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright The Lightning team.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import sys
16+
17+
import click
18+
import rich
19+
from lightning_cloud.openapi import ProjectIdDataConnectionsBody
20+
from rich.live import Live
21+
from rich.spinner import Spinner
22+
from rich.text import Text
23+
24+
from lightning_app.utilities.app_helpers import Logger
25+
from lightning_app.utilities.cli_helpers import _error_and_exit
26+
from lightning_app.utilities.cloud import _get_project
27+
from lightning_app.utilities.network import LightningClient
28+
29+
logger = Logger(__name__)
30+
31+
32+
@click.argument("name", required=True)
33+
@click.argument("region", required=True)
34+
@click.argument("source", required=True)
35+
@click.argument("destination", required=False)
36+
@click.argument("project_name", required=False)
37+
def connect_data(
38+
name: str,
39+
region: str,
40+
source: str,
41+
destination: str = "",
42+
project_name: str = "",
43+
) -> None:
44+
"""Create a new data connection."""
45+
46+
if sys.platform == "win32":
47+
_error_and_exit("Data connection isn't supported on windows. Open an issue on Github.")
48+
49+
with Live(Spinner("point", text=Text("pending...", style="white")), transient=True) as live:
50+
51+
live.stop()
52+
53+
client = LightningClient()
54+
projects = client.projects_service_list_memberships()
55+
56+
project_id = None
57+
58+
for project in projects.memberships:
59+
60+
if project.name == project_name:
61+
project_id = project.project_id
62+
break
63+
64+
if project_id is None:
65+
project_id = _get_project(client).project_id
66+
67+
if not source.startswith("s3://"):
68+
return _error_and_exit(
69+
"Only public S3 folders are supported for now. Please, open a Github issue with your use case."
70+
)
71+
72+
try:
73+
_ = client.data_connection_service_create_data_connection(
74+
body=ProjectIdDataConnectionsBody(
75+
name=name,
76+
region=region,
77+
source=source,
78+
destination=destination,
79+
),
80+
project_id=project_id,
81+
)
82+
83+
# Note: Expose through lightning show data {DATA_NAME}
84+
# response = client.data_connection_service_list_data_connection_artifacts(
85+
# project_id=project_id,
86+
# id=response.id,
87+
# )
88+
# print(response)
89+
except Exception:
90+
_error_and_exit("The data connection creation failed.")
91+
92+
rich.print(f"[green]Succeeded[/green]: You have created a new data connection {name}.")

src/lightning_app/cli/lightning_cli.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@
3434
from lightning_app.cli.cmd_clusters import AWSClusterManager
3535
from lightning_app.cli.commands.app_commands import _run_app_command
3636
from lightning_app.cli.commands.cd import cd
37-
from lightning_app.cli.commands.connection import (
38-
_list_app_commands,
39-
_retrieve_connection_to_an_app,
40-
connect,
41-
disconnect,
42-
)
4337
from lightning_app.cli.commands.cp import cp
4438
from lightning_app.cli.commands.logs import logs
4539
from lightning_app.cli.commands.ls import ls
4640
from lightning_app.cli.commands.pwd import pwd
41+
from lightning_app.cli.connect.app import (
42+
_list_app_commands,
43+
_retrieve_connection_to_an_app,
44+
connect_app,
45+
disconnect_app,
46+
)
47+
from lightning_app.cli.connect.data import connect_data
4748
from lightning_app.cli.lightning_cli_create import create
4849
from lightning_app.cli.lightning_cli_delete import delete
4950
from lightning_app.cli.lightning_cli_list import get_list
@@ -121,8 +122,21 @@ def show() -> None:
121122
pass
122123

123124

124-
_main.command()(connect)
125-
_main.command()(disconnect)
125+
@_main.group()
126+
def connect() -> None:
127+
"""Connect apps and data."""
128+
pass
129+
130+
131+
@_main.group()
132+
def disconnect() -> None:
133+
"""Disconnect apps."""
134+
pass
135+
136+
137+
connect.command("app")(connect_app)
138+
disconnect.command("app")(disconnect_app)
139+
connect.command("data")(connect_data)
126140
_main.command()(ls)
127141
_main.command()(cd)
128142
_main.command()(cp)
@@ -240,7 +254,7 @@ def login() -> None:
240254
def logout() -> None:
241255
"""Log out of your lightning.ai account."""
242256
Auth().clear()
243-
disconnect(logout=True)
257+
disconnect_app(logout=True)
244258

245259

246260
def _run_app(

0 commit comments

Comments
 (0)