Skip to content

Commit 65e2f6a

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 65e2f6a

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed

build.sh

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

68+
analyze_logs_by_logdetective() {
69+
local log_file_name="$1"
70+
echo "Sending failed log by fpaste command to paste bin."
71+
paste_bin_link=$(fpaste "$log_file_name")
72+
# shellcheck disable=SC2181
73+
if [[ $? -ne 0 ]]; then
74+
echo "ERROR: Failed to send log file to private bin: ${log_file_name}"
75+
return
76+
fi
77+
# pastebin link is "https://paste.centos.org/view/ee98ba05"
78+
# We need a raw link that is "https://paste.centos.org/view/raw/ee98ba05"
79+
raw_paste_bin_link="${paste_bin_link//view/view\/raw}"
80+
echo "Sending log file to logdetective server: ${raw_paste_bin_link}"
81+
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS START --------"
82+
# shellcheck disable=SC2181
83+
if ! curl -k --insecure --header "Content-Type: application/json" --request POST --data "{\"url\":\"${raw_paste_bin_link}\"}" "$LOGDETECTIVE_SERVER/analyze" > /tmp/logdetective_output.txt; then
84+
echo "ERROR: Failed to analyze log file by logdetective server."
85+
cat "/tmp/logdetective_output.txt"
86+
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS FAILED --------"
87+
return
88+
fi
89+
jq -rC '.explanation.text' < "/tmp/logdetective_output.txt"
90+
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS FINISHED --------"
91+
}
92+
6893
# "best-effort" cleanup of image
6994
function clean_image {
7095
for id_file in .image-id .image-id-from; do
@@ -191,10 +216,37 @@ function docker_build_with_version {
191216
if [[ "$SKIP_SQUASH" -eq 0 ]] && [[ "$is_podman" -eq 1 ]]; then
192217
BUILD_OPTIONS+=" --squash"
193218
fi
219+
220+
command="docker build ${BUILD_OPTIONS} -f $dockerfile ${DOCKER_BUILD_CONTEXT}"
221+
echo "-> building using $command"
222+
223+
tmp_file=$(mktemp "/tmp/${dir}-${OS}.XXXXXX")
224+
$command 2>&1 | tee "$tmp_file"
225+
cat "$tmp_file"
226+
last_row=$(< "$tmp_file" tail -n 1)
227+
commit_message=$(< "$tmp_file" tail -n 3)
228+
# Structure of log build is as follows:
229+
# COMMIT
230+
# --> e191d12b5928
231+
# e191d12b5928360dd6024fe80d31e08f994d42577f76b9b143e014749afc8ab4
232+
# shellcheck disable=SC2016
233+
if [[ "$commit_message" == *"COMMIT"* ]] && [[ "$last_row" =~ (^-->)?(Using cache )?[a-fA-F0-9]+$ ]]; then
234+
IMAGE_ID="$last_row"
235+
else
236+
# Do not fail in case of sending log to pastebin or logdetective fails.
237+
echo "Analyse logs by logdetective, why it failed."
238+
analyze_logs_by_logdetective "${tmp_file}"
239+
exit 1
240+
fi
241+
242+
rm -f "$tmp_file"
243+
194244
# 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
245+
246+
# parse_output 'docker build '"$BUILD_OPTIONS"' -f "$dockerfile" "${DOCKER_BUILD_CONTEXT}"' \
247+
# "tail -n 1 | awk '/Successfully built|(^--> )?(Using cache )?[a-fA-F0-9]+$/{print \$NF}'" \
248+
# IMAGE_ID
249+
# analyze_logs_by_logdetective "$?" "${tmp_file}"
198250
echo "$IMAGE_ID" > .image-id
199251
}
200252

test.sh

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

27+
analyze_logs_by_logdetective() {
28+
local log_file_name="$1"
29+
echo "Sending failed log by fpaste command to paste bin."
30+
paste_bin_link=$(fpaste "$log_file_name")
31+
# shellcheck disable=SC2181
32+
if [[ $? -ne 0 ]]; then
33+
echo "ERROR: Failed to send log file to private bin: ${log_file_name}"
34+
return
35+
fi
36+
# pastebin link is "https://paste.centos.org/view/ee98ba05"
37+
# We need a raw link that is "https://paste.centos.org/view/raw/ee98ba05"
38+
raw_paste_bin_link="${paste_bin_link//view/view\/raw}"
39+
echo "Sending log file to logdetective server: ${raw_paste_bin_link}"
40+
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS START --------"
41+
# shellcheck disable=SC2181
42+
if ! curl -k --insecure --header "Content-Type: application/json" --request POST --data "{\"url\":\"${raw_paste_bin_link}\"}" "$LOGDETECTIVE_SERVER/analyze" > /tmp/logdetective_test_output.txt; then
43+
echo "ERROR: Failed to analyze log file by logdetective server."
44+
cat "/tmp/logdetective_test_output.txt"
45+
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS FAILED --------"
46+
return
47+
fi
48+
jq -rC '.explanation.text' < "/tmp/logdetective_test_output.txt"
49+
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS FINISHED --------"
50+
}
51+
2752
# This adds backwards compatibility if only single version needs to be testing
2853
# In CI we would like to test single version but VERSIONS= means, that nothing is tested
2954
# make test TARGET=<OS> VERSIONS=<something> ... checks single version for CLI
@@ -47,8 +72,12 @@ for dir in ${VERSIONS}; do
4772
fi
4873

4974
if [ -n "${TEST_MODE}" ]; then
50-
VERSION=$dir test/run
51-
failed_version "$?" "$dir"
75+
tmp_file=$(mktemp "/tmp/${IMAGE_NAME}-${OS}-${dir}.XXXXXX")
76+
VERSION=$dir test/run 2>&1 | tee "$tmp_file"
77+
ret_code=$?
78+
analyze_logs_by_logdetective "$tmp_file"
79+
failed_version "$ret_code" "$dir"
80+
rm -f "$tmp_file"
5281
fi
5382

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

0 commit comments

Comments
 (0)