backport various docker_common fixes from devel

This commit is contained in:
Matt Davis 2016-10-25 08:20:35 -07:00
parent 12a38bc75f
commit b5c95ea6fa

View file

@ -22,6 +22,7 @@ import json
import sys import sys
import copy import copy
from distutils.version import LooseVersion
from urlparse import urlparse from urlparse import urlparse
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
@ -37,7 +38,7 @@ try:
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.utils.types import Ulimit, LogConfig
from docker import auth from docker import auth
except ImportError, exc: except ImportError as exc:
HAS_DOCKER_ERROR = str(exc) HAS_DOCKER_ERROR = str(exc)
HAS_DOCKER_PY = False HAS_DOCKER_PY = False
@ -151,7 +152,7 @@ class AnsibleDockerClient(Client):
if not HAS_DOCKER_PY: if not HAS_DOCKER_PY:
self.fail("Failed to import docker-py - %s. Try `pip install docker-py`" % HAS_DOCKER_ERROR) self.fail("Failed to import docker-py - %s. Try `pip install docker-py`" % HAS_DOCKER_ERROR)
if docker_version < MIN_DOCKER_VERSION: if LooseVersion(docker_version) < LooseVersion(MIN_DOCKER_VERSION):
self.fail("Error: docker-py version is %s. Minimum version required is %s." % (docker_version, self.fail("Error: docker-py version is %s. Minimum version required is %s." % (docker_version,
MIN_DOCKER_VERSION)) MIN_DOCKER_VERSION))
@ -161,9 +162,9 @@ class AnsibleDockerClient(Client):
try: try:
super(AnsibleDockerClient, self).__init__(**self._connect_params) super(AnsibleDockerClient, self).__init__(**self._connect_params)
except APIError, exc: except APIError as exc:
self.fail("Docker API error: %s" % exc) self.fail("Docker API error: %s" % exc)
except Exception, exc: except Exception as exc:
self.fail("Error connecting: %s" % exc) self.fail("Error connecting: %s" % exc)
def log(self, msg, pretty_print=False): def log(self, msg, pretty_print=False):
@ -233,7 +234,7 @@ class AnsibleDockerClient(Client):
tls_hostname=self._get_value('tls_hostname', params['tls_hostname'], tls_hostname=self._get_value('tls_hostname', params['tls_hostname'],
'DOCKER_TLS_HOSTNAME', 'localhost'), 'DOCKER_TLS_HOSTNAME', 'localhost'),
api_version=self._get_value('api_version', params['api_version'], 'DOCKER_API_VERSION', api_version=self._get_value('api_version', params['api_version'], 'DOCKER_API_VERSION',
DEFAULT_DOCKER_API_VERSION), 'auto'),
cacert_path=self._get_value('cacert_path', params['cacert_path'], 'DOCKER_CERT_PATH', None), cacert_path=self._get_value('cacert_path', params['cacert_path'], 'DOCKER_CERT_PATH', None),
cert_path=self._get_value('cert_path', params['cert_path'], 'DOCKER_CERT_PATH', None), cert_path=self._get_value('cert_path', params['cert_path'], 'DOCKER_CERT_PATH', None),
key_path=self._get_value('key_path', params['key_path'], 'DOCKER_CERT_PATH', None), key_path=self._get_value('key_path', params['key_path'], 'DOCKER_CERT_PATH', None),
@ -262,7 +263,7 @@ class AnsibleDockerClient(Client):
try: try:
tls_config = TLSConfig(**kwargs) tls_config = TLSConfig(**kwargs)
return tls_config return tls_config
except TLSParameterError, exc: except TLSParameterError as exc:
self.fail("TLS config error: %s" % exc) self.fail("TLS config error: %s" % exc)
def _get_connect_params(self): def _get_connect_params(self):
@ -372,9 +373,9 @@ class AnsibleDockerClient(Client):
if container['Id'] == name: if container['Id'] == name:
result = container result = container
break break
except SSLError, exc: except SSLError as exc:
self._handle_ssl_error(exc) self._handle_ssl_error(exc)
except Exception, exc: except Exception as exc:
self.fail("Error retrieving container list: %s" % exc) self.fail("Error retrieving container list: %s" % exc)
if result is not None: if result is not None:
@ -382,7 +383,7 @@ class AnsibleDockerClient(Client):
self.log("Inspecting container Id %s" % result['Id']) self.log("Inspecting container Id %s" % result['Id'])
result = self.inspect_container(container=result['Id']) result = self.inspect_container(container=result['Id'])
self.log("Completed container inspection") self.log("Completed container inspection")
except Exception, exc: except Exception as exc:
self.fail("Error inspecting container: %s" % exc) self.fail("Error inspecting container: %s" % exc)
return result return result
@ -411,7 +412,7 @@ class AnsibleDockerClient(Client):
if len(images) == 1: if len(images) == 1:
try: try:
inspection = self.inspect_image(images[0]['Id']) inspection = self.inspect_image(images[0]['Id'])
except Exception, exc: except Exception as exc:
self.fail("Error inspecting image %s:%s - %s" % (name, tag, str(exc))) self.fail("Error inspecting image %s:%s - %s" % (name, tag, str(exc)))
return inspection return inspection
@ -431,9 +432,10 @@ class AnsibleDockerClient(Client):
images = response images = response
if tag: if tag:
lookup = "%s:%s" % (name, tag) lookup = "%s:%s" % (name, tag)
images = []
for image in response: for image in response:
self.log(image, pretty_print=True) tags = image.get('RepoTags')
if image.get('RepoTags') and lookup in image.get('RepoTags'): if tags and lookup in tags:
images = [image] images = [image]
break break
return images return images
@ -444,8 +446,7 @@ class AnsibleDockerClient(Client):
''' '''
self.log("Pulling image %s:%s" % (name, tag)) self.log("Pulling image %s:%s" % (name, tag))
try: try:
for line in self.pull(name, tag=tag, stream=True): for line in self.pull(name, tag=tag, stream=True, decode=True):
line = json.loads(line)
self.log(line, pretty_print=True) self.log(line, pretty_print=True)
if line.get('error'): if line.get('error'):
if line.get('errorDetail'): if line.get('errorDetail'):
@ -455,7 +456,7 @@ class AnsibleDockerClient(Client):
error_detail.get('message'))) error_detail.get('message')))
else: else:
self.fail("Error pulling %s - %s" % (name, line.get('error'))) self.fail("Error pulling %s - %s" % (name, line.get('error')))
except Exception, exc: except Exception as exc:
self.fail("Error pulling image %s:%s - %s" % (name, tag, str(exc))) self.fail("Error pulling image %s:%s - %s" % (name, tag, str(exc)))
return self.find_image(name, tag) return self.find_image(name, tag)