blob: 71c277dd6b7bbf784fe777187da7feb203feffc1 [file] [log] [blame]
Mohammed Naser336caf42022-03-11 17:56:45 -05001# Copyright (c) 2022 VEXXHOST, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Mohammed Naser4badc922022-12-10 21:17:46 +000015# NOTE(mnaser): We're using this playbook to capture logs for the run since
16# there is no Molecule phase that runs after the converge phase.
17- hosts: controllers[0]
18 gather_facts: false
okozachenko120376e81f82022-12-25 02:11:20 +110019 ignore_unreachable: true
Mohammed Naser4badc922022-12-10 21:17:46 +000020 vars:
21 logs_dir: /tmp/logs
22 tasks:
23 - name: End the play if the infrastructure is not deployed
24 ansible.builtin.meta: end_host
25 when:
26 - ansible_host is not defined
27
28 - name: Retrieve all container logs, current and previous (if they exist)
29 become: true
30 shell: |-
31 set -e
32 PARALLELISM_FACTOR=4
33 function get_namespaces () {
34 kubectl get namespaces -o name | awk -F '/' '{ print $NF }'
35 }
36 function get_pods () {
37 NAMESPACE=$1
38 kubectl get pods -n ${NAMESPACE} -o name | awk -F '/' '{ print $NF }' | xargs -L1 -P 1 -I {} echo ${NAMESPACE} {}
39 }
40 export -f get_pods
41 function get_pod_logs () {
42 NAMESPACE=${1% *}
43 POD=${1#* }
44 INIT_CONTAINERS=$(kubectl get pod $POD -n ${NAMESPACE} -o jsonpath="{.spec.initContainers[*].name}")
45 CONTAINERS=$(kubectl get pod $POD -n ${NAMESPACE} -o jsonpath="{.spec.containers[*].name}")
46 for CONTAINER in ${INIT_CONTAINERS} ${CONTAINERS}; do
47 echo "${NAMESPACE}/${POD}/${CONTAINER}"
48 mkdir -p "{{ logs_dir }}/pod-logs/${NAMESPACE}/${POD}"
49 mkdir -p "{{ logs_dir }}/pod-logs/failed-pods/${NAMESPACE}/${POD}"
50 kubectl logs ${POD} -n ${NAMESPACE} -c ${CONTAINER} > "{{ logs_dir }}/pod-logs/${NAMESPACE}/${POD}/${CONTAINER}.txt"
51 kubectl logs --previous ${POD} -n ${NAMESPACE} -c ${CONTAINER} > "{{ logs_dir }}/pod-logs/failed-pods/${NAMESPACE}/${POD}/${CONTAINER}.txt"
52 done
53 find {{ logs_dir }} -type f -empty -print -delete
54 find {{ logs_dir }} -empty -type d -delete
55 }
56 export -f get_pod_logs
57 get_namespaces | \
58 xargs -r -n 1 -P ${PARALLELISM_FACTOR} -I {} bash -c 'get_pods "$@"' _ {} | \
59 xargs -r -n 2 -P ${PARALLELISM_FACTOR} -I {} bash -c 'get_pod_logs "$@"' _ {}
60 args:
61 executable: /bin/bash
62 ignore_errors: True
63
64 - name: Upload logs to object storage
65 ignore_errors: True
66 when:
Michiel Piscaer7d40bf22022-12-16 08:15:31 +010067 - lookup('env', 'GITHUB_RUN_ID')|length > 0
68 - lookup('env', 'GITHUB_RUN_NUMBER')|length > 0
Mohammed Naser4badc922022-12-10 21:17:46 +000069 vars:
70 build_id: "{{ lookup('env', 'GITHUB_RUN_ID') }}-{{ lookup('env', 'GITHUB_RUN_NUMBER') }}"
71 container_name: atmosphere-ci-logs
72 block:
73 - name: Authenticate to cloud to get token to use in Swift client
74 delegate_to: localhost
75 openstack.cloud.auth:
76 register: _auth
77
78 - name: Generate storage URL
79 set_fact:
80 storage_url: "{{ ((service_catalog | selectattr('name', 'equalto', 'swift') | first).endpoints | selectattr('interface', 'equalto', 'public') | first).url }}"
81
82 - name: Install Swift client
83 become: true
84 ansible.builtin.apt:
85 name: ['python3-swiftclient', 'tree']
86 state: present
87
88 - name: Generate listing for all files
89 become: true
90 shell: tree -H '.' --charset utf-8 -o {{ logs_dir }}/index.html {{ logs_dir }}
91
92 - name: Upload logs to swift
93 shell: |-
94 set -e
95 swift post -H "X-Container-Read: .r:*,.rlistings" {{ container_name }}
96 swift upload -H "X-Delete-After: 604800" -m 'web-index:index.html' --object-name {{ build_id }} {{ container_name }} {{ logs_dir }}
97 environment:
98 OS_STORAGE_URL: "{{ storage_url }}"
99 OS_AUTH_TOKEN: "{{ auth_token }}"
100
101 - name: Print logs URL
102 debug:
103 msg: "Logs are available at {{ storage_url }}/{{ container_name }}/{{ build_id }}/index.html"
104
Mohammed Naser336caf42022-03-11 17:56:45 -0500105- hosts: localhost
106 connection: local
107 gather_facts: false
108 no_log: "{{ molecule_no_log }}"
109 vars:
Mohammed Naser46e15522022-03-19 16:07:44 -0400110 workspace_path: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}"
Michiel Piscaer97b7fd32022-03-17 12:15:21 +0100111
112 stack_name: "{{ lookup('env', 'ATMOSPHERE_STACK_NAME') | default('atmosphere', True) }}"
Mohammed Naser336caf42022-03-11 17:56:45 -0500113 tasks:
114 - os_stack:
115 name: "{{ stack_name }}"
116 state: absent
117
118 - file:
119 path: "{{ molecule_instance_config }}"
120 state: absent
Mohammed Naser206e5f82022-03-16 20:21:14 -0400121
okozachenko674f9b72022-04-19 01:28:33 +1000122 - name: Capture var files to delete
123 find:
124 paths:
125 - "{{ workspace_path }}/group_vars"
126 - "{{ workspace_path }}/host_vars"
127 file_type: file
128 recurse: true
129 excludes:
130 - "molecule.yml"
131 register: _var_files
132
133 - name: Delete var files
134 file:
135 path: "{{ item.path }}"
136 state: absent
137 with_items: "{{ _var_files['files'] }}"