Skip to content

Commit 55d624e

Browse files
committed
Add support for sending failed logs to logdetective
New function called 'analyze_logs_by_logdetective()' is used as for 'build' part as for 'test' part. Each function is marked by 'LOGDETECTIVE {BUILD|TEST} tag so we can easily find them. Also parsing logs have been enhanced no parse_output is needed. Let's call it directly. It is used on Fedora. We use podman for building and there is no needed to use complicated functions. Signed-off-by: Petr "Stone" Hracek <[email protected]>
1 parent 3b998a5 commit 55d624e

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

build.sh

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ parse_output ()
6565
(exit $rc)
6666
}
6767

68+
analyze_logs_by_logdetective() {
69+
local result="$1"
70+
local log_file_name="$2"
71+
if [[ "$result" != "0" ]]; then
72+
echo "Sending log file to logdetective server: ${log_file_name}"
73+
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS START --------"
74+
logdetective "${log_file_name}"
75+
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS FINISHED --------"
76+
fi
77+
}
78+
6879
# "best-effort" cleanup of image
6980
function clean_image {
7081
for id_file in .image-id .image-id-from; do
@@ -191,10 +202,35 @@ function docker_build_with_version {
191202
if [[ "$SKIP_SQUASH" -eq 0 ]] && [[ "$is_podman" -eq 1 ]]; then
192203
BUILD_OPTIONS+=" --squash"
193204
fi
205+
206+
command="docker build "$BUILD_OPTIONS" -f $dockerfile ${DOCKER_BUILD_CONTEXT}"
207+
echo "-> building using $command"
208+
209+
tmp_file=$(mktemp "/tmp/${dir}-${OS}.XXXXXX")
210+
$command 2>&1 | tee "$tmp_file" #> $tmp_file 2>&1
211+
cat $tmp_file
212+
last_row=$(cat $tmp_file | tail -n 1)
213+
# Structure of log build is following:
214+
# COMMIT
215+
# --> e191d12b5928
216+
# e191d12b5928360dd6024fe80d31e08f994d42577f76b9b143e014749afc8ab4
194217
# shellcheck disable=SC2016
195-
parse_output 'docker build '"$BUILD_OPTIONS"' -f "$dockerfile" "${DOCKER_BUILD_CONTEXT}"' \
196-
"tail -n 1 | awk '/Successfully built|(^--> )?(Using cache )?[a-fA-F0-9]+$/{print \$NF}'" \
197-
IMAGE_ID
218+
if [[ "$(cat $tmp_file | tail -n 3)" == *"COMMIT"* ]] && [[ "$last_row" =~ (^-->)?(Using cache )?[a-fA-F0-9]+$ ]]; then
219+
IMAGE_ID="$last_row"
220+
else
221+
echo "Analyse logs by logdetective, why it failed."
222+
analyze_logs_by_logdetective "1" "${tmp_file}"
223+
exit 1
224+
fi
225+
226+
rm -f "$tmp_file"
227+
228+
# shellcheck disable=SC2016
229+
230+
# parse_output 'docker build '"$BUILD_OPTIONS"' -f "$dockerfile" "${DOCKER_BUILD_CONTEXT}"' \
231+
# "tail -n 1 | awk '/Successfully built|(^--> )?(Using cache )?[a-fA-F0-9]+$/{print \$NF}'" \
232+
# IMAGE_ID
233+
# analyze_logs_by_logdetective "$?" "${tmp_file}"
198234
echo "$IMAGE_ID" > .image-id
199235
}
200236

test.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ failed_version() {
2424
return "$result"
2525
}
2626

27+
analyze_logs_by_logdetective() {
28+
local result="$1"
29+
local log_file_name="$2"
30+
if [[ "$result" != "0" ]]; then
31+
echo "Sending log file to logdetective server: ${log_file_name}"
32+
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS START --------"
33+
logdetective "${log_file_name}"
34+
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS FINISHED --------"
35+
fi
36+
}
37+
2738
# This adds backwards compatibility if only single version needs to be testing
2839
# In CI we would like to test single version but VERSIONS= means, that nothing is tested
2940
# make test TARGET=<OS> VERSIONS=<something> ... checks single version for CLI
@@ -47,8 +58,12 @@ for dir in ${VERSIONS}; do
4758
fi
4859

4960
if [ -n "${TEST_MODE}" ]; then
50-
VERSION=$dir test/run
51-
failed_version "$?" "$dir"
61+
tmp_file=$(mktemp "/tmp/${IMAGE_NAME}-${OS}-${dir}.XXXXXX")
62+
VERSION=$dir test/run 2>&1 | tee $tmp_file
63+
ret_code=$?
64+
analyze_logs_by_logdetective "$ret_code" "$tmp_file"
65+
failed_version "$ret_code" "$dir"
66+
rm -f "$tmp_file"
5267
fi
5368

5469
if [ -n "${TEST_OPENSHIFT_4}" ]; then

0 commit comments

Comments
 (0)