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

feat(docker-compose): Simplify docker compose environment injection #64

Merged
merged 2 commits into from
Oct 3, 2024
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
58 changes: 27 additions & 31 deletions devservices/utils/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import re
import subprocess
import tempfile
from typing import cast

from packaging import version
Expand Down Expand Up @@ -80,34 +79,31 @@ def run_docker_compose_command(
service_config_file_path = os.path.join(
service.repo_path, DEVSERVICES_DIR_NAME, CONFIG_FILE_NAME
)
with tempfile.NamedTemporaryFile(mode="w") as temp_env_file:
temp_env_file.write(
f"{DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY}={relative_local_dependency_directory}\n"
# Set the environment variable for the local dependencies directory to be used by docker compose
current_env = os.environ.copy()
current_env[
DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY
] = relative_local_dependency_directory
cmd = [
"docker",
"compose",
"-p",
service.name,
"-f",
service_config_file_path,
] + command.split()
try:
return subprocess.run(
cmd,
check=True,
capture_output=True,
text=True,
env=current_env,
)
temp_env_file.flush()
temp_env_file_path = temp_env_file.name
cmd = [
"docker",
"compose",
"-p",
service.name,
"-f",
service_config_file_path,
"--env-file",
temp_env_file_path,
] + command.split()
try:
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True,
)
except subprocess.CalledProcessError as e:
raise DockerComposeError(
command=command,
returncode=e.returncode,
stdout=e.stdout,
stderr=e.stderr,
) from e
return result
except subprocess.CalledProcessError as e:
raise DockerComposeError(
command=command,
returncode=e.returncode,
stdout=e.stdout,
stderr=e.stderr,
) from e
12 changes: 6 additions & 6 deletions tests/commands/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from devservices.commands.start import start
from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY
from testing.utils import create_config_file


Expand Down Expand Up @@ -46,6 +47,10 @@ def test_start_simple(mock_run: mock.Mock, tmp_path: Path) -> None:

start(args)

# Ensure the DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY is set and is relative
env_vars = mock_run.call_args[1]["env"]
assert env_vars[DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY] == "../dependency-dir"

mock_run.assert_called_once_with(
[
"docker",
Expand All @@ -54,8 +59,6 @@ def test_start_simple(mock_run: mock.Mock, tmp_path: Path) -> None:
"example-service",
"-f",
f"{service_path}/{DEVSERVICES_DIR_NAME}/{CONFIG_FILE_NAME}",
"--env-file",
mock.ANY,
"up",
"-d",
"redis",
Expand All @@ -64,12 +67,9 @@ def test_start_simple(mock_run: mock.Mock, tmp_path: Path) -> None:
check=True,
capture_output=True,
text=True,
env=mock.ANY,
)

# Ensure the temp file got cleaned up properly
temp_env_file_path = mock_run.call_args[0][0][7]
assert not os.path.exists(temp_env_file_path)


@mock.patch("devservices.utils.docker_compose.subprocess.run")
def test_start_error(
Expand Down
12 changes: 6 additions & 6 deletions tests/commands/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from devservices.commands.stop import stop
from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY
from testing.utils import create_config_file


Expand Down Expand Up @@ -46,6 +47,10 @@ def test_stop_simple(mock_run: mock.Mock, tmp_path: Path) -> None:

stop(args)

# Ensure the DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY is set and is relative
env_vars = mock_run.call_args[1]["env"]
assert env_vars[DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY] == "../dependency-dir"

mock_run.assert_called_once_with(
[
"docker",
Expand All @@ -54,21 +59,16 @@ def test_stop_simple(mock_run: mock.Mock, tmp_path: Path) -> None:
"example-service",
"-f",
f"{service_path}/{DEVSERVICES_DIR_NAME}/{CONFIG_FILE_NAME}",
"--env-file",
mock.ANY,
"down",
"redis",
"clickhouse",
],
check=True,
capture_output=True,
text=True,
env=mock.ANY,
)

# Ensure the temp file got cleaned up properly
temp_env_file_path = mock_run.call_args[0][0][7]
assert not os.path.exists(temp_env_file_path)


@mock.patch("devservices.utils.docker_compose.subprocess.run")
def test_stop_error(
Expand Down