mapletree/lxc1/p0-infrastructure/keepalived/deploy_keepalived.sh
2026-02-02 08:57:04 -07:00

64 lines
No EOL
2.1 KiB
Bash

#!/bin/bash
# 1. ROBUST ENV SOURCING (Strips quotes and exports vars)
ENV_PATH="/docker/keepalived/.env"
if [ -f "$ENV_PATH" ]; then
while read -r line || [ -n "$line" ]; do
# Skip comments and empty lines
[[ "$line" =~ ^#.*$ ]] && continue
[[ -z "$line" ]] && continue
# Export the variable, removing literal double quotes
export "${line//\"/}"
done < "$ENV_PATH"
echo "Successfully loaded environment from $ENV_PATH"
else
echo "ERROR: .env file not found!"
exit 1
fi
# 2. PATHS & HEADER
CONFIG_FILE="./config/keepalived.conf"
mkdir -p ./config ./checks
cp "./keepalived_header.tpl" "$CONFIG_FILE"
# Replace global header vars
sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$CONFIG_FILE"
# 3. DUAL-STAGE PARSING
# Split the string into individual slots using the semicolon
IFS=';' read -ra SLOTS <<< "$SLOT_DEFINITIONS"
for SLOT in "${SLOTS[@]}"; do
# Split the individual slot by commas
IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk <<< "$SLOT"
[[ -z "$s_num" ]] && continue
echo "Processing Slot $s_num: $s_name (VIP: $s_vip)"
TEMP_SLOT="/tmp/slot_${s_num}.conf"
cp "./keepalived_slot.tpl" "$TEMP_SLOT"
# Generate unique auth pass from the base password
AUTH_PASS="${BASE_PASSWORD:0:7}-${s_num}"
# Perform replacements
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}}/${AUTH_PASS}/g" "$TEMP_SLOT"
# Append to main config and cleanup temp
cat "$TEMP_SLOT" >> "$CONFIG_FILE"
rm "$TEMP_SLOT"
done
# 4. FINALIZE SCRIPTS
cp ./check_services.sh ./checks/check_services.sh
chmod +x ./checks/check_services.sh
echo "Keepalived configuration generated successfully."