From cb648eec9cb8d4e537cc869cfdc4fbfc42c09102 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Tue, 3 Mar 2026 13:28:41 +0100 Subject: [PATCH] Track preflight aborts and add exit codes Add preflight/validation exit codes and telemetry reporting for aborted preflight checks. Introduces new exit codes (103-110) and maps them to a new "preflight" error category. Adds post_preflight_to_api(), a fire-and-forget telemetry sender that posts an "aborted" status and detailed error summary (only when diagnostics/UUIDs are present), and extends post_update_to_api() to handle an "aborted" status. Changes are defensive (no blocking failures, short curl timeout) and aim to improve analytics for validation/preflight failures. --- misc/api.func | 95 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/misc/api.func b/misc/api.func index 62635b355..2abb61e60 100644 --- a/misc/api.func +++ b/misc/api.func @@ -12,6 +12,7 @@ # Features: # - Container/VM creation statistics # - Installation success/failure tracking +# - Pre-flight abort tracking ("aborted" status) # - Error code mapping and reporting # - Privacy-respecting anonymous telemetry # @@ -189,6 +190,16 @@ explain_exit_code() { 101) echo "APT: Configuration error (bad sources.list, malformed config)" ;; 102) echo "APT: Lock held by another process (dpkg/apt still running)" ;; + # --- Validation / Preflight (103-108) --- + 103) echo "Validation: Shell is not Bash" ;; + 104) echo "Validation: Not running as root (or invoked via sudo)" ;; + 105) echo "Validation: Proxmox VE version not supported" ;; + 106) echo "Validation: Architecture not supported (ARM / PiMox)" ;; + 107) echo "Validation: Kernel key parameters unreadable" ;; + 108) echo "Validation: Kernel key limits exceeded" ;; + 109) echo "Proxmox: No available container ID after max attempts" ;; + 110) echo "Proxmox: Failed to apply default.vars" ;; + # --- BSD sysexits.h (64-78) --- 64) echo "Usage error (wrong arguments)" ;; 65) echo "Data format error (bad input data)" ;; @@ -768,6 +779,76 @@ post_progress_to_api() { -d "{\"random_id\":\"${RANDOM_UUID}\",\"execution_id\":\"${EXECUTION_ID:-${RANDOM_UUID}}\",\"type\":\"${telemetry_type}\",\"nsapp\":\"${app_name}\",\"status\":\"${progress_status}\"}" &>/dev/null || true } +# ------------------------------------------------------------------------------ +# post_preflight_to_api() +# +# - Reports preflight failure to telemetry with "aborted" status +# - Uses PREFLIGHT_FAILURES array from build.func for error details +# - Sends error_category "preflight" for analytics grouping +# - Fire-and-forget: never blocks or fails script execution +# ------------------------------------------------------------------------------ +post_preflight_to_api() { + # Silent fail - telemetry should never break scripts + command -v curl &>/dev/null || return 0 + [[ "${DIAGNOSTICS:-no}" == "no" ]] && return 0 + [[ -z "${RANDOM_UUID:-}" ]] && return 0 + + # Set type for telemetry + TELEMETRY_TYPE="lxc" + + local pve_version="" + if command -v pveversion &>/dev/null; then + pve_version=$(pveversion 2>/dev/null | awk -F'[/ ]' '{print $2}') || true + fi + + # Build error summary from preflight failures + local error_summary="" + for failure in "${PREFLIGHT_FAILURES[@]}"; do + local code="${failure%%|*}" + local msg="${failure#*|}" + if [[ -n "$error_summary" ]]; then + error_summary="${error_summary}\n" + fi + error_summary="${error_summary}[${code}] ${msg}" + done + error_summary=$(json_escape "$error_summary") + + local exit_code="${PREFLIGHT_EXIT_CODE:-1}" + local short_error + short_error=$(json_escape "$(explain_exit_code "$exit_code")") + local error_category="preflight" + + local JSON_PAYLOAD + JSON_PAYLOAD=$( + cat </dev/null || true +} + # ------------------------------------------------------------------------------ # post_update_to_api() # @@ -815,7 +896,7 @@ post_update_to_api() { # Get RAM info (if detected) local ram_speed="${RAM_SPEED:-}" - # Map status to telemetry values: installing, success, failed, unknown + # Map status to telemetry values: installing, success, failed, aborted, unknown case "$status" in done | success) pb_status="success" @@ -826,14 +907,17 @@ post_update_to_api() { failed) pb_status="failed" ;; + aborted) + pb_status="aborted" + ;; *) pb_status="unknown" ;; esac - # For failed/unknown status, resolve exit code and error description + # For failed/aborted/unknown status, resolve exit code and error description local short_error="" - if [[ "$pb_status" == "failed" ]] || [[ "$pb_status" == "unknown" ]]; then + if [[ "$pb_status" == "failed" ]] || [[ "$pb_status" == "aborted" ]] || [[ "$pb_status" == "unknown" ]]; then if [[ "$raw_exit_code" =~ ^[0-9]+$ ]]; then exit_code="$raw_exit_code" else @@ -994,7 +1078,7 @@ EOF # categorize_error() # # - Maps exit codes to error categories for better analytics -# - Categories: network, storage, dependency, permission, timeout, config, resource, unknown +# - Categories: network, storage, dependency, permission, timeout, config, resource, preflight, unknown # - Used to group errors in dashboard # ------------------------------------------------------------------------------ categorize_error() { @@ -1018,6 +1102,9 @@ categorize_error() { # Permission errors 126 | 152) echo "permission" ;; + # Validation / Preflight errors + 103 | 104 | 105 | 106 | 107 | 108) echo "preflight" ;; + # Configuration errors (Proxmox config, invalid args) 128 | 203 | 204 | 205 | 206 | 207 | 208) echo "config" ;;