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.
This commit is contained in:
parent
06a4091019
commit
c2b7f4e298
@ -47,6 +47,7 @@ variables() {
|
|||||||
METHOD="default" # sets the METHOD variable to "default", used for the API call.
|
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.
|
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
|
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}}"}
|
CTTYPE="${CTTYPE:-${CT_TYPE:-1}}"}
|
||||||
|
|
||||||
# Get Proxmox VE version and kernel version
|
# Get Proxmox VE version and kernel version
|
||||||
@ -2206,6 +2207,8 @@ build_container() {
|
|||||||
export DIAGNOSTICS="$DIAGNOSTICS"
|
export DIAGNOSTICS="$DIAGNOSTICS"
|
||||||
export RANDOM_UUID="$RANDOM_UUID"
|
export RANDOM_UUID="$RANDOM_UUID"
|
||||||
export SESSION_ID="$SESSION_ID"
|
export SESSION_ID="$SESSION_ID"
|
||||||
|
export BUILD_LOG="$BUILD_LOG"
|
||||||
|
export INSTALL_LOG="/root/.install-${SESSION_ID}.log"
|
||||||
export CACHER="$APT_CACHER"
|
export CACHER="$APT_CACHER"
|
||||||
export CACHER_IP="$APT_CACHER_IP"
|
export CACHER_IP="$APT_CACHER_IP"
|
||||||
export tz="$timezone"
|
export tz="$timezone"
|
||||||
@ -2576,10 +2579,12 @@ EOF'
|
|||||||
# Install SSH keys
|
# Install SSH keys
|
||||||
install_ssh_keys_into_ct
|
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)"
|
lxc-attach -n "$CTID" -- bash -c "$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/install/${var_install}.sh)"
|
||||||
local lxc_exit=$?
|
local lxc_exit=$?
|
||||||
|
set -e
|
||||||
|
|
||||||
# Check for error flag file in container (more reliable than lxc-attach exit code)
|
# Check for error flag file in container (more reliable than lxc-attach exit code)
|
||||||
local install_exit_code=0
|
local install_exit_code=0
|
||||||
if [[ -n "${SESSION_ID:-}" ]]; then
|
if [[ -n "${SESSION_ID:-}" ]]; then
|
||||||
@ -2589,21 +2594,35 @@ EOF'
|
|||||||
pct exec "$CTID" -- rm -f "$error_flag" 2>/dev/null || true
|
pct exec "$CTID" -- rm -f "$error_flag" 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Fallback to lxc-attach exit code if no flag file
|
# Fallback to lxc-attach exit code if no flag file
|
||||||
if [[ $install_exit_code -eq 0 && $lxc_exit -ne 0 ]]; then
|
if [[ $install_exit_code -eq 0 && $lxc_exit -ne 0 ]]; then
|
||||||
install_exit_code=$lxc_exit
|
install_exit_code=$lxc_exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Installation failed?
|
# Installation failed?
|
||||||
if [[ $install_exit_code -ne 0 ]]; then
|
if [[ $install_exit_code -ne 0 ]]; then
|
||||||
msg_error "Installation failed in container ${CTID} (exit code: ${install_exit_code})"
|
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 [[ -n "$CTID" && -n "${SESSION_ID:-}" ]]; then
|
||||||
if pct pull "$CTID" "/root/.install-${SESSION_ID}.log" "/tmp/install-${SESSION_ID}.log" 2>/dev/null; then
|
# Copy BUILD_LOG (creation log) if it exists
|
||||||
echo -e "${GN}✔${CL} Installation log: ${BL}/tmp/install-${SESSION_ID}.log${CL}"
|
if [[ -f "${BUILD_LOG}" ]]; then
|
||||||
|
cp "${BUILD_LOG}" "/tmp/create-lxc-${CTID}-${SESSION_ID}.log" 2>/dev/null && build_log_copied=true
|
||||||
fi
|
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
|
fi
|
||||||
|
|
||||||
# Prompt user for cleanup with 60s timeout (plain echo - no msg_info to avoid spinner)
|
# Prompt user for cleanup with 60s timeout (plain echo - no msg_info to avoid spinner)
|
||||||
|
|||||||
@ -287,12 +287,32 @@ ssh_check() {
|
|||||||
# SECTION 3: EXECUTION HELPERS
|
# 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()
|
# 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
|
# - On error: displays last 10 lines of log and exits with original exit code
|
||||||
# - Temporarily disables error trap to capture exit code correctly
|
# - Temporarily disables error trap to capture exit code correctly
|
||||||
# - Sources explain_exit_code() for detailed error messages
|
# - 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() {
|
silent() {
|
||||||
local cmd="$*"
|
local cmd="$*"
|
||||||
local caller_line="${BASH_LINENO[0]:-unknown}"
|
local caller_line="${BASH_LINENO[0]:-unknown}"
|
||||||
|
local logfile="$(get_active_logfile)"
|
||||||
|
|
||||||
set +Eeuo pipefail
|
set +Eeuo pipefail
|
||||||
trap - ERR
|
trap - ERR
|
||||||
|
|
||||||
"$@" >>"$SILENT_LOGFILE" 2>&1
|
"$@" >>"$logfile" 2>&1
|
||||||
local rc=$?
|
local rc=$?
|
||||||
|
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
@ -323,15 +344,15 @@ silent() {
|
|||||||
msg_error "in line ${caller_line}: exit code ${rc} (${explanation})"
|
msg_error "in line ${caller_line}: exit code ${rc} (${explanation})"
|
||||||
msg_custom "→" "${YWB}" "${cmd}"
|
msg_custom "→" "${YWB}" "${cmd}"
|
||||||
|
|
||||||
if [[ -s "$SILENT_LOGFILE" ]]; then
|
if [[ -s "$logfile" ]]; then
|
||||||
local log_lines=$(wc -l <"$SILENT_LOGFILE")
|
local log_lines=$(wc -l <"$logfile")
|
||||||
echo "--- Last 10 lines of silent log ---"
|
echo "--- Last 10 lines of silent log ---"
|
||||||
tail -n 10 "$SILENT_LOGFILE"
|
tail -n 10 "$logfile"
|
||||||
echo "-----------------------------------"
|
echo "-----------------------------------"
|
||||||
|
|
||||||
# Show how to view full log if there are more lines
|
# Show how to view full log if there are more lines
|
||||||
if [[ $log_lines -gt 10 ]]; then
|
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
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
@ -196,30 +196,39 @@ error_handler() {
|
|||||||
} >>"$DEBUG_LOGFILE"
|
} >>"$DEBUG_LOGFILE"
|
||||||
fi
|
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 ---"
|
echo "--- Last 20 lines of silent log ---"
|
||||||
tail -n 20 "$SILENT_LOGFILE"
|
tail -n 20 "$active_log"
|
||||||
echo "-----------------------------------"
|
echo "-----------------------------------"
|
||||||
|
|
||||||
# Copy log to container home for later retrieval (if running inside container via pct exec)
|
# Detect context: Container (INSTALL_LOG set + /root exists) vs Host (BUILD_LOG)
|
||||||
if [[ -d /root ]]; then
|
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"
|
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
|
# 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
|
if declare -f msg_custom >/dev/null 2>&1; then
|
||||||
msg_custom "📋" "${YW}" "Log saved to: ${container_log}"
|
msg_custom "📋" "${YW}" "Log saved to: ${container_log}"
|
||||||
else
|
else
|
||||||
echo -e "${YW}Log saved to:${CL} ${BL}${container_log}${CL}"
|
echo -e "${YW}Log saved to:${CL} ${BL}${container_log}${CL}"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# Running on host - show local path
|
# HOST CONTEXT: Show local log path
|
||||||
if declare -f msg_custom >/dev/null 2>&1; then
|
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
|
else
|
||||||
echo -e "${YW}Full log:${CL} ${BL}${SILENT_LOGFILE}${CL}"
|
echo -e "${YW}Full log:${CL} ${BL}${active_log}${CL}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@ -302,9 +311,9 @@ catch_errors() {
|
|||||||
if [ "${STRICT_UNSET:-0}" = "1" ]; then
|
if [ "${STRICT_UNSET:-0}" = "1" ]; then
|
||||||
set -u
|
set -u
|
||||||
fi
|
fi
|
||||||
|
|
||||||
trap 'error_handler' ERR
|
trap 'error_handler' ERR
|
||||||
trap on_exit EXIT
|
trap on_exit EXIT
|
||||||
trap on_interrupt INT
|
trap on_interrupt INT
|
||||||
trap on_terminate TERM
|
trap on_terminate TERM
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,11 @@
|
|||||||
# SECTION 1: INITIALIZATION
|
# 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
|
if ! command -v curl >/dev/null 2>&1; then
|
||||||
printf "\r\e[2K%b" '\033[93m Setup Source \033[m' >&2
|
printf "\r\e[2K%b" '\033[93m Setup Source \033[m' >&2
|
||||||
apt-get update >/dev/null 2>&1
|
apt-get update >/dev/null 2>&1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user