Update build.func
Some checks failed
Bump build.func Revision / bump-revision (push) Has been cancelled

This commit is contained in:
CanbiZ 2025-10-21 15:48:44 +02:00
parent ff3bd05452
commit 0c2382c2d7

View File

@ -2988,86 +2988,74 @@ create_lxc_container() {
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Template discovery & validation # Template discovery & validation
# Simple approach: var_os (alpine/ubuntu/debian) + var_version (24.04/11/12/13/22.04)
# 1. Check local storage
# 2. Check for newer version in pveam
# 3. If not found at all, offer alternatives
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}" msg_info "Finding template for ${PCT_OSTYPE} ${PCT_OSVERSION}"
# Determine template pattern based on OS
case "$PCT_OSTYPE" in case "$PCT_OSTYPE" in
debian | ubuntu) TEMPLATE_PATTERN="-standard_" ;; debian | ubuntu) TEMPLATE_PATTERN="-standard_" ;;
alpine | fedora | rocky | centos) TEMPLATE_PATTERN="-default_" ;; alpine | fedora | rocky | centos) TEMPLATE_PATTERN="-default_" ;;
*) TEMPLATE_PATTERN="" ;; *) TEMPLATE_PATTERN="-" ;;
esac esac
msg_info "Searching for template '$TEMPLATE_SEARCH'" # Step 1: Search in local storage
LOCAL_TEMPLATE=$(pveam list "$TEMPLATE_STORAGE" 2>/dev/null | awk '{print $NF}' | grep "^${PCT_OSTYPE}-${PCT_OSVERSION}.*${TEMPLATE_PATTERN}" | sort -V | tail -1)
# Build regex patterns outside awk/grep for clarity
SEARCH_PATTERN="^${TEMPLATE_SEARCH}-"
echo "[DEBUG] TEMPLATE_SEARCH='$TEMPLATE_SEARCH'"
echo "[DEBUG] SEARCH_PATTERN='$SEARCH_PATTERN'"
echo "[DEBUG] TEMPLATE_PATTERN='$TEMPLATE_PATTERN'"
mapfile -t LOCAL_TEMPLATES < <(
pveam list "$TEMPLATE_STORAGE" 2>/dev/null |
awk -v search="${SEARCH_PATTERN}" -v pattern="${TEMPLATE_PATTERN}" '$1 ~ search && $1 ~ pattern {print $1}' |
sed 's|.*/||' | sort -t - -k 2 -V
)
pveam update >/dev/null 2>&1 || msg_warn "Could not update template catalog (pveam update failed)."
echo "[DEBUG] pveam available output (first 5 lines with .tar files):"
pveam available -section system 2>/dev/null | grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | head -5 | sed 's/^/ /'
mapfile -t ONLINE_TEMPLATES < <(
pveam available -section system 2>/dev/null |
grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' |
awk '{print $NF}' |
grep -E "${SEARCH_PATTERN}.*${TEMPLATE_PATTERN}" |
sort -V 2>/dev/null
)
echo "[DEBUG] After filtering: ${#ONLINE_TEMPLATES[@]} online templates found" # Step 2: Check pveam for available version (could be newer)
if [[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]]; then ONLINE_TEMPLATE=$(pveam available -section system 2>/dev/null | awk '{print $NF}' | grep "^${PCT_OSTYPE}-${PCT_OSVERSION}.*${TEMPLATE_PATTERN}" | sort -V | tail -1)
echo "[DEBUG] Online templates:"
for tmpl in "${ONLINE_TEMPLATES[@]}"; do # Decide what to use
echo " - $tmpl" if [[ -n "$LOCAL_TEMPLATE" && -n "$ONLINE_TEMPLATE" ]]; then
done # Both exist - check if online is newer
fi if [[ "$LOCAL_TEMPLATE" != "$ONLINE_TEMPLATE" ]]; then
msg_info "Local template: $LOCAL_TEMPLATE"
ONLINE_TEMPLATE="" msg_info "Newer version available: $ONLINE_TEMPLATE"
[[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]] && ONLINE_TEMPLATE="${ONLINE_TEMPLATES[-1]}" echo ""
read -p "Download newer version? [y/N]: " answer
msg_debug "SEARCH_PATTERN='${SEARCH_PATTERN}' TEMPLATE_PATTERN='${TEMPLATE_PATTERN}'" case "${answer,,}" in
msg_debug "Found ${#LOCAL_TEMPLATES[@]} local templates, ${#ONLINE_TEMPLATES[@]} online templates" y|yes)
if [[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]]; then TEMPLATE="$ONLINE_TEMPLATE"
msg_debug "First 3 online templates:" TEMPLATE_SOURCE="online"
for i in {0..2}; do msg_ok "Using newer version: $TEMPLATE"
[[ -n "${ONLINE_TEMPLATES[$i]}" ]] && msg_debug " [$i]: ${ONLINE_TEMPLATES[$i]}" ;;
done *)
fi TEMPLATE="$LOCAL_TEMPLATE"
msg_debug "ONLINE_TEMPLATE='$ONLINE_TEMPLATE'" TEMPLATE_SOURCE="local"
msg_ok "Using local version: $TEMPLATE"
if [[ ${#LOCAL_TEMPLATES[@]} -gt 0 ]]; then ;;
TEMPLATE="${LOCAL_TEMPLATES[-1]}" esac
else
# Same version
TEMPLATE="$LOCAL_TEMPLATE"
TEMPLATE_SOURCE="local"
msg_ok "Using local template: $TEMPLATE"
fi
elif [[ -n "$LOCAL_TEMPLATE" ]]; then
# Only local exists
TEMPLATE="$LOCAL_TEMPLATE"
TEMPLATE_SOURCE="local" TEMPLATE_SOURCE="local"
else msg_ok "Using local template: $TEMPLATE"
elif [[ -n "$ONLINE_TEMPLATE" ]]; then
# Only online exists
TEMPLATE="$ONLINE_TEMPLATE" TEMPLATE="$ONLINE_TEMPLATE"
TEMPLATE_SOURCE="online" TEMPLATE_SOURCE="online"
fi msg_ok "Template found online: $TEMPLATE"
else
# If still no template, try to find alternatives # Nothing found - offer alternatives
if [[ -z "$TEMPLATE" ]]; then msg_error "No template found for ${PCT_OSTYPE} ${PCT_OSVERSION}"
echo ""
echo "[DEBUG] No template found for ${PCT_OSTYPE} ${PCT_OSVERSION}, searching for alternatives..."
# Get all available versions for this OS type
mapfile -t AVAILABLE_VERSIONS < <( mapfile -t AVAILABLE_VERSIONS < <(
pveam available -section system 2>/dev/null | pveam available -section system 2>/dev/null |
grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | awk '{print $NF}' |
awk -F'\t' '{print $1}' |
grep "^${PCT_OSTYPE}-" | grep "^${PCT_OSTYPE}-" |
sed -E "s/.*${PCT_OSTYPE}-([0-9]+(\.[0-9]+)?).*/\1/" | sed -E "s/.*${PCT_OSTYPE}-([0-9]+(\.[0-9]+)?).*/\1/" |
sort -u -V 2>/dev/null sort -u -V
) )
if [[ ${#AVAILABLE_VERSIONS[@]} -gt 0 ]]; then if [[ ${#AVAILABLE_VERSIONS[@]} -gt 0 ]]; then
echo "" echo ""
echo "${BL}Available ${PCT_OSTYPE} versions:${CL}" echo "${BL}Available ${PCT_OSTYPE} versions:${CL}"
@ -3076,36 +3064,23 @@ create_lxc_container() {
done done
echo "" echo ""
read -p "Select version [1-${#AVAILABLE_VERSIONS[@]}] or press Enter to cancel: " choice read -p "Select version [1-${#AVAILABLE_VERSIONS[@]}] or press Enter to cancel: " choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le ${#AVAILABLE_VERSIONS[@]} ]]; then if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le ${#AVAILABLE_VERSIONS[@]} ]]; then
PCT_OSVERSION="${AVAILABLE_VERSIONS[$((choice - 1))]}" PCT_OSVERSION="${AVAILABLE_VERSIONS[$((choice - 1))]}"
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION}" TEMPLATE=$(pveam available -section system 2>/dev/null | awk '{print $NF}' | grep "^${PCT_OSTYPE}-${PCT_OSVERSION}.*${TEMPLATE_PATTERN}" | sort -V | tail -1)
SEARCH_PATTERN="^${TEMPLATE_SEARCH}-" if [[ -n "$TEMPLATE" ]]; then
msg_ok "Selected: $TEMPLATE"
echo "[DEBUG] Retrying with version: $PCT_OSVERSION"
mapfile -t ONLINE_TEMPLATES < <(
pveam available -section system 2>/dev/null |
grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' |
awk '{print $NF}' |
grep -E "${SEARCH_PATTERN}.*${TEMPLATE_PATTERN}" |
sort -V 2>/dev/null
)
if [[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]]; then
TEMPLATE="${ONLINE_TEMPLATES[-1]}"
TEMPLATE_SOURCE="online" TEMPLATE_SOURCE="online"
echo "[DEBUG] Found alternative: $TEMPLATE"
else else
msg_error "No templates available for ${PCT_OSTYPE} ${PCT_OSVERSION}" msg_error "No template found for ${PCT_OSTYPE} ${PCT_OSVERSION}"
exit 225 exit 225
fi fi
else else
msg_info "Installation cancelled" msg_info "Cancelled"
exit 0 exit 0
fi fi
else else
msg_error "No ${PCT_OSTYPE} templates available at all" msg_error "No templates available for ${PCT_OSTYPE}"
exit 225 exit 225
fi fi
fi fi