From 9b17d6c9df482c8b042db4ae102037bd2f910ddc Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Tue, 20 Apr 2021 12:07:15 +1000
Subject: [PATCH] collect-container-logs: don't copy on failure

After a docker failure on a recent job I noticed a single file called
"Got" in the container logs directory; turns out this had tried to
parse the error message "Got permission denied while trying to connect
to the Docker daemon socket ..." as containers.

We don't want this to fail the job hence the ignores here.  However,
we shouldn't try to collect any logs if listing the containers fails.
Move creating the directory first as that is safe.

Change-Id: I41db956964f695cfcc15e30cab8cd4f8c31d3706
---
 roles/collect-container-logs/tasks/main.yaml | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/roles/collect-container-logs/tasks/main.yaml b/roles/collect-container-logs/tasks/main.yaml
index c1cea7541..c2a612620 100644
--- a/roles/collect-container-logs/tasks/main.yaml
+++ b/roles/collect-container-logs/tasks/main.yaml
@@ -1,14 +1,14 @@
-- name: List containers
-  command: "{{ container_command }} ps -a --format '{{ '{{ .Names }}' }}'"
-  register: docker_containers
-  failed_when: false
-
 - name: Create container log dir
   file:
     path: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}"
     state: directory
     mode: 0755
 
+- name: List containers
+  command: "{{ container_command }} ps -a --format '{{ '{{ .Names }}' }}'"
+  register: docker_containers
+  failed_when: false
+
 - name: Save container logs
   loop: "{{ docker_containers.stdout_lines | default([]) }}"
   # We can't use the default 'item' because roles may be used in
@@ -19,6 +19,7 @@
   args:
     executable: /bin/bash
   failed_when: false
+  when: docker_containers.rc == 0
 
 - name: Open container logs permissions
   file: