From 482d8da2dc25e16e5e22b3a129c1dbfa22aa5ea2 Mon Sep 17 00:00:00 2001 From: Maksim Losev Date: Mon, 27 Apr 2015 11:58:20 +0300 Subject: [PATCH] Use HostConfig object when creating container with Docker Remote API > 1.15 This is mlosev's patch (from #1208), rebased against devel as of 2790af2. It resolves #1707, which was caused by an API incompatibility between the docker module and server API version 1.19. --- lib/ansible/modules/cloud/docker/docker.py | 61 +++++++++++++++++++--- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/ansible/modules/cloud/docker/docker.py b/lib/ansible/modules/cloud/docker/docker.py index 11ee8e8571b..8198a0db247 100644 --- a/lib/ansible/modules/cloud/docker/docker.py +++ b/lib/ansible/modules/cloud/docker/docker.py @@ -542,6 +542,7 @@ class DockerManager(object): 'extra_hosts': ((0, 7, 0), '1.3.1'), 'pid': ((1, 0, 0), '1.17'), 'log_driver': ((1, 2, 0), '1.18'), + 'host_config': ((0, 7, 0), '1.15'), # Clientside only 'insecure_registry': ((0, 5, 0), '0.0') } @@ -751,6 +752,52 @@ class DockerManager(object): else: return None + def get_start_params(self): + """ + Create start params + """ + params = { + 'lxc_conf': self.lxc_conf, + 'binds': self.binds, + 'port_bindings': self.port_bindings, + 'publish_all_ports': self.module.params.get('publish_all_ports'), + 'privileged': self.module.params.get('privileged'), + 'links': self.links, + 'network_mode': self.module.params.get('net'), + } + + optionals = {} + for optional_param in ('dns', 'volumes_from', 'restart_policy', + 'restart_policy_retry', 'pid'): + optionals[optional_param] = self.module.params.get(optional_param) + + if optionals['dns'] is not None: + self.ensure_capability('dns') + params['dns'] = optionals['dns'] + + if optionals['volumes_from'] is not None: + self.ensure_capability('volumes_from') + params['volumes_from'] = optionals['volumes_from'] + + if optionals['restart_policy'] is not None: + self.ensure_capability('restart_policy') + params['restart_policy'] = { 'Name': optionals['restart_policy'] } + if params['restart_policy']['Name'] == 'on-failure': + params['restart_policy']['MaximumRetryCount'] = optionals['restart_policy_retry'] + + if optionals['pid'] is not None: + self.ensure_capability('pid') + params['pid_mode'] = optionals['pid'] + + return params + + def get_host_config(self): + """ + Create HostConfig object + """ + params = self.get_start_params() + return docker.utils.create_host_config(**params) + def get_port_bindings(self, ports): """ Parse the `ports` string into a port bindings dict for the `start_container` call. @@ -1304,16 +1351,10 @@ class DockerManager(object): return docker.utils.create_host_config(**params) def create_containers(self, count=1): - try: - mem_limit = _human_to_bytes(self.module.params.get('memory_limit')) - except ValueError as e: - self.module.fail_json(msg=str(e)) - params = {'image': self.module.params.get('image'), 'command': self.module.params.get('command'), 'ports': self.exposed_ports, 'volumes': self.volumes, - 'mem_limit': mem_limit, 'environment': self.env, 'hostname': self.module.params.get('hostname'), 'domainname': self.module.params.get('domainname'), @@ -1337,6 +1378,9 @@ class DockerManager(object): if self.ensure_capability('insecure_registry', fail=False): extra_params['insecure_registry'] = self.module.params.get('insecure_registry') + if self.ensure_capability('host_config', fail=False): + params['host_config'] = self.get_host_config() + def do_create(count, params): results = [] for _ in range(count): @@ -1358,6 +1402,11 @@ class DockerManager(object): return containers def start_containers(self, containers): + params = {} + + if not self.ensure_capability('host_config', fail=False): + params = self.get_start_params() + for i in containers: self.client.start(i) self.increment_counter('started')