Skip to content

Commit 7b2788e

Browse files
rlizzoBorda
andauthored
Deduplicate top level lighting CLI command groups (#15761)
* unify remove and delete command groups & the add and delete command groups * added changelog * fix tests * Apply suggestions from code review Co-authored-by: Jirka Borovec <[email protected]>
1 parent 75b0573 commit 7b2788e

File tree

8 files changed

+53
-68
lines changed

8 files changed

+53
-68
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1313

1414
### Changed
1515

16-
-
16+
- `lightning add ssh-key` CLI command has been transitioned to `lightning create ssh-key` with the same calling signature ([#15761](https://github.com/Lightning-AI/lightning/pull/15761))
17+
- `lightning remove ssh-key` CLI command has been transitioned to `lightning delete ssh-key` with the same calling signature ([#15761](https://github.com/Lightning-AI/lightning/pull/15761))
1718

1819

1920
### Deprecated

src/lightning_app/cli/cmd_ssh_keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def as_json(self) -> str:
1919
return json.dumps(self.ssh_keys)
2020

2121
def as_table(self) -> Table:
22-
table = Table("id", "public_key", "created", show_header=True)
22+
table = Table("id", "public_key", "created", show_header=True, header_style="bold green")
2323
for ssh_key in self.ssh_keys:
2424
table.add_row(
2525
ssh_key.id,

src/lightning_app/cli/lightning_cli.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
disconnect,
2626
)
2727
from lightning_app.cli.commands.logs import logs
28-
from lightning_app.cli.lightning_cli_add import cli_add
2928
from lightning_app.cli.lightning_cli_create import create
3029
from lightning_app.cli.lightning_cli_delete import delete
3130
from lightning_app.cli.lightning_cli_list import get_list
32-
from lightning_app.cli.lightning_cli_remove import cli_remove
3331
from lightning_app.core.constants import DEBUG, ENABLE_APP_COMMENT_COMMAND_EXECUTION, get_lightning_cloud_url
3432
from lightning_app.runners.runtime import dispatch
3533
from lightning_app.runners.runtime_type import RuntimeType
@@ -376,8 +374,6 @@ def run_app(
376374
_main.add_command(get_list)
377375
_main.add_command(delete)
378376
_main.add_command(create)
379-
_main.add_command(cli_add)
380-
_main.add_command(cli_remove)
381377

382378

383379
@_main.command("ssh")

src/lightning_app/cli/lightning_cli_add.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/lightning_app/cli/lightning_cli_create.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
from typing import Any
1+
import os
2+
from pathlib import Path
3+
from typing import Any, Optional, Union
24

35
import click
6+
from lightning_cloud.openapi.rest import ApiException
47

58
from lightning_app.cli.cmd_clusters import _check_cluster_name_is_valid, AWSClusterManager
9+
from lightning_app.cli.cmd_ssh_keys import _SSHKeyManager
610

711

812
@click.group("create")
@@ -77,3 +81,29 @@ def create_cluster(
7781
cost_savings=not enable_performance,
7882
wait=wait,
7983
)
84+
85+
86+
@create.command("ssh-key")
87+
@click.option("--name", "key_name", default=None, help="name of ssh key")
88+
@click.option("--comment", "comment", default="", help="comment detailing your SSH key")
89+
@click.option(
90+
"--public-key",
91+
"public_key",
92+
help="public key or path to public key file",
93+
required=True,
94+
)
95+
def add_ssh_key(
96+
public_key: Union[str, "os.PathLike[str]"], key_name: Optional[str] = None, comment: Optional[str] = None
97+
) -> None:
98+
"""Add a new Lightning AI ssh-key to your account."""
99+
ssh_key_manager = _SSHKeyManager()
100+
101+
new_public_key = Path(str(public_key)).read_text() if os.path.isfile(str(public_key)) else public_key
102+
try:
103+
ssh_key_manager.add_key(name=key_name, comment=comment, public_key=str(new_public_key))
104+
except ApiException as e:
105+
# if we got an exception it might be the user passed the private key file
106+
if os.path.isfile(str(public_key)) and os.path.isfile(f"{public_key}.pub"):
107+
ssh_key_manager.add_key(name=key_name, comment=comment, public_key=Path(f"{public_key}.pub").read_text())
108+
else:
109+
raise e

src/lightning_app/cli/lightning_cli_delete.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import click
22

33
from lightning_app.cli.cmd_clusters import AWSClusterManager
4+
from lightning_app.cli.cmd_ssh_keys import _SSHKeyManager
45

56

67
@click.group("delete")
@@ -49,3 +50,11 @@ def delete_cluster(cluster: str, force: bool = False, wait: bool = False) -> Non
4950
"""
5051
cluster_manager = AWSClusterManager()
5152
cluster_manager.delete(cluster_id=cluster, force=force, wait=wait)
53+
54+
55+
@delete.command("ssh-key")
56+
@click.argument("key_id")
57+
def remove_ssh_key(key_id: str) -> None:
58+
"""Delete a ssh-key from your Lightning AI account."""
59+
ssh_key_manager = _SSHKeyManager()
60+
ssh_key_manager.remove_key(key_id=key_id)

src/lightning_app/cli/lightning_cli_remove.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/tests_app/cli/test_cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ def test_main_lightning_cli_no_arguments():
6161
assert "create " in res
6262
assert "show " in res
6363
assert "ssh " in res
64-
assert "add " in res
65-
assert "remove " in res
6664

6765

6866
def test_main_lightning_cli_help():
@@ -76,8 +74,6 @@ def test_main_lightning_cli_help():
7674
assert "create " in res
7775
assert "show " in res
7876
assert "ssh " in res
79-
assert "add " in res
80-
assert "remove " in res
8177

8278
res = os.popen("lightning run --help").read()
8379
assert "app " in res
@@ -97,6 +93,16 @@ def test_main_lightning_cli_help():
9793
res = os.popen("lightning show cluster --help").read()
9894
assert "logs " in res
9995

96+
# inspect create group
97+
res = os.popen("lightning create --help").read()
98+
assert "cluster " in res
99+
assert "ssh-key " in res
100+
101+
# inspect delete group
102+
res = os.popen("lightning delete --help").read()
103+
assert "cluster " in res
104+
assert "ssh-key " in res
105+
100106

101107
@mock.patch("lightning_cloud.login.Auth.authenticate", MagicMock())
102108
@mock.patch("lightning_app.cli.cmd_clusters.AWSClusterManager.create")

0 commit comments

Comments
 (0)