Skip to content

Commit 70611a8

Browse files
committed
allow to terminate instances
1 parent 0be5ef2 commit 70611a8

File tree

2 files changed

+44
-1
lines changed
  • scripts/maintenance/computational-clusters/autoscaled_monitor

2 files changed

+44
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,19 @@ def check_database_connection() -> None:
209209
asyncio.run(api.check_database_connection(state))
210210

211211

212+
@app.command()
213+
def terminate_instances(
214+
user_id: Annotated[int, typer.Option(help="the user ID")],
215+
wallet_id: Annotated[
216+
Optional[int | None], # noqa: UP007 # typer does not understand | syntax
217+
typer.Option(help="the wallet ID"),
218+
] = None,
219+
*,
220+
force: Annotated[bool, typer.Option(help="will not ask for confirmation")] = False,
221+
) -> None:
222+
"""this will terminate the instance(s) used for the given user/wallet"""
223+
asyncio.run(api.terminate_instances(state, user_id, wallet_id, force=force))
224+
225+
212226
if __name__ == "__main__":
213227
app()

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ async def cancel_jobs( # noqa: C901, PLR0912
728728

729729

730730
async def trigger_cluster_termination(
731-
state: AppState, user_id: int, wallet_id: int, *, force: bool
731+
state: AppState, user_id: int, wallet_id: int | None, *, force: bool
732732
) -> None:
733733
assert state.ec2_resource_clusters_keeper
734734
computational_instances = await ec2.list_computational_instances_from_ec2(
@@ -746,6 +746,7 @@ async def trigger_cluster_termination(
746746
computational_clusters,
747747
state.environment,
748748
state.ec2_resource_clusters_keeper.meta.client.meta.region_name,
749+
output=None,
749750
)
750751
if (force is True) or typer.confirm(
751752
"Are you sure you want to trigger termination of that cluster?"
@@ -775,3 +776,31 @@ async def trigger_cluster_termination(
775776

776777
async def check_database_connection(state: AppState) -> None:
777778
await db.check_db_connection(state)
779+
780+
781+
async def terminate_instances(
782+
state: AppState, user_id: int | None, wallet_id: int | None, *, force: bool
783+
) -> None:
784+
assert state.ec2_resource_autoscaling
785+
dynamic_instances = await ec2.list_dynamic_instances_from_ec2(
786+
state, user_id, wallet_id
787+
)
788+
dynamic_autoscaled_instances = await _parse_dynamic_instances(
789+
state, dynamic_instances, state.ssh_key_path, user_id, wallet_id
790+
)
791+
792+
if not dynamic_autoscaled_instances:
793+
rich.print("no instances found")
794+
raise typer.Exit(0)
795+
796+
for instance in dynamic_autoscaled_instances:
797+
rich.print(
798+
f"terminating instance {instance.ec2_instance.instance_id} with name {utils.get_instance_name(instance)}"
799+
)
800+
if force is True or typer.confirm(
801+
f"Are you sure you want to terminate instance {instance.ec2_instance.instance_id}?"
802+
):
803+
instance.ec2_instance.terminate()
804+
rich.print(f"terminated instance {instance.ec2_instance.instance_id}")
805+
else:
806+
rich.print("not terminating anything")

0 commit comments

Comments
 (0)