Skip to content

Commit e56b23a

Browse files
authored
✨Maintenance: autoscaled clusters allows to write outputs to a file (#7628)
1 parent 37cf6d5 commit e56b23a

File tree

2 files changed

+40
-7
lines changed
  • scripts/maintenance/computational-clusters/autoscaled_monitor

2 files changed

+40
-7
lines changed

scripts/maintenance/computational-clusters/autoscaled_monitor/cli.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ def main(
127127

128128
@app.command()
129129
def summary(
130+
*,
130131
user_id: Annotated[int, typer.Option(help="filters by the user ID")] = 0,
131132
wallet_id: Annotated[int, typer.Option(help="filters by the wallet ID")] = 0,
132133
as_json: Annotated[bool, typer.Option(help="outputs as json")] = False,
134+
output: Annotated[Path | None, typer.Option(help="outputs to a file")] = None,
133135
) -> None:
134136
"""Show a summary of the current situation of autoscaled EC2 instances.
135137
@@ -142,7 +144,13 @@ def summary(
142144
"""
143145

144146
if not asyncio.run(
145-
api.summary(state, user_id or None, wallet_id or None, output_json=as_json)
147+
api.summary(
148+
state,
149+
user_id or None,
150+
wallet_id or None,
151+
output_json=as_json,
152+
output=output,
153+
)
146154
):
147155
raise typer.Exit(1)
148156

scripts/maintenance/computational-clusters/autoscaled_monitor/core.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

158163
def _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

251261
async def _fetch_instance_details(
@@ -416,6 +426,7 @@ async def _parse_dynamic_instances(
416426
def _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

468483
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,
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

706730
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
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

Comments
 (0)