1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Don't die when docker CLI doesn't want to work with docker daemon (#366)

* Don't die when docker CLI doesn't want to work with docker daemon.

* Allow more 'docker volume ls' failures

* More.

* Fix more failures.

* Forgot ignore_errors.

* Another one.

* More places.

* Try to install newer docker on RHEL8.

* Also restrict docker-ce-cli.
This commit is contained in:
Felix Fontein 2020-05-20 08:40:04 +02:00 committed by GitHub
parent 49f56697bb
commit 6fca49f445
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 251 additions and 181 deletions

View file

@ -53,6 +53,7 @@
- name: Inspect config - name: Inspect config
command: "docker config inspect {{ config_id }}" command: "docker config inspect {{ config_id }}"
register: inspect register: inspect
ignore_errors: yes
- debug: var=inspect - debug: var=inspect
@ -61,6 +62,11 @@
that: that:
- "'db_password' in inspect.stdout" - "'db_password' in inspect.stdout"
- "'ansible_key' in inspect.stdout" - "'ansible_key' in inspect.stdout"
when: inspect is not failed
- assert:
that:
- "'is too new. Maximum supported API version is' in inspect.stderr"
when: inspect is failed
- name: Create config again - name: Create config again
docker_config: docker_config:

View file

@ -39,17 +39,28 @@
- name: "Comparison: use 'docker inspect'" - name: "Comparison: use 'docker inspect'"
command: docker inspect "{{ cname }}" command: docker inspect "{{ cname }}"
register: docker_inspect register: docker_inspect
ignore_errors: yes
- block:
- set_fact: - set_fact:
docker_inspect_result: "{{ docker_inspect.stdout | from_json }}" docker_inspect_result: "{{ docker_inspect.stdout | from_json }}"
- name: Dump docker inspect result - name: Dump docker inspect result
debug: var=docker_inspect_result debug: var=docker_inspect_result
when: docker_inspect is not failed
- assert: - assert:
that: that:
- result.exists - result.exists
- "'container' in result" - "'container' in result"
- "result.container" - "result.container"
- assert:
that:
- "result.container == docker_inspect_result[0]" - "result.container == docker_inspect_result[0]"
when: docker_inspect is not failed
- assert:
that:
- "'is too new. Maximum supported API version is' in docker_inspect.stderr"
when: docker_inspect is failed
always: always:
- name: Cleanup - name: Cleanup

View file

@ -1,5 +1,6 @@
--- ---
- name: Log in with wrong password (check mode) - block:
- name: Log in with wrong password (check mode)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -9,7 +10,7 @@
ignore_errors: yes ignore_errors: yes
check_mode: yes check_mode: yes
- name: Log in with wrong password - name: Log in with wrong password
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -18,7 +19,7 @@
register: login_failed register: login_failed
ignore_errors: yes ignore_errors: yes
- name: Make sure that login failed - name: Make sure that login failed
assert: assert:
that: that:
- login_failed_check is failed - login_failed_check is failed
@ -26,7 +27,7 @@
- login_failed is failed - login_failed is failed
- "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed.msg" - "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed.msg"
- name: Log in (check mode) - name: Log in (check mode)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -35,7 +36,7 @@
register: login_1 register: login_1
check_mode: yes check_mode: yes
- name: Log in - name: Log in
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -43,12 +44,12 @@
state: present state: present
register: login_2 register: login_2
- name: Get permissions of ~/.docker/config.json - name: Get permissions of ~/.docker/config.json
stat: stat:
path: ~/.docker/config.json path: ~/.docker/config.json
register: login_2_stat register: login_2_stat
- name: Log in (idempotent) - name: Log in (idempotent)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -56,7 +57,7 @@
state: present state: present
register: login_3 register: login_3
- name: Log in (idempotent, check mode) - name: Log in (idempotent, check mode)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -65,7 +66,7 @@
register: login_4 register: login_4
check_mode: yes check_mode: yes
- name: Make sure that login worked - name: Make sure that login worked
assert: assert:
that: that:
- login_1 is changed - login_1 is changed
@ -74,7 +75,7 @@
- login_4 is not changed - login_4 is not changed
- login_2_stat.stat.mode == '0600' - login_2_stat.stat.mode == '0600'
- name: Log in again with wrong password (check mode) - name: Log in again with wrong password (check mode)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -84,7 +85,7 @@
ignore_errors: yes ignore_errors: yes
check_mode: yes check_mode: yes
- name: Log in again with wrong password - name: Log in again with wrong password
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
username: testuser username: testuser
@ -93,7 +94,7 @@
register: login_failed register: login_failed
ignore_errors: yes ignore_errors: yes
- name: Make sure that login failed again - name: Make sure that login failed again
assert: assert:
that: that:
- login_failed_check is failed - login_failed_check is failed
@ -101,36 +102,38 @@
- login_failed is failed - login_failed is failed
- "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed.msg" - "('login attempt to http://' ~ registry_frontend_address ~ '/v2/ failed') in login_failed.msg"
- name: Log out (check mode) - name: Log out (check mode)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
state: absent state: absent
register: logout_1 register: logout_1
check_mode: yes check_mode: yes
- name: Log out - name: Log out
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
state: absent state: absent
register: logout_2 register: logout_2
- name: Log out (idempotent) - name: Log out (idempotent)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
state: absent state: absent
register: logout_3 register: logout_3
- name: Log out (idempotent, check mode) - name: Log out (idempotent, check mode)
docker_login: docker_login:
registry_url: "{{ registry_frontend_address }}" registry_url: "{{ registry_frontend_address }}"
state: absent state: absent
register: logout_4 register: logout_4
check_mode: yes check_mode: yes
- name: Make sure that login worked - name: Make sure that login worked
assert: assert:
that: that:
- logout_1 is changed - logout_1 is changed
- logout_2 is changed - logout_2 is changed
- logout_3 is not changed - logout_3 is not changed
- logout_4 is not changed - logout_4 is not changed
when: registry_frontend_address != 'n/a'

View file

@ -36,10 +36,13 @@
- name: "Comparison: use 'docker network inspect'" - name: "Comparison: use 'docker network inspect'"
command: docker network inspect "{{ nname }}" command: docker network inspect "{{ nname }}"
register: docker_inspect register: docker_inspect
ignore_errors: yes
- block:
- set_fact: - set_fact:
docker_inspect_result: "{{ docker_inspect.stdout | from_json }}" docker_inspect_result: "{{ docker_inspect.stdout | from_json }}"
- name: Dump docker inspect result - name: Dump docker inspect result
debug: var=docker_inspect_result debug: var=docker_inspect_result
when: docker_inspect is not failed
- name: Cleanup - name: Cleanup
docker_network: docker_network:
@ -52,7 +55,15 @@
- result.exists - result.exists
- "'network' in result" - "'network' in result"
- "result.network" - "result.network"
- assert:
that:
- "result.network == docker_inspect_result[0]" - "result.network == docker_inspect_result[0]"
when: docker_inspect is not failed
- assert:
that:
- "'is too new. Maximum supported API version is' in docker_inspect.stderr"
when: docker_inspect is failed
when: docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.21', '>=') when: docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.21', '>=')

View file

@ -18,6 +18,7 @@
become: yes become: yes
- name: Wait for docker daemon to be fully restarted - name: Wait for docker daemon to be fully restarted
command: docker ps command: docker ps
ignore_errors: yes
- name: Cleanup - name: Cleanup
docker_swarm: docker_swarm:

View file

@ -49,6 +49,7 @@
- name: Inspect secret - name: Inspect secret
command: "docker secret inspect {{ secret_id }}" command: "docker secret inspect {{ secret_id }}"
register: inspect register: inspect
ignore_errors: yes
- debug: var=inspect - debug: var=inspect
@ -57,6 +58,11 @@
that: that:
- "'db_password' in inspect.stdout" - "'db_password' in inspect.stdout"
- "'ansible_key' in inspect.stdout" - "'ansible_key' in inspect.stdout"
when: inspect is not failed
- assert:
that:
- "'is too new. Maximum supported API version is' in inspect.stderr"
when: inspect is failed
- name: Create secret again - name: Create secret again
docker_secret: docker_secret:

View file

@ -34,10 +34,13 @@
- name: "Comparison: use 'docker volume inspect'" - name: "Comparison: use 'docker volume inspect'"
command: docker volume inspect "{{ cname }}" command: docker volume inspect "{{ cname }}"
register: docker_volume_inspect register: docker_volume_inspect
ignore_errors: yes
- block:
- set_fact: - set_fact:
docker_volume_inspect_result: "{{ docker_volume_inspect.stdout | from_json }}" docker_volume_inspect_result: "{{ docker_volume_inspect.stdout | from_json }}"
- name: Dump docker volume inspect result - name: Dump docker volume inspect result
debug: var=docker_volume_inspect_result debug: var=docker_volume_inspect_result
when: docker_volume_inspect is not failed
- name: Cleanup - name: Cleanup
docker_volume: docker_volume:
@ -49,7 +52,15 @@
- result.exists - result.exists
- "'volume' in result" - "'volume' in result"
- "result.volume" - "result.volume"
- assert:
that:
- "result.volume == docker_volume_inspect_result[0]" - "result.volume == docker_volume_inspect_result[0]"
when: docker_volume_inspect is not failed
- assert:
that:
- "'is too new. Maximum supported API version is' in docker_volume_inspect.stderr"
when: docker_volume_inspect is failed
# Requirements for docker_volume # Requirements for docker_volume
when: docker_py_version is version('1.10.0', '>=') and docker_api_version is version('1.24', '>=') when: docker_py_version is version('1.10.0', '>=') and docker_api_version is version('1.24', '>=')

View file

@ -66,49 +66,56 @@
retries: 3 retries: 3
delay: 3 delay: 3
until: docker_containers is success until: docker_containers is success
ignore_errors: yes
- name: "Remove all ansible-test-* docker volumes" - name: "Remove all ansible-test-* docker volumes"
shell: 'docker volume ls --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-test-" | xargs -r docker volume rm -f' shell: 'docker volume ls --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-test-" | xargs -r docker volume rm -f'
register: docker_volumes register: docker_volumes
ignore_errors: yes
- name: "Remove all ansible-test-* docker networks" - name: "Remove all ansible-test-* docker networks"
shell: 'docker network ls --no-trunc --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-test-" | xargs -r docker network rm' shell: 'docker network ls --no-trunc --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-test-" | xargs -r docker network rm'
register: docker_networks register: docker_networks
ignore_errors: yes
- name: Cleaned docker resources - name: Cleaned docker resources
debug: debug:
var: docker_resources var: docker_resources
vars: vars:
docker_resources: docker_resources:
containers: "{{ docker_containers.stdout_lines }}" containers: "{{ docker_containers.stdout_lines | default([]) }}"
volumes: "{{ docker_volumes.stdout_lines }}" volumes: "{{ docker_volumes.stdout_lines | default([]) }}"
networks: "{{ docker_networks.stdout_lines }}" networks: "{{ docker_networks.stdout_lines | default([]) }}"
# List all existing docker resources # List all existing docker resources
- name: List all docker containers - name: List all docker containers
command: docker ps --no-trunc -a command: docker ps --no-trunc -a
register: docker_containers register: docker_containers
ignore_errors: yes
- name: List all docker volumes - name: List all docker volumes
command: docker volume ls command: docker volume ls
register: docker_volumes register: docker_volumes
ignore_errors: yes
- name: List all docker networks - name: List all docker networks
command: docker network ls --no-trunc command: docker network ls --no-trunc
register: docker_networks register: docker_networks
ignore_errors: yes
- name: List all docker images - name: List all docker images
command: docker images --no-trunc -a command: docker images --no-trunc -a
register: docker_images register: docker_images
ignore_errors: yes
- name: Still existing docker resources - name: Still existing docker resources
debug: debug:
var: docker_resources var: docker_resources
vars: vars:
docker_resources: docker_resources:
containers: "{{ docker_containers.stdout_lines }}" containers: "{{ docker_containers.stdout_lines | default([]) }}"
volumes: "{{ docker_volumes.stdout_lines }}" volumes: "{{ docker_volumes.stdout_lines | default([]) }}"
networks: "{{ docker_networks.stdout_lines }}" networks: "{{ docker_networks.stdout_lines | default([]) }}"
images: "{{ docker_images.stdout_lines }}" images: "{{ docker_images.stdout_lines | default([]) }}"
when: docker_cli_version is version('0.0', '>') when: docker_cli_version is version('0.0', '>')

View file

@ -7,3 +7,4 @@ docker_prereq_packages:
# Docker CE > 3:18.09.1 requires containerd.io >= 1.2.2-3 which is unavaible at this time # Docker CE > 3:18.09.1 requires containerd.io >= 1.2.2-3 which is unavaible at this time
docker_packages: docker_packages:
- docker-ce-3:18.09.1 - docker-ce-3:18.09.1
- docker-ce-cli-1:18.09.1 # otherwise 19.03.9 is installed, which won't work with docker daemon 18.09.1

View file

@ -7,14 +7,20 @@
command: "docker logs {{ registry_name }}" command: "docker logs {{ registry_name }}"
register: registry_logs register: registry_logs
no_log: yes no_log: yes
ignore_errors: yes
- name: "Printing registry logs" - name: "Printing registry logs"
debug: var=registry_logs.stdout_lines debug:
var: registry_logs.stdout_lines
when: registry_logs is not failed
- name: "Get nginx logs" - name: "Get nginx logs"
command: "docker logs {{ nginx_name }}" command: "docker logs {{ nginx_name }}"
register: nginx_logs register: nginx_logs
no_log: yes no_log: yes
ignore_errors: yes
- name: "Printing nginx logs" - name: "Printing nginx logs"
debug: var=nginx_logs.stdout_lines debug:
var: nginx_logs.stdout_lines
when: nginx_logs is not failed
- name: "Make sure all containers are removed" - name: "Make sure all containers are removed"
docker_container: docker_container:
name: "{{ item }}" name: "{{ item }}"

View file

@ -52,6 +52,9 @@
loop: loop:
- nginx.conf - nginx.conf
- nginx.htpasswd - nginx.htpasswd
register: can_copy_files
ignore_errors: yes
- block:
- name: Create private key for frontend certificate - name: Create private key for frontend certificate
community.crypto.openssl_privatekey: community.crypto.openssl_privatekey:
path: '{{ output_dir }}/cert.key' path: '{{ output_dir }}/cert.key'
@ -93,6 +96,10 @@
- name: Get registry URL - name: Get registry URL
set_fact: set_fact:
registry_frontend_address: localhost:{{ nginx_container.container.NetworkSettings.Ports['5000/tcp'].0.HostPort }} registry_frontend_address: localhost:{{ nginx_container.container.NetworkSettings.Ports['5000/tcp'].0.HostPort }}
when: can_copy_files is not failed
- set_fact:
registry_frontend_address: 'n/a'
when: can_copy_files is failed
- debug: msg="Registry available under {{ registry_address }}, NGINX frontend available under {{ registry_frontend_address }}" - debug: msg="Registry available under {{ registry_address }}, NGINX frontend available under {{ registry_frontend_address }}"
when: docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.20', '>=') when: docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.20', '>=')
- fail: msg="Too old docker / docker-py version to run docker_image tests!" - fail: msg="Too old docker / docker-py version to run docker_image tests!"