Fix the incompatible of docker-py 2.0.0

docker-py 2.0.0 is released with renaming docker.Client to
docker.APIClient. and with a new python package name "docker" rather
than "docker-py".
This patch support docker and docker-py packages.

Change-Id: Ib4400a4d2ce803191ee6215c5b28f0052970332c
Closes-Bug: #1668346
This commit is contained in:
Jeffrey Zhang 2017-02-28 14:10:59 +08:00
parent ef34e2a1c5
commit 78e5c865f4
3 changed files with 24 additions and 3 deletions

View File

@ -52,6 +52,13 @@ EXAMPLES = '''
import docker import docker
def get_docker_client():
try:
return docker.Client
except AttributeError:
return docker.APIClient
def main(): def main():
argument_spec = dict( argument_spec = dict(
name=dict(required=False, type='list', default=[]), name=dict(required=False, type='list', default=[]),
@ -61,7 +68,7 @@ def main():
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
results = dict(changed=False, _containers=[]) results = dict(changed=False, _containers=[])
client = docker.Client(version=module.params.get('api_version')) client = get_docker_client()(version=module.params.get('api_version'))
containers = client.containers() containers = client.containers()
names = module.params.get('name') names = module.params.get('name')
if names and not isinstance(names, list): if names and not isinstance(names, list):

View File

@ -197,6 +197,13 @@ import traceback
import docker import docker
def get_docker_client():
try:
return docker.Client
except AttributeError:
return docker.APIClient
class DockerWorker(object): class DockerWorker(object):
def __init__(self, module): def __init__(self, module):
@ -211,7 +218,7 @@ class DockerWorker(object):
'version': self.params.get('api_version') 'version': self.params.get('api_version')
} }
self.dc = docker.Client(**options) self.dc = get_docker_client()(**options)
def generate_tls(self): def generate_tls(self):
tls = {'verify': self.params.get('tls_verify')} tls = {'verify': self.params.get('tls_verify')}

View File

@ -102,6 +102,13 @@ def gen_commandline(params):
return command return command
def get_docker_client():
try:
return docker.Client
except AttributeError:
return docker.APIClient
def main(): def main():
specs = dict( specs = dict(
module_name=dict(type='str'), module_name=dict(type='str'),
@ -109,7 +116,7 @@ def main():
module_extra_vars=dict(type='json') module_extra_vars=dict(type='json')
) )
module = AnsibleModule(argument_spec=specs, bypass_checks=True) module = AnsibleModule(argument_spec=specs, bypass_checks=True)
client = docker.Client() client = get_docker_client()()
command_line = gen_commandline(module.params) command_line = gen_commandline(module.params)
kolla_toolbox = client.containers(filters=dict(name='kolla_toolbox', kolla_toolbox = client.containers(filters=dict(name='kolla_toolbox',
status='running')) status='running'))