From 93af5e1b29ddabef3f79072c2ea5cfbbaf5f7e23 Mon Sep 17 00:00:00 2001 From: Atin Sood Date: Wed, 26 Oct 2022 15:41:29 -0400 Subject: [PATCH] minor changes for print refactoring --- src/codeflare_sdk/cluster/cluster.py | 12 ++++++++---- src/codeflare_sdk/utils/pretty_print.py | 19 ++++++++++++++----- tests/test_clusters.py | 5 +++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/codeflare_sdk/cluster/cluster.py b/src/codeflare_sdk/cluster/cluster.py index 70347522b..4ec1f258d 100644 --- a/src/codeflare_sdk/cluster/cluster.py +++ b/src/codeflare_sdk/cluster/cluster.py @@ -41,16 +41,18 @@ def down(self, namespace='default'): oc.invoke("delete", ["AppWrapper", self.app_wrapper_yaml]) def status(self, print_to_console=True): - cluster = _ray_cluster_status(self.config.name) + cluster = _ray_cluster_status(self.config.name) if cluster: if print_to_console: pretty_print.print_clusters([cluster]) return cluster.status else: + if print_to_console: + pretty_print.print_no_resources_found() return None # checks whether the ray cluster is ready - def is_ready(self): + def is_ready(self, print_to_console=True): ready = False status = CodeFlareClusterStatus.UNKNOWN # check the app wrapper status @@ -73,11 +75,13 @@ def is_ready(self): if cluster: if cluster.status == RayClusterStatus.READY: ready = True - status = CodeFlareClusterStatus.READY + status = CodeFlareClusterStatus.READY elif cluster.status in [RayClusterStatus.UNHEALTHY, RayClusterStatus.FAILED]: ready = False status = CodeFlareClusterStatus.FAILED - + + if print_to_console: + pretty_print.print_clusters([cluster]) return status, ready diff --git a/src/codeflare_sdk/utils/pretty_print.py b/src/codeflare_sdk/utils/pretty_print.py index 34b89ff5d..a31da3d42 100644 --- a/src/codeflare_sdk/utils/pretty_print.py +++ b/src/codeflare_sdk/utils/pretty_print.py @@ -5,21 +5,30 @@ from rich.panel import Panel from rich import box from typing import List -from ..cluster.model import RayCluster, AppWrapper +from ..cluster.model import RayCluster, AppWrapper, RayClusterStatus -def _print_no_cluster_found(): +def print_no_resources_found(): + console = Console() + console.print(Panel("[red]No resources found")) + + +def print_appwrappers_status(): pass def print_clusters(clusters:List[RayCluster], verbose=True): + if not clusters == 0: + print_no_resources_found() + return #shortcircuit + console = Console() title_printed = False #FIXME handle case where no clusters are found if len(clusters) == 0: - _print_no_cluster_found() + print_no_resources_found() return #exit early for cluster in clusters: - status = "Active :white_heavy_check_mark:" if cluster.status.lower() == 'ready' else "InActive :x:" + status = "Active :white_heavy_check_mark:" if cluster.status == RayClusterStatus.READY else "InActive :x:" name = cluster.name dashboard = f"https://codeflare-raydashboard.research.ibm.com?rayclustername={name}" mincount = str(cluster.min_workers) @@ -38,7 +47,7 @@ def print_clusters(clusters:List[RayCluster], verbose=True): table0.add_row("") table0.add_row("[bold underline]"+name,status) table0.add_row() - table0.add_row(f"[bold]URI:[/bold] ray://{name}-head.codeflare.svc:1001") + table0.add_row(f"[bold]URI:[/bold] ray://{name}-head-svc:1001") #format that is used to generate the name of the service table0.add_row() table0.add_row(f"[link={dashboard} blue underline]Dashboard:link:[/link]") table0.add_row("") #empty row for spacing diff --git a/tests/test_clusters.py b/tests/test_clusters.py index 6e86ef9ff..8ef3aced6 100644 --- a/tests/test_clusters.py +++ b/tests/test_clusters.py @@ -1,5 +1,6 @@ from codeflare_sdk.cluster.cluster import list_all_clusters, _app_wrapper_status from codeflare_sdk.cluster.cluster import Cluster, ClusterConfiguration + import time def test_cluster_up(): @@ -21,3 +22,7 @@ def test_cluster_down(): cluster = Cluster(ClusterConfiguration(name='raycluster-autoscaler')) cluster.down() + +def test_no_resources_found(): + from codeflare_sdk.utils import pretty_print + pretty_print.print_no_resources_found() \ No newline at end of file