Skip to content

Commit 1433e6e

Browse files
GitHKAndrei Neagumergify[bot]
authored
🎨 Expose wallet_id to computational and dynamic services via env vars upon request (#7125)
Co-authored-by: Andrei Neagu <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 1713021 commit 1433e6e

File tree

7 files changed

+24
-0
lines changed

7 files changed

+24
-0
lines changed

services/director-v2/src/simcore_service_director_v2/modules/dask_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ async def send_computation_tasks(
399399
node_image=node_image,
400400
metadata=metadata,
401401
resource_tracking_run_id=resource_tracking_run_id,
402+
wallet_id=metadata.get("wallet_id"),
402403
)
403404
task_owner = dask_utils.compute_task_owner(
404405
user_id, project_id, node_id, metadata.get("project_metadata", {})

services/director-v2/src/simcore_service_director_v2/modules/dynamic_sidecar/docker_compose_specs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from models_library.services_types import ServiceRunID
2424
from models_library.users import UserID
2525
from models_library.utils.docker_compose import replace_env_vars_in_compose_spec
26+
from models_library.wallets import WalletID
2627
from pydantic import ByteSize
2728
from servicelib.resources import CPU_RESOURCE_LIMIT_KEY, MEM_RESOURCE_LIMIT_KEY
2829
from settings_library.docker_registry import RegistrySettings
@@ -280,6 +281,7 @@ async def assemble_spec( # pylint: disable=too-many-arguments # noqa: PLR0913
280281
simcore_user_agent: str,
281282
swarm_stack_name: str,
282283
service_run_id: ServiceRunID,
284+
wallet_id: WalletID | None,
283285
) -> str:
284286
"""
285287
returns a docker-compose spec used by
@@ -353,6 +355,7 @@ async def assemble_spec( # pylint: disable=too-many-arguments # noqa: PLR0913
353355
project_id=project_id,
354356
node_id=node_id,
355357
service_run_id=service_run_id,
358+
wallet_id=wallet_id,
356359
)
357360

358361
add_egress_configuration(
@@ -392,6 +395,7 @@ async def assemble_spec( # pylint: disable=too-many-arguments # noqa: PLR0913
392395
project_id=project_id,
393396
node_id=node_id,
394397
service_run_id=service_run_id,
398+
wallet_id=wallet_id,
395399
)
396400

397401
stringified_service_spec: str = replace_env_vars_in_compose_spec(

services/director-v2/src/simcore_service_director_v2/modules/dynamic_sidecar/scheduler/_core/_events_user_services.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ async def submit_compose_sepc(app: FastAPI, scheduler_data: SchedulerData) -> No
101101
simcore_user_agent=scheduler_data.request_simcore_user_agent,
102102
swarm_stack_name=dynamic_services_scheduler_settings.SWARM_STACK_NAME,
103103
service_run_id=scheduler_data.run_id,
104+
wallet_id=(
105+
scheduler_data.wallet_info.wallet_id if scheduler_data.wallet_info else None
106+
),
104107
)
105108

106109
_logger.debug(

services/director-v2/src/simcore_service_director_v2/modules/osparc_variables/substitutions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from models_library.services_types import ServiceRunID
2020
from models_library.users import UserID
2121
from models_library.utils.specs_substitution import SpecsSubstitutionsResolver
22+
from models_library.wallets import WalletID
2223
from pydantic import BaseModel
2324
from servicelib.fastapi.app_state import SingletonInAppStateMixin
2425
from servicelib.logging_utils import log_context
@@ -121,6 +122,7 @@ def create(cls, app: FastAPI):
121122
("OSPARC_VARIABLE_PRODUCT_NAME", "product_name"),
122123
("OSPARC_VARIABLE_STUDY_UUID", "project_id"),
123124
("OSPARC_VARIABLE_SERVICE_RUN_ID", "run_id"),
125+
("OSPARC_VARIABLE_WALLET_ID", "wallet_id"),
124126
("OSPARC_VARIABLE_USER_ID", "user_id"),
125127
("OSPARC_VARIABLE_API_HOST", "api_server_base_url"),
126128
]:
@@ -183,6 +185,7 @@ async def resolve_and_substitute_session_variables_in_model(
183185
project_id: ProjectID,
184186
node_id: NodeID,
185187
service_run_id: ServiceRunID,
188+
wallet_id: WalletID | None,
186189
) -> TBaseModel:
187190
result: TBaseModel = model
188191
try:
@@ -204,6 +207,7 @@ async def resolve_and_substitute_session_variables_in_model(
204207
project_id=project_id,
205208
node_id=node_id,
206209
run_id=service_run_id,
210+
wallet_id=wallet_id,
207211
api_server_base_url=app_settings.DIRECTOR_V2_PUBLIC_API_BASE_URL,
208212
),
209213
)
@@ -226,6 +230,7 @@ async def resolve_and_substitute_session_variables_in_specs(
226230
project_id: ProjectID,
227231
node_id: NodeID,
228232
service_run_id: ServiceRunID,
233+
wallet_id: WalletID | None,
229234
) -> dict[str, Any]:
230235
table = OsparcSessionVariablesTable.get_from_app_state(app)
231236
resolver = SpecsSubstitutionsResolver(specs, upgrade=False)
@@ -248,6 +253,7 @@ async def resolve_and_substitute_session_variables_in_specs(
248253
project_id=project_id,
249254
node_id=node_id,
250255
run_id=service_run_id,
256+
wallet_id=wallet_id,
251257
api_server_base_url=app_settings.DIRECTOR_V2_PUBLIC_API_BASE_URL,
252258
),
253259
)

services/director-v2/src/simcore_service_director_v2/utils/dask.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from models_library.services import ServiceKey, ServiceVersion
2929
from models_library.services_types import ServiceRunID
3030
from models_library.users import UserID
31+
from models_library.wallets import WalletID
3132
from pydantic import AnyUrl, ByteSize, TypeAdapter, ValidationError
3233
from servicelib.logging_utils import log_catch, log_context
3334
from simcore_sdk import node_ports_v2
@@ -344,6 +345,7 @@ async def compute_task_envs(
344345
node_image: Image,
345346
metadata: RunMetadataDict,
346347
resource_tracking_run_id: ServiceRunID,
348+
wallet_id: WalletID | None,
347349
) -> ContainerEnvsDict:
348350
product_name = metadata.get("product_name", UNDEFINED_DOCKER_LABEL)
349351
task_envs = node_image.envs
@@ -363,6 +365,7 @@ async def compute_task_envs(
363365
project_id=project_id,
364366
node_id=node_id,
365367
service_run_id=resource_tracking_run_id,
368+
wallet_id=wallet_id,
366369
)
367370
# NOTE: see https://github.com/ITISFoundation/osparc-simcore/issues/3638
368371
# we currently do not validate as we are using illegal docker key names with underscores

services/director-v2/tests/unit/test_modules_osparc_variables.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from models_library.users import UserID
2323
from models_library.utils.specs_substitution import SubstitutionValue
2424
from models_library.utils.string_substitution import OSPARC_IDENTIFIER_PREFIX
25+
from models_library.wallets import WalletID
2526
from pydantic import TypeAdapter
2627
from pytest_mock import MockerFixture
2728
from pytest_simcore.helpers.faker_compose_specs import generate_fake_docker_compose
@@ -159,11 +160,13 @@ async def fake_app(faker: Faker) -> AsyncIterable[FastAPI]:
159160
yield app
160161

161162

163+
@pytest.mark.parametrize("wallet_id", [None, 12])
162164
async def test_resolve_and_substitute_session_variables_in_specs(
163165
mock_user_repo: None,
164166
mock_osparc_variables_api_auth_rpc: None,
165167
fake_app: FastAPI,
166168
faker: Faker,
169+
wallet_id: WalletID | None,
167170
):
168171
specs = {
169172
"product_name": "${OSPARC_VARIABLE_PRODUCT_NAME}",
@@ -175,6 +178,7 @@ async def test_resolve_and_substitute_session_variables_in_specs(
175178
"api_key": "${OSPARC_VARIABLE_API_KEY}",
176179
"api_secret": "${OSPARC_VARIABLE_API_SECRET}",
177180
"service_run_id": "${OSPARC_VARIABLE_SERVICE_RUN_ID}",
181+
"wallet_id": "${OSPARC_VARIABLE_WALLET_ID}",
178182
}
179183
print("SPECS\n", specs)
180184

@@ -186,10 +190,12 @@ async def test_resolve_and_substitute_session_variables_in_specs(
186190
project_id=faker.uuid4(cast_to=None),
187191
node_id=faker.uuid4(cast_to=None),
188192
service_run_id=ServiceRunID("some_run_id"),
193+
wallet_id=wallet_id,
189194
)
190195
print("REPLACED SPECS\n", replaced_specs)
191196

192197
assert OSPARC_IDENTIFIER_PREFIX not in f"{replaced_specs}"
198+
assert f"'wallet_id': '{wallet_id}'" in f"{replaced_specs}"
193199

194200

195201
@pytest.fixture

services/director-v2/tests/unit/with_dbs/test_utils_dask.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,5 +661,6 @@ async def test_compute_task_envs(
661661
node_image=sleeper_task.image,
662662
metadata=run_metadata,
663663
resource_tracking_run_id=resource_tracking_run_id,
664+
wallet_id=run_metadata.get("wallet_id", None),
664665
)
665666
assert task_envs == expected_computed_task_envs

0 commit comments

Comments
 (0)