Mohammed Naser | 336caf4 | 2022-03-11 17:56:45 -0500 | [diff] [blame] | 1 | # 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 Naser | 4badc92 | 2022-12-10 21:17:46 +0000 | [diff] [blame] | 15 | # 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 |
| 19 | vars: |
| 20 | logs_dir: /tmp/logs |
| 21 | tasks: |
| 22 | - name: End the play if the infrastructure is not deployed |
| 23 | ansible.builtin.meta: end_host |
| 24 | when: |
| 25 | - ansible_host is not defined |
| 26 | |
| 27 | - name: Retrieve all container logs, current and previous (if they exist) |
| 28 | become: true |
| 29 | shell: |- |
| 30 | set -e |
| 31 | PARALLELISM_FACTOR=4 |
| 32 | function get_namespaces () { |
| 33 | kubectl get namespaces -o name | awk -F '/' '{ print $NF }' |
| 34 | } |
| 35 | function get_pods () { |
| 36 | NAMESPACE=$1 |
| 37 | kubectl get pods -n ${NAMESPACE} -o name | awk -F '/' '{ print $NF }' | xargs -L1 -P 1 -I {} echo ${NAMESPACE} {} |
| 38 | } |
| 39 | export -f get_pods |
| 40 | function get_pod_logs () { |
| 41 | NAMESPACE=${1% *} |
| 42 | POD=${1#* } |
| 43 | INIT_CONTAINERS=$(kubectl get pod $POD -n ${NAMESPACE} -o jsonpath="{.spec.initContainers[*].name}") |
| 44 | CONTAINERS=$(kubectl get pod $POD -n ${NAMESPACE} -o jsonpath="{.spec.containers[*].name}") |
| 45 | for CONTAINER in ${INIT_CONTAINERS} ${CONTAINERS}; do |
| 46 | echo "${NAMESPACE}/${POD}/${CONTAINER}" |
| 47 | mkdir -p "{{ logs_dir }}/pod-logs/${NAMESPACE}/${POD}" |
| 48 | mkdir -p "{{ logs_dir }}/pod-logs/failed-pods/${NAMESPACE}/${POD}" |
| 49 | kubectl logs ${POD} -n ${NAMESPACE} -c ${CONTAINER} > "{{ logs_dir }}/pod-logs/${NAMESPACE}/${POD}/${CONTAINER}.txt" |
| 50 | kubectl logs --previous ${POD} -n ${NAMESPACE} -c ${CONTAINER} > "{{ logs_dir }}/pod-logs/failed-pods/${NAMESPACE}/${POD}/${CONTAINER}.txt" |
| 51 | done |
| 52 | find {{ logs_dir }} -type f -empty -print -delete |
| 53 | find {{ logs_dir }} -empty -type d -delete |
| 54 | } |
| 55 | export -f get_pod_logs |
| 56 | get_namespaces | \ |
| 57 | xargs -r -n 1 -P ${PARALLELISM_FACTOR} -I {} bash -c 'get_pods "$@"' _ {} | \ |
| 58 | xargs -r -n 2 -P ${PARALLELISM_FACTOR} -I {} bash -c 'get_pod_logs "$@"' _ {} |
| 59 | args: |
| 60 | executable: /bin/bash |
| 61 | ignore_errors: True |
| 62 | |
| 63 | - name: Upload logs to object storage |
| 64 | ignore_errors: True |
| 65 | when: |
| 66 | - lookup('env', 'GITHUB_RUN_ID') is defined |
| 67 | - lookup('env', 'GITHUB_RUN_NUMBER') is defined |
| 68 | vars: |
| 69 | build_id: "{{ lookup('env', 'GITHUB_RUN_ID') }}-{{ lookup('env', 'GITHUB_RUN_NUMBER') }}" |
| 70 | container_name: atmosphere-ci-logs |
| 71 | block: |
| 72 | - name: Authenticate to cloud to get token to use in Swift client |
| 73 | delegate_to: localhost |
| 74 | openstack.cloud.auth: |
| 75 | register: _auth |
| 76 | |
| 77 | - name: Generate storage URL |
| 78 | set_fact: |
| 79 | storage_url: "{{ ((service_catalog | selectattr('name', 'equalto', 'swift') | first).endpoints | selectattr('interface', 'equalto', 'public') | first).url }}" |
| 80 | |
| 81 | - name: Install Swift client |
| 82 | become: true |
| 83 | ansible.builtin.apt: |
| 84 | name: ['python3-swiftclient', 'tree'] |
| 85 | state: present |
| 86 | |
| 87 | - name: Generate listing for all files |
| 88 | become: true |
| 89 | shell: tree -H '.' --charset utf-8 -o {{ logs_dir }}/index.html {{ logs_dir }} |
| 90 | |
| 91 | - name: Upload logs to swift |
| 92 | shell: |- |
| 93 | set -e |
| 94 | swift post -H "X-Container-Read: .r:*,.rlistings" {{ container_name }} |
| 95 | swift upload -H "X-Delete-After: 604800" -m 'web-index:index.html' --object-name {{ build_id }} {{ container_name }} {{ logs_dir }} |
| 96 | environment: |
| 97 | OS_STORAGE_URL: "{{ storage_url }}" |
| 98 | OS_AUTH_TOKEN: "{{ auth_token }}" |
| 99 | |
| 100 | - name: Print logs URL |
| 101 | debug: |
| 102 | msg: "Logs are available at {{ storage_url }}/{{ container_name }}/{{ build_id }}/index.html" |
| 103 | |
Mohammed Naser | 336caf4 | 2022-03-11 17:56:45 -0500 | [diff] [blame] | 104 | - hosts: localhost |
| 105 | connection: local |
| 106 | gather_facts: false |
| 107 | no_log: "{{ molecule_no_log }}" |
| 108 | vars: |
Mohammed Naser | 46e1552 | 2022-03-19 16:07:44 -0400 | [diff] [blame] | 109 | workspace_path: "{{ lookup('env', 'MOLECULE_SCENARIO_DIRECTORY') }}" |
Michiel Piscaer | 97b7fd3 | 2022-03-17 12:15:21 +0100 | [diff] [blame] | 110 | |
| 111 | stack_name: "{{ lookup('env', 'ATMOSPHERE_STACK_NAME') | default('atmosphere', True) }}" |
Mohammed Naser | 336caf4 | 2022-03-11 17:56:45 -0500 | [diff] [blame] | 112 | tasks: |
| 113 | - os_stack: |
| 114 | name: "{{ stack_name }}" |
| 115 | state: absent |
| 116 | |
| 117 | - file: |
| 118 | path: "{{ molecule_instance_config }}" |
| 119 | state: absent |
Mohammed Naser | 206e5f8 | 2022-03-16 20:21:14 -0400 | [diff] [blame] | 120 | |
okozachenko | 674f9b7 | 2022-04-19 01:28:33 +1000 | [diff] [blame] | 121 | - name: Capture var files to delete |
| 122 | find: |
| 123 | paths: |
| 124 | - "{{ workspace_path }}/group_vars" |
| 125 | - "{{ workspace_path }}/host_vars" |
| 126 | file_type: file |
| 127 | recurse: true |
| 128 | excludes: |
| 129 | - "molecule.yml" |
| 130 | register: _var_files |
| 131 | |
| 132 | - name: Delete var files |
| 133 | file: |
| 134 | path: "{{ item.path }}" |
| 135 | state: absent |
| 136 | with_items: "{{ _var_files['files'] }}" |