From 1ae327524882bb94a47dea0caaeeda47b1004248 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Tue, 3 Mar 2026 13:36:02 +0100 Subject: [PATCH] Add template availability preflight check Introduce preflight_template_available() in misc/build.func to verify a matching OS/version template exists locally or in the online pveam catalog, and to fail early with a helpful list of available versions when none is found (exit code 225). Integrate this check into run_preflight. Also move the run_preflight invocation inside install_script to run after header_info (so preflight output is visible) and remove the earlier preflight call. --- misc/build.func | 68 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/misc/build.func b/misc/build.func index 3f59c26f0..327056ce3 100644 --- a/misc/build.func +++ b/misc/build.func @@ -472,6 +472,69 @@ preflight_template_connectivity() { return 1 } +# ------------------------------------------------------------------------------ +# preflight_template_available() +# +# - Validates that a template exists for the configured var_os/var_version +# - Checks both local templates and the online pveam catalog +# - Fails if no matching template can be found anywhere +# ------------------------------------------------------------------------------ +preflight_template_available() { + local os="${var_os:-}" + local version="${var_version:-}" + + # Skip if os/version not set (e.g. Alpine scripts set them differently) + if [[ -z "$os" || -z "$version" ]]; then + preflight_pass "Template check skipped (OS/version not configured yet)" + return 0 + fi + + local search_pattern="${os}-${version}" + + # Check local templates first + local local_match=0 + while read -r storage_name _; do + [[ -z "$storage_name" ]] && continue + if pveam list "$storage_name" 2>/dev/null | awk '{print $1}' | grep -qE "^${storage_name}:vztmpl/${search_pattern}"; then + local_match=1 + break + fi + done < <(pvesm status -content vztmpl 2>/dev/null | awk 'NR>1 {print $1}') + + if [[ "$local_match" -eq 1 ]]; then + preflight_pass "Template available locally for ${os} ${version}" + return 0 + fi + + # Check online catalog + local online_match=0 + if pveam available -section system 2>/dev/null | awk '{print $2}' | grep -qE "^${search_pattern}[.-]"; then + online_match=1 + fi + + if [[ "$online_match" -eq 1 ]]; then + preflight_pass "Template available online for ${os} ${version}" + return 0 + fi + + # Gather available versions for the hint + local available_versions + available_versions=$( + pveam available -section system 2>/dev/null | + awk '{print $2}' | + grep -oE "^${os}-[0-9]+(\.[0-9]+)?" | + sed "s/^${os}-//" | + sort -uV 2>/dev/null | tr '\n' ', ' | sed 's/,$//' | sed 's/,/, /g' + ) + + preflight_fail "No template found for ${os} ${version}" 225 + if [[ -n "$available_versions" ]]; then + echo -e " ${TAB}${INFO} Available ${os} versions: ${GN}${available_versions}${CL}" + fi + echo -e " ${TAB}${INFO} Check var_version in your CT script or use an available version" + return 1 +} + # ------------------------------------------------------------------------------ # run_preflight() # @@ -511,6 +574,7 @@ run_preflight() { # --- Template availability --- preflight_template_connectivity + preflight_template_available echo "" @@ -3379,7 +3443,6 @@ install_script() { arch_check ssh_check diagnostics_check - run_preflight if systemctl is-active -q ping-instances.service; then systemctl -q stop ping-instances.service @@ -3398,8 +3461,9 @@ install_script() { fi [[ "${timezone:-}" == Etc/* ]] && timezone="host" # pct doesn't accept Etc/* zones - # Show APP Header + # Show APP Header then run preflight (after header_info so output is visible) header_info + run_preflight # --- Support CLI argument as direct preset (default, advanced, …) --- CHOICE="${mode:-${1:-}}"