Skip to content

Commit a16e9ef

Browse files
committed
done
1 parent 5a52309 commit a16e9ef

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,16 @@ def check_database_connection() -> None:
210210

211211

212212
@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,
213+
def terminate_dynamic_instances(
214+
user_id: Annotated[int | None, typer.Option(help="the user ID")] = None,
215+
instance_id: Annotated[str | None, typer.Option(help="the instance ID")] = None,
219216
*,
220217
force: Annotated[bool, typer.Option(help="will not ask for confirmation")] = False,
221218
) -> 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))
219+
"""this will terminate the instance(s) used for the given user or instance ID."""
220+
asyncio.run(
221+
api.terminate_dynamic_instances(state, user_id, instance_id, force=force)
222+
)
224223

225224

226225
if __name__ == "__main__":

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ async def summary(
491491
# get all the running instances
492492
assert state.ec2_resource_autoscaling
493493
dynamic_instances = await ec2.list_dynamic_instances_from_ec2(
494-
state, user_id, wallet_id
494+
state, user_id=user_id, wallet_id=wallet_id, instance_id=None
495495
)
496496
dynamic_autoscaled_instances = await _parse_dynamic_instances(
497497
state, dynamic_instances, state.ssh_key_path, user_id, wallet_id
@@ -778,22 +778,35 @@ async def check_database_connection(state: AppState) -> None:
778778
await db.check_db_connection(state)
779779

780780

781-
async def terminate_instances(
782-
state: AppState, user_id: int | None, wallet_id: int | None, *, force: bool
781+
async def terminate_dynamic_instances(
782+
state: AppState,
783+
user_id: int | None,
784+
instance_id: str | None,
785+
*,
786+
force: bool,
783787
) -> None:
784-
assert state.ec2_resource_autoscaling
788+
if not user_id and not instance_id:
789+
rich.print("either define user_id or instance_id!")
790+
raise typer.Exit(127)
785791
dynamic_instances = await ec2.list_dynamic_instances_from_ec2(
786-
state, user_id, wallet_id
792+
state, user_id=None, wallet_id=None, instance_id=instance_id
787793
)
788794

789795
dynamic_autoscaled_instances = await _parse_dynamic_instances(
790-
state, dynamic_instances, state.ssh_key_path, user_id, wallet_id
796+
state, dynamic_instances, state.ssh_key_path, user_id, None
791797
)
792798

793799
if not dynamic_autoscaled_instances:
794800
rich.print("no instances found")
795801
raise typer.Exit(0)
796802

803+
_print_dynamic_instances(
804+
dynamic_autoscaled_instances,
805+
state.environment,
806+
state.ec2_resource_autoscaling.meta.client.meta.region_name,
807+
output=None,
808+
)
809+
797810
for instance in dynamic_autoscaled_instances:
798811
rich.print(
799812
f"terminating instance {instance.ec2_instance.instance_id} with name {utils.get_instance_name(instance.ec2_instance)}"

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def _list_running_ec2_instances(
1818
custom_tags: dict[str, str],
1919
user_id: int | None,
2020
wallet_id: int | None,
21+
instance_id: str | None,
2122
) -> ServiceResourceInstancesCollection:
2223
# get all the running instances
2324

@@ -37,7 +38,8 @@ def _list_running_ec2_instances(
3738
ec2_filters.append({"Name": "tag:user_id", "Values": [f"{user_id}"]})
3839
if wallet_id:
3940
ec2_filters.append({"Name": "tag:wallet_id", "Values": [f"{wallet_id}"]})
40-
41+
if instance_id:
42+
ec2_filters.append({"Name": "instance-id", "Values": [f"{instance_id}"]})
4143
return ec2_resource.instances.filter(Filters=ec2_filters)
4244

4345

@@ -66,13 +68,16 @@ async def list_computational_instances_from_ec2(
6668
custom_tags,
6769
user_id,
6870
wallet_id,
71+
None,
6972
)
7073

7174

7275
async def list_dynamic_instances_from_ec2(
7376
state: AppState,
77+
*,
7478
user_id: int | None,
7579
wallet_id: int | None,
80+
instance_id: str | None,
7681
) -> ServiceResourceInstancesCollection:
7782
assert state.environment["EC2_INSTANCES_KEY_NAME"]
7883
custom_tags = {}
@@ -85,6 +90,7 @@ async def list_dynamic_instances_from_ec2(
8590
custom_tags,
8691
user_id,
8792
wallet_id,
93+
instance_id,
8894
)
8995

9096

@@ -101,6 +107,7 @@ async def get_computational_bastion_instance(state: AppState) -> Instance:
101107
{},
102108
None,
103109
None,
110+
None,
104111
)
105112

106113
possible_bastions = list(
@@ -120,6 +127,7 @@ async def get_dynamic_bastion_instance(state: AppState) -> Instance:
120127
{},
121128
None,
122129
None,
130+
None,
123131
)
124132

125133
possible_bastions = list(

0 commit comments

Comments
 (0)