[docker] Add compatibility with docker-py v2.0
parent
339312a6b4
commit
e2a1ce2916
|
@ -27,17 +27,24 @@ from urlparse import urlparse
|
||||||
from ansible.module_utils.basic import *
|
from ansible.module_utils.basic import *
|
||||||
|
|
||||||
HAS_DOCKER_PY = True
|
HAS_DOCKER_PY = True
|
||||||
|
HAS_DOCKER_PY_2 = False
|
||||||
HAS_DOCKER_ERROR = None
|
HAS_DOCKER_ERROR = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from requests.exceptions import SSLError
|
from requests.exceptions import SSLError
|
||||||
from docker import Client
|
|
||||||
from docker import __version__ as docker_version
|
from docker import __version__ as docker_version
|
||||||
from docker.errors import APIError, TLSParameterError, NotFound
|
from docker.errors import APIError, TLSParameterError, NotFound
|
||||||
from docker.tls import TLSConfig
|
from docker.tls import TLSConfig
|
||||||
from docker.constants import DEFAULT_TIMEOUT_SECONDS, DEFAULT_DOCKER_API_VERSION
|
from docker.constants import DEFAULT_TIMEOUT_SECONDS, DEFAULT_DOCKER_API_VERSION
|
||||||
from docker.utils.types import Ulimit, LogConfig
|
|
||||||
from docker import auth
|
from docker import auth
|
||||||
|
if LooseVersion(docker_version) >= LooseVersion('2.0.0'):
|
||||||
|
HAS_DOCKER_PY_2 = True
|
||||||
|
from docker import APIClient as Client
|
||||||
|
from docker.types import Ulimit, LogConfig
|
||||||
|
else:
|
||||||
|
from docker import Client
|
||||||
|
from docker.utils.types import Ulimit, LogConfig
|
||||||
|
|
||||||
except ImportError as exc:
|
except ImportError as exc:
|
||||||
HAS_DOCKER_ERROR = str(exc)
|
HAS_DOCKER_ERROR = str(exc)
|
||||||
HAS_DOCKER_PY = False
|
HAS_DOCKER_PY = False
|
||||||
|
@ -135,7 +142,7 @@ class AnsibleDockerClient(Client):
|
||||||
MIN_DOCKER_VERSION))
|
MIN_DOCKER_VERSION))
|
||||||
|
|
||||||
self.debug = self.module.params.get('debug')
|
self.debug = self.module.params.get('debug')
|
||||||
self.check_mode = self.module.check_mode
|
self.check_mode = self.module.check_mode
|
||||||
self._connect_params = self._get_connect_params()
|
self._connect_params = self._get_connect_params()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -377,9 +384,9 @@ class AnsibleDockerClient(Client):
|
||||||
images = self._image_lookup(name, tag)
|
images = self._image_lookup(name, tag)
|
||||||
if len(images) == 0:
|
if len(images) == 0:
|
||||||
# In API <= 1.20 seeing 'docker.io/<name>' as the name of images pulled from docker hub
|
# In API <= 1.20 seeing 'docker.io/<name>' as the name of images pulled from docker hub
|
||||||
registry, repo_name = auth.resolve_repository_name(name)
|
registry, repo_name = auth.resolve_repository_name(name)
|
||||||
if registry == 'docker.io':
|
if registry == 'docker.io':
|
||||||
# the name does not contain a registry, so let's see if docker.io works
|
# the name does not contain a registry, so let's see if docker.io works
|
||||||
lookup = "docker.io/%s" % name
|
lookup = "docker.io/%s" % name
|
||||||
self.log("Check for docker.io image: %s" % lookup)
|
self.log("Check for docker.io image: %s" % lookup)
|
||||||
images = self._image_lookup(lookup, tag)
|
images = self._image_lookup(lookup, tag)
|
||||||
|
@ -397,9 +404,9 @@ class AnsibleDockerClient(Client):
|
||||||
self.log("Image %s:%s not found." % (name, tag))
|
self.log("Image %s:%s not found." % (name, tag))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _image_lookup(self, name, tag):
|
def _image_lookup(self, name, tag):
|
||||||
'''
|
'''
|
||||||
Including a tag in the name parameter sent to the docker-py images method does not
|
Including a tag in the name parameter sent to the docker-py images method does not
|
||||||
work consistently. Instead, get the result set for name and manually check if the tag
|
work consistently. Instead, get the result set for name and manually check if the tag
|
||||||
exists.
|
exists.
|
||||||
'''
|
'''
|
||||||
|
@ -408,7 +415,7 @@ class AnsibleDockerClient(Client):
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.fail("Error searching for image %s - %s" % (name, str(exc)))
|
self.fail("Error searching for image %s - %s" % (name, str(exc)))
|
||||||
images = response
|
images = response
|
||||||
if tag:
|
if tag:
|
||||||
lookup = "%s:%s" % (name, tag)
|
lookup = "%s:%s" % (name, tag)
|
||||||
images = []
|
images = []
|
||||||
for image in response:
|
for image in response:
|
||||||
|
|
|
@ -656,7 +656,10 @@ from ansible.module_utils.docker_common import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from docker import utils
|
from docker import utils
|
||||||
from docker.utils.types import Ulimit
|
if HAS_DOCKER_PY_2:
|
||||||
|
from docker.types import Ulimit
|
||||||
|
else:
|
||||||
|
from docker.utils.types import Ulimit
|
||||||
except:
|
except:
|
||||||
# missing docker-py handled in ansible.module_utils.docker
|
# missing docker-py handled in ansible.module_utils.docker
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -250,7 +250,10 @@ image:
|
||||||
from ansible.module_utils.docker_common import *
|
from ansible.module_utils.docker_common import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from docker.auth.auth import resolve_repository_name
|
if HAS_DOCKER_PY_2:
|
||||||
|
from docker.auth import resolve_repository_name
|
||||||
|
else:
|
||||||
|
from docker.auth.auth import resolve_repository_name
|
||||||
from docker.utils.utils import parse_repository_tag
|
from docker.utils.utils import parse_repository_tag
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# missing docker-py handled in docker_common
|
# missing docker-py handled in docker_common
|
||||||
|
|
|
@ -164,7 +164,10 @@ from ansible.module_utils.docker_common import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from docker import utils
|
from docker import utils
|
||||||
from docker.utils.types import Ulimit
|
if HAS_DOCKER_PY_2:
|
||||||
|
from docker.types import Ulimit, IPAMPool, IPAMConfig
|
||||||
|
else:
|
||||||
|
from docker.utils.types import Ulimit
|
||||||
except:
|
except:
|
||||||
# missing docker-py handled in ansible.module_utils.docker
|
# missing docker-py handled in ansible.module_utils.docker
|
||||||
pass
|
pass
|
||||||
|
@ -275,9 +278,16 @@ class DockerNetworkManager(object):
|
||||||
if not self.existing_network:
|
if not self.existing_network:
|
||||||
ipam_pools = []
|
ipam_pools = []
|
||||||
if self.parameters.ipam_options:
|
if self.parameters.ipam_options:
|
||||||
ipam_pools.append(utils.create_ipam_pool(**self.parameters.ipam_options))
|
if HAS_DOCKER_PY_2:
|
||||||
|
ipam_pools.append(IPAMPool(**self.parameters.ipam_options))
|
||||||
|
else:
|
||||||
|
ipam_pools.append(utils.create_ipam_pool(**self.parameters.ipam_options))
|
||||||
|
|
||||||
ipam_config = utils.create_ipam_config(driver=self.parameters.ipam_driver,
|
if HAS_DOCKER_PY_2:
|
||||||
|
ipam_config = IPAMConfig(driver=self.parameters.ipam_driver,
|
||||||
|
pool_configs=ipam_pools)
|
||||||
|
else:
|
||||||
|
ipam_config = utils.create_ipam_config(driver=self.parameters.ipam_driver,
|
||||||
pool_configs=ipam_pools)
|
pool_configs=ipam_pools)
|
||||||
|
|
||||||
if not self.check_mode:
|
if not self.check_mode:
|
||||||
|
|
Loading…
Reference in New Issue