Add docker_compose_v2_pull module (#751)

* Add docker_compose_v2_pull module.

* Improve and extend parsing of events.

* Add ignores.

* --policy is only available since Compose 2.22.0.
This commit is contained in:
Felix Fontein
2024-01-13 14:36:26 +01:00
committed by GitHub
parent 8ca5e2f810
commit 307dc4045a
12 changed files with 821 additions and 58 deletions
@@ -0,0 +1,6 @@
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
azp/4
destructive
@@ -0,0 +1,10 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
dependencies:
- setup_docker_cli_compose
# The Python dependencies are needed for the other modules
- setup_docker_python_deps
- setup_remote_tmp_dir
@@ -0,0 +1,48 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################
# Create random name prefix (for services, ...)
- name: Create random container name prefix
set_fact:
name_prefix: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}"
cnames: []
dnetworks: []
- debug:
msg: "Using name prefix {{ name_prefix }}"
# Run the tests
- block:
- command: docker compose --help
- include_tasks: run-test.yml
with_fileglob:
- "tests/*.yml"
loop_control:
loop_var: test_name
always:
- name: "Make sure all containers are removed"
docker_container:
name: "{{ item }}"
state: absent
force_kill: true
with_items: "{{ cnames }}"
diff: false
- name: "Make sure all networks are removed"
docker_network:
name: "{{ item }}"
state: absent
force: true
with_items: "{{ dnetworks }}"
diff: false
when: docker_has_compose and docker_compose_version is version('2.18.0', '>=')
@@ -0,0 +1,7 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: "Loading tasks from {{ test_name }}"
include_tasks: "{{ test_name }}"
@@ -0,0 +1,187 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- vars:
pname: "{{ name_prefix }}-pull"
cname: "{{ name_prefix }}-cont"
non_existing_image: does-not-exist:latest
project_src: "{{ remote_tmp_dir }}/{{ pname }}"
test_service_non_existing: |
version: '3'
services:
{{ cname }}:
image: {{ non_existing_image }}
test_service_alpine: |
version: '3'
services:
{{ cname }}:
image: {{ docker_test_image_alpine }}
command: /bin/sh -c 'sleep 10m'
stop_grace_period: 1s
block:
- name: Registering container name
set_fact:
cnames: "{{ cnames + [pname ~ '-' ~ cname ~ '-1'] }}"
dnetworks: "{{ dnetworks + [pname ~ '_default'] }}"
- name: Create project directory
file:
path: '{{ project_src }}'
state: directory
- name: Make sure images are not around
docker_image_remove:
name: '{{ item }}'
loop:
- '{{ non_existing_image }}'
- '{{ docker_test_image_alpine }}'
####################################################################
## Missing image ###################################################
####################################################################
- name: Template project file with non-existing image
copy:
dest: '{{ project_src }}/docker-compose.yml'
content: '{{ test_service_non_existing }}'
- name: Pull (check)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
check_mode: true
register: pull_1_check
ignore_errors: true
- name: Pull
docker_compose_v2_pull:
project_src: '{{ project_src }}'
register: pull_1
ignore_errors: true
- assert:
that:
- pull_1_check is failed or pull_1_check is changed
- pull_1_check is changed or pull_1_check.msg.startswith('Error when processing ')
- pull_1 is failed
- pull_1.msg.startswith('Error when processing ')
####################################################################
## Regular image ###################################################
####################################################################
- name: Template project file with Alpine image
copy:
dest: '{{ project_src }}/docker-compose.yml'
content: '{{ test_service_alpine }}'
- when: docker_compose_version is version('2.22.0', '>=')
block:
- name: Pull with policy=missing (check)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: missing
check_mode: true
register: pull_1_check
- name: Pull with policy=missing
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: missing
register: pull_1
- name: Pull with policy=missing (idempotent, check)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: missing
check_mode: true
register: pull_2_check
- name: Pull with policy=missing (idempotent)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: missing
register: pull_2
- name: Pull with policy=always (check)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: always
check_mode: true
register: pull_3_check
- name: Pull with policy=always
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: always
register: pull_3
- assert:
that:
- pull_1_check is changed
- pull_1_check.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_1_check.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_1_check.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- pull_1 is changed
- pull_1.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_1.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_1.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- pull_2_check is not changed
- pull_2 is not changed
- pull_3_check is changed
- pull_3_check.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_3_check.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_3_check.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- pull_3 is changed
- pull_3.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_3.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_3.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- when: docker_compose_version is version('2.22.0', '<')
block:
- name: Pull with policy=always (check)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: always
check_mode: true
register: pull_1_check
- name: Pull with policy=always
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: always
register: pull_1
- name: Pull with policy=always (again, check)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: always
check_mode: true
register: pull_2_check
- name: Pull with policy=always (again)
docker_compose_v2_pull:
project_src: '{{ project_src }}'
policy: always
register: pull_2
- assert:
that:
- pull_1_check is changed
- pull_1_check.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_1_check.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_1_check.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- pull_1 is changed
- pull_1.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_1.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_1.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- pull_2_check is changed
- pull_2_check.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_2_check.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_2_check.actions | selectattr('status', 'eq', 'Recreating') | length == 0
- pull_2 is changed
- pull_2.actions | selectattr('status', 'eq', 'Pulling') | first
- pull_2.actions | selectattr('status', 'eq', 'Creating') | length == 0
- pull_2.actions | selectattr('status', 'eq', 'Recreating') | length == 0
+1
View File
@@ -8,5 +8,6 @@ plugins/modules/current_container_facts.py validate-modules:return-syntax-error
plugins/module_utils/module_container/module.py compile-2.6!skip # Uses Python 2.7+ syntax
plugins/module_utils/module_container/module.py import-2.6!skip # Uses Python 2.7+ syntax
plugins/modules/docker_compose_v2.py validate-modules:return-syntax-error
plugins/modules/docker_compose_v2_pull.py validate-modules:return-syntax-error
plugins/modules/docker_container.py import-2.6!skip # Import uses Python 2.7+ syntax
plugins/modules/docker_container_copy_into.py validate-modules:undocumented-parameter # _max_file_size_for_diff is used by the action plugin
+1
View File
@@ -8,5 +8,6 @@ plugins/modules/current_container_facts.py validate-modules:return-syntax-error
plugins/module_utils/module_container/module.py compile-2.6!skip # Uses Python 2.7+ syntax
plugins/module_utils/module_container/module.py import-2.6!skip # Uses Python 2.7+ syntax
plugins/modules/docker_compose_v2.py validate-modules:return-syntax-error
plugins/modules/docker_compose_v2_pull.py validate-modules:return-syntax-error
plugins/modules/docker_container.py import-2.6!skip # Import uses Python 2.7+ syntax
plugins/modules/docker_container_copy_into.py validate-modules:undocumented-parameter # _max_file_size_for_diff is used by the action plugin
+1
View File
@@ -1,4 +1,5 @@
.azure-pipelines/scripts/publish-codecov.py replace-urlopen
plugins/modules/current_container_facts.py validate-modules:return-syntax-error
plugins/modules/docker_compose_v2.py validate-modules:return-syntax-error
plugins/modules/docker_compose_v2_pull.py validate-modules:return-syntax-error
plugins/modules/docker_container_copy_into.py validate-modules:undocumented-parameter # _max_file_size_for_diff is used by the action plugin
+1
View File
@@ -1,3 +1,4 @@
.azure-pipelines/scripts/publish-codecov.py replace-urlopen
plugins/modules/docker_compose_v2.py validate-modules:return-syntax-error
plugins/modules/docker_compose_v2_pull.py validate-modules:return-syntax-error
plugins/modules/docker_container_copy_into.py validate-modules:undocumented-parameter # _max_file_size_for_diff is used by the action plugin
+1
View File
@@ -7,5 +7,6 @@
plugins/module_utils/module_container/module.py compile-2.6!skip # Uses Python 2.7+ syntax
plugins/module_utils/module_container/module.py import-2.6!skip # Uses Python 2.7+ syntax
plugins/modules/docker_compose_v2.py validate-modules:return-syntax-error
plugins/modules/docker_compose_v2_pull.py validate-modules:return-syntax-error
plugins/modules/docker_container.py import-2.6!skip # Import uses Python 2.7+ syntax
plugins/modules/docker_container_copy_into.py validate-modules:undocumented-parameter # _max_file_size_for_diff is used by the action plugin