#!/bin/bash # --- 1. Setup --- mkdir -p /docker/keepalived/config /docker/keepalived/checks REPO_BASE="/etc/komodo/repos/mapletree-pve${PVE_NUM}lxc${LXC_NUM}/lxc${LXC_NUM}/keepalived" CONFIG_FILE="/docker/keepalived/config/keepalived.conf" # --- 2. Build Header --- if [ ! -f "${REPO_BASE}/keepalived_header.tpl" ]; then echo "ERROR: Header template not found at ${REPO_BASE}" exit 1 fi cp "${REPO_BASE}/keepalived_header.tpl" "$CONFIG_FILE" sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$CONFIG_FILE" # --- 3. Parse "Hashtable" (SLOT_DEFINITIONS) --- # Use a process substitution to avoid subshell variable scope issues while IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk do # SANITY CHECKS: # Skip if the line is empty [[ -z "$s_num" ]] && continue # Skip if the line doesn't have enough columns (VIP is our 7th column) [[ -z "$s_vip" ]] && { echo "Skipping malformed line: $s_num"; continue; } echo "Processing Slot $s_num: $s_name" TEMP_SLOT="/tmp/slot_${s_num}.conf" cp "${REPO_BASE}/keepalived_slot.tpl" "$TEMP_SLOT" # Handle Password (8 char limit) FULL_PASS="${BASE_PASSWORD}-${s_num}" SHORT_PASS="${FULL_PASS:0:8}" # Inject variables sed -i "s/{{SLOT_NUM}}/${s_num}/g" "$TEMP_SLOT" sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$TEMP_SLOT" sed -i "s/{{SVC_NAME}}/${s_name}/g" "$TEMP_SLOT" sed -i "s/{{SVC_DEP}}/${s_dep}/g" "$TEMP_SLOT" sed -i "s/{{STATE}}/${s_state}/g" "$TEMP_SLOT" sed -i "s/{{PRIORITY}}/${s_prio}/g" "$TEMP_SLOT" sed -i "s/{{PEER}}/${s_peer}/g" "$TEMP_SLOT" sed -i "s/{{SVC_VIP}}/${s_vip}/g" "$TEMP_SLOT" sed -i "s/{{AUTH_PASS}}/${SHORT_PASS}/g" "$TEMP_SLOT" cat "$TEMP_SLOT" >> "$CONFIG_FILE" rm "$TEMP_SLOT" done <<< "$SLOT_DEFINITIONS" # --- 4. Finalize --- cp "${REPO_BASE}/check_services.sh" /docker/keepalived/checks/check_services.sh chmod +x /docker/keepalived/checks/check_services.sh echo "Dynamic configuration generated for LXC