export-logs.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. function prepare_ansi_git_info_log_header() {
  2. # writes to stdout, ANSI format
  3. declare prefix_sed_cmd="/^-->/!s/^/ /;" # some spacing in front of git info
  4. cat <<- GIT_ANSI_HEADER
  5. $(echo -e -n "${bright_blue_color:-}")# GIT revision$(echo -e -n "${ansi_reset_color:-}")
  6. $(LC_ALL=C LANG=C git --git-dir="${SRC}/.git" log -1 --color --format=short --decorate | sed -e "${prefix_sed_cmd}" || true)
  7. ${dim_line_separator}
  8. $(echo -e -n "${bright_blue_color:-}")# GIT status$(echo -e -n "${ansi_reset_color:-}")
  9. $(LC_ALL=C LANG=C git -c color.status=always --work-tree="${SRC}" --git-dir="${SRC}/.git" status | sed -e "${prefix_sed_cmd}" || true)
  10. ${dim_line_separator}
  11. $(echo -e -n "${bright_blue_color:-}")# GIT changes$(echo -e -n "${ansi_reset_color:-}")
  12. $(LC_ALL=C LANG=C git --work-tree="${SRC}" --git-dir="${SRC}/.git" diff -u --color | sed -e "${prefix_sed_cmd}" || true)
  13. ${dim_line_separator}
  14. GIT_ANSI_HEADER
  15. }
  16. # This only includes a header, and all the .md logfiles, nothing else.
  17. function export_markdown_logs() {
  18. # check target_file variable is not empty
  19. if [[ -z "${target_file}" ]]; then
  20. display_alert "No target file specified for export_markdown_logs()" "${target_file}" "err"
  21. return 0
  22. fi
  23. local ascii_log_file="${1:-}"
  24. display_alert "Preparing Markdown log from" "${LOGDIR} (${ascii_log_file})" "debug"
  25. cat <<- MARKDOWN_HEADER > "${target_file}"
  26. <details><summary>Build: ${ARMBIAN_ORIGINAL_ARGV[*]}</summary>
  27. <p>
  28. ### Armbian logs for ${ARMBIAN_BUILD_UUID}
  29. #### Armbian build at $(LC_ALL=C LANG=C date) on $(hostname || true)
  30. #### ARGs: \`${ARMBIAN_ORIGINAL_ARGV[@]@Q}\`
  31. MARKDOWN_HEADER
  32. if [[ -n "$(command -v git)" && -d "${SRC}/.git" ]]; then
  33. # If in GHA, very unlikely there will be changes, don't waste space.
  34. if [[ "${CI}" == "true" ]] && [[ "${GITHUB_ACTIONS}" == "true" ]]; then
  35. display_alert "Gathering git info for logs" "Processing git information, GHA version" "debug"
  36. cat <<- GIT_MARKDOWN_HEADER_GHA >> "${target_file}"
  37. #### Current revision:
  38. \`\`\`
  39. $(LC_ALL=C LANG=C git --git-dir="${SRC}/.git" log -1 --format=short --decorate)
  40. \`\`\`
  41. GIT_MARKDOWN_HEADER_GHA
  42. else
  43. display_alert "Gathering git info for logs" "Processing git information, please wait..." "debug"
  44. cat <<- GIT_MARKDOWN_HEADER >> "${target_file}"
  45. #### Current revision:
  46. \`\`\`
  47. $(LC_ALL=C LANG=C git --git-dir="${SRC}/.git" log -1 --format=short --decorate)
  48. \`\`\`
  49. #### Git status:
  50. \`\`\`
  51. $(LC_ALL=C LANG=C git --work-tree="${SRC}" --git-dir="${SRC}/.git" status)
  52. \`\`\`
  53. #### Git changes:
  54. \`\`\`
  55. $(LC_ALL=C LANG=C git --work-tree="${SRC}" --git-dir="${SRC}/.git" diff -u)
  56. \`\`\`
  57. GIT_MARKDOWN_HEADER
  58. fi
  59. fi
  60. # FOOTER: Newlines are relevant here.
  61. echo -e "\n\n</p></details>\n\n" >> "${target_file}"
  62. display_alert "Preparing Markdown logs..." "Processing log files..." "debug"
  63. # Find and sort the files there, store in array one per logfile
  64. declare -a logfiles_array
  65. mapfile -t logfiles_array < <(find "${LOGDIR}" -type f | grep "\.md\$" | LC_ALL=C sort -h) # "human" sorting
  66. for logfile_full in "${logfiles_array[@]}"; do
  67. cat "${logfile_full}" >> "${target_file}"
  68. done
  69. # If running in GHA, include the ascii logs as well, side a collapsible section.
  70. if [[ "${CI}" == "true" ]] && [[ "${GITHUB_ACTIONS}" == "true" ]]; then
  71. if [[ -f "${ascii_log_file}" ]]; then
  72. # Newlines are relevant here.
  73. cat <<- MARKDOWN_LOG_HEADER >> "${target_file}"
  74. <details><summary>ASCII logs: ${ARMBIAN_BUILD_UUID}</summary>
  75. <p>
  76. \`\`\`bash
  77. MARKDOWN_LOG_HEADER
  78. # GHA has a 1mb limit for Markdown. 500kb of logs, max, from the end.
  79. tail --bytes 500000 "${ascii_log_file}" >> "${target_file}" || true
  80. echo -e "\n\`\`\`\n\n</p></details>\n\n" >> "${target_file}"
  81. fi
  82. fi
  83. return 0
  84. }
  85. # Export logs in plain format.
  86. function export_ansi_logs() {
  87. # check target_file variable is not empty
  88. if [[ -z "${target_file}" ]]; then
  89. display_alert "No target file specified for export_markdown_logs()" "${target_file}" "err"
  90. return 0
  91. fi
  92. display_alert "Preparing ANSI log from" "${LOGDIR}" "debug"
  93. declare dim_line_separator
  94. dim_line_separator=$(echo -e -n "${gray_color:-}")------------------------------------------------------------------------------------------------------------$(echo -e -n "${ansi_reset_color:-}")
  95. cat <<- ANSI_HEADER > "${target_file}"
  96. # Armbian ANSI build logs for ${ARMBIAN_BUILD_UUID} - use "less -SR" to view
  97. $(echo -e -n "${bright_blue_color:-}")# Armbian build at $(LC_ALL=C LANG=C date) on $(hostname || true)$(echo -e -n "${ansi_reset_color}")
  98. ${dim_line_separator}
  99. $(echo -e -n "${bright_blue_color}")# ARGs: ${ARMBIAN_ORIGINAL_ARGV[@]@Q}$(echo -e -n "${ansi_reset_color}")
  100. ${dim_line_separator}
  101. ANSI_HEADER
  102. if [[ -n "${GIT_INFO_ANSI}" ]]; then
  103. echo "${GIT_INFO_ANSI}" >> "${target_file}"
  104. elif [[ -n "$(command -v git)" && -d "${SRC}/.git" ]]; then # we don't have .git inside Docker...
  105. display_alert "Gathering git info for logs" "Processing git information, please wait..." "debug"
  106. prepare_ansi_git_info_log_header >> "${target_file}"
  107. else
  108. display_alert "Gathering git info for logs" "No git information available" "debug"
  109. fi
  110. display_alert "Preparing ANSI logs..." "Processing log files..." "debug"
  111. # Find and sort the files there, store in array one per logfile
  112. declare -a logfiles_array
  113. mapfile -t logfiles_array < <(find "${LOGDIR}" -type f | LC_ALL=C sort -h) # "human" sorting
  114. declare prefix_sed_contents
  115. prefix_sed_contents=" $(echo -n -e "${ansi_reset_color}${tool_color:-}")" # spaces are significant
  116. declare prefix_sed_cmd="/^-->/!s/^/${prefix_sed_contents}/;"
  117. declare logfile_full
  118. for logfile_full in "${logfiles_array[@]}"; do
  119. [[ ! -s "${logfile_full}" ]] && continue # skip empty files
  120. declare logfile_base logfile_title
  121. logfile_base="$(basename "${logfile_full}")"
  122. [[ ! "${logfile_base}" =~ \.log$ ]] && continue # only .log files; others should be in Markdown logs
  123. # remove everything before the second dot to produce the title
  124. # shellcheck disable=SC2001 # I saw, and I can't
  125. logfile_title="$(echo "${logfile_base}" | sed -e 's/^[^.]*\.[^.]*\.//')"
  126. # shellcheck disable=SC2002 # cats, not useless, I like.
  127. cat <<- ANSI_ONE_LOGFILE >> "${target_file}"
  128. $(echo -e -n "${bright_blue_color}")### ${logfile_title} $(echo -e -n "${ansi_reset_color}")
  129. $(cat "${logfile_full}" | sed -e "${prefix_sed_cmd}")
  130. ${dim_line_separator}
  131. ANSI_ONE_LOGFILE
  132. done
  133. declare target_relative_to_src
  134. target_relative_to_src="$(realpath --relative-to="${SRC}" "${target_file}")"
  135. if [[ "${show_message_after_export:-"yes"}" != "skip" && "${ARMBIAN_INSIDE_DOCKERFILE_BUILD:-"no"}" != "yes" ]]; then
  136. display_alert "ANSI log file built; inspect it by running:" "less -RS ${target_relative_to_src}"
  137. # @TODO: compress...
  138. if [[ "${SHARE_LOG:-"no"}" == "yes" ]]; then
  139. display_alert "SHARE_LOG=yes, uploading log" "uploading logs" "info"
  140. declare logs_url="undetermined"
  141. logs_url=$(curl --silent --data-binary "@${target_relative_to_src}" "https://paste.next.armbian.com/log" | xargs echo -n || true) # don't fail
  142. display_alert "Log uploaded, share URL:" "${logs_url}" ""
  143. else
  144. display_alert "Share log manually (or SHARE_LOG=yes):" "curl --data-binary @${target_relative_to_src} https://paste.next.armbian.com/log"
  145. fi
  146. fi
  147. return 0
  148. }
  149. function export_raw_logs() {
  150. display_alert "Exporting RAW logs from" "${LOGDIR}" "info"
  151. if [[ -z "${target_file}" ]]; then
  152. display_alert "No target file specified for export_raw_logs()" "${target_file}" "err"
  153. return 0
  154. fi
  155. # Just tar the logs directory into target_file
  156. tar -C "${LOGDIR}" -cf "${target_file}" .
  157. }