Skip to content

Commit

Permalink
Add envs parameter to compose up/config
Browse files Browse the repository at this point in the history
  • Loading branch information
lugoues committed Nov 6, 2022
1 parent 7dc3102 commit 6c49ed4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions python_on_whales/components/compose/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def build(
full_cmd += services
run(full_cmd, capture_stdout=False)

def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, Any]]:
def config(self, return_json: bool = False, envs: Dict[str,str] = {}) -> Union[ComposeConfig, Dict[str, Any]]:
"""Returns the configuration of the compose stack for further inspection.
For example
Expand All @@ -76,12 +76,13 @@ def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, An
lists and dicts corresponding to the json response, unmodified.
It may be useful if you just want to print the config or want to access
a field that was not in the `ComposeConfig` class.
envs: A dictionary of environment variables to set for the compose process.
# Returns
A `ComposeConfig` object if `return_json` is `False`, and a `dict` otherwise.
"""
full_cmd = self.docker_compose_cmd + ["config", "--format", "json"]
result = run(full_cmd, capture_stdout=True)
result = run(full_cmd, capture_stdout=True, env=envs)
if return_json:
return json.loads(result)
else:
Expand Down Expand Up @@ -651,6 +652,7 @@ def up(
log_prefix: bool = True,
start: bool = True,
quiet: bool = False,
envs: Dict[str, str] = {},
):
"""Start the containers.
Expand Down Expand Up @@ -681,6 +683,7 @@ def up(
start: Start the service after creating them.
quiet: By default, some progress bars and logs are sent to stderr and stdout.
Set `quiet=True` to avoid having any output.
envs: A dictionary of environment variables to set for the compose process.
# Returns
`None` at the moment. The plan is to be able to capture and stream the logs later.
Expand All @@ -706,7 +709,7 @@ def up(
services = to_list(services)
full_cmd += services
# important information is written to both stdout AND stderr.
run(full_cmd, capture_stdout=quiet, capture_stderr=quiet)
run(full_cmd, capture_stdout=quiet, capture_stderr=quiet, env=envs)

def version(self) -> str:
"""Returns the version of docker compose as a `str`."""
Expand Down
2 changes: 2 additions & 0 deletions tests/python_on_whales/components/dummy_compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ services:
busybox:
image: busybox:latest
command: sleep infinity
environment:
- SOME_VARIABLE=${SOME_VARIABLE_TO_INSERT:-nothing}
busybox-2-electric-boogaloo:
image: busybox:latest
depends_on:
Expand Down
16 changes: 16 additions & 0 deletions tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ def test_docker_compose_up_down_some_services():
docker.compose.down(timeout=1)


def test_docker_compose_config_envs(tmp_path: Path):
output = docker.compose.config(envs={"SOME_VARIABLE_TO_INSERT": "test-value"})
assert output.services["busybox"].environment["SOME_VARIABLE"] == "test-value"


def test_docker_compose_up_envs(tmp_path: Path):
docker.compose.up(["busybox"], detach=True, envs={"SOME_VARIABLE_TO_INSERT": "hello world"},)
output = docker.compose.execute(
"busybox",
["sh", "-c", "echo $SOME_VARIABLE"],
tty=False,
)
assert output == "hello world"
docker.compose.down(timeout=1)


def test_docker_compose_ps():
docker.compose.up(["my_service", "busybox"], detach=True)
containers = docker.compose.ps()
Expand Down

0 comments on commit 6c49ed4

Please sign in to comment.