Skip to content

Commit

Permalink
Added docker.compose.config(...) (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldemarmiesse authored Apr 15, 2021
1 parent a8672bc commit feca4f4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
35 changes: 31 additions & 4 deletions python_on_whales/components/compose/cli_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from typing import List, Optional, Union
import json
from typing import Any, Dict, List, Optional, Union

import python_on_whales.components.container.cli_wrapper
from python_on_whales.client_config import DockerCLICaller
from python_on_whales.components.compose.models import ComposeConfig
from python_on_whales.utils import run, to_list


Expand All @@ -19,9 +21,34 @@ def build(self, services: List[str] = []):
full_cmd += services
run(full_cmd, capture_stdout=False)

def config(self):
"""Not yet implemented"""
raise NotImplementedError
def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, Any]]:
"""Returns the configuration of the compose stack for further inspection.
For example
```python
from python_on_whales import docker
project_config = docker.compose.config()
print(project_config.services["my_first_service"].image)
"redis"
```
# Arguments
return_json: If `False`, a `ComposeConfig` object will be returned, and you
'll be able to take advantage of your IDE autocompletion. If you want the
full json output, you may use `return_json`. In this case, you'll get
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.
# 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)
if return_json:
return json.loads(result)
else:
return ComposeConfig.parse_raw(result)

def create(
self,
Expand Down
62 changes: 62 additions & 0 deletions python_on_whales/components/compose/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field


class ServiceDeployConfig(BaseModel):
labels: Optional[Dict[str, str]]
resources: Any
placement: Any


class DependencyCondition(BaseModel):
condition: str


class ComposeConfigService(BaseModel):
deploy: Optional[ServiceDeployConfig]
blkio_config: Any
cpu_count: Optional[float]
cpu_percent: Optional[float]
cpu_shares: Optional[int]
cpuset: Optional[str]
build: Any
cap_add: List[str] = Field(default_factory=list)
cap_drop: List[str] = Field(default_factory=list)
cgroup_parent: Optional[str]
command: Optional[List[str]]
configs: Any
container_name: Optional[str]
depends_on: Dict[str, DependencyCondition] = Field(default_factory=dict)
device_cgroup_rules: List[str] = Field(default_factory=list)
devices: Any
environment: Optional[Dict[str, Optional[str]]]
image: Optional[str]


class ComposeConfigNetwork(BaseModel):
driver: Optional[str]
name: Optional[str]
external: bool = False
driver_opts: Optional[Dict[str, Any]]
attachable: Optional[bool]
enable_ipv6: Optional[bool]
ipam: Any
internal: Optional[bool]
labels: Dict[str, str] = Field(default_factory=dict)


class ComposeConfigVolume(BaseModel):
driver: Optional[str]
driver_opts: Optional[Dict[str, Any]]
external: Optional[bool]
labels: Dict[str, str] = Field(default_factory=dict)
name: Optional[str]


class ComposeConfig(BaseModel):
services: Dict[str, ComposeConfigService]
networks: Dict[str, ComposeConfigNetwork] = Field(default_factory=dict)
volumes: Dict[str, ComposeConfigVolume] = Field(default_factory=dict)
configs: Any
secrets: Any
8 changes: 8 additions & 0 deletions tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def test_docker_compose_create_down():
docker.compose.down()


def test_docker_compose_config():
compose_config = docker.compose.config()
assert compose_config.services["alpine"].image == "alpine:latest"

compose_config = docker.compose.config(return_json=True)
assert compose_config["services"]["alpine"]["image"] == "alpine:latest"


def test_docker_compose_create_extra_options_down():
docker.compose.create(build=True, force_recreate=True)
docker.compose.create(build=True, force_recreate=True)
Expand Down

0 comments on commit feca4f4

Please sign in to comment.