Add compatibility for docker-py version 3 (#36973)

This commit is contained in:
skylerbunny 2018-03-06 04:14:31 -08:00 committed by Chris Houseknecht
parent ebc71bcb16
commit d984afa5ba
4 changed files with 39 additions and 18 deletions

View file

@ -688,12 +688,12 @@ import re
import shlex
from ansible.module_utils.basic import human_to_bytes
from ansible.module_utils.docker_common import HAS_DOCKER_PY_2, AnsibleDockerClient, DockerBaseClass
from ansible.module_utils.docker_common import HAS_DOCKER_PY_2, HAS_DOCKER_PY_3, AnsibleDockerClient, DockerBaseClass
from ansible.module_utils.six import string_types
try:
from docker import utils
if HAS_DOCKER_PY_2:
if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
from docker.types import Ulimit, LogConfig
else:
from docker.utils.types import Ulimit, LogConfig
@ -894,14 +894,16 @@ class TaskParameters(DockerBaseClass):
environment='env',
name='name',
entrypoint='entrypoint',
cpu_shares='cpu_shares',
mac_address='mac_address',
labels='labels',
stop_signal='stop_signal',
volume_driver='volume_driver',
working_dir='working_dir',
)
if not HAS_DOCKER_PY_3:
create_params['cpu_shares'] = 'cpu_shares'
create_params['volume_driver'] = 'volume_driver'
result = dict(
host_config=self._host_config(),
volumes=self._get_mounts(),
@ -990,10 +992,15 @@ class TaskParameters(DockerBaseClass):
tmpfs='tmpfs'
)
if HAS_DOCKER_PY_2:
if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3:
# auto_remove is only supported in docker>=2
host_config_params['auto_remove'] = 'auto_remove'
if HAS_DOCKER_PY_3:
# cpu_shares and volume_driver moved to create_host_config in > 3
host_config_params['cpu_shares'] = 'cpu_shares'
host_config_params['volume_driver'] = 'volume_driver'
params = dict()
for key, value in host_config_params.items():
if getattr(self, value, None) is not None:
@ -1973,7 +1980,10 @@ class ContainerManager(DockerBaseClass):
self.fail("Error starting container %s: %s" % (container_id, str(exc)))
if not self.parameters.detach:
status = self.client.wait(container_id)
if HAS_DOCKER_PY_3:
status = self.client.wait(container_id)['StatusCode']
else:
status = self.client.wait(container_id)
config = self.client.inspect_container(container_id)
logging_driver = config['HostConfig']['LogConfig']['Type']
@ -2141,8 +2151,8 @@ def main():
supports_check_mode=True
)
if not HAS_DOCKER_PY_2 and client.module.params.get('auto_remove'):
client.module.fail_json(msg="'auto_remove' is not compatible with docker-py, and requires the docker python module")
if (not (HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3)) and client.module.params.get('auto_remove'):
client.module.fail_json(msg="'auto_remove' is not compatible with the 'docker-py' Python package. It requires the newer 'docker' Python package.")
cm = ContainerManager(client)
client.module.exit_json(**cm.results)