diff --git a/CHANGELOG.md b/CHANGELOG.md index 31ea637..1042a86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ We follow [Semantic Versions](https://semver.org/). - Make `alembic.wait_for_database` as task - Make `docker.up` check for compose file - Make `pytest.run`, `celery.run` use `docker.up` +- Make sure that `DJANGO_SETTINGS_MODULE` is set for manage invocations ## 1.1.1 diff --git a/saritasa_invocations/_config.py b/saritasa_invocations/_config.py index 2642750..75492c3 100644 --- a/saritasa_invocations/_config.py +++ b/saritasa_invocations/_config.py @@ -64,7 +64,7 @@ class PythonSettings: """Settings for python module.""" entry: str = "python" - docker_service: str = "web" + docker_service: str = "app" docker_service_params: str = "--rm" mypy_entry: str = "-m mypy" pytest_entry: str = "-m pytest" diff --git a/saritasa_invocations/django.py b/saritasa_invocations/django.py index a57225e..a35511a 100644 --- a/saritasa_invocations/django.py +++ b/saritasa_invocations/django.py @@ -25,8 +25,11 @@ def wait_for_database(context: invoke.Context) -> None: python.run( context, command=( - f"{config.django.manage_file_path} " "wait_for_database --stable 0" + f"{config.django.manage_file_path} wait_for_database --stable 0" ), + env={ + "DJANGO_SETTINGS_MODULE": config.django.settings_path, + }, ) wait_for_database._called = True # type: ignore @@ -35,7 +38,7 @@ def wait_for_database(context: invoke.Context) -> None: def manage( context: invoke.Context, command: str, - docker_params: str | None = None, + docker_params: str = "", watchers: collections.abc.Sequence[invoke.StreamWatcher] = (), ) -> None: """Run `manage.py` command. @@ -58,6 +61,9 @@ def manage( docker_params=docker_params, command=f"{config.django.manage_file_path} {command}", watchers=watchers, + env={ + "DJANGO_SETTINGS_MODULE": config.django.settings_path, + }, ) diff --git a/saritasa_invocations/docker.py b/saritasa_invocations/docker.py index 501359e..65a1f62 100644 --- a/saritasa_invocations/docker.py +++ b/saritasa_invocations/docker.py @@ -41,10 +41,11 @@ def buildpack( def docker_compose_run( context: invoke.Context, - params: str | None, container: str, command: str, + params: str = "", watchers: collections.abc.Sequence[invoke.StreamWatcher] = (), + env: dict[str, str] | None = None, ) -> None: """Run ``command`` using docker-compose. @@ -61,11 +62,17 @@ def docker_compose_run( container: Name of container to start command: Command to run in started container watchers: Automated responders to command + env: environmental variables for run """ compose_cmd = _config.Config.from_context(context).docker.compose_cmd + env_params = " ".join( + f"--env {env_key}={value}" for env_key, value in (env or {}).items() + ) context.run( - command=f"{compose_cmd} run {params or ''} {container} {command}", + command=( + f"{compose_cmd} run {params} {env_params} {container} {command}" + ), watchers=watchers, ) diff --git a/saritasa_invocations/python.py b/saritasa_invocations/python.py index b466c47..bc0cc43 100644 --- a/saritasa_invocations/python.py +++ b/saritasa_invocations/python.py @@ -38,6 +38,7 @@ def run_docker( command: str, params: str | None = None, watchers: collections.abc.Sequence[invoke.StreamWatcher] = (), + env: dict[str, str] | None = None, ) -> None: """Run command in `python` container.""" config = _config.Config.from_context(context) @@ -49,6 +50,7 @@ def run_docker( container=config.python.docker_service, command=command, watchers=watchers, + env=env, ) @@ -57,6 +59,7 @@ def run_docker_python( command: str, params: str | None = None, watchers: collections.abc.Sequence[invoke.StreamWatcher] = (), + env: dict[str, str] | None = None, ) -> None: """Run command using docker python interpreter.""" config = _config.Config.from_context(context) @@ -65,6 +68,7 @@ def run_docker_python( params=params, command=f"{config.python.entry} {command}", watchers=watchers, + env=env, ) @@ -72,12 +76,14 @@ def run_local_python( context: invoke.Context, command: str, watchers: collections.abc.Sequence[invoke.StreamWatcher] = (), + env: dict[str, str] | None = None, ) -> None: """Run command using local python interpreter.""" config = _config.Config.from_context(context) context.run( command=f"{config.python.entry} {command}", watchers=watchers, + env=env, ) @@ -87,6 +93,7 @@ def run( command: str, docker_params: str | None = None, watchers: collections.abc.Sequence[invoke.StreamWatcher] = (), + env: dict[str, str] | None = None, ) -> None: """Execute python command.""" match get_python_env(): @@ -95,6 +102,7 @@ def run( context=context, command=command, watchers=watchers, + env=env, ) case PythonEnv.DOCKER: run_docker_python( @@ -102,4 +110,5 @@ def run( command=command, params=docker_params, watchers=watchers, + env=env, )