|
5 | 5 | from pathlib import Path |
6 | 6 | from typing import List, Tuple, Union |
7 | 7 |
|
| 8 | +import arrow |
8 | 9 | import click |
9 | 10 | import requests |
10 | 11 | import rich |
|
13 | 14 |
|
14 | 15 | from lightning_app import __version__ as ver |
15 | 16 | from lightning_app.cli import cmd_init, cmd_install, cmd_pl_init, cmd_react_ui_init |
| 17 | +from lightning_app.cli.cmd_clusters import AWSClusterManager |
16 | 18 | from lightning_app.cli.lightning_cli_create import create |
17 | 19 | from lightning_app.cli.lightning_cli_delete import delete |
18 | 20 | from lightning_app.cli.lightning_cli_list import get_list |
|
21 | 23 | from lightning_app.runners.runtime_type import RuntimeType |
22 | 24 | from lightning_app.utilities.app_logs import _app_logs_reader |
23 | 25 | from lightning_app.utilities.cli_helpers import ( |
| 26 | + _arrow_time_callback, |
24 | 27 | _format_input_env_variables, |
25 | 28 | _retrieve_application_url_and_available_commands, |
26 | 29 | ) |
27 | 30 | from lightning_app.utilities.cloud import _get_project |
| 31 | +from lightning_app.utilities.cluster_logs import _cluster_logs_reader |
28 | 32 | from lightning_app.utilities.enum import OpenAPITags |
29 | 33 | from lightning_app.utilities.install_components import register_all_external_components |
30 | 34 | from lightning_app.utilities.login import Auth |
@@ -141,6 +145,94 @@ def logs(app_name: str, components: List[str], follow: bool) -> None: |
141 | 145 | rich.print(f"[{color}]{log_event.component_name}[/{color}] {date} {log_event.message}") |
142 | 146 |
|
143 | 147 |
|
| 148 | +@show.group() |
| 149 | +def cluster(): |
| 150 | + """Groups cluster commands inside show.""" |
| 151 | + pass |
| 152 | + |
| 153 | + |
| 154 | +@cluster.command(name="logs") |
| 155 | +@click.argument("cluster_name", required=True) |
| 156 | +@click.option( |
| 157 | + "--from", |
| 158 | + "from_time", |
| 159 | + default="24 hours ago", |
| 160 | + help="The starting timestamp to query cluster logs from. Human-readable (e.g. '48 hours ago') or ISO 8601 " |
| 161 | + "(e.g. '2022-08-23 12:34') formats.", |
| 162 | + callback=_arrow_time_callback, |
| 163 | +) |
| 164 | +@click.option( |
| 165 | + "--to", |
| 166 | + "to_time", |
| 167 | + default="0 seconds ago", |
| 168 | + callback=_arrow_time_callback, |
| 169 | + help="The end timestamp / relative time increment to query logs for. This is ignored when following logs (with " |
| 170 | + "-f/--follow). The same format as --from option has.", |
| 171 | +) |
| 172 | +@click.option("--limit", default=1000, help="The max number of log lines returned.") |
| 173 | +@click.option("-f", "--follow", required=False, is_flag=True, help="Wait for new logs, to exit use CTRL+C.") |
| 174 | +def cluster_logs(cluster_name: str, to_time: arrow.Arrow, from_time: arrow.Arrow, limit: int, follow: bool) -> None: |
| 175 | + """Show cluster logs. |
| 176 | +
|
| 177 | + Example uses: |
| 178 | +
|
| 179 | + Print cluster logs: |
| 180 | +
|
| 181 | + $ lightning show cluster logs my-cluster |
| 182 | +
|
| 183 | +
|
| 184 | + Print cluster logs and wait for new logs: |
| 185 | +
|
| 186 | + $ lightning show cluster logs my-cluster --follow |
| 187 | +
|
| 188 | +
|
| 189 | + Print cluster logs, from 48 hours ago to now: |
| 190 | +
|
| 191 | + $ lightning show cluster logs my-cluster --from "48 hours ago" |
| 192 | +
|
| 193 | +
|
| 194 | + Print cluster logs, 10 most recent lines: |
| 195 | +
|
| 196 | + $ lightning show cluster logs my-cluster --limit 10 |
| 197 | + """ |
| 198 | + |
| 199 | + client = LightningClient() |
| 200 | + cluster_manager = AWSClusterManager() |
| 201 | + existing_cluster_list = cluster_manager.get_clusters() |
| 202 | + |
| 203 | + clusters = {cluster.name: cluster.id for cluster in existing_cluster_list.clusters} |
| 204 | + |
| 205 | + if not clusters: |
| 206 | + raise click.ClickException("You don't have any clusters.") |
| 207 | + |
| 208 | + if not cluster_name: |
| 209 | + raise click.ClickException( |
| 210 | + f"You have not specified any clusters. Please select one of available: [{', '.join(clusters.keys())}]" |
| 211 | + ) |
| 212 | + |
| 213 | + if cluster_name not in clusters: |
| 214 | + raise click.ClickException( |
| 215 | + f"The cluster '{cluster_name}' does not exist." |
| 216 | + f" Please select one of the following: [{', '.join(clusters.keys())}]" |
| 217 | + ) |
| 218 | + |
| 219 | + log_reader = _cluster_logs_reader( |
| 220 | + client=client, |
| 221 | + cluster_id=clusters[cluster_name], |
| 222 | + start=from_time.int_timestamp, |
| 223 | + end=to_time.int_timestamp, |
| 224 | + limit=limit, |
| 225 | + follow=follow, |
| 226 | + ) |
| 227 | + |
| 228 | + colors = {"error": "red", "warn": "yellow", "info": "green"} |
| 229 | + |
| 230 | + for log_event in log_reader: |
| 231 | + date = log_event.timestamp.strftime("%m/%d/%Y %H:%M:%S") |
| 232 | + color = colors.get(log_event.labels.level, "green") |
| 233 | + rich.print(f"[{color}]{log_event.labels.level:5}[/{color}] {date} {log_event.message.rstrip()}") |
| 234 | + |
| 235 | + |
144 | 236 | @_main.command() |
145 | 237 | def login(): |
146 | 238 | """Log in to your lightning.ai account.""" |
|
0 commit comments