Skip to content

Add support for sending failed logs to logdetective #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 69 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ parse_output ()
(exit $rc)
}

analyze_logs_by_logdetective() {
# logdetective should not break the build functionality
set +e
local log_file_name="$1"
echo "Sending failed log by fpaste command to paste bin."
paste_bin_link=$(fpaste "$log_file_name")
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
echo "ERROR: Failed to send log file to private bin: ${log_file_name}"
return
fi
# pastebin link is "https://paste.centos.org/view/ee98ba05"
# We need a raw link that is "https://paste.centos.org/view/raw/ee98ba05"
raw_paste_bin_link="${paste_bin_link//view/view\/raw}"
echo "Sending log file to logdetective server: ${raw_paste_bin_link}"
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS START --------"
logdetective_build_file=$(mktemp "/tmp/logdetective_build.XXXXXX")
# shellcheck disable=SC2181
if ! curl -k --insecure --header "Content-Type: application/json" --request POST --data "{\"url\":\"${raw_paste_bin_link}\"}" "$LOGDETECTIVE_SERVER/analyze" >> "${logdetective_build_file}"; then
echo "ERROR: Failed to analyze log file by logdetective server."
cat "${logdetective_build_file}"
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS FAILED --------"
set -e
return
fi
set -e
jq -rC '.explanation.text' < "${logdetective_build_file}"
# This part of code is from https://github.com/teemtee/tmt/blob/main/tmt/steps/scripts/tmt-file-submit
if [ -z "$TMT_TEST_PIDFILE" ]; then
echo "File submit to data dir can be used only in the context of a running test."
return
fi
# This variable is set by tmt
[ -d "$TMT_TEST_DATA" ] || mkdir -p "$TMT_TEST_DATA"
cp -f "${logdetective_build_file}" "$TMT_TEST_DATA"
echo "File '${logdetective_build_file}' stored to '$TMT_TEST_DATA'."
echo "-------- LOGDETECTIVE BUILD LOG ANALYSIS FINISHED --------"
}

# "best-effort" cleanup of image
function clean_image {
for id_file in .image-id .image-id-from; do
Expand Down Expand Up @@ -191,10 +230,37 @@ function docker_build_with_version {
if [[ "$SKIP_SQUASH" -eq 0 ]] && [[ "$is_podman" -eq 1 ]]; then
BUILD_OPTIONS+=" --squash"
fi

command="docker build ${BUILD_OPTIONS} -f $dockerfile ${DOCKER_BUILD_CONTEXT}"
echo "-> building using $command"
tmp_file=$(mktemp "/tmp/${dir}-${OS}.XXXXXX")
$command 2>&1 | tee "$tmp_file"
cat "$tmp_file"
last_row=$(< "$tmp_file" tail -n 1)
# Structure of log build is as follows:
# COMMIT
# --> e191d12b5928
# e191d12b5928360dd6024fe80d31e08f994d42577f76b9b143e014749afc8ab4
# shellcheck disable=SC2016
if [[ "$last_row" =~ (^-->)?(Using cache )?[a-fA-F0-9]+$ ]]; then
IMAGE_ID="$last_row"
else
if [[ "${OS}" == "rhel8" ]] || [[ "${OS}" == "rhel9" ]] || [[ "${OS}" == "rhel10" ]]; then
# Do not fail in case of sending log to pastebin or logdetective fails.
echo "Analyse logs by logdetective, why it failed."
analyze_logs_by_logdetective "${tmp_file}"
fi
exit 1
fi

rm -f "$tmp_file"

# shellcheck disable=SC2016
parse_output 'docker build '"$BUILD_OPTIONS"' -f "$dockerfile" "${DOCKER_BUILD_CONTEXT}"' \
"tail -n 1 | awk '/Successfully built|(^--> )?(Using cache )?[a-fA-F0-9]+$/{print \$NF}'" \
IMAGE_ID

# parse_output 'docker build '"$BUILD_OPTIONS"' -f "$dockerfile" "${DOCKER_BUILD_CONTEXT}"' \
# "tail -n 1 | awk '/Successfully built|(^--> )?(Using cache )?[a-fA-F0-9]+$/{print \$NF}'" \
# IMAGE_ID
# analyze_logs_by_logdetective "$?" "${tmp_file}"
echo "$IMAGE_ID" > .image-id
}

Expand Down
52 changes: 50 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@ failed_version() {
return "$result"
}

analyze_logs_by_logdetective() {
# logdetective should not break the test functionality
set +e
local log_file_name="$1"
echo "Sending failed log by fpaste command to paste bin."
paste_bin_link=$(fpaste "$log_file_name")
# shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
echo "ERROR: Failed to send log file to private bin: ${log_file_name}"
return
fi
# pastebin link is "https://paste.centos.org/view/ee98ba05"
# We need a raw link that is "https://paste.centos.org/view/raw/ee98ba05"
raw_paste_bin_link="${paste_bin_link//view/view\/raw}"
echo "Sending log file to logdetective server: ${raw_paste_bin_link}"
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS START --------"
logdetective_test_file=$(mktemp "/tmp/logdetective_test.XXXXXX")
# shellcheck disable=SC2181
if ! curl -k --insecure --header "Content-Type: application/json" --request POST --data "{\"url\":\"${raw_paste_bin_link}\"}" "$LOGDETECTIVE_SERVER/analyze" >> "${logdetective_test_file}"; then
echo "ERROR: Failed to analyze log file by logdetective server."
cat "${logdetective_test_file}"
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS FAILED --------"
set -e
return
fi
set -e
jq -rC '.explanation.text' < "${logdetective_test_file}"
# This part of code is from https://github.com/teemtee/tmt/blob/main/tmt/steps/scripts/tmt-file-submit
if [ -z "$TMT_TEST_PIDFILE" ]; then
echo "File submit to data dir can be used only in the context of a running test."
return
fi
# This variable is set by tmt
[ -d "$TMT_TEST_DATA" ] || mkdir -p "$TMT_TEST_DATA"
cp -f "${logdetective_test_file}" "$TMT_TEST_DATA"
echo "File '${logdetective_test_file}' stored to '$TMT_TEST_DATA'."
echo "-------- LOGDETECTIVE TEST LOG ANALYSIS FINISHED --------"
}

# This adds backwards compatibility if only single version needs to be testing
# In CI we would like to test single version but VERSIONS= means, that nothing is tested
# make test TARGET=<OS> VERSIONS=<something> ... checks single version for CLI
Expand All @@ -47,8 +86,17 @@ for dir in ${VERSIONS}; do
fi

if [ -n "${TEST_MODE}" ]; then
VERSION=$dir test/run
failed_version "$?" "$dir"
set -o pipefail
tmp_file=$(mktemp "/tmp/${OS}-${dir}.XXXXXX")
VERSION=$dir test/run 2>&1 | tee "$tmp_file"
ret_code=$?
set +o pipefail
cat "${tmp_file}"
if [[ "${OS}" == "rhel8" ]] || [[ "${OS}" == "rhel9" ]] || [[ "${OS}" == "rhel10" ]]; then
analyze_logs_by_logdetective "$tmp_file"
fi
failed_version "$ret_code" "$dir"
rm -f "$tmp_file"
fi

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