Skip to content

Commit

Permalink
Add check_files_existing option. (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein authored Apr 21, 2024
1 parent 6368854 commit 8bcc351
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelogs/fragments/839-compose_v2-check-file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
minor_changes:
- "docker_compose_v2* - the new option ``check_files_existing`` allows to disable the check for one of the files
``compose.yaml``, ``compose.yml``, ``docker-compose.yaml``, and ``docker-compose.yml`` in ``project_src``
if ``files`` is not specified. This is necessary if a non-standard compose filename is specified through other
means, like the ``COMPOSE_FILE`` environment variable
(https://github.com/ansible-collections/community.docker/issues/838, https://github.com/ansible-collections/community.docker/pull/839)."
11 changes: 11 additions & 0 deletions plugins/doc_fragments/compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ class ModuleDocFragment(object):
- Equivalent to C(docker compose --profile).
type: list
elements: str
check_files_existing:
description:
- If set to V(false), the module will not check whether one of the files
C(compose.yaml), C(compose.yml), C(docker-compose.yaml), or C(docker-compose.yml)
exists in O(project_src) if O(files) is not provided.
- This can be useful if environment files with C(COMPOSE_FILE) are used to configure a different
filename. The module currently does not check for C(COMPOSE_FILE) in environment files or the
current environment.
type: bool
default: true
version_added: 3.9.0
requirements:
- "PyYAML if O(definition) is used"
notes:
Expand Down
4 changes: 3 additions & 1 deletion plugins/module_utils/compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ def common_compose_argspec():
definition=dict(type='dict'),
env_files=dict(type='list', elements='path'),
profiles=dict(type='list', elements='str'),
check_files_existing=dict(type='bool', default=True),
)


Expand Down Expand Up @@ -584,12 +585,13 @@ def __init__(self, client, min_version=MINIMUM_COMPOSE_VERSION):
if not os.path.isdir(self.project_src):
self.fail('"{0}" is not a directory'.format(self.project_src))

self.check_files_existing = parameters['check_files_existing']
if self.files:
for file in self.files:
path = os.path.join(self.project_src, file)
if not os.path.exists(path):
self.fail('Cannot find Compose file "{0}" relative to project directory "{1}"'.format(file, self.project_src))
elif all(not os.path.exists(os.path.join(self.project_src, f)) for f in DOCKER_COMPOSE_FILES):
elif self.check_files_existing and all(not os.path.exists(os.path.join(self.project_src, f)) for f in DOCKER_COMPOSE_FILES):
filenames = ', '.join(DOCKER_COMPOSE_FILES[:-1])
self.fail('"{0}" does not contain {1}, or {2}'.format(self.project_src, filenames, DOCKER_COMPOSE_FILES[-1]))

Expand Down

0 comments on commit 8bcc351

Please sign in to comment.