@@ -90,6 +90,7 @@ def _print_dynamic_instances(
9090 instances : list [DynamicInstance ],
9191 environment : dict [str , str | None ],
9292 aws_region : str ,
93+ output : Path | None ,
9394) -> None :
9495 time_now = arrow .utcnow ()
9596 table = Table (
@@ -152,13 +153,18 @@ def _print_dynamic_instances(
152153 f"{ _create_graylog_permalinks (environment , instance .ec2_instance )} " ,
153154 end_section = True ,
154155 )
155- rich .print (table , flush = True )
156+ if output :
157+ with output .open ("w" ) as fp :
158+ rich .print (table , flush = True , file = fp )
159+ else :
160+ rich .print (table , flush = True )
156161
157162
158163def _print_computational_clusters (
159164 clusters : list [ComputationalCluster ],
160165 environment : dict [str , str | None ],
161166 aws_region : str ,
167+ output : Path | None ,
162168) -> None :
163169 time_now = arrow .utcnow ()
164170 table = Table (
@@ -245,7 +251,11 @@ def _print_computational_clusters(
245251 ),
246252 )
247253 table .add_row (end_section = True )
248- rich .print (table )
254+ if output :
255+ with output .open ("a" ) as fp :
256+ rich .print (table , file = fp )
257+ else :
258+ rich .print (table )
249259
250260
251261async def _fetch_instance_details (
@@ -416,6 +426,7 @@ async def _parse_dynamic_instances(
416426def _print_summary_as_json (
417427 dynamic_instances : list [DynamicInstance ],
418428 computational_clusters : list [ComputationalCluster ],
429+ output : Path | None ,
419430) -> None :
420431 result = {
421432 "dynamic_instances" : [
@@ -462,11 +473,20 @@ def _print_summary_as_json(
462473 for cluster in computational_clusters
463474 ],
464475 }
465- rich .print_json (json .dumps (result ))
476+
477+ if output :
478+ output .write_text (json .dumps (result ))
479+ else :
480+ rich .print_json (json .dumps (result ))
466481
467482
468483async def summary (
469- state : AppState , user_id : int | None , wallet_id : int | None , * , output_json : bool
484+ state : AppState ,
485+ user_id : int | None ,
486+ wallet_id : int | None ,
487+ * ,
488+ output_json : bool ,
489+ output : Path | None ,
470490) -> bool :
471491 # get all the running instances
472492 assert state .ec2_resource_autoscaling
@@ -486,18 +506,22 @@ async def summary(
486506 )
487507
488508 if output_json :
489- _print_summary_as_json (dynamic_autoscaled_instances , computational_clusters )
509+ _print_summary_as_json (
510+ dynamic_autoscaled_instances , computational_clusters , output = output
511+ )
490512
491513 if not output_json :
492514 _print_dynamic_instances (
493515 dynamic_autoscaled_instances ,
494516 state .environment ,
495517 state .ec2_resource_autoscaling .meta .client .meta .region_name ,
518+ output = output ,
496519 )
497520 _print_computational_clusters (
498521 computational_clusters ,
499522 state .environment ,
500523 state .ec2_resource_clusters_keeper .meta .client .meta .region_name ,
524+ output = output ,
501525 )
502526
503527 time_threshold = arrow .utcnow ().shift (minutes = - 30 ).datetime
@@ -704,7 +728,7 @@ async def cancel_jobs( # noqa: C901, PLR0912
704728
705729
706730async def trigger_cluster_termination (
707- state : AppState , user_id : int , wallet_id : int , * , force : bool
731+ state : AppState , user_id : int , wallet_id : int | None , * , force : bool
708732) -> None :
709733 assert state .ec2_resource_clusters_keeper
710734 computational_instances = await ec2 .list_computational_instances_from_ec2 (
@@ -722,6 +746,7 @@ async def trigger_cluster_termination(
722746 computational_clusters ,
723747 state .environment ,
724748 state .ec2_resource_clusters_keeper .meta .client .meta .region_name ,
749+ output = None ,
725750 )
726751 if (force is True ) or typer .confirm (
727752 "Are you sure you want to trigger termination of that cluster?"
0 commit comments