@@ -90,6 +90,7 @@ def _print_dynamic_instances(
90
90
instances : list [DynamicInstance ],
91
91
environment : dict [str , str | None ],
92
92
aws_region : str ,
93
+ output : Path | None ,
93
94
) -> None :
94
95
time_now = arrow .utcnow ()
95
96
table = Table (
@@ -152,13 +153,18 @@ def _print_dynamic_instances(
152
153
f"{ _create_graylog_permalinks (environment , instance .ec2_instance )} " ,
153
154
end_section = True ,
154
155
)
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 )
156
161
157
162
158
163
def _print_computational_clusters (
159
164
clusters : list [ComputationalCluster ],
160
165
environment : dict [str , str | None ],
161
166
aws_region : str ,
167
+ output : Path | None ,
162
168
) -> None :
163
169
time_now = arrow .utcnow ()
164
170
table = Table (
@@ -245,7 +251,11 @@ def _print_computational_clusters(
245
251
),
246
252
)
247
253
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 )
249
259
250
260
251
261
async def _fetch_instance_details (
@@ -416,6 +426,7 @@ async def _parse_dynamic_instances(
416
426
def _print_summary_as_json (
417
427
dynamic_instances : list [DynamicInstance ],
418
428
computational_clusters : list [ComputationalCluster ],
429
+ output : Path | None ,
419
430
) -> None :
420
431
result = {
421
432
"dynamic_instances" : [
@@ -462,11 +473,20 @@ def _print_summary_as_json(
462
473
for cluster in computational_clusters
463
474
],
464
475
}
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 ))
466
481
467
482
468
483
async 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 ,
470
490
) -> bool :
471
491
# get all the running instances
472
492
assert state .ec2_resource_autoscaling
@@ -486,18 +506,22 @@ async def summary(
486
506
)
487
507
488
508
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
+ )
490
512
491
513
if not output_json :
492
514
_print_dynamic_instances (
493
515
dynamic_autoscaled_instances ,
494
516
state .environment ,
495
517
state .ec2_resource_autoscaling .meta .client .meta .region_name ,
518
+ output = output ,
496
519
)
497
520
_print_computational_clusters (
498
521
computational_clusters ,
499
522
state .environment ,
500
523
state .ec2_resource_clusters_keeper .meta .client .meta .region_name ,
524
+ output = output ,
501
525
)
502
526
503
527
time_threshold = arrow .utcnow ().shift (minutes = - 30 ).datetime
@@ -704,7 +728,7 @@ async def cancel_jobs( # noqa: C901, PLR0912
704
728
705
729
706
730
async 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
708
732
) -> None :
709
733
assert state .ec2_resource_clusters_keeper
710
734
computational_instances = await ec2 .list_computational_instances_from_ec2 (
@@ -722,6 +746,7 @@ async def trigger_cluster_termination(
722
746
computational_clusters ,
723
747
state .environment ,
724
748
state .ec2_resource_clusters_keeper .meta .client .meta .region_name ,
749
+ output = None ,
725
750
)
726
751
if (force is True ) or typer .confirm (
727
752
"Are you sure you want to trigger termination of that cluster?"
0 commit comments