Improve container install error handling and logging

Enhances reliability of application installation error detection in containers by using an error flag file, improves log copying and user prompts, and updates error handler to create a flag file with the exit code for host-side detection.
This commit is contained in:
CanbiZ 2025-11-17 12:23:51 +01:00
parent 5386da93e8
commit 06a4091019
2 changed files with 35 additions and 12 deletions

View File

@ -2577,37 +2577,56 @@ EOF'
install_ssh_keys_into_ct install_ssh_keys_into_ct
# Run application installer # 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 lxc-attach -n "$CTID" -- bash -c "$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main/install/${var_install}.sh)"
local install_exit_code=$? local lxc_exit=$?
# Check for error flag file in container (more reliable than lxc-attach exit code)
local install_exit_code=0
if [[ -n "${SESSION_ID:-}" ]]; then
local error_flag="/root/.install-${SESSION_ID}.failed"
if pct exec "$CTID" -- test -f "$error_flag" 2>/dev/null; then
install_exit_code=$(pct exec "$CTID" -- cat "$error_flag" 2>/dev/null || echo "1")
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})" msg_error "Installation failed in container ${CTID} (exit code: ${install_exit_code})"
# Try to copy installation log from container before cleanup prompt # Try to copy installation log from container (without spinner/msg_info)
if [[ -n "$CTID" && -n "${SESSION_ID:-}" ]]; then if [[ -n "$CTID" && -n "${SESSION_ID:-}" ]]; then
pct pull "$CTID" "/root/.install-${SESSION_ID}.log" "/tmp/install-${SESSION_ID}.log" 2>/dev/null || true if pct pull "$CTID" "/root/.install-${SESSION_ID}.log" "/tmp/install-${SESSION_ID}.log" 2>/dev/null; then
msg_info "Installation log copied to /tmp/install-${SESSION_ID}.log" echo -e "${GN}✔${CL} Installation log: ${BL}/tmp/install-${SESSION_ID}.log${CL}"
fi
fi fi
# Prompt user for cleanup with 60s timeout # Prompt user for cleanup with 60s timeout (plain echo - no msg_info to avoid spinner)
local response echo ""
echo -en "${YW}Remove broken container ${CTID}? (Y/n) [auto-remove in 60s]: ${CL}" echo -en "${YW}Remove broken container ${CTID}? (Y/n) [auto-remove in 60s]: ${CL}"
if read -t 60 -r response; then if read -t 60 -r response; then
if [[ -z "$response" || "$response" =~ ^[Yy]$ ]]; then if [[ -z "$response" || "$response" =~ ^[Yy]$ ]]; then
# Remove container # Remove container
msg_info "Removing container ${CTID}" echo -e "\n${TAB}${HOLD}${YW}Removing container ${CTID}${CL}"
pct stop "$CTID" &>/dev/null || true pct stop "$CTID" &>/dev/null || true
pct destroy "$CTID" &>/dev/null || true pct destroy "$CTID" &>/dev/null || true
msg_ok "Container ${CTID} removed" echo -e "${BFR}${CM}${GN}Container ${CTID} removed${CL}"
elif [[ "$response" =~ ^[Nn]$ ]]; then elif [[ "$response" =~ ^[Nn]$ ]]; then
msg_info "Container ${CTID} kept for debugging" echo -e "\n${TAB}${YW}Container ${CTID} kept for debugging${CL}"
fi fi
else else
# Timeout - auto-remove # Timeout - auto-remove
echo -e "\n${YW}No response - auto-removing container${CL}" echo -e "\n${YW}No response - auto-removing container${CL}"
msg_info "Removing container ${CTID}" echo -e "${TAB}${HOLD}${YW}Removing container ${CTID}${CL}"
pct stop "$CTID" &>/dev/null || true pct stop "$CTID" &>/dev/null || true
pct destroy "$CTID" &>/dev/null || true pct destroy "$CTID" &>/dev/null || true
msg_ok "Container ${CTID} removed" echo -e "${BFR}${CM}${GN}Container ${CTID} removed${CL}"
fi fi
exit $install_exit_code exit $install_exit_code

View File

@ -205,6 +205,10 @@ error_handler() {
if [[ -d /root ]]; then if [[ -d /root ]]; then
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 "$SILENT_LOGFILE" "$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
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