fetch local templates

This commit is contained in:
CanbiZ 2025-08-19 11:32:44 +02:00
parent b342474da8
commit 3fd51d7a40

View File

@ -258,34 +258,38 @@ fi
# Update LXC template list
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
msg_info "Updating LXC Template List"
if ! pveam update >/dev/null 2>&1; then
TEMPLATE_FALLBACK=$(pveam list "$TEMPLATE_STORAGE" | awk "/$TEMPLATE_SEARCH/ {print \$2}" | sort -t - -k 2 -V | tail -n1)
if [[ -z "$TEMPLATE_FALLBACK" ]]; then
msg_error "Failed to update LXC template list and no local template matching '$TEMPLATE_SEARCH' found."
exit 201
fi
msg_info "Skipping template update using local fallback: $TEMPLATE_FALLBACK"
else
msg_ok "LXC Template List Updated"
fi
msg_info "Searching for online LXC template for '$TEMPLATE_SEARCH'..."
mapfile -t TEMPLATES < <(
pveam update >/dev/null 2>&1 &&
pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V
)
# Get LXC template string
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V)
# If nothing found online, search local templates
if [ ${#TEMPLATES[@]} -eq 0 ]; then
msg_info "Online search failed or no template found. Checking for local fallbacks..."
mapfile -t TEMPLATES < <(
pveam list "$TEMPLATE_STORAGE" | awk "/$TEMPLATE_SEARCH/ {print \$2}" | sort -t - -k 2 -V
)
if [ ${#TEMPLATES[@]} -eq 0 ]; then
msg_error "No matching LXC template found for '${TEMPLATE_SEARCH}'. Make sure your host can reach the Proxmox template repository."
msg_error "No online or local LXC template found for '${TEMPLATE_SEARCH}'. Please check network or install a template manually."
exit 207
fi
msg_ok "Found local fallback template."
else
msg_ok "Found online template."
fi
# Use the newest available template (last in sorted list)
TEMPLATE="${TEMPLATES[-1]}"
TEMPLATE_PATH="$(pvesm path $TEMPLATE_STORAGE:vztmpl/$TEMPLATE 2>/dev/null || echo "/var/lib/vz/template/cache/$TEMPLATE")"
msg_debug "TEMPLATE_SEARCH=$TEMPLATE_SEARCH"
msg_debug "TEMPLATES=(${TEMPLATES[*]})"
msg_debug "Selected TEMPLATE=$TEMPLATE"
msg_debug "TEMPLATE_PATH=$TEMPLATE_PATH"
# Validate that template file exists and is not corrupted
TEMPLATE_VALID=1
if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE"; then
TEMPLATE_VALID=0