3
3
# This library holds functions that are used to clean up local
4
4
# system state after other scripts have run.
5
5
6
+ # os::cleanup::all will clean up all of the processes and data that
7
+ # a script leaves around after running. All of the sub-tasks called
8
+ # from this function should gracefully handle when they do not need
9
+ # to do anything.
10
+ #
11
+ # Globals:
12
+ # - ARTIFACT_DIR
13
+ # - SKIP_TEARDOWN
14
+ # - SKIP_IMAGE_CLEANUP
15
+ # Arguments:
16
+ # 1 - return code of the script
17
+ # Returns:
18
+ # None
19
+ function os::cleanup::all() {
20
+ local return_code=" $1 "
21
+
22
+ # All of our cleanup is best-effort, so we do not care
23
+ # if any specific step fails.
24
+ set +o errexit
25
+
26
+ os::test::junit::generate_report
27
+
28
+ os::log::info " [CLEANUP] Beginning cleanup routines..."
29
+ os::cleanup::dump_events
30
+ os::cleanup::dump_etcd
31
+ os::cleanup::dump_container_logs
32
+ os::cleanup::dump_pprof_output
33
+ os::cleanup::find_cache_alterations
34
+ os::cleanup::truncate_large_logs
35
+
36
+ if [[ -z " ${SKIP_TEARDOWN:- } " ]]; then
37
+ os::cleanup::containers
38
+ os::cleanup::processes
39
+ os::cleanup::prune_etcd
40
+ fi
41
+ os::util::describe_return_code " ${return_code} "
42
+ }
43
+ readonly -f os::cleanup::all
44
+
6
45
# os::cleanup::dump_etcd dumps the full contents of etcd to a file.
7
46
#
8
47
# Globals:
9
- # ARTIFACT_DIR
48
+ # - ARTIFACT_DIR
49
+ # - API_SCHEME
50
+ # - API_HOST
51
+ # - ETCD_PORT
10
52
# Arguments:
11
53
# None
12
54
# Returns:
13
55
# None
14
56
function os::cleanup::dump_etcd() {
15
- os::log::info " Dumping etcd contents to ${ARTIFACT_DIR} /etcd_dump.json"
16
- os::util::curl_etcd " /v2/keys/?recursive=true" > " ${ARTIFACT_DIR} /etcd_dump.json"
57
+ if [[ -n " ${API_SCHEME:- } " && -n " ${API_HOST:- } " && -n " ${ETCD_PORT:- } " ]]; then
58
+ os::log::info " [CLEANUP] Dumping etcd contents to $( os::util::repository_relative_path " ${ARTIFACT_DIR} /etcd_dump.json" ) "
59
+ os::util::curl_etcd " /v2/keys/?recursive=true" > " ${ARTIFACT_DIR} /etcd_dump.json"
60
+ fi
17
61
}
62
+ readonly -f os::cleanup::dump_etcd
63
+
64
+ # os::cleanup::prune_etcd removes the etcd data store from disk.
65
+ #
66
+ # Globals:
67
+ # ARTIFACT_DIR
68
+ # Arguments:
69
+ # None
70
+ # Returns:
71
+ # None
72
+ function os::cleanup::prune_etcd() {
73
+ if [[ -n " ${ETCD_DATA_DIR:- } " ]]; then
74
+ os::log::info " [CLEANUP] Pruning etcd data directory"
75
+ ${USE_SUDO: +sudo} rm -rf " ${ETCD_DATA_DIR} "
76
+ fi
77
+ }
78
+ readonly -f os::cleanup::prune_etcd
18
79
19
80
# os::cleanup::containers operates on our containers to stop the containers
20
81
# and optionally remove the containers and any volumes they had attached.
@@ -27,11 +88,11 @@ function os::cleanup::dump_etcd() {
27
88
# None
28
89
function os::cleanup::containers() {
29
90
if ! os::util::find::system_binary docker > /dev/null 2>&1 ; then
30
- os::log::warning " No \` docker\` binary found, skipping container cleanup."
91
+ os::log::warning " [CLEANUP] No \` docker\` binary found, skipping container cleanup."
31
92
return
32
93
fi
33
94
34
- os::log::info " Stopping docker containers"
95
+ os::log::info " [CLEANUP] Stopping docker containers"
35
96
for id in $( os::cleanup::internal::list_our_containers ) ; do
36
97
os::log::debug " Stopping ${id} "
37
98
docker stop " ${id} " > /dev/null
@@ -41,7 +102,7 @@ function os::cleanup::containers() {
41
102
return
42
103
fi
43
104
44
- os::log::info " Removing docker containers"
105
+ os::log::info " [CLEANUP] Removing docker containers"
45
106
for id in $( os::cleanup::internal::list_our_containers ) ; do
46
107
os::log::debug " Removing ${id} "
47
108
docker stop " ${id} " > /dev/null
@@ -60,14 +121,14 @@ readonly -f os::cleanup::containers
60
121
# None
61
122
function os::cleanup::dump_container_logs() {
62
123
if ! os::util::find::system_binary docker > /dev/null 2>&1 ; then
63
- os::log::warning " No \` docker\` binary found, skipping container cleanup ."
124
+ os::log::warning " [CLEANUP] No \` docker\` binary found, skipping container log harvest ."
64
125
return
65
126
fi
66
127
67
128
local container_log_dir=" ${LOG_DIR} /containers"
68
129
mkdir -p " ${container_log_dir} "
69
130
70
- os::log::info " Dumping container logs to ${container_log_dir} "
131
+ os::log::info " [CLEANUP] Dumping container logs to $( os::util::repository_relative_path " $ {container_log_dir}" ) "
71
132
for id in $( os::cleanup::internal::list_our_containers ) ; do
72
133
local name; name=" $( docker inspect --format ' {{ .Name }}' " ${id} " ) "
73
134
os::log::debug " Dumping logs for ${id} to ${name} .log"
@@ -138,6 +199,7 @@ readonly -f os::cleanup::internal::list_containers
138
199
# Returns:
139
200
# None
140
201
function os::cleanup::tmpdir() {
202
+ os::log::info " [CLEANUP] Cleaning up temporary directories"
141
203
# ensure that the directories are clean
142
204
if os::util::find::system_binary " findmnt" & > /dev/null; then
143
205
for target in $( ${USE_SUDO: +sudo} findmnt --output TARGET --list ) ; do
@@ -163,11 +225,87 @@ readonly -f os::cleanup::tmpdir
163
225
# Returns:
164
226
# None
165
227
function os::cleanup::dump_events() {
166
- os::log::info " Dumping cluster events to ${ARTIFACT_DIR} /events.txt"
228
+ os::log::info " [CLEANUP] Dumping cluster events to $( os::util::repository_relative_path " $ {ARTIFACT_DIR} /events.txt" ) "
167
229
local kubeconfig
168
- if [[ -n " ${ADMIN_KUBECONFIG} " ]]; then
230
+ if [[ -n " ${ADMIN_KUBECONFIG:- } " ]]; then
169
231
kubeconfig=" --config=${ADMIN_KUBECONFIG} "
170
232
fi
171
- oc get events --all-namespaces ${kubeconfig} > " ${ARTIFACT_DIR} /events.txt"
233
+ oc get events --all-namespaces ${kubeconfig:- } > " ${ARTIFACT_DIR} /events.txt" 2>&1
172
234
}
173
235
readonly -f os::cleanup::dump_events
236
+
237
+ # os::cleanup::find_cache_alterations ulls information out of the server
238
+ # log so that we can get failure management in jenkins to highlight it
239
+ # and really have it smack people in their logs. This is a severe
240
+ # correctness problem.
241
+ #
242
+ # Globals:
243
+ # - LOG_DIR
244
+ # Arguments:
245
+ # None
246
+ # Returns:
247
+ # None
248
+ function os::cleanup::find_cache_alterations() {
249
+ grep -ra5 " CACHE.*ALTERED" " ${LOG_DIR} " || true
250
+ }
251
+ readonly -f os::cleanup::find_cache_alterations
252
+
253
+ # os::cleanup::dump_pprof_output dumps profiling output for the
254
+ # `openshift` binary
255
+ #
256
+ # Globals:
257
+ # - LOG_DIR
258
+ # Arguments:
259
+ # None
260
+ # Returns:
261
+ # None
262
+ function os::cleanup::dump_pprof_output() {
263
+ if go tool -n pprof > /dev/null 2>&1 && [[ -s cpu.pprof ]]; then
264
+ os::log::info " [CLEANUP] \` pprof\` output logged to $( os::util::repository_relative_path " ${LOG_DIR} /pprof.out" ) "
265
+ go tool pprof -text " ./_output/local/bin/$( os::util::host_platform) /openshift" cpu.pprof > " ${LOG_DIR} /pprof.out" 2>&1
266
+ fi
267
+ }
268
+ readonly -f os::cleanup::dump_pprof_output
269
+
270
+ # os::cleanup::truncate_large_logs truncates very large files under
271
+ # $LOG_DIR and $ARTIFACT_DIR so we do not upload them to cloud storage
272
+ # after CI runs.
273
+ #
274
+ # Globals:
275
+ # - LOG_DIR
276
+ # - ARTIFACT_DIR
277
+ # Arguments:
278
+ # None
279
+ # Returns:
280
+ # None
281
+ function os::cleanup::truncate_large_logs() {
282
+ local max_file_size=" 100M"
283
+ os::log::info " [CLEANUP] Truncating log files over ${max_file_size} "
284
+ for file in $( find " ${ARTIFACT_DIR} " " ${LOG_DIR} " -type f -name ' *.log' \( -size +${max_file_size} \) ) ; do
285
+ mv " ${file} " " ${file} .tmp"
286
+ echo " LOGFILE TOO LONG ($( du -h " ${file} .tmp" ) ), PREVIOUS BYTES TRUNCATED. LAST ${max_file_size} OF LOGFILE:" > " ${file} "
287
+ tail -c ${max_file_size} " ${file} .tmp" >> " ${file} "
288
+ rm " ${file} .tmp"
289
+ done
290
+ }
291
+ readonly -f os::cleanup::truncate_large_logs
292
+
293
+ # os::cleanup::processes kills all processes created by the test
294
+ # script.
295
+ #
296
+ # Globals:
297
+ # None
298
+ # Arguments:
299
+ # None
300
+ # Returns:
301
+ # None
302
+ function os::cleanup::processes() {
303
+ os::log::info " [CLEANUP] Killing child processes"
304
+ for job in $( jobs -pr ) ; do
305
+ for child in $( pgrep -P " ${job} " ) ; do
306
+ ${USE_SUDO: +sudo} kill " ${child} " & > /dev/null
307
+ done
308
+ ${USE_SUDO: +sudo} kill " ${job} " & > /dev/null
309
+ done
310
+ }
311
+ readonly -f os::cleanup::processes
0 commit comments