Skip to content

Commit

Permalink
Determine the compose version via a CLI call and not the docker API. (#…
Browse files Browse the repository at this point in the history
…1021)

* Determine the compose version via a CLI call and not the docker API.

* Update plugins/module_utils/compose_v2.py

Co-authored-by: Felix Fontein <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
apollo13 and felixfontein authored Dec 29, 2024
1 parent bd99258 commit 6172a92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "docker_compose_v2* modules - determine compose version with ``docker compose version`` and only then fall back to ``docker info`` (https://github.com/ansible-collections/community.docker/pull/1021)."
38 changes: 28 additions & 10 deletions plugins/module_utils/compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,16 +701,7 @@ def __init__(self, client, min_version=MINIMUM_COMPOSE_VERSION):
self.env_files = parameters['env_files']
self.profiles = parameters['profiles']

compose = self.client.get_client_plugin_info('compose')
if compose is None:
self.fail('Docker CLI {0} does not have the compose plugin installed'.format(self.client.get_cli()))
if compose['Version'] == 'dev':
self.fail(
'Docker CLI {0} has a compose plugin installed, but it reports version "dev".'
' Please use a version of the plugin that returns a proper version.'
.format(self.client.get_cli())
)
compose_version = compose['Version'].lstrip('v')
compose_version = self.get_compose_version()
self.compose_version = LooseVersion(compose_version)
if self.compose_version < LooseVersion(min_version):
self.fail('Docker CLI {cli} has the compose plugin with version {version}; need version {min_version} or later'.format(
Expand All @@ -736,6 +727,33 @@ def __init__(self, client, min_version=MINIMUM_COMPOSE_VERSION):
# more precisely in https://github.com/docker/compose/pull/11478
self.use_json_events = self.compose_version >= LooseVersion('2.29.0')

def get_compose_version(self):
return self.get_compose_version_from_cli() or self.get_compose_version_from_api()

def get_compose_version_from_cli(self):
rc, version_info, stderr = self.client.call_cli('compose', 'version', '--format', 'json')
if rc:
return None
try:
version = json.loads(version_info)['version']
if version == 'dev':
return None
return version.lstrip('v')
except Exception:
return None

def get_compose_version_from_api(self):
compose = self.client.get_client_plugin_info('compose')
if compose is None:
self.fail('Docker CLI {0} does not have the compose plugin installed'.format(self.client.get_cli()))
if compose['Version'] == 'dev':
self.fail(
'Docker CLI {0} has a compose plugin installed, but it reports version "dev".'
' Please use a version of the plugin that returns a proper version.'
.format(self.client.get_cli())
)
return compose['Version'].lstrip('v')

def fail(self, msg, **kwargs):
self.cleanup()
self.client.fail(msg, **kwargs)
Expand Down

0 comments on commit 6172a92

Please sign in to comment.