From c2b7f4e29801f0552f8e6eb3230d07c5cb6ddd66 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Mon, 17 Nov 2025 12:45:24 +0100 Subject: [PATCH] Improve container build/install logging and error handling Introduces distinct BUILD_LOG and INSTALL_LOG variables for host and container operations, ensuring logs are properly captured and retrievable. Refactors silent execution and error handling to use the active log file, improves log copying after failures, and enhances context detection for log management. Also adds fallback logic for INSTALL_LOG initialization in install.func. --- misc/build.func | 33 ++++++++++++++++++++++++++------- misc/core.func | 35 ++++++++++++++++++++++++++++------- misc/error_handler.func | 35 ++++++++++++++++++++++------------- misc/install.func | 5 +++++ 4 files changed, 81 insertions(+), 27 deletions(-) diff --git a/misc/build.func b/misc/build.func index 17c11fd47..ca68c5d2c 100644 --- a/misc/build.func +++ b/misc/build.func @@ -47,6 +47,7 @@ variables() { METHOD="default" # sets the METHOD variable to "default", used for the API call. RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" # generates a random UUID and sets it to the RANDOM_UUID variable. SESSION_ID="${RANDOM_UUID:0:8}" # Short session ID (first 8 chars of UUID) for log files + BUILD_LOG="/tmp/create-lxc-${SESSION_ID}.log" # Host-side container creation log CTTYPE="${CTTYPE:-${CT_TYPE:-1}}"} # Get Proxmox VE version and kernel version @@ -2206,6 +2207,8 @@ build_container() { export DIAGNOSTICS="$DIAGNOSTICS" export RANDOM_UUID="$RANDOM_UUID" export SESSION_ID="$SESSION_ID" + export BUILD_LOG="$BUILD_LOG" + export INSTALL_LOG="/root/.install-${SESSION_ID}.log" export CACHER="$APT_CACHER" export CACHER_IP="$APT_CACHER_IP" export tz="$timezone" @@ -2576,10 +2579,12 @@ EOF' # Install SSH keys install_ssh_keys_into_ct - # Run application installer + # Run application installer (disable ERR trap to handle errors manually) + set +e lxc-attach -n "$CTID" -- bash -c "$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/install/${var_install}.sh)" local lxc_exit=$? - + set -e + # Check for error flag file in container (more reliable than lxc-attach exit code) local install_exit_code=0 if [[ -n "${SESSION_ID:-}" ]]; then @@ -2589,21 +2594,35 @@ EOF' pct exec "$CTID" -- rm -f "$error_flag" 2>/dev/null || true fi fi - + # Fallback to lxc-attach exit code if no flag file if [[ $install_exit_code -eq 0 && $lxc_exit -ne 0 ]]; then install_exit_code=$lxc_exit fi - + # Installation failed? if [[ $install_exit_code -ne 0 ]]; then msg_error "Installation failed in container ${CTID} (exit code: ${install_exit_code})" - # Try to copy installation log from container (without spinner/msg_info) + # Copy both logs from container before potential deletion + local build_log_copied=false + local install_log_copied=false + if [[ -n "$CTID" && -n "${SESSION_ID:-}" ]]; then - if pct pull "$CTID" "/root/.install-${SESSION_ID}.log" "/tmp/install-${SESSION_ID}.log" 2>/dev/null; then - echo -e "${GN}✔${CL} Installation log: ${BL}/tmp/install-${SESSION_ID}.log${CL}" + # Copy BUILD_LOG (creation log) if it exists + if [[ -f "${BUILD_LOG}" ]]; then + cp "${BUILD_LOG}" "/tmp/create-lxc-${CTID}-${SESSION_ID}.log" 2>/dev/null && build_log_copied=true fi + + # Copy INSTALL_LOG from container + if pct pull "$CTID" "/root/.install-${SESSION_ID}.log" "/tmp/install-lxc-${CTID}-${SESSION_ID}.log" 2>/dev/null; then + install_log_copied=true + fi + + # Show available logs + echo "" + [[ $build_log_copied == true ]] && echo -e "${GN}✔${CL} Container creation log: ${BL}/tmp/create-lxc-${CTID}-${SESSION_ID}.log${CL}" + [[ $install_log_copied == true ]] && echo -e "${GN}✔${CL} Installation log: ${BL}/tmp/install-lxc-${CTID}-${SESSION_ID}.log${CL}" fi # Prompt user for cleanup with 60s timeout (plain echo - no msg_info to avoid spinner) diff --git a/misc/core.func b/misc/core.func index a3c4ac51f..86a563250 100644 --- a/misc/core.func +++ b/misc/core.func @@ -287,12 +287,32 @@ ssh_check() { # SECTION 3: EXECUTION HELPERS # ============================================================================== -SILENT_LOGFILE="/tmp/install-$(date +%Y%m%d_%H%M%S)_${SESSION_ID:-$(date +%s)}.log" +# ------------------------------------------------------------------------------ +# get_active_logfile() +# +# - Returns the appropriate log file based on execution context +# - BUILD_LOG: Host operations (container creation) +# - INSTALL_LOG: Container operations (application installation) +# - Fallback to BUILD_LOG if neither is set +# ------------------------------------------------------------------------------ +get_active_logfile() { + if [[ -n "${INSTALL_LOG:-}" ]]; then + echo "$INSTALL_LOG" + elif [[ -n "${BUILD_LOG:-}" ]]; then + echo "$BUILD_LOG" + else + # Fallback for legacy scripts + echo "/tmp/build-$(date +%Y%m%d_%H%M%S).log" + fi +} + +# Legacy compatibility: SILENT_LOGFILE points to active log +SILENT_LOGFILE="$(get_active_logfile)" # ------------------------------------------------------------------------------ # silent() # -# - Executes command with output redirected to SILENT_LOGFILE +# - Executes command with output redirected to active log file # - On error: displays last 10 lines of log and exits with original exit code # - Temporarily disables error trap to capture exit code correctly # - Sources explain_exit_code() for detailed error messages @@ -300,11 +320,12 @@ SILENT_LOGFILE="/tmp/install-$(date +%Y%m%d_%H%M%S)_${SESSION_ID:-$(date +%s)}.l silent() { local cmd="$*" local caller_line="${BASH_LINENO[0]:-unknown}" + local logfile="$(get_active_logfile)" set +Eeuo pipefail trap - ERR - "$@" >>"$SILENT_LOGFILE" 2>&1 + "$@" >>"$logfile" 2>&1 local rc=$? set -Eeuo pipefail @@ -323,15 +344,15 @@ silent() { msg_error "in line ${caller_line}: exit code ${rc} (${explanation})" msg_custom "→" "${YWB}" "${cmd}" - if [[ -s "$SILENT_LOGFILE" ]]; then - local log_lines=$(wc -l <"$SILENT_LOGFILE") + if [[ -s "$logfile" ]]; then + local log_lines=$(wc -l <"$logfile") echo "--- Last 10 lines of silent log ---" - tail -n 10 "$SILENT_LOGFILE" + tail -n 10 "$logfile" echo "-----------------------------------" # Show how to view full log if there are more lines if [[ $log_lines -gt 10 ]]; then - msg_custom "📋" "${YW}" "View full log (${log_lines} lines): /tmp/install-*_${SESSION_ID:-*}.log" + msg_custom "📋" "${YW}" "View full log (${log_lines} lines): ${logfile}" fi fi diff --git a/misc/error_handler.func b/misc/error_handler.func index a4d10e692..17fc12a0b 100644 --- a/misc/error_handler.func +++ b/misc/error_handler.func @@ -196,30 +196,39 @@ error_handler() { } >>"$DEBUG_LOGFILE" fi - if [[ -n "${SILENT_LOGFILE:-}" && -s "$SILENT_LOGFILE" ]]; then + # Get active log file (BUILD_LOG or INSTALL_LOG) + local active_log="" + if declare -f get_active_logfile >/dev/null 2>&1; then + active_log="$(get_active_logfile)" + elif [[ -n "${SILENT_LOGFILE:-}" ]]; then + active_log="$SILENT_LOGFILE" + fi + + if [[ -n "$active_log" && -s "$active_log" ]]; then echo "--- Last 20 lines of silent log ---" - tail -n 20 "$SILENT_LOGFILE" + tail -n 20 "$active_log" echo "-----------------------------------" - # Copy log to container home for later retrieval (if running inside container via pct exec) - if [[ -d /root ]]; then + # Detect context: Container (INSTALL_LOG set + /root exists) vs Host (BUILD_LOG) + if [[ -n "${INSTALL_LOG:-}" && -d /root ]]; then + # CONTAINER CONTEXT: Copy log and create flag file for host local container_log="/root/.install-${SESSION_ID:-error}.log" - cp "$SILENT_LOGFILE" "$container_log" 2>/dev/null || true - + cp "$active_log" "$container_log" 2>/dev/null || true + # Create error flag file with exit code for host detection - echo "$exit_code" > "/root/.install-${SESSION_ID:-error}.failed" 2>/dev/null || true - + echo "$exit_code" >"/root/.install-${SESSION_ID:-error}.failed" 2>/dev/null || true + if declare -f msg_custom >/dev/null 2>&1; then msg_custom "📋" "${YW}" "Log saved to: ${container_log}" else echo -e "${YW}Log saved to:${CL} ${BL}${container_log}${CL}" fi else - # Running on host - show local path + # HOST CONTEXT: Show local log path if declare -f msg_custom >/dev/null 2>&1; then - msg_custom "📋" "${YW}" "Full log: ${SILENT_LOGFILE}" + msg_custom "📋" "${YW}" "Full log: ${active_log}" else - echo -e "${YW}Full log:${CL} ${BL}${SILENT_LOGFILE}${CL}" + echo -e "${YW}Full log:${CL} ${BL}${active_log}${CL}" fi fi fi @@ -302,9 +311,9 @@ catch_errors() { if [ "${STRICT_UNSET:-0}" = "1" ]; then set -u fi - + trap 'error_handler' ERR trap on_exit EXIT trap on_interrupt INT trap on_terminate TERM -} \ No newline at end of file +} diff --git a/misc/install.func b/misc/install.func index 3d0a08d33..1822fb582 100644 --- a/misc/install.func +++ b/misc/install.func @@ -28,6 +28,11 @@ # SECTION 1: INITIALIZATION # ============================================================================== +# Ensure INSTALL_LOG is set (exported from build.func, but fallback if missing) +if [[ -z "${INSTALL_LOG:-}" ]]; then + INSTALL_LOG="/root/.install-${SESSION_ID:-unknown}.log" +fi + if ! command -v curl >/dev/null 2>&1; then printf "\r\e[2K%b" '\033[93m Setup Source \033[m' >&2 apt-get update >/dev/null 2>&1