armbian-next: somewhat-better handling/logging when using Docker (don't show two logfiles to inspect)

- do not show path to Docker run-host's logfile, unless the Docker run itself didn't produce logs
- introduce `global_final_exit_code` to allow to maintain/preserve Docker's exit code without triggering an error host-side
This commit is contained in:
Ricardo Pardini 2023-01-18 21:03:13 +01:00
parent bf3f346bee
commit 242a5dad1f
No known key found for this signature in database
GPG Key ID: 3D38CA12A66C5D02
4 changed files with 42 additions and 8 deletions

View File

@ -42,8 +42,20 @@ function cli_docker_run() {
;;
*)
# this does NOT exit with the same exit code as the docker instance.
# instead, it sets the docker_exit_code variable.
declare -i docker_exit_code docker_produced_logs=0
docker_cli_launch "${ARMBIAN_CLI_RELAUNCH_ARGS[@]}" # MARK: this "re-launches" using the passed params.
# Set globals to avoid:
# 1) showing the controlling host's log; we only want to show a ref to the Docker logfile, unless it didn't produce one.
if [[ $docker_produced_logs -gt 0 ]]; then
declare -g show_message_after_export="skip" # handled by export_ansi_logs()
fi
# 2) actually exiting with the same error code as the docker instance, but without triggering an error.
declare -g -i global_final_exit_code=$docker_exit_code # handled by .... @TODO
;;
esac
}

View File

@ -407,27 +407,37 @@ function docker_cli_launch() {
display_alert "Showing Docker cmdline" "Docker args: '${DOCKER_ARGS[*]}'" "debug"
display_alert "Relaunching in Docker" "${*}" "debug"
display_alert "-----------------Relaunching in Docker-----------------------------------" "here comes the 🐳" "info"
display_alert "-----------------Relaunching in Docker------------------------------------" "here comes the 🐳" "info"
local -i docker_build_result=1
local -i docker_build_result
if docker run -it "${DOCKER_ARGS[@]}" "${DOCKER_ARMBIAN_INITIAL_IMAGE_TAG}" /bin/bash "${DOCKER_ARMBIAN_TARGET_PATH}/compile.sh" "$@"; then
display_alert "-------------Docker Build finished----------------------------------------" "successfully" "info"
docker_build_result=0
docker_build_result=$? # capture exit code of test done in the line above.
display_alert "-------------Docker run finished------------------------------------------" "successfully" "info"
else
display_alert "-------------Docker Build failed-------------------------------------------" "with errors" "err"
docker_build_result=$? # capture exit code of test done 4 lines above.
display_alert "-------------Docker run failed--------------------------------------------" "with errors" "err"
fi
# Find and show the path to the log file for the ARMBIAN_BUILD_UUID.
local logs_path="${DEST}/logs" log_file
log_file="$(find "${logs_path}" -type f -name "*${ARMBIAN_BUILD_UUID}*.*" -print -quit)"
display_alert "Build log done inside Docker" "${log_file}" "info"
docker_produced_logs=0 # outer scope variable
if [[ -f "${log_file}" ]]; then
docker_produced_logs=1 # outer scope variable
display_alert "Build log done inside Docker" "${log_file}" "debug"
else
display_alert "Docker Log file for this run" "not found" "err"
fi
# Show and help user understand space usage in Docker volumes.
# This is done in a loop; `docker df` fails sometimes (for no good reason).
# @TODO: this is very, very slow when the volumes are full. disable.
# docker_cli_show_armbian_volumes_disk_usage
return ${docker_build_result}
docker_exit_code="${docker_build_result}" # set outer scope variable -- do NOT exit with error.
# return ${docker_build_result}
return 0 # always exit with success. caller (CLI) will handle the exit code
}
function docker_cli_show_armbian_volumes_disk_usage() {

View File

@ -104,5 +104,9 @@ function export_ansi_logs() {
declare target_relative_to_src
target_relative_to_src="$(realpath --relative-to="${SRC}" "${target_file}")"
display_alert "ANSI log file built; inspect it by running:" "less -RS ${target_relative_to_src}"
if [[ "${show_message_after_export:-"yes"}" != "skip" ]]; then
display_alert "ANSI log file built; inspect it by running:" "less -RS ${target_relative_to_src}"
fi
return 0
}

View File

@ -77,6 +77,14 @@ function main_trap_handler() {
# Run the cleanup handlers, always.
run_cleanup_handlers || true
# If global_final_exit_code is set, use it as the exit code. (used by docker CLI handler)
if [[ -n "${global_final_exit_code}" ]]; then
display_alert "Final exit code" "Final exit code ${global_final_exit_code}" "debug"
# disable the trap, so we don't get called again.
trap - EXIT
exit "${global_final_exit_code}"
fi
;;
*)
display_alert "main_trap_handler" "Unknown trap type '${trap_type}'" "err"