Skip to content

Commit f39fa91

Browse files
author
Andrei Neagu
committed
refactor to use custom setting var name
1 parent 096f943 commit f39fa91

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

packages/service-library/src/servicelib/docker_utils.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dataclasses import dataclass
55
from datetime import datetime
66
from functools import cached_property
7-
from typing import Any, Final, Literal
7+
from typing import Any, AsyncContextManager, Final, Literal
88

99
import aiodocker
1010
import aiohttp
@@ -287,44 +287,56 @@ async def pull_image(
287287
)
288288

289289

290-
@asynccontextmanager
291-
async def lifespan_remote_docker_client(app: FastAPI) -> AsyncIterator[None]:
292-
"""Ensures `setup` and `teardown` for the docker client.
290+
def get_lifespan_remote_docker_client(
291+
docker_api_proxy_settings_property_name: str,
292+
) -> Callable[[FastAPI], AsyncContextManager[None]]:
293+
"""Ensures `setup` and `teardown` for the remote docker client.
293294
294-
NOTE: the `DockerApiProxysettings` must be placed as `DOCKER_API_PROXY`
295-
on the Application's Settings object
295+
Arguments:
296+
docker_api_proxy_settings_property_name -- if the name is `PROP_NAME`
297+
then it should be accessible as `app.state.settings.PROP_NAME`
298+
299+
Returns:
300+
docker client lifespan manager
296301
"""
297-
settings: DockerApiProxysettings = app.state.settings.DOCKER_API_PROXY
298-
299-
session: ClientSession | None = None
300-
if settings.DOCKER_API_PROXY_USER and settings.DOCKER_API_PROXY_PASSWORD:
301-
session = ClientSession(
302-
auth=aiohttp.BasicAuth(
303-
login=settings.DOCKER_API_PROXY_USER,
304-
password=settings.DOCKER_API_PROXY_PASSWORD.get_secret_value(),
305-
)
302+
303+
@asynccontextmanager
304+
async def _(app: FastAPI) -> AsyncIterator[None]:
305+
settings: DockerApiProxysettings = getattr(
306+
app.state.settings, docker_api_proxy_settings_property_name
306307
)
307308

308-
async with AsyncExitStack() as exit_stack:
309+
session: ClientSession | None = None
309310
if settings.DOCKER_API_PROXY_USER and settings.DOCKER_API_PROXY_PASSWORD:
310-
await exit_stack.enter_async_context(
311-
ClientSession(
312-
auth=aiohttp.BasicAuth(
313-
login=settings.DOCKER_API_PROXY_USER,
314-
password=settings.DOCKER_API_PROXY_PASSWORD.get_secret_value(),
311+
session = ClientSession(
312+
auth=aiohttp.BasicAuth(
313+
login=settings.DOCKER_API_PROXY_USER,
314+
password=settings.DOCKER_API_PROXY_PASSWORD.get_secret_value(),
315+
)
316+
)
317+
318+
async with AsyncExitStack() as exit_stack:
319+
if settings.DOCKER_API_PROXY_USER and settings.DOCKER_API_PROXY_PASSWORD:
320+
await exit_stack.enter_async_context(
321+
ClientSession(
322+
auth=aiohttp.BasicAuth(
323+
login=settings.DOCKER_API_PROXY_USER,
324+
password=settings.DOCKER_API_PROXY_PASSWORD.get_secret_value(),
325+
)
315326
)
316327
)
328+
329+
client = await exit_stack.enter_async_context(
330+
aiodocker.Docker(url=settings.base_url, session=session)
317331
)
318332

319-
client = await exit_stack.enter_async_context(
320-
aiodocker.Docker(url=settings.base_url, session=session)
321-
)
333+
app.state.remote_docker_client = client
322334

323-
app.state.remote_docker_client = client
335+
yield
324336

325-
yield
337+
return _
326338

327339

328340
def get_remote_docker_client(app: FastAPI) -> aiodocker.Docker:
329-
client: aiodocker.Docker = app.state.remote_docker_client
330-
return client
341+
assert isinstance(app.state.remote_docker_client, aiodocker.Docker) # nosec
342+
return app.state.remote_docker_client

services/docker-api-proxy/tests/integration/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from pydantic import Field
1212
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
1313
from servicelib.docker_utils import (
14+
get_lifespan_remote_docker_client,
1415
get_remote_docker_client,
15-
lifespan_remote_docker_client,
1616
)
1717
from servicelib.fastapi.lifespan_utils import combine_lfiespan_context_managers
1818
from settings_library.application import BaseApplicationSettings
@@ -40,7 +40,9 @@ class ApplicationSetting(BaseApplicationSettings):
4040
]
4141

4242
app = FastAPI(
43-
lifespan=combine_lfiespan_context_managers(lifespan_remote_docker_client)
43+
lifespan=combine_lfiespan_context_managers(
44+
get_lifespan_remote_docker_client("DOCKER_API_PROXY")
45+
)
4446
)
4547
app.state.settings = ApplicationSetting.create_from_envs()
4648

services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/core/application.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from fastapi import FastAPI
2+
from servicelib.docker_utils import get_lifespan_remote_docker_client
3+
from servicelib.fastapi.lifespan_utils import combine_lfiespan_context_managers
24
from servicelib.fastapi.openapi import override_fastapi_openapi_method
35
from servicelib.fastapi.profiler_middleware import ProfilerMiddleware
46
from servicelib.fastapi.prometheus_instrumentation import (
@@ -41,6 +43,9 @@ def create_app(settings: ApplicationSettings | None = None) -> FastAPI:
4143
"/doc" if app_settings.DYNAMIC_SCHEDULER_SWAGGER_API_DOC_ENABLED else None
4244
),
4345
redoc_url=None, # default disabled, see below
46+
lifespan=combine_lfiespan_context_managers(
47+
get_lifespan_remote_docker_client("DYNAMIC_SCHEDULER_DOCKER_API_PROXY")
48+
),
4449
)
4550
override_fastapi_openapi_method(app)
4651

services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/core/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from settings_library.basic_types import LogLevel, VersionTag
88
from settings_library.director_v0 import DirectorV0Settings
99
from settings_library.director_v2 import DirectorV2Settings
10+
from settings_library.docker_api_proxy import DockerApiProxysettings
1011
from settings_library.http_client_request import ClientRequestSettings
1112
from settings_library.rabbit import RabbitSettings
1213
from settings_library.redis import RedisSettings
@@ -152,6 +153,11 @@ class ApplicationSettings(_BaseApplicationSettings):
152153
description="settings for opentelemetry tracing",
153154
)
154155

156+
DYNAMIC_SCHEDULER_DOCKER_API_PROXY: Annotated[
157+
DockerApiProxysettings,
158+
Field(json_schema_extra={"auto_default_from_env": True}),
159+
]
160+
155161
@field_validator("DYNAMIC_SCHEDULER_UI_MOUNT_PATH", mode="before")
156162
@classmethod
157163
def _ensure_ends_with_slash(cls, v: str) -> str:

0 commit comments

Comments
 (0)