updated for static mapping

This commit is contained in:
admin 2026-01-30 13:06:05 -07:00
parent f1b1003677
commit 6a8c1b8a7a

View file

@ -1,37 +1,41 @@
#!/bin/bash #!/bin/bash
# --- 1. Setup --- # Move to the folder where this script lives
mkdir -p /docker/keepalived/config /docker/keepalived/checks cd "$(dirname "$0")"
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 --- # Load the variables Komodo just wrote
if [ ! -f "${REPO_BASE}/keepalived_header.tpl" ]; then if [ -f .env ]; then
echo "ERROR: Header template not found at ${REPO_BASE}" export $(grep -v '^#' .env | xargs)
else
echo "ERROR: .env file not found in $(pwd)"
exit 1 exit 1
fi fi
cp "${REPO_BASE}/keepalived_header.tpl" "$CONFIG_FILE" # Define paths relative to the current folder
CONFIG_DIR="./config"
CHECKS_DIR="./checks"
HEADER_TPL="./keepalived_header.tpl"
SLOT_TPL="./keepalived_slot.tpl"
CONFIG_FILE="${CONFIG_DIR}/keepalived.conf"
mkdir -p "$CONFIG_DIR" "$CHECKS_DIR"
# Build Header
cp "$HEADER_TPL" "$CONFIG_FILE"
sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$CONFIG_FILE" sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$CONFIG_FILE"
# --- 3. Parse "Hashtable" (SLOT_DEFINITIONS) --- # Parse Slots
# 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 while IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk
do do
# SANITY CHECKS:
# Skip if the line is empty
[[ -z "$s_num" ]] && continue [[ -z "$s_num" ]] && continue
# Skip if the line doesn't have enough columns (VIP is our 7th column) [[ -z "$s_vip" ]] && continue
[[ -z "$s_vip" ]] && { echo "Skipping malformed line: $s_num"; continue; }
echo "Processing Slot $s_num: $s_name" echo "Processing Slot $s_num: $s_name"
TEMP_SLOT="/tmp/slot_${s_num}.conf" TEMP_SLOT="/tmp/slot_${s_num}.conf"
cp "${REPO_BASE}/keepalived_slot.tpl" "$TEMP_SLOT" cp "$SLOT_TPL" "$TEMP_SLOT"
# Handle Password (8 char limit)
FULL_PASS="${BASE_PASSWORD}-${s_num}" FULL_PASS="${BASE_PASSWORD}-${s_num}"
SHORT_PASS="${FULL_PASS:0:8}" SHORT_PASS="${FULL_PASS:0:8}"
# Inject variables
sed -i "s/{{SLOT_NUM}}/${s_num}/g" "$TEMP_SLOT" sed -i "s/{{SLOT_NUM}}/${s_num}/g" "$TEMP_SLOT"
sed -i "s/{{LXC_NUM}}/${LXC_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_NAME}}/${s_name}/g" "$TEMP_SLOT"
@ -46,8 +50,6 @@ do
rm "$TEMP_SLOT" rm "$TEMP_SLOT"
done <<< "$SLOT_DEFINITIONS" done <<< "$SLOT_DEFINITIONS"
# --- 4. Finalize --- # Move check script to its final home
cp "${REPO_BASE}/check_services.sh" /docker/keepalived/checks/check_services.sh cp ./check_services.sh "${CHECKS_DIR}/check_services.sh"
chmod +x /docker/keepalived/checks/check_services.sh chmod +x "${CHECKS_DIR}/check_services.sh"
echo "Dynamic configuration generated for LXC