Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
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
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
Loading