Skip to content

Commit 7f7e476

Browse files
committed
fixed mypy
1 parent 8c72037 commit 7f7e476

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from typing import Final
22

3-
from aws_library.ec2._models import AWSTagKey
3+
from aws_library.ec2._models import AWSTagKey, AWSTagValue
44
from pydantic import parse_obj_as
55

66
DOCKER_STACK_DEPLOY_COMMAND_NAME: Final[str] = "private cluster docker deploy"
77
DOCKER_STACK_DEPLOY_COMMAND_EC2_TAG_KEY: Final[AWSTagKey] = parse_obj_as(
88
AWSTagKey, "io.simcore.clusters-keeper.private_cluster_docker_deploy"
99
)
10+
11+
USER_ID_TAG_KEY: Final[AWSTagKey] = parse_obj_as(AWSTagKey, "user_id")
12+
WALLET_ID_TAG_KEY: Final[AWSTagKey] = parse_obj_as(AWSTagKey, "wallet_id")
13+
ROLE_TAG_KEY: Final[AWSTagKey] = parse_obj_as(AWSTagKey, "role")
14+
WORKER_ROLE_TAG_VALUE: Final[AWSTagValue] = parse_obj_as(AWSTagValue, "worker")
15+
MANAGER_ROLE_TAG_VALUE: Final[AWSTagValue] = parse_obj_as(AWSTagValue, "manager")

services/clusters-keeper/src/simcore_service_clusters_keeper/core/settings.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ class Config(EC2Settings.Config):
5353
class ClustersKeeperSSMSettings(SSMSettings):
5454
class Config(SSMSettings.Config):
5555
env_prefix = CLUSTERS_KEEPER_ENV_PREFIX
56-
prefixed_examples: ClassVar[list[dict[str, Any]]] = [
57-
{f"{CLUSTERS_KEEPER_ENV_PREFIX}{key}": var for key, var in example.items()}
58-
for example in SSMSettings.Config.schema_extra["examples"]
59-
]
6056

61-
schema_extra: ClassVar[dict[str, Any]] = {
62-
"examples": prefixed_examples,
57+
schema_extra: ClassVar[dict[str, Any]] = { # type: ignore[misc]
58+
"examples": [
59+
{
60+
f"{CLUSTERS_KEEPER_ENV_PREFIX}{key}": var
61+
for key, var in example.items()
62+
}
63+
for example in SSMSettings.Config.schema_extra["examples"]
64+
],
6365
}
6466

6567

services/clusters-keeper/src/simcore_service_clusters_keeper/modules/clusters_management_core.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
from ..constants import (
1717
DOCKER_STACK_DEPLOY_COMMAND_EC2_TAG_KEY,
1818
DOCKER_STACK_DEPLOY_COMMAND_NAME,
19+
ROLE_TAG_KEY,
20+
USER_ID_TAG_KEY,
21+
WALLET_ID_TAG_KEY,
22+
WORKER_ROLE_TAG_VALUE,
1923
)
2024
from ..core.settings import get_application_settings
2125
from ..modules.clusters import (
@@ -26,7 +30,12 @@
2630
)
2731
from ..utils.clusters import create_deploy_cluster_stack_script
2832
from ..utils.dask import get_scheduler_auth, get_scheduler_url
29-
from ..utils.ec2 import HEARTBEAT_TAG_KEY, get_cluster_name
33+
from ..utils.ec2 import (
34+
HEARTBEAT_TAG_KEY,
35+
get_cluster_name,
36+
user_id_from_instance_tags,
37+
wallet_id_from_instance_tags,
38+
)
3039
from .dask import is_scheduler_busy, ping_scheduler
3140
from .ec2 import get_ec2_client
3241
from .ssm import get_ssm_client
@@ -196,24 +205,26 @@ async def check_clusters(app: FastAPI) -> None:
196205
]
197206
started_instances_ready_for_command = ec2_connected_to_ssm_server
198207
if started_instances_ready_for_command:
199-
ssm_command = await ssm_client.send_command(
200-
[i.id for i in started_instances_ready_for_command],
201-
command=create_deploy_cluster_stack_script(
202-
app_settings,
203-
cluster_machines_name_prefix=get_cluster_name(
208+
# we need to send 1 command per machine here, as the user_id/wallet_id changes
209+
for i in started_instances_ready_for_command:
210+
ssm_command = await ssm_client.send_command(
211+
[i.id],
212+
command=create_deploy_cluster_stack_script(
204213
app_settings,
205-
user_id=user_id,
206-
wallet_id=wallet_id,
207-
is_manager=False,
214+
cluster_machines_name_prefix=get_cluster_name(
215+
app_settings,
216+
user_id=user_id_from_instance_tags(i.tags),
217+
wallet_id=wallet_id_from_instance_tags(i.tags),
218+
is_manager=False,
219+
),
220+
additional_custom_tags={
221+
USER_ID_TAG_KEY: i.tags[USER_ID_TAG_KEY],
222+
WALLET_ID_TAG_KEY: i.tags[WALLET_ID_TAG_KEY],
223+
ROLE_TAG_KEY: WORKER_ROLE_TAG_VALUE,
224+
},
208225
),
209-
additional_custom_tags={
210-
AWSTagKey("user_id"): AWSTagValue(f"{user_id}"),
211-
AWSTagKey("wallet_id"): AWSTagValue(f"{wallet_id}"),
212-
AWSTagKey("role"): AWSTagValue("worker"),
213-
},
214-
),
215-
command_name=DOCKER_STACK_DEPLOY_COMMAND_NAME,
216-
)
226+
command_name=DOCKER_STACK_DEPLOY_COMMAND_NAME,
227+
)
217228
await ec2_client.set_instances_tags(
218229
started_instances_ready_for_command,
219230
tags={

services/clusters-keeper/src/simcore_service_clusters_keeper/modules/ssm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def on_startup() -> None:
2121
app.state.ssm_client = None
2222
settings: SSMSettings | None = get_application_settings(
2323
app
24-
).AUTOSCALING_SSM_ACCESS
24+
).CLUSTERS_KEEPER_SSM_ACCESS
2525

2626
if not settings:
2727
_logger.warning("SSM client is de-activated in the settings")

services/clusters-keeper/src/simcore_service_clusters_keeper/utils/ec2.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
from pydantic import parse_obj_as
88

99
from .._meta import VERSION
10+
from ..constants import (
11+
MANAGER_ROLE_TAG_VALUE,
12+
ROLE_TAG_KEY,
13+
USER_ID_TAG_KEY,
14+
WALLET_ID_TAG_KEY,
15+
)
1016
from ..core.settings import ApplicationSettings
1117

1218
_APPLICATION_TAG_KEY: Final[str] = "io.simcore.clusters-keeper"
@@ -50,9 +56,9 @@ def creation_ec2_tags(
5056
app_settings, user_id=user_id, wallet_id=wallet_id, is_manager=True
5157
)
5258
),
53-
AWSTagKey("user_id"): AWSTagValue(f"{user_id}"),
54-
AWSTagKey("wallet_id"): AWSTagValue(f"{wallet_id}"),
55-
AWSTagKey("role"): AWSTagValue("manager"),
59+
USER_ID_TAG_KEY: AWSTagValue(f"{user_id}"),
60+
WALLET_ID_TAG_KEY: AWSTagValue(f"{wallet_id}"),
61+
ROLE_TAG_KEY: MANAGER_ROLE_TAG_VALUE,
5662
}
5763
| app_settings.CLUSTERS_KEEPER_PRIMARY_EC2_INSTANCES.PRIMARY_EC2_INSTANCES_CUSTOM_TAGS
5864
)
@@ -67,8 +73,8 @@ def ec2_instances_for_user_wallet_filter(
6773
) -> EC2Tags:
6874
return (
6975
_minimal_identification_tag(app_settings)
70-
| {AWSTagKey("user_id"): AWSTagValue(f"{user_id}")}
71-
| {AWSTagKey("wallet_id"): AWSTagValue(f"{wallet_id}")}
76+
| {USER_ID_TAG_KEY: AWSTagValue(f"{user_id}")}
77+
| {WALLET_ID_TAG_KEY: AWSTagValue(f"{wallet_id}")}
7278
)
7379

7480

@@ -81,3 +87,14 @@ def compose_user_data(bash_command: str) -> str:
8187
echo "completed user data bash script"
8288
"""
8389
)
90+
91+
92+
def wallet_id_from_instance_tags(tags: EC2Tags) -> WalletID | None:
93+
wallet_id_str = tags[WALLET_ID_TAG_KEY]
94+
if wallet_id_str == "None":
95+
return None
96+
return WalletID(wallet_id_str)
97+
98+
99+
def user_id_from_instance_tags(tags: EC2Tags) -> UserID:
100+
return UserID(tags[USER_ID_TAG_KEY])

0 commit comments

Comments
 (0)