Handle failed LXC install and cleanup in build.func

Moved failed LXC installation cleanup logic from error_handler.func to build.func. Now, if installation fails, the user is prompted to remove the broken container with a 60s timeout, and the installation log is copied for debugging. Removed the cleanup_failed_lxc function and related trap from error_handler.func for better encapsulation.
This commit is contained in:
CanbiZ 2025-11-17 12:11:09 +01:00
parent 14310879d2
commit 5386da93e8
2 changed files with 36 additions and 82 deletions

View File

@ -2578,12 +2578,39 @@ EOF'
# Run application installer
if ! lxc-attach -n "$CTID" -- bash -c "$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/install/${var_install}.sh)"; then
local exit_code=$?
# Try to copy installation log from container before exiting
local install_exit_code=$?
msg_error "Installation failed in container ${CTID} (exit code: ${install_exit_code})"
# Try to copy installation log from container before cleanup prompt
if [[ -n "$CTID" && -n "${SESSION_ID:-}" ]]; then
pct pull "$CTID" "/root/.install-${SESSION_ID}.log" "/tmp/install-${SESSION_ID}.log" 2>/dev/null || true
msg_info "Installation log copied to /tmp/install-${SESSION_ID}.log"
fi
exit $exit_code
# Prompt user for cleanup with 60s timeout
local response
echo -en "${YW}Remove broken container ${CTID}? (Y/n) [auto-remove in 60s]: ${CL}"
if read -t 60 -r response; then
if [[ -z "$response" || "$response" =~ ^[Yy]$ ]]; then
# Remove container
msg_info "Removing container ${CTID}"
pct stop "$CTID" &>/dev/null || true
pct destroy "$CTID" &>/dev/null || true
msg_ok "Container ${CTID} removed"
elif [[ "$response" =~ ^[Nn]$ ]]; then
msg_info "Container ${CTID} kept for debugging"
fi
else
# Timeout - auto-remove
echo -e "\n${YW}No response - auto-removing container${CL}"
msg_info "Removing container ${CTID}"
pct stop "$CTID" &>/dev/null || true
pct destroy "$CTID" &>/dev/null || true
msg_ok "Container ${CTID} removed"
fi
exit $install_exit_code
fi
}

View File

@ -224,81 +224,7 @@ error_handler() {
}
# ==============================================================================
# SECTION 3: CLEANUP HANDLERS
# ==============================================================================
# ------------------------------------------------------------------------------
# cleanup_failed_lxc()
#
# - Cleanup handler for failed LXC installations
# - Only prompts during install phase (when CTID is set)
# - Asks user if they want to remove the broken container
# - 60 second timeout - auto-removes if no response
# - Stops and destroys container on timeout or confirmation
# - Silent if CTID not set or container doesn't exist
# ------------------------------------------------------------------------------
cleanup_failed_lxc() {
local exit_code=$?
# Only cleanup on error and during install (CTID must be set)
if [[ $exit_code -eq 0 || -z "${CTID:-}" ]]; then
return 0
fi
# Check if container exists
if ! pct status "$CTID" &>/dev/null; then
return 0
fi
# Prompt user for cleanup
if declare -f msg_warn >/dev/null 2>&1; then
msg_warn "Installation failed for container ${CTID}"
else
echo -e "\n${YW}[WARN]${CL} Installation failed for container ${CTID}"
fi
# Ask for confirmation with 60s timeout
local response
echo -en "${YW}Remove broken container ${CTID}? (Y/n) [auto-remove in 60s]: ${CL}"
if read -t 60 -r response; then
# User provided input within timeout
if [[ -z "$response" || "$response" =~ ^[Yy]$ ]]; then
# Empty (Enter) or Y/y = remove
:
elif [[ "$response" =~ ^[Nn]$ ]]; then
# N/n = keep for debugging
if declare -f msg_info >/dev/null 2>&1; then
msg_info "Container ${CTID} kept for debugging"
else
echo -e "${YW}Container ${CTID} kept for debugging${CL}"
fi
return 0
fi
else
# Timeout reached - auto-remove
echo -e "\n${YW}No response - auto-removing container${CL}"
fi
# Cleanup confirmed or timeout
if declare -f msg_info >/dev/null 2>&1; then
msg_info "Removing container ${CTID}"
else
echo -e "${YW}Removing container ${CTID}...${CL}"
fi
pct stop "$CTID" &>/dev/null || true
pct destroy "$CTID" &>/dev/null || true
if declare -f msg_ok >/dev/null 2>&1; then
msg_ok "Container ${CTID} removed"
else
echo -e "${GN}Container ${CTID} removed${CL}"
fi
}
# ==============================================================================
# SECTION 4: SIGNAL HANDLERS
# SECTION 3: SIGNAL HANDLERS
# ==============================================================================
# ------------------------------------------------------------------------------
@ -349,7 +275,7 @@ on_terminate() {
}
# ==============================================================================
# SECTION 5: INITIALIZATION
# SECTION 4: INITIALIZATION
# ==============================================================================
# ------------------------------------------------------------------------------
@ -362,7 +288,7 @@ on_terminate() {
# * set -u: (optional) Exit on undefined variable (if STRICT_UNSET=1)
# - Sets up traps:
# * ERR → error_handler
# * EXIT → on_exit (+ cleanup_failed_lxc if in install context)
# * EXIT → on_exit
# * INT → on_interrupt
# * TERM → on_terminate
# - Call this function early in every script
@ -372,8 +298,9 @@ catch_errors() {
if [ "${STRICT_UNSET:-0}" = "1" ]; then
set -u
fi
trap 'error_handler' ERR
trap 'cleanup_failed_lxc; on_exit' EXIT
trap on_exit EXIT
trap on_interrupt INT
trap on_terminate TERM
}
}