Make insecure_registry feature version check against the client API

This commit is contained in:
Toshio Kuratomi 2014-12-18 12:45:13 -08:00
parent 4172d445d3
commit f65d9ab793

View file

@ -469,6 +469,8 @@ class DockerManager:
'dns': ((0, 3, 0), '1.10'), 'dns': ((0, 3, 0), '1.10'),
'volume_from': ((0, 3, 0), '1.10'), 'volume_from': ((0, 3, 0), '1.10'),
'restart_policy': ((0, 5, 0), '1.14'), 'restart_policy': ((0, 5, 0), '1.14'),
# Clientside only
'insecure_registry': ((0, 5, 0), '0.0')
} }
def __init__(self, module): def __init__(self, module):
@ -536,11 +538,15 @@ class DockerManager:
docker.utils.compare_version(req_vers[1], api_version) >= 0): docker.utils.compare_version(req_vers[1], api_version) >= 0):
self._capabilities.add(cap) self._capabilities.add(cap)
def ensure_capability(self, capability): def ensure_capability(self, capability, fail=True):
""" """
Some of the functionality this ansible module implements are only Some of the functionality this ansible module implements are only
available in newer versions of docker. Ensure that the capability available in newer versions of docker. Ensure that the capability
is available here. is available here.
If fail is set to False then return True or False depending on whether
we have the capability. Otherwise, simply fail and exit the module if
we lack the capability.
""" """
if not self._capabilities: if not self._capabilities:
self._check_capabilties() self._check_capabilties()
@ -548,6 +554,9 @@ class DockerManager:
if capability in self._capabilities: if capability in self._capabilities:
return True return True
if not fail:
return False
api_version = self.client.version()['ApiVersion'] api_version = self.client.version()['ApiVersion']
self.module.fail_json(msg='Specifying the `%s` parameter requires' self.module.fail_json(msg='Specifying the `%s` parameter requires'
' docker-py: %s, docker server apiversion %s; found' ' docker-py: %s, docker server apiversion %s; found'
@ -725,6 +734,11 @@ class DockerManager:
if params['volumes_from'] is not None: if params['volumes_from'] is not None:
self.ensure_capability('volumes_from') self.ensure_capability('volumes_from')
extra_params = {}
if self.module.params.get('insecure_registry'):
if self.ensure_capability('insecure_registry', fail=False):
extra_params['insecure_registry'] = self.module.params.get('insecure_registry')
def do_create(count, params): def do_create(count, params):
results = [] results = []
for _ in range(count): for _ in range(count):
@ -750,7 +764,7 @@ class DockerManager:
except: except:
self.module.fail_json(msg="failed to login to the remote registry, check your username/password.") self.module.fail_json(msg="failed to login to the remote registry, check your username/password.")
try: try:
self.client.pull(image, tag=tag, insecure_registry=self.module.params.get('insecure_registry')) self.client.pull(image, tag=tag, **extra_params)
except: except:
self.module.fail_json(msg="failed to pull the specified image: %s" % resource) self.module.fail_json(msg="failed to pull the specified image: %s" % resource)
self.increment_counter('pull') self.increment_counter('pull')