Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that DJANGO_SETTINGS_MODULE is set for manage invocations #98

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion saritasa_invocations/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 8 additions & 2 deletions saritasa_invocations/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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,
},
)


Expand Down
11 changes: 9 additions & 2 deletions saritasa_invocations/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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,
)

Expand Down
9 changes: 9 additions & 0 deletions saritasa_invocations/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -49,6 +50,7 @@ def run_docker(
container=config.python.docker_service,
command=command,
watchers=watchers,
env=env,
)


Expand All @@ -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)
Expand All @@ -65,19 +68,22 @@ def run_docker_python(
params=params,
command=f"{config.python.entry} {command}",
watchers=watchers,
env=env,
)


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,
)


Expand All @@ -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():
Expand All @@ -95,11 +102,13 @@ def run(
context=context,
command=command,
watchers=watchers,
env=env,
)
case PythonEnv.DOCKER:
run_docker_python(
context=context,
command=command,
params=docker_params,
watchers=watchers,
env=env,
)