From 1e0363ec7df5caa669e6f2b31c18937217f3c940 Mon Sep 17 00:00:00 2001 From: Akshay Gaikwad Date: Fri, 14 Dec 2018 19:19:40 +0000 Subject: [PATCH] Support for cache_from parameter in docker_image module (#49787) * Support for cache_from parameter in docker_image module Fixes #21704 Signed-off-by: Akshay Gaikwad * Minimum docker version check for cache_from option Signed-off-by: Akshay Gaikwad * Add changelog fragment Signed-off-by: Akshay Gaikwad * cache_from fix when docker-py is older Signed-off-by: Akshay Gaikwad --- .../49787-docker_image-cache_from.yaml | 2 ++ .../modules/cloud/docker/docker_image.py | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/49787-docker_image-cache_from.yaml diff --git a/changelogs/fragments/49787-docker_image-cache_from.yaml b/changelogs/fragments/49787-docker_image-cache_from.yaml new file mode 100644 index 00000000000..2ad67ef3656 --- /dev/null +++ b/changelogs/fragments/49787-docker_image-cache_from.yaml @@ -0,0 +1,2 @@ +minor_changes: +- "docker_image - Add ``cache_from`` option." diff --git a/lib/ansible/modules/cloud/docker/docker_image.py b/lib/ansible/modules/cloud/docker/docker_image.py index 911e5525a02..36f6dcb5b73 100644 --- a/lib/ansible/modules/cloud/docker/docker_image.py +++ b/lib/ansible/modules/cloud/docker/docker_image.py @@ -30,6 +30,12 @@ options: - Use with state C(present) to archive an image to a .tar file. required: false version_added: "2.1" + cache_from: + description: + - List of image names to consider as cache source. + required: false + type: list + version_added: "2.8" load_path: description: - Use with state C(present) to load an image from a .tar file. @@ -238,6 +244,15 @@ EXAMPLES = ''' buildargs: log_volume: /var/log/myapp listen_port: 8080 + +- name: Build image using cache source + docker_image: + name: myimage:latest + path: /path/to/build/dir + # Use as cache source for building myimage + cache_from: + - nginx:latest + - alpine:3.8 ''' RETURN = ''' @@ -278,6 +293,7 @@ class ImageManager(DockerBaseClass): self.check_mode = self.client.check_mode self.archive_path = parameters.get('archive_path') + self.cache_from = parameters.get('cache_from') self.container_limits = parameters.get('container_limits') self.dockerfile = parameters.get('dockerfile') self.force = parameters.get('force') @@ -521,7 +537,7 @@ class ImageManager(DockerBaseClass): pull=self.pull, forcerm=self.rm, dockerfile=self.dockerfile, - decode=True + decode=True, ) if not HAS_DOCKER_PY_3: params['stream'] = True @@ -534,6 +550,8 @@ class ImageManager(DockerBaseClass): for key, value in self.buildargs.items(): self.buildargs[key] = to_native(value) params['buildargs'] = self.buildargs + if self.cache_from: + params['cache_from'] = self.cache_from for line in self.client.build(**params): # line = json.loads(line) @@ -583,6 +601,7 @@ class ImageManager(DockerBaseClass): def main(): argument_spec = dict( archive_path=dict(type='path'), + cache_from=dict(type='list', elements='str'), container_limits=dict(type='dict', options=dict( memory=dict(type='int'), memswap=dict(type='int'), @@ -606,10 +625,16 @@ def main(): buildargs=dict(type='dict', default=None), ) + option_minimal_versions = dict( + cache_from=dict(docker_py_version='2.1.0', docker_api_version='1.25'), + ) + client = AnsibleDockerClient( argument_spec=argument_spec, supports_check_mode=True, + min_docker_version='1.8.0', min_docker_api_version='1.20', + option_minimal_versions=option_minimal_versions, ) results = dict(