From 242a5dad1fb4e5185469cfbba08ebf25158d30fd Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Wed, 18 Jan 2023 21:03:13 +0100 Subject: [PATCH] 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 --- lib/functions/cli/cli-docker.sh | 12 ++++++++++++ lib/functions/host/docker.sh | 24 +++++++++++++++++------- lib/functions/logging/export-logs.sh | 6 +++++- lib/functions/logging/traps.sh | 8 ++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/functions/cli/cli-docker.sh b/lib/functions/cli/cli-docker.sh index f28056d66c..6733481508 100644 --- a/lib/functions/cli/cli-docker.sh +++ b/lib/functions/cli/cli-docker.sh @@ -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 } diff --git a/lib/functions/host/docker.sh b/lib/functions/host/docker.sh index f230ceef0f..4af3dd0ba1 100755 --- a/lib/functions/host/docker.sh +++ b/lib/functions/host/docker.sh @@ -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() { diff --git a/lib/functions/logging/export-logs.sh b/lib/functions/logging/export-logs.sh index 185d6c19f7..8e32926d12 100644 --- a/lib/functions/logging/export-logs.sh +++ b/lib/functions/logging/export-logs.sh @@ -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 } diff --git a/lib/functions/logging/traps.sh b/lib/functions/logging/traps.sh index e32b831f70..a9d09d6d67 100644 --- a/lib/functions/logging/traps.sh +++ b/lib/functions/logging/traps.sh @@ -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"