This repository has been archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.yaml
110 lines (99 loc) · 3.66 KB
/
main.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
- name: Register sources
stat:
path: "{{ item.key }}"
with_dict: "{{ zuul_copy_output }}"
register: sources
no_log: true
- name: Output a warning when input is not a dict and not empty
debug:
msg: "WARNING: extensions_to_txt is a list, values defined by parents will be overwritten"
when:
- extensions_to_txt is not mapping
- extensions_to_txt
- name: Build the extensions list when input is not a dict (including empty)
set_fact:
extension_list: >
{% set extensions = ['__does_not_match__'] -%}
{% if extensions_to_txt -%}
{% set extensions = extensions_to_txt -%}
{% endif -%}
{{- extensions -}}
when: extensions_to_txt is not mapping
- name: Build the extensions list when input is a dict
set_fact:
extension_list: >
{% set extensions = [] -%}
{% for extension, extension_bool in extensions_to_txt.items() -%}
{% if extension_bool -%}
{% set _ = extensions.append(extension) -%}
{% endif -%}
{% endfor -%}
{{- extensions -}}
when: extensions_to_txt is mapping
- name: Build the extensions regular expression
set_fact:
extensions_regex: "^(.*)\\.({{ extension_list | join('|') }})$"
# TODO(andreaf) We might want to enforce that item.value is a valid value
# in docs, artifacts, logs. Null case already handled.
# NOTE(andreaf) Files or folders that start with a '.' are renamed to starting
# with an '_' else they would not be visible in the logs folder once uploaded.
# Extension changes are handled later via find as we want to rename files
# included of folders specified in `zuul_copy_output`.
- name: Set source and destination for files and folders
set_fact:
source: "{{ item.stat.path }}"
dest: "{{ item.item.value }}/{{ item.stat.path|basename|regex_replace('^(\\..*)$', '_\\1') }}"
type: "{{ item.item.value }}"
with_items: "{{ sources.results }}"
when:
- item.stat.exists
- item.item.value
register: results
no_log: true
- name: Build a list of source, dest dictionaries
set_fact:
all_sources: "{{ results.results | selectattr('ansible_facts', 'defined') | map(attribute='ansible_facts') | list }}"
- name: Ensure target folders exist
become: true
file:
path: "{{ stage_dir }}/{{ item }}"
state: directory
owner: "{{ ansible_user }}"
with_items:
- docs
- artifacts
- logs
- name: Copy files and folders to staging folder
# remote_src copy does not work recursively, synchronise is restricted by
# zuul, using command
command: cp -pRL {{ item.source}} {{ stage_dir }}/{{ item.dest }}
with_items: "{{ all_sources }}"
- name: Discover log files that match extension_list
find:
paths: "{{ stage_dir }}/logs"
patterns: "{{ extensions_regex }}"
use_regex: true
recurse: true
file_type: 'file'
register: log_files_to_rename
- name: Rename log files that match extension_list
shell: "mv {{ item.path }} {{ item.path | regex_replace(extensions_regex, '\\1_\\2.txt') }}"
with_items: "{{ log_files_to_rename.files }}"
chdir: "{{ stage_dir }}/logs"
tags:
- skip_ansible_lint
# NOTE(andreaf) The ansible module does not support recursive archive, so
# using gzip is the only option here. The good bit is that gzip itself is
# almost idempotent, as it will not compress again files with .gz extension.
# gzip will however return 1 if any compressed file is encountered, so we
# must ignore that (there's no specific error code).
- name: Archive everything from docs sources
shell: gzip --recursive --best {{ item.dest }} || true
args:
chdir: "{{ stage_dir }}"
with_items: "{{ all_sources }}"
when:
- stage_compress_logs
- item.type == 'logs'
tags:
- skip_ansible_lint