Skip to content
Open
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
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.9]
python-version: [3.9, 3.11]
os: [ubuntu-latest]

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Docker
uses: docker/setup-buildx-action@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
Expand Down
1 change: 0 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Click
docker-compose==1.29.2
pyyaml>=5.4.1
MarkupSafe
jinja2
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Click
docker-compose>=1.26.2, <=1.29.2
pyyaml>=5.4.1
MarkupSafe
jinja2
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ universal = 1

[metadata]
description_file = README.md
version = 1.0.2
version = 1.1.0

27 changes: 16 additions & 11 deletions stackconfig/stackconfig.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import tempfile

from compose.config.serialize import serialize_config
from compose.cli.command import get_config_from_options

import subprocess

from stackconfig.utils.yaml_utils import save_compose, load_compose, remove_files
from stackconfig.utils.validate_compose import validate_docker_stack_compose
Expand All @@ -17,14 +14,22 @@ def __init__(self, files, output, version=None):

def merge_stack_compose(self):
"""
Merges docker-compose files using docker-compose library
Merges docker-compose files using docker stack config CLI
"""
# using docker-compose library merge process
compose_config = get_config_from_options(
".", {"--file": self.files}, {"--no-interpolate": False}
)
compose_config_str = serialize_config(compose_config, None, escape_dollar=True)

# Build command: docker stack config -c file1 -c file2
cmd = ["docker", "stack", "config"]
for f in self.files:
cmd.extend(["-c", f])
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
check=True
)
except subprocess.CalledProcessError as e:
raise Exception(e.stderr or str(e))
compose_config_str = result.stdout
# tmp file
name_tmp_file = tempfile.NamedTemporaryFile().name
save_compose(compose_config_str, name_tmp_file, as_text=True)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_stackconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ def test_merge_compose_files_invalid(version, mock_success_subprocess):
def test_merge_compose_files_syntax_error(mock_success_subprocess):
with open("/tmp/invalid-compose.yml", "+w") as file:
file.writelines("{}\ntests: test_value".format("test"))
with open("/tmp/override_invalid.yml", "+w") as file:
file.writelines("{}\ntests: test_value".format("test"))
with pytest.raises(Exception) as exc:
c = StackConfigCompose(
["/tmp/invalid-compose.yml", "/tmp/override_invalid.yml"],
"/tmp/temp_result_invalid.yml",
)
c.merge_stack_compose()
assert "mapping values are not allowed here" in str(exc)
assert "mapping values are not allowed in this context" in str(exc)


def test_merge_compose_files_invalid_syntax_compose_validation(mock_error_subprocess):
Expand All @@ -73,4 +75,4 @@ def test_merge_compose_files_invalid_syntax_compose_validation(mock_error_subpro
["tests/example_compose.yml"] + templates, "/tmp/temp_result.yml"
)
c.merge_stack_compose()
assert f"services.service_custom.deploy.replicas contains an invalid type" in str(err)
assert f"services.service_custom.deploy.replicas must be a integer" in str(err)