|
| 1 | +from typing import Optional |
| 2 | + |
1 | 3 | import click
|
| 4 | +import inquirer |
| 5 | +from inquirer.themes import GreenPassion |
| 6 | +from lightning_cloud.openapi import V1ClusterType |
| 7 | +from rich.console import Console |
2 | 8 |
|
| 9 | +from lightning_app.cli.cmd_apps import _AppManager |
3 | 10 | from lightning_app.cli.cmd_clusters import AWSClusterManager
|
4 | 11 | from lightning_app.cli.cmd_ssh_keys import _SSHKeyManager
|
5 | 12 |
|
@@ -40,6 +47,168 @@ def delete_cluster(cluster: str, force: bool = False, do_async: bool = False) ->
|
40 | 47 | cluster_manager.delete(cluster_id=cluster, force=force, do_async=do_async)
|
41 | 48 |
|
42 | 49 |
|
| 50 | +def _find_cluster_for_user(app_name: str, cluster_id: Optional[str]) -> str: |
| 51 | + console = Console() |
| 52 | + cluster_manager = AWSClusterManager() |
| 53 | + |
| 54 | + default_cluster, valid_cluster_list = None, [] |
| 55 | + for cluster in cluster_manager.list_clusters(): |
| 56 | + valid_cluster_list.append(cluster.id) |
| 57 | + if cluster.spec.cluster_type == V1ClusterType.GLOBAL and default_cluster is None: |
| 58 | + default_cluster = cluster.id |
| 59 | + break |
| 60 | + |
| 61 | + # when no cluster-id is passed in, use the default (Lightning Cloud) cluster |
| 62 | + if cluster_id is None: |
| 63 | + cluster_id = default_cluster |
| 64 | + |
| 65 | + if cluster_id not in valid_cluster_list: |
| 66 | + console.print(f'[b][yellow]You don\'t have access to cluster "{cluster_id}"[/yellow][/b]') |
| 67 | + if len(valid_cluster_list) == 1: |
| 68 | + # if there are no BYOC clusters, then confirm that |
| 69 | + # the user wants to fall back to the Lightning Cloud. |
| 70 | + try: |
| 71 | + ask = [ |
| 72 | + inquirer.Confirm( |
| 73 | + "confirm", |
| 74 | + message=f'Did you mean to specify the default Lightning Cloud cluster "{default_cluster}"?', |
| 75 | + default=True, |
| 76 | + ), |
| 77 | + ] |
| 78 | + if inquirer.prompt(ask, theme=GreenPassion(), raise_keyboard_interrupt=True)["confirm"] is False: |
| 79 | + console.print("[b][red]Aborted![/b][/red]") |
| 80 | + raise InterruptedError |
| 81 | + except KeyboardInterrupt: |
| 82 | + console.print("[b][red]Cancelled by user![/b][/red]") |
| 83 | + raise InterruptedError |
| 84 | + cluster_id = default_cluster |
| 85 | + else: |
| 86 | + # When there are BYOC clusters, have them select which cluster to use from all available. |
| 87 | + try: |
| 88 | + ask = [ |
| 89 | + inquirer.List( |
| 90 | + "cluster", |
| 91 | + message=f'Please select which cluster app "{app_name}" should be deleted from', |
| 92 | + choices=valid_cluster_list, |
| 93 | + default=default_cluster, |
| 94 | + ), |
| 95 | + ] |
| 96 | + cluster_id = inquirer.prompt(ask, theme=GreenPassion(), raise_keyboard_interrupt=True)["cluster"] |
| 97 | + except KeyboardInterrupt: |
| 98 | + console.print("[b][red]Cancelled by user![/b][/red]") |
| 99 | + raise InterruptedError |
| 100 | + |
| 101 | + if cluster_id is None: |
| 102 | + # stupid mypy thing... |
| 103 | + cluster_id = "" |
| 104 | + |
| 105 | + return cluster_id |
| 106 | + |
| 107 | + |
| 108 | +def _find_selected_app_instance_id(app_name: str, cluster_id: str) -> str: |
| 109 | + console = Console() |
| 110 | + app_manager = _AppManager() |
| 111 | + |
| 112 | + all_app_names_and_ids = {} |
| 113 | + selected_app_instance_id = None |
| 114 | + |
| 115 | + for app in app_manager.list_apps(cluster_id=cluster_id): |
| 116 | + all_app_names_and_ids[app.name] = app.id |
| 117 | + # figure out the ID of some app_name on the cluster. |
| 118 | + if app_name == app.name or app_name == app.id: |
| 119 | + selected_app_instance_id = app.id |
| 120 | + break |
| 121 | + |
| 122 | + if selected_app_instance_id is None: |
| 123 | + # when there is no app with the given app_name on the cluster, |
| 124 | + # ask the user which app they would like to delete. |
| 125 | + console.print(f'[b][yellow]Cluster "{cluster_id}" does not have an app named "{app_name}"[/yellow][/b]') |
| 126 | + try: |
| 127 | + ask = [ |
| 128 | + inquirer.List( |
| 129 | + "app_name", |
| 130 | + message="Select the app name to delete", |
| 131 | + choices=list(all_app_names_and_ids.keys()), |
| 132 | + ), |
| 133 | + ] |
| 134 | + app_name = inquirer.prompt(ask, theme=GreenPassion(), raise_keyboard_interrupt=True)["app_name"] |
| 135 | + selected_app_instance_id = all_app_names_and_ids[app_name] |
| 136 | + except KeyboardInterrupt: |
| 137 | + console.print("[b][red]Cancelled by user![/b][/red]") |
| 138 | + raise InterruptedError |
| 139 | + |
| 140 | + return selected_app_instance_id |
| 141 | + |
| 142 | + |
| 143 | +def _delete_app_confirmation_prompt(app_name: str, cluster_id: str) -> None: |
| 144 | + console = Console() |
| 145 | + |
| 146 | + # when the --yes / -y flags were not passed, do a final |
| 147 | + # confirmation that the user wants to delete the app. |
| 148 | + try: |
| 149 | + ask = [ |
| 150 | + inquirer.Confirm( |
| 151 | + "confirm", |
| 152 | + message=f'Are you sure you want to delete app "{app_name}" on cluster "{cluster_id}"?', |
| 153 | + default=False, |
| 154 | + ), |
| 155 | + ] |
| 156 | + if inquirer.prompt(ask, theme=GreenPassion(), raise_keyboard_interrupt=True)["confirm"] is False: |
| 157 | + console.print("[b][red]Aborted![/b][/red]") |
| 158 | + raise InterruptedError |
| 159 | + except KeyboardInterrupt: |
| 160 | + console.print("[b][red]Cancelled by user![/b][/red]") |
| 161 | + raise InterruptedError |
| 162 | + |
| 163 | + |
| 164 | +@delete.command("app") |
| 165 | +@click.argument("app-name", type=str) |
| 166 | +@click.option( |
| 167 | + "--cluster-id", |
| 168 | + type=str, |
| 169 | + default=None, |
| 170 | + help="Delete the Lighting App from a specific Lightning AI BYOC compute cluster", |
| 171 | +) |
| 172 | +@click.option( |
| 173 | + "skip_user_confirm_prompt", |
| 174 | + "--yes", |
| 175 | + "-y", |
| 176 | + is_flag=True, |
| 177 | + default=False, |
| 178 | + help="Do not prompt for confirmation.", |
| 179 | +) |
| 180 | +def delete_app(app_name: str, cluster_id: str, skip_user_confirm_prompt: bool) -> None: |
| 181 | + """Delete a Lightning app. |
| 182 | +
|
| 183 | + Deleting an app also deletes all app websites, works, artifacts, and logs. This permanently removes any record of |
| 184 | + the app as well as all any of its associated resources and data. This does not affect any resources and data |
| 185 | + associated with other Lightning apps on your account. |
| 186 | + """ |
| 187 | + console = Console() |
| 188 | + |
| 189 | + try: |
| 190 | + cluster_id = _find_cluster_for_user(app_name=app_name, cluster_id=cluster_id) |
| 191 | + selected_app_instance_id = _find_selected_app_instance_id(app_name=app_name, cluster_id=cluster_id) |
| 192 | + if not skip_user_confirm_prompt: |
| 193 | + _delete_app_confirmation_prompt(app_name=app_name, cluster_id=cluster_id) |
| 194 | + except InterruptedError: |
| 195 | + return |
| 196 | + |
| 197 | + try: |
| 198 | + # Delete the app! |
| 199 | + app_manager = _AppManager() |
| 200 | + app_manager.delete(app_id=selected_app_instance_id) |
| 201 | + except Exception as e: |
| 202 | + console.print( |
| 203 | + f'[b][red]An issue occurred while deleting app "{app_name}. If the issue persists, please ' |
| 204 | + "reach out to us at [link=mailto:[email protected]][email protected][/link][/b][/red]." |
| 205 | + ) |
| 206 | + raise click.ClickException(str(e)) |
| 207 | + |
| 208 | + console.print(f'[b][green]App "{app_name}" has been successfully deleted from cluster "{cluster_id}"![/green][/b]') |
| 209 | + return |
| 210 | + |
| 211 | + |
43 | 212 | @delete.command("ssh-key")
|
44 | 213 | @click.argument("key_id")
|
45 | 214 | def remove_ssh_key(key_id: str) -> None:
|
|
0 commit comments