mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Add docker_image_load module (#90)
* Add docker_image_load module. * Polish module. * Fix bug and add tests. * Apply suggestions from code review Co-authored-by: Amin Vakil <info@aminvakil.com> * Make sure that containers that still exist are also cleared. * Always return stdout. * Try to work around removal problems. * Accept that the Docker daemon sometimes only reports the named image. * More debug output. * Also prune containers, in the hope that these cause the problems. * Let's see whether pruning containers (but not images) is enough. * Apply suggestions from code review Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru> * Update plugins/modules/docker_image_load.py Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru> Co-authored-by: Amin Vakil <info@aminvakil.com> Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
shippable/posix/group4
|
||||
destructive
|
||||
@@ -0,0 +1,3 @@
|
||||
---
|
||||
dependencies:
|
||||
- setup_docker
|
||||
@@ -0,0 +1,8 @@
|
||||
####################################################################
|
||||
# WARNING: These are designed specifically for Ansible tests #
|
||||
# and should not be used as examples of how to write Ansible roles #
|
||||
####################################################################
|
||||
|
||||
- when: ansible_facts.distribution ~ ansible_facts.distribution_major_version not in ['CentOS6', 'RedHat6']
|
||||
include_tasks:
|
||||
file: test.yml
|
||||
@@ -0,0 +1,3 @@
|
||||
---
|
||||
- name: "Loading tasks from {{ item }}"
|
||||
include_tasks: "{{ item }}"
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
- name: Create random name prefix
|
||||
set_fact:
|
||||
name_prefix: "{{ 'ansible-test-%0x' % ((2**32) | random) }}"
|
||||
- name: Create image and container list
|
||||
set_fact:
|
||||
inames: []
|
||||
cnames: []
|
||||
|
||||
- debug:
|
||||
msg: "Using name prefix {{ name_prefix }}"
|
||||
|
||||
- block:
|
||||
- include_tasks: run-test.yml
|
||||
with_fileglob:
|
||||
- "tests/*.yml"
|
||||
|
||||
always:
|
||||
- name: "Make sure all images are removed"
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
with_items: "{{ inames }}"
|
||||
- name: "Make sure all containers are removed"
|
||||
docker_container:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
force_kill: yes
|
||||
with_items: "{{ cnames }}"
|
||||
|
||||
when: docker_py_version is version('2.5.0', '>=') and docker_api_version is version('1.23', '>=')
|
||||
|
||||
- fail: msg="Too old docker / docker-py version to run docker_image tests!"
|
||||
when: not(docker_py_version is version('2.5.0', '>=') and docker_api_version is version('1.23', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6)
|
||||
@@ -0,0 +1,213 @@
|
||||
---
|
||||
- set_fact:
|
||||
image_names:
|
||||
- "{{ docker_test_image_hello_world }}"
|
||||
- "{{ docker_test_image_busybox }}"
|
||||
- "{{ docker_test_image_alpine }}"
|
||||
|
||||
- name: Make sure images are there
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
source: pull
|
||||
register: images
|
||||
loop: "{{ image_names }}"
|
||||
|
||||
- name: Compile list of all image names and IDs
|
||||
set_fact:
|
||||
image_ids: "{{ images.results | map(attribute='image') | map(attribute='Id') | list }}"
|
||||
all_images: "{{ image_names + (images.results | map(attribute='image') | map(attribute='Id') | list) }}"
|
||||
|
||||
- name: Create archives
|
||||
command: docker save {{ item.images | join(' ') }} -o {{ output_dir }}/{{ item.file }}
|
||||
loop:
|
||||
- file: archive-1.tar
|
||||
images: "{{ image_names }}"
|
||||
- file: archive-2.tar
|
||||
images: "{{ image_ids }}"
|
||||
- file: archive-3.tar
|
||||
images:
|
||||
- "{{ image_names[0] }}"
|
||||
- "{{ image_ids[1] }}"
|
||||
- file: archive-4.tar
|
||||
images:
|
||||
- "{{ image_ids[0] }}"
|
||||
- "{{ image_names[0] }}"
|
||||
- file: archive-5.tar
|
||||
images:
|
||||
- "{{ image_ids[0] }}"
|
||||
|
||||
# All images by IDs
|
||||
|
||||
- name: Remove all images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
force_absent: true
|
||||
loop: "{{ all_images }}"
|
||||
ignore_errors: true
|
||||
register: remove_all_images
|
||||
|
||||
- name: Prune all containers (if removing failed)
|
||||
docker_prune:
|
||||
containers: true
|
||||
when: remove_all_images is failed
|
||||
|
||||
- name: Obtain all docker containers and images (if removing failed)
|
||||
shell: docker ps -a ; docker images -a
|
||||
when: remove_all_images is failed
|
||||
register: docker_container_image_list
|
||||
|
||||
- name: Show all docker containers and images (if removing failed)
|
||||
debug:
|
||||
var: docker_container_image_list.stdout_lines
|
||||
when: remove_all_images is failed
|
||||
|
||||
- name: Remove all images (after pruning)
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
force_absent: true
|
||||
loop: "{{ all_images }}"
|
||||
when: remove_all_images is failed
|
||||
|
||||
- name: Load all images (IDs)
|
||||
docker_image_load:
|
||||
path: "{{ output_dir }}/archive-2.tar"
|
||||
register: result
|
||||
|
||||
- name: Print loaded image names
|
||||
debug:
|
||||
var: result.image_names
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.image_names | sort == image_ids | sort
|
||||
- result.image_names | length == result.images | length
|
||||
|
||||
- name: Load all images (IDs, should be same result)
|
||||
docker_image_load:
|
||||
path: "{{ output_dir }}/archive-2.tar"
|
||||
register: result_2
|
||||
|
||||
- name: Print loaded image names
|
||||
debug:
|
||||
var: result_2.image_names
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result_2 is changed
|
||||
- result_2.image_names | sort == image_ids | sort
|
||||
- result_2.image_names | length == result_2.images | length
|
||||
|
||||
# Mixed images and IDs
|
||||
|
||||
- name: Remove all images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ all_images }}"
|
||||
|
||||
- name: Load all images (mixed images and IDs)
|
||||
docker_image_load:
|
||||
path: "{{ output_dir }}/archive-3.tar"
|
||||
register: result
|
||||
|
||||
- name: Print loading log
|
||||
debug:
|
||||
var: result.stdout_lines
|
||||
|
||||
- name: Print loaded image names
|
||||
debug:
|
||||
var: result.image_names
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result is changed
|
||||
# For some reason, *sometimes* only the named image is found; in fact, in that case, the log only mentions that image and nothing else
|
||||
- "result.images | length == 3 or ('Loaded image: ' ~ docker_test_image_hello_world) == result.stdout"
|
||||
- (result.image_names | sort) in [[image_names[0], image_ids[0], image_ids[1]] | sort, [image_names[0]]]
|
||||
- result.images | length in [1, 3]
|
||||
- (result.images | map(attribute='Id') | sort) in [[image_ids[0], image_ids[0], image_ids[1]] | sort, [image_ids[0]]]
|
||||
|
||||
# Same image twice
|
||||
|
||||
- name: Remove all images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ all_images }}"
|
||||
|
||||
- name: Load all images (same image twice)
|
||||
docker_image_load:
|
||||
path: "{{ output_dir }}/archive-4.tar"
|
||||
register: result
|
||||
|
||||
- name: Print loaded image names
|
||||
debug:
|
||||
var: result.image_names
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.image_names | length == 1
|
||||
- result.image_names[0] == image_names[0]
|
||||
- result.images | length == 1
|
||||
- result.images[0].Id == image_ids[0]
|
||||
|
||||
# Single image by ID
|
||||
|
||||
- name: Remove all images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ all_images }}"
|
||||
|
||||
- name: Load all images (single image by ID)
|
||||
docker_image_load:
|
||||
path: "{{ output_dir }}/archive-5.tar"
|
||||
register: result
|
||||
|
||||
- name: Print loaded image names
|
||||
debug:
|
||||
var: result.image_names
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result is changed
|
||||
- result.image_names | length == 1
|
||||
- result.image_names[0] == image_ids[0]
|
||||
- result.images | length == 1
|
||||
- result.images[0].Id == image_ids[0]
|
||||
|
||||
- name: Try to get image info by name
|
||||
docker_image_info:
|
||||
name: "{{ image_names[0] }}"
|
||||
register: result
|
||||
|
||||
- name: Make sure that image does not exist by name
|
||||
assert:
|
||||
that:
|
||||
- result.images | length == 0
|
||||
|
||||
# All images by names
|
||||
|
||||
- name: Remove all images
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop: "{{ all_images }}"
|
||||
|
||||
- name: Load all images (names)
|
||||
docker_image_load:
|
||||
path: "{{ output_dir }}/archive-1.tar"
|
||||
register: result
|
||||
|
||||
- name: Print loaded image names
|
||||
debug:
|
||||
var: result.image_names
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- result.image_names | sort == image_names | sort
|
||||
- result.image_names | length == result.images | length
|
||||
Reference in New Issue
Block a user