From 228033e78a86a4fa29cc69191038e759d8755bea Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Thu, 9 Jan 2020 19:33:43 -0500 Subject: [PATCH] collect-container-logs: add role We use this in a few different places and it's really useful to collect all the logs of all containers. Change-Id: Idc46a47f444bf48cd040f4f9724f3a6ee8bc8f8e --- doc/source/container-roles.rst | 1 + roles/collect-container-logs/README.rst | 8 ++++++ .../collect-container-logs/defaults/main.yaml | 1 + roles/collect-container-logs/tasks/main.yaml | 22 ++++++++++++++++ .../registry/test-registry-post.yaml | 25 ++----------------- 5 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 roles/collect-container-logs/README.rst create mode 100644 roles/collect-container-logs/defaults/main.yaml create mode 100644 roles/collect-container-logs/tasks/main.yaml diff --git a/doc/source/container-roles.rst b/doc/source/container-roles.rst index 3a329a55f..d2d7a7f3f 100644 --- a/doc/source/container-roles.rst +++ b/doc/source/container-roles.rst @@ -3,6 +3,7 @@ Container Roles .. zuul:autorole:: build-container-image .. zuul:autorole:: build-docker-image +.. zuul:autorole:: collect-container-logs .. zuul:autorole:: deploy-openshift .. zuul:autorole:: install-docker .. zuul:autorole:: install-kubernetes diff --git a/roles/collect-container-logs/README.rst b/roles/collect-container-logs/README.rst new file mode 100644 index 000000000..52e452c4c --- /dev/null +++ b/roles/collect-container-logs/README.rst @@ -0,0 +1,8 @@ +An ansible role to collect all container logs. + +**Role Variables** + +.. zuul:rolevar:: container_command + :default: docker + + Container run-time CLI command diff --git a/roles/collect-container-logs/defaults/main.yaml b/roles/collect-container-logs/defaults/main.yaml new file mode 100644 index 000000000..1d18111b7 --- /dev/null +++ b/roles/collect-container-logs/defaults/main.yaml @@ -0,0 +1 @@ +container_command: docker \ No newline at end of file diff --git a/roles/collect-container-logs/tasks/main.yaml b/roles/collect-container-logs/tasks/main.yaml new file mode 100644 index 000000000..ef7bc9607 --- /dev/null +++ b/roles/collect-container-logs/tasks/main.yaml @@ -0,0 +1,22 @@ +- name: List containers + command: "{{ container_command }} ps -a --format '{{ '{{ .Names }}' }}'" + register: docker_containers + ignore_errors: true + +- name: Create container log dir + file: + path: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}" + state: directory + +- name: Save container logs + loop: "{{ docker_containers.stdout_lines | default([]) }}" + shell: "{{ container_command }} logs {{ item }} &> {{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}/{{ item }}.txt" + args: + executable: /bin/bash + ignore_errors: true + +- name: Open container logs permissions + file: + dest: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}" + mode: u=rwX,g=rX,o=rX + recurse: yes diff --git a/test-playbooks/registry/test-registry-post.yaml b/test-playbooks/registry/test-registry-post.yaml index 2fda35820..5242b7bf9 100644 --- a/test-playbooks/registry/test-registry-post.yaml +++ b/test-playbooks/registry/test-registry-post.yaml @@ -1,24 +1,3 @@ - hosts: all - tasks: - - name: List containers - command: "{{ container_command }} ps -a --format '{{ '{{ .Names }}' }}'" - register: docker_containers - ignore_errors: true - - - name: Create container log dir - file: - path: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}" - state: directory - - - name: Save container logs - loop: "{{ docker_containers.stdout_lines | default([]) }}" - shell: "{{ container_command }} logs {{ item }} &> {{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}/{{ item }}.txt" - args: - executable: /bin/bash - ignore_errors: true - - - name: Open container logs permissions - file: - dest: "{{ ansible_user_dir }}/zuul-output/logs/{{ container_command }}" - mode: u=rwX,g=rX,o=rX - recurse: yes + roles: + - collect-container-logs