diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0f68ed9..26da174 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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: diff --git a/requirements.in b/requirements.in index 3f644b8..c1a5818 100644 --- a/requirements.in +++ b/requirements.in @@ -1,5 +1,4 @@ Click -docker-compose==1.29.2 pyyaml>=5.4.1 MarkupSafe jinja2 diff --git a/requirements.txt b/requirements.txt index 1c0c2ba..c1a5818 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ Click -docker-compose>=1.26.2, <=1.29.2 pyyaml>=5.4.1 MarkupSafe jinja2 diff --git a/setup.cfg b/setup.cfg index 592a9fd..a0762c2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,5 +3,5 @@ universal = 1 [metadata] description_file = README.md -version = 1.0.2 +version = 1.1.0 diff --git a/stackconfig/stackconfig.py b/stackconfig/stackconfig.py index 8f32a32..7a4e6f0 100644 --- a/stackconfig/stackconfig.py +++ b/stackconfig/stackconfig.py @@ -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 @@ -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) diff --git a/tests/test_stackconfig.py b/tests/test_stackconfig.py index ff6a8ec..d493048 100644 --- a/tests/test_stackconfig.py +++ b/tests/test_stackconfig.py @@ -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): @@ -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)