Skip to content

Commit 4371d64

Browse files
committed
Refactor error handling and clean up debug comments
Standardized bash variable checks, removed unnecessary debug and commented code, and clarified error handling logic in container build and setup scripts. These changes improve code readability and maintainability without altering functional behavior.
1 parent 7bc0722 commit 4371d64

File tree

4 files changed

+7
-52
lines changed

4 files changed

+7
-52
lines changed

misc/alpine-install.func

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ catch_errors
1515
verb_ip6() {
1616
set_std_mode # Set STD mode based on VERBOSE
1717

18-
if [ "$IPV6_METHOD" == "disable" ]; then
18+
if [ "${IPV6_METHOD:-}" = "disable" ]; then
1919
msg_info "Disabling IPv6 (this may affect some services)"
2020
$STD sysctl -w net.ipv6.conf.all.disable_ipv6=1
2121
$STD sysctl -w net.ipv6.conf.default.disable_ipv6=1
@@ -42,7 +42,6 @@ error_handler() {
4242
local line_number="$2"
4343
local command="$3"
4444

45-
# Exitcode 0 = kein Fehler → ignorieren
4645
if [[ "$exit_code" -eq 0 ]]; then
4746
return 0
4847
fi

misc/build.func

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ elif command -v wget >/dev/null 2>&1; then
9292
source <(wget -qO- https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/error_handler.func)
9393
load_functions
9494
catch_errors
95-
#echo "(build.func) Loaded core.func via wget"
9695
fi
9796

9897
# ==============================================================================
@@ -2279,7 +2278,7 @@ build_container() {
22792278
none) ;;
22802279
esac
22812280

2282-
# Build FEATURES string (simple working version)
2281+
# Build FEATURES string
22832282
if [ "$CT_TYPE" == "1" ]; then
22842283
FEATURES="keyctl=1,nesting=1"
22852284
else
@@ -2290,9 +2289,7 @@ build_container() {
22902289
FEATURES="$FEATURES,fuse=1"
22912290
fi
22922291

2293-
# NEW IMPLEMENTATION (Fixed): Build PCT_OPTIONS properly
2294-
# Key insight: Bash cannot export arrays, so we build the options as a string
2295-
2292+
# Build PCT_OPTIONS as string for export
22962293
TEMP_DIR=$(mktemp -d)
22972294
pushd "$TEMP_DIR" >/dev/null
22982295
if [ "$var_os" == "alpine" ]; then
@@ -2382,11 +2379,6 @@ build_container() {
23822379
export TEMPLATE_STORAGE="${var_template_storage:-}"
23832380
export CONTAINER_STORAGE="${var_container_storage:-}"
23842381

2385-
# # DEBUG: Show final PCT_OPTIONS being exported
2386-
# echo "[DEBUG] PCT_OPTIONS to be exported:"
2387-
# echo "$PCT_OPTIONS" | sed 's/^/ /'
2388-
# echo "[DEBUG] Calling create_lxc_container..."
2389-
23902382
create_lxc_container || exit $?
23912383

23922384
LXC_CONFIG="/etc/pve/lxc/${CTID}.conf"
@@ -2713,12 +2705,7 @@ EOF'
27132705
install_ssh_keys_into_ct
27142706

27152707
# Run application installer
2716-
# NOTE: We disable error handling here because:
2717-
# 1. Container errors are caught by error_handler INSIDE container
2718-
# 2. Container creates flag file with exit code
2719-
# 3. We read flag file and handle cleanup manually below
2720-
# 4. We DON'T want host error_handler to fire for lxc-attach command itself
2721-
2708+
# Disable error trap - container errors are handled internally via flag file
27222709
set +Eeuo pipefail # Disable ALL error handling temporarily
27232710
trap - ERR # Remove ERR trap completely
27242711

@@ -2864,7 +2851,6 @@ destroy_lxc() {
28642851
# ------------------------------------------------------------------------------
28652852
# Storage discovery / selection helpers
28662853
# ------------------------------------------------------------------------------
2867-
# ===== Storage discovery / selection helpers (ported from create_lxc.sh) =====
28682854
resolve_storage_preselect() {
28692855
local class="$1" preselect="$2" required_content=""
28702856
case "$class" in
@@ -3249,10 +3235,6 @@ create_lxc_container() {
32493235
# Build regex patterns outside awk/grep for clarity
32503236
SEARCH_PATTERN="^${TEMPLATE_SEARCH}"
32513237

3252-
#echo "[DEBUG] TEMPLATE_SEARCH='$TEMPLATE_SEARCH'"
3253-
#echo "[DEBUG] SEARCH_PATTERN='$SEARCH_PATTERN'"
3254-
#echo "[DEBUG] TEMPLATE_PATTERN='$TEMPLATE_PATTERN'"
3255-
32563238
mapfile -t LOCAL_TEMPLATES < <(
32573239
pveam list "$TEMPLATE_STORAGE" 2>/dev/null |
32583240
awk -v search="${SEARCH_PATTERN}" -v pattern="${TEMPLATE_PATTERN}" '$1 ~ search && $1 ~ pattern {print $1}' |
@@ -3263,30 +3245,13 @@ create_lxc_container() {
32633245

32643246
msg_ok "Template search completed"
32653247

3266-
#echo "[DEBUG] pveam available output (first 5 lines with .tar files):"
3267-
#pveam available -section system 2>/dev/null | grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | head -5 | sed 's/^/ /'
3268-
32693248
set +u
32703249
mapfile -t ONLINE_TEMPLATES < <(pveam available -section system 2>/dev/null | grep -E '\.(tar\.zst|tar\.xz|tar\.gz)$' | awk '{print $2}' | grep -E "${SEARCH_PATTERN}.*${TEMPLATE_PATTERN}" | sort -t - -k 2 -V 2>/dev/null || true)
3271-
#echo "[DEBUG] After filtering: ${#ONLINE_TEMPLATES[@]} online templates found"
32723250
set -u
32733251

32743252
ONLINE_TEMPLATE=""
32753253
[[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]] && ONLINE_TEMPLATE="${ONLINE_TEMPLATES[-1]}"
32763254

3277-
#msg_debug "SEARCH_PATTERN='${SEARCH_PATTERN}' TEMPLATE_PATTERN='${TEMPLATE_PATTERN}'"
3278-
#msg_debug "Found ${#LOCAL_TEMPLATES[@]} local templates, ${#ONLINE_TEMPLATES[@]} online templates"
3279-
if [[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]]; then
3280-
#msg_debug "First 3 online templates:"
3281-
count=0
3282-
for idx in "${!ONLINE_TEMPLATES[@]}"; do
3283-
#msg_debug " [$idx]: ${ONLINE_TEMPLATES[$idx]}"
3284-
((count++))
3285-
[[ $count -ge 3 ]] && break
3286-
done
3287-
fi
3288-
#msg_debug "ONLINE_TEMPLATE='$ONLINE_TEMPLATE'"
3289-
32903255
if [[ ${#LOCAL_TEMPLATES[@]} -gt 0 ]]; then
32913256
TEMPLATE="${LOCAL_TEMPLATES[-1]}"
32923257
TEMPLATE_SOURCE="local"
@@ -3337,7 +3302,6 @@ create_lxc_container() {
33373302
if [[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]]; then
33383303
TEMPLATE="${ONLINE_TEMPLATES[-1]}"
33393304
TEMPLATE_SOURCE="online"
3340-
#echo "[DEBUG] Found alternative: $TEMPLATE"
33413305
else
33423306
msg_error "No templates available for ${PCT_OSTYPE} ${PCT_OSVERSION}"
33433307
exit 225
@@ -3352,9 +3316,6 @@ create_lxc_container() {
33523316
fi
33533317
fi
33543318

3355-
#echo "[DEBUG] Selected TEMPLATE='$TEMPLATE' SOURCE='$TEMPLATE_SOURCE'"
3356-
#msg_debug "Selected TEMPLATE='$TEMPLATE' SOURCE='$TEMPLATE_SOURCE'"
3357-
33583319
TEMPLATE_PATH="$(pvesm path $TEMPLATE_STORAGE:vztmpl/$TEMPLATE 2>/dev/null || true)"
33593320
if [[ -z "$TEMPLATE_PATH" ]]; then
33603321
TEMPLATE_BASE=$(awk -v s="$TEMPLATE_STORAGE" '$1==s {f=1} f && /path/ {print $2; exit}' /etc/pve/storage.cfg)

misc/install.func

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ catch_errors
5151
verb_ip6() {
5252
set_std_mode # Set STD mode based on VERBOSE
5353

54-
if [ "$IPV6_METHOD" == "disable" ]; then
54+
if [ "${IPV6_METHOD:-}" = "disable" ]; then
5555
msg_info "Disabling IPv6 (this may affect some services)"
5656
mkdir -p /etc/sysctl.d
5757
$STD tee /etc/sysctl.d/99-disable-ipv6.conf >/dev/null <<EOF

misc/tools.func

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,7 +3238,6 @@ function setup_mongodb() {
32383238
return 1
32393239
}
32403240

3241-
# Verify MongoDB was installed correctly
32423241
if ! command -v mongod >/dev/null 2>&1; then
32433242
msg_error "MongoDB binary not found after installation"
32443243
return 1
@@ -3481,14 +3480,11 @@ function setup_nodejs() {
34813480
return 1
34823481
}
34833482

3484-
# CRITICAL: Force APT cache refresh AFTER repository setup
3485-
# This ensures NodeSource is the only nodejs source in APT cache
3483+
# Force APT cache refresh after repository setup
34863484
$STD apt update
34873485

3488-
# Install dependencies (NodeSource is now the only nodejs source)
34893486
ensure_dependencies curl ca-certificates gnupg
34903487

3491-
# Install Node.js from NodeSource
34923488
install_packages_with_retry "nodejs" || {
34933489
msg_error "Failed to install Node.js ${NODE_VERSION} from NodeSource"
34943490
return 1
@@ -3639,7 +3635,7 @@ function setup_php() {
36393635
local CURRENT_PHP=""
36403636
CURRENT_PHP=$(is_tool_installed "php" 2>/dev/null) || true
36413637

3642-
# CRITICAL: If wrong version is installed, remove it FIRST before any pinning
3638+
# Remove conflicting PHP version before pinning
36433639
if [[ -n "$CURRENT_PHP" && "$CURRENT_PHP" != "$PHP_VERSION" ]]; then
36443640
msg_info "Removing conflicting PHP ${CURRENT_PHP} (need ${PHP_VERSION})"
36453641
stop_all_services "php.*-fpm"
@@ -3786,7 +3782,6 @@ EOF
37863782

37873783
local INSTALLED_VERSION=$(php -v 2>/dev/null | awk '/^PHP/{print $2}' | cut -d. -f1,2)
37883784

3789-
# Critical: if major.minor doesn't match, fail and cleanup
37903785
if [[ "$INSTALLED_VERSION" != "$PHP_VERSION" ]]; then
37913786
msg_error "PHP version mismatch: requested ${PHP_VERSION} but got ${INSTALLED_VERSION}"
37923787
msg_error "This indicates a critical package installation issue"

0 commit comments

Comments
 (0)