Skip to content

Commit 89cf160

Browse files
authored
♻️ ✨ Is638/adds CLI to dynamic-sidecar (round 2) (ITISFoundation#3152)
1 parent 7b11dd1 commit 89cf160

File tree

10 files changed

+95
-46
lines changed

10 files changed

+95
-46
lines changed

services/dynamic-sidecar/Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
include ../../scripts/common.Makefile
22
include ../../scripts/common-service.Makefile
33

4-
APP_NAME := $(notdir $(CURDIR))
5-
64
.DEFAULT_GOAL := help
75

86

@@ -12,10 +10,17 @@ APP_NAME := $(notdir $(CURDIR))
1210
@echo "WARNING ##### $@ does not exist, cloning $< as $@ ############"; cp $< $@)
1311

1412

15-
.PHONY: openapi.json
13+
14+
15+
.PHONY: openapi-specs openapi.json
16+
openapi-specs: openapi.json
1617
openapi.json: .env ## Creates OAS document openapi.json
17-
# generating openapi specs (OAS) file
18-
export $(shell grep -v '^#' $< | xargs -0) && python3 -c "import json; from $(APP_PACKAGE_NAME).main import *; print( json.dumps(app.openapi(), indent=2) )" > $@
18+
# generating openapi specs file under $<
19+
@set -o allexport; \
20+
source .env; \
21+
set +o allexport; \
22+
simcore-service-dynamic-sidecar openapi > $@
23+
1924
# validates OAS file: $@
2025
@cd $(CURDIR); \
2126
$(SCRIPTS_DIR)/openapi-generator-cli.bash validate --input-spec /local/$@

services/dynamic-sidecar/docker/boot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ if [ "${SC_BOOT_MODE}" = "debug-ptvsd" ]; then
3535

3636
exec sh -c "
3737
cd services/dynamic-sidecar/src/simcore_service_dynamic_sidecar && \
38-
uvicorn main:app \
38+
uvicorn main:the_app \
3939
--host 0.0.0.0 \
4040
--reload \
4141
$reload_dir_packages
4242
--reload-dir . \
4343
--log-level \"${SERVER_LOG_LEVEL}\"
4444
"
4545
else
46-
exec uvicorn simcore_service_dynamic_sidecar.main:app \
46+
exec uvicorn simcore_service_dynamic_sidecar.main:the_app \
4747
--host 0.0.0.0 \
4848
--log-level "${SERVER_LOG_LEVEL}"
4949

services/dynamic-sidecar/scripts/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ push-outputs: ## push the outputs for this service
1515
@echo ">>>>> Expect a 204 reply if OK <<<<<"
1616
curl -v -X POST ${BASE_ADDRESS}/containers/ports/outputs:push
1717

18+
.PHONY: info
19+
info: ## displays app info
20+
# app settings
21+
@simcore-service-dynamic-sidecar settings --as-json
1822

1923
.PHONY: help
2024
help: ## this help

services/dynamic-sidecar/setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def read_reqs(reqs_path: Path) -> set[str]:
5353
PROD_REQUIREMENTS=PROD_REQUIREMENTS,
5454
TEST_REQUIREMENTS=TEST_REQUIREMENTS,
5555
setup_requires=["setuptools_scm"],
56+
entry_points={
57+
"console_scripts": [
58+
"simcore-service-dynamic-sidecar=simcore_service_dynamic_sidecar.cli:main",
59+
],
60+
},
5661
)
5762

5863

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import logging
3+
4+
import typer
5+
from settings_library.utils_cli import create_settings_command
6+
from simcore_service_dynamic_sidecar.core.application import create_basic_app
7+
8+
from ._meta import PROJECT_NAME
9+
from .core.settings import DynamicSidecarSettings
10+
11+
log = logging.getLogger(__name__)
12+
main = typer.Typer(name=PROJECT_NAME)
13+
14+
15+
main.command()(create_settings_command(settings_cls=DynamicSidecarSettings, logger=log))
16+
17+
18+
@main.command()
19+
def openapi():
20+
"""Prints OpenAPI specifications in json format"""
21+
app = create_basic_app()
22+
typer.secho(json.dumps(app.openapi(), indent=2))
23+
24+
25+
#
26+
# NOTE: We intentionally did NOT create a command to run the application
27+
# Use instead $ uvicorn simcore_service_dynamic_sidecar.main:the_app
28+
#

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/core/application.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,65 +45,72 @@ def setup_logger(settings: DynamicSidecarSettings):
4545
logging.root.setLevel(settings.log_level)
4646

4747

48-
def assemble_application() -> FastAPI:
49-
"""
50-
Creates the application from using the env vars as a context
51-
Also stores inside the state all instances of classes
52-
needed in other requests and used to share data.
53-
"""
48+
def create_basic_app() -> FastAPI:
49+
# settings
5450
settings = DynamicSidecarSettings.create_from_envs()
5551
setup_logger(settings)
5652
logger.debug(settings.json(indent=2))
5753

58-
application = FastAPI(
54+
# minimal
55+
app = FastAPI(
5956
debug=settings.DEBUG,
6057
openapi_url=f"/api/{API_VTAG}/openapi.json",
6158
docs_url="/dev/doc",
6259
)
63-
override_fastapi_openapi_method(application)
60+
override_fastapi_openapi_method(app)
61+
app.state.settings = settings
6462

65-
application.state.settings = settings
66-
application.state.shared_store = SharedStore()
67-
application.state.application_health = ApplicationHealth()
63+
app.include_router(main_router)
64+
return app
6865

69-
# ROUTES --------------------
70-
application.include_router(main_router)
7166

72-
# ERROR HANDLERS ------------
73-
application.add_exception_handler(NodeNotFound, node_not_found_error_handler)
74-
application.add_exception_handler(BaseDynamicSidecarError, http_error_handler)
67+
def create_app():
68+
"""
69+
Creates the application from using the env vars as a context
70+
Also stores inside the state all instances of classes
71+
needed in other requests and used to share data.
72+
"""
73+
74+
app = create_basic_app()
7575

7676
# MODULES SETUP --------------
7777

78-
if settings.is_development_mode:
79-
remote_debug_setup(application)
78+
app.state.shared_store = SharedStore()
79+
app.state.application_health = ApplicationHealth()
8080

81-
if settings.RABBIT_SETTINGS:
82-
setup_rabbitmq(application)
83-
setup_background_log_fetcher(application)
81+
if app.state.settings.is_development_mode:
82+
remote_debug_setup(app)
83+
84+
if app.state.settings.RABBIT_SETTINGS:
85+
setup_rabbitmq(app)
86+
setup_background_log_fetcher(app)
8487

8588
# also sets up mounted_volumes
86-
setup_mounted_fs(application)
87-
setup_directory_watcher(application)
89+
setup_mounted_fs(app)
90+
setup_directory_watcher(app)
91+
92+
# ERROR HANDLERS ------------
93+
app.add_exception_handler(NodeNotFound, node_not_found_error_handler)
94+
app.add_exception_handler(BaseDynamicSidecarError, http_error_handler)
8895

8996
# EVENTS ---------------------
9097
async def _on_startup() -> None:
91-
await login_registry(settings.REGISTRY_SETTINGS)
92-
await volumes_fix_permissions(application.state.mounted_volumes)
98+
await login_registry(app.state.settings.REGISTRY_SETTINGS)
99+
await volumes_fix_permissions(app.state.mounted_volumes)
93100
print(WELCOME_MSG, flush=True)
94101

95102
async def _on_shutdown() -> None:
96103
logger.info("Going to remove spawned containers")
97104
result = await remove_the_compose_spec(
98-
shared_store=application.state.shared_store,
99-
settings=settings,
100-
command_timeout=settings.DYNAMIC_SIDECAR_DOCKER_COMPOSE_DOWN_TIMEOUT,
105+
shared_store=app.state.shared_store,
106+
settings=app.state.settings,
107+
command_timeout=app.state.settings.DYNAMIC_SIDECAR_DOCKER_COMPOSE_DOWN_TIMEOUT,
101108
)
102109
logger.info("Container removal did_succeed=%s\n%s", result[0], result[1])
103110

104111
logger.info("shutdown cleanup completed")
105112

106-
application.add_event_handler("startup", _on_startup)
107-
application.add_event_handler("shutdown", _on_shutdown)
113+
app.add_event_handler("startup", _on_startup)
114+
app.add_event_handler("shutdown", _on_shutdown)
108115

109-
return application
116+
return app

services/dynamic-sidecar/src/simcore_service_dynamic_sidecar/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33

44
from fastapi import FastAPI
5-
from simcore_service_dynamic_sidecar.core.application import assemble_application
5+
from simcore_service_dynamic_sidecar.core.application import create_app
66

77
# SINGLETON FastAPI app
8-
app: FastAPI = assemble_application()
8+
the_app: FastAPI = create_app()

services/dynamic-sidecar/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from fastapi import FastAPI
2121
from pytest_mock.plugin import MockerFixture
2222
from simcore_service_dynamic_sidecar.core import utils
23-
from simcore_service_dynamic_sidecar.core.application import assemble_application
23+
from simcore_service_dynamic_sidecar.core.application import create_app
2424
from simcore_service_dynamic_sidecar.core.docker_utils import docker_client
2525
from simcore_service_dynamic_sidecar.core.settings import DynamicSidecarSettings
2626
from simcore_service_dynamic_sidecar.core.shared_handlers import (
@@ -132,7 +132,7 @@ async def _mock_is_registry_reachable(*args, **kwargs) -> None:
132132

133133
@pytest.fixture(scope="module")
134134
def app(mock_environment: None, disable_registry_check: None) -> FastAPI:
135-
app = assemble_application()
135+
app = create_app()
136136
app.state.rabbitmq = AsyncMock()
137137
return app
138138

services/dynamic-sidecar/tests/unit/test_core_docker_logs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from _pytest.monkeypatch import MonkeyPatch
1414
from async_asgi_testclient import TestClient
1515
from fastapi import FastAPI
16-
from simcore_service_dynamic_sidecar.core.application import assemble_application
16+
from simcore_service_dynamic_sidecar.core.application import create_app
1717
from simcore_service_dynamic_sidecar.core.docker_logs import (
1818
_get_background_log_fetcher,
1919
start_log_fetching,
@@ -62,7 +62,7 @@ def app(
6262
monkeypatch_module.setenv("S3_SECURE", "false")
6363
monkeypatch_module.setenv("R_CLONE_PROVIDER", "MINIO")
6464

65-
yield assemble_application()
65+
yield create_app()
6666

6767

6868
@pytest.fixture

services/dynamic-sidecar/tests/unit/test_core_rabbitmq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from models_library.users import UserID
1919
from pytest_mock.plugin import MockerFixture
2020
from settings_library.rabbit import RabbitSettings
21-
from simcore_service_dynamic_sidecar.core.application import assemble_application
21+
from simcore_service_dynamic_sidecar.core.application import create_app
2222
from simcore_service_dynamic_sidecar.core.rabbitmq import SLEEP_BETWEEN_SENDS, RabbitMQ
2323
from simcore_service_dynamic_sidecar.modules import mounted_fs
2424

@@ -114,7 +114,7 @@ def mock_environment(
114114

115115
@pytest.fixture
116116
def app(mock_environment: None) -> FastAPI:
117-
return assemble_application()
117+
return create_app()
118118

119119

120120
# UTILS

0 commit comments

Comments
 (0)