2023-03-09 17:30:40 +00:00
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
2023-01-12 17:58:51 +00:00
# Cleanup for logging.
function trap_handler_cleanup_logging( ) {
[ [ " x ${ LOGDIR } x " = = "xx" ] ] && return 0
[ [ " x ${ LOGDIR } x " = = "x/x" ] ] && return 0
[ [ ! -d " ${ LOGDIR } " ] ] && return 0
display_alert "Cleaning up log files" " LOGDIR: ' ${ LOGDIR } ' " "debug"
# `pwd` might not even be valid anymore. Move back to ${SRC}
cd " ${ SRC } " || exit_with_error " cray-cray about SRC: ${ SRC } "
2023-07-30 22:48:09 +00:00
# Just delete LOGDIR if in CONFIG_DEFS_ONLY mode, or if ANSI_COLOR is "none".
if [ [ " ${ CONFIG_DEFS_ONLY } " = = "yes" || " ${ ANSI_COLOR } " = = "none" ] ] ; then
display_alert "Discarding logs" " CONFIG_DEFS_ONLY= ${ CONFIG_DEFS_ONLY } ;ANSI_COLOR= ${ ANSI_COLOR } " "debug"
2023-01-12 17:58:51 +00:00
discard_logs_tmp_dir
return 0
fi
local target_path = " ${ DEST } /logs "
2023-01-16 13:26:24 +00:00
mkdir_recursive_and_set_uid_owner " ${ target_path } " # @TODO: this might be full of logs and is slow
2023-01-12 17:58:51 +00:00
# Before writing new logfile, compress and move existing ones to archive folder.
# - Unless running under CI.
# - Also not if signalled via SKIP_LOG_ARCHIVE=yes
2023-01-21 01:45:24 +00:00
2023-01-12 17:58:51 +00:00
if [ [ " ${ CI :- false } " != "true" && " ${ SKIP_LOG_ARCHIVE :- no } " != "yes" ] ] ; then
2023-01-21 01:45:24 +00:00
declare -a existing_log_files_array # array of existing log files: dash and dot in filename required.
mapfile -t existing_log_files_array < <( find " ${ target_path } " -maxdepth 1 -type f -name "*-*.*" )
2023-01-27 00:06:34 +00:00
# if more than 14 log files, warn user...
if [ [ " ${# existing_log_files_array [@] } " -gt 14 ] ] ; then
2023-01-21 01:45:24 +00:00
# Hey, I fixed Docker archiving, so this should not happen again... heh.
display_alert "Archiving" " ${# existing_log_files_array [@] } old log files - be patient & thanks for testing armbian-next! 👍 " "wrn"
wait_for_disk_sync # for dramatic effect
fi
2023-01-12 17:58:51 +00:00
declare one_old_logfile old_logfile_fn target_archive_path = " ${ target_path } " /archive
for one_old_logfile in " ${ existing_log_files_array [@] } " ; do
old_logfile_fn = " $( basename " ${ one_old_logfile } " ) "
if [ [ " ${ old_logfile_fn } " = = *${ ARMBIAN_BUILD_UUID } * ] ] ; then
2023-01-17 00:15:28 +00:00
display_alert "Skipping archiving of current logfile" " ${ old_logfile_fn } " "cleanup"
2023-01-12 17:58:51 +00:00
continue
fi
2023-01-17 00:15:28 +00:00
display_alert "Archiving old logfile" " ${ old_logfile_fn } " "cleanup"
2023-01-16 13:26:24 +00:00
mkdir_recursive_and_set_uid_owner " ${ target_archive_path } " # @TODO: slow
2023-01-12 17:58:51 +00:00
# Check if we have `zstdmt` at this stage; if not, use standard gzip
if [ [ -n " $( command -v zstdmt) " ] ] ; then
zstdmt --quiet " ${ one_old_logfile } " -o " ${ target_archive_path } / ${ old_logfile_fn } .zst "
reset_uid_owner " ${ target_archive_path } / ${ old_logfile_fn } .zst "
else
# shellcheck disable=SC2002 # my cat is not useless. a bit whiny. not useless.
cat " ${ one_old_logfile } " | gzip > " ${ target_archive_path } / ${ old_logfile_fn } .gz "
reset_uid_owner " ${ target_archive_path } / ${ old_logfile_fn } .gz "
fi
rm -f " ${ one_old_logfile } "
done
else
display_alert "Not archiving old logs." " CI= ${ CI :- false } , SKIP_LOG_ARCHIVE= ${ SKIP_LOG_ARCHIVE :- no } " "debug"
fi
2023-04-20 16:04:28 +00:00
# Gather repeat build args, included in the logs.
declare -g repeat_args = ( )
produce_repeat_args_array # produces repeat_args
declare repeat_args_string = " ${ repeat_args [*] } "
2023-01-18 19:22:39 +00:00
## Here -- we need to definitely stop logging, cos we're gonna consolidate and delete the logs.
display_alert "End of logging" " STOP LOGGING: CURRENT_LOGFILE: ${ CURRENT_LOGFILE } " "debug"
# Stop logging to file...
CURRENT_LOGFILE = ""
unset CURRENT_LOGFILE
# Check if fd 13 is still open; close it and wait for tee to die. This is done again in discard_logs_tmp_dir()
check_and_close_fd_13
2023-01-24 23:04:34 +00:00
# Export ANSI logs.
2023-01-21 01:45:53 +00:00
local target_file = " ${ target_path } /log- ${ ARMBIAN_LOG_CLI_ID } - ${ ARMBIAN_BUILD_UUID } .log.ans "
2023-01-12 17:58:51 +00:00
export_ansi_logs
reset_uid_owner " ${ target_file } "
2023-01-24 23:04:34 +00:00
local ansi_log_file = " ${ target_file } "
2023-01-12 17:58:51 +00:00
2023-01-24 23:04:34 +00:00
# ASCII logs, via ansi2txt, if available.
local ascii_log_file = " ${ target_path } /log- ${ ARMBIAN_LOG_CLI_ID } - ${ ARMBIAN_BUILD_UUID } .log "
if [ [ -n " $( command -v ansi2txt) " ] ] ; then
# shellcheck disable=SC2002 # gotta pipe, man. I know.
cat " ${ ansi_log_file } " | ansi2txt >> " ${ ascii_log_file } "
fi
2023-08-12 09:42:08 +00:00
# Export Markdown assets, but not if in GHA and GHA_EXPORT_MD_SUMMARY != yes
2024-07-08 15:00:38 +00:00
if [ [ " ${ CI } " = = "true" ] ] && ( [ [ " ${ GITHUB_ACTIONS } " != "true" ] ] || [ [ " ${ GHA_EXPORT_MD_SUMMARY :- no } " != "yes" ] ] ) ; then
2023-08-12 09:42:08 +00:00
display_alert "Not exporting Markdown logs to GitHub Actions" " GITHUB_ACTIONS: ' ${ GITHUB_ACTIONS } ', GHA_EXPORT_MD_SUMMARY: ' ${ GHA_EXPORT_MD_SUMMARY } ' " "debug"
else
# Export Markdown logs.
local target_file = " ${ target_path } /summary- ${ ARMBIAN_LOG_CLI_ID } - ${ ARMBIAN_BUILD_UUID } .md "
export_markdown_logs " ${ ascii_log_file } " # it might include the ASCII as well, if in GHA.
reset_uid_owner " ${ target_file } "
local markdown_log_file = " ${ target_file } "
fi
2023-01-21 01:45:53 +00:00
2023-02-02 12:12:00 +00:00
# Export raw logs, in a tar. For development.
if [ [ " ${ RAW_LOG :- no } " = = "yes" ] ] ; then
local target_file = " ${ target_path } /log- ${ ARMBIAN_LOG_CLI_ID } - ${ ARMBIAN_BUILD_UUID } .raw.tar "
export_raw_logs
reset_uid_owner " ${ target_file } "
fi
2023-08-12 09:42:08 +00:00
# If running in Github Actions, and GHA_EXPORT_MD_SUMMARY=yes, cat the markdown file to GITHUB_STEP_SUMMARY. It appends, docker and build logs will be together.
if [ [ " ${ CI } " = = "true" ] ] && [ [ " ${ GITHUB_ACTIONS } " = = "true" ] ] && [ [ " ${ GHA_EXPORT_MD_SUMMARY :- no } " = = "yes" ] ] ; then
2023-01-24 02:24:38 +00:00
display_alert "Exporting Markdown logs to GitHub Actions" " GITHUB_STEP_SUMMARY: ' ${ GITHUB_STEP_SUMMARY } ' " "info"
2023-01-24 23:04:34 +00:00
cat " ${ markdown_log_file } " >> " ${ GITHUB_STEP_SUMMARY } " || true
2023-01-24 02:24:38 +00:00
fi
2023-04-20 16:04:28 +00:00
unset repeat_args_string
2023-01-12 17:58:51 +00:00
discard_logs_tmp_dir
}