var refinement

This commit is contained in:
admin 2026-01-30 13:28:46 -07:00
parent b2c0eef337
commit 34a6e33cab

View file

@ -1,30 +1,40 @@
#!/bin/bash #!/bin/bash
# Move to the folder where this script lives cd /docker/keepalived
cd "$(dirname "$0")"
# Load the variables Komodo just wrote # 1. SOURCING
# Force export of all variables in .env so sed can access them
if [ -f .env ]; then if [ -f .env ]; then
export $(grep -v '^#' .env | xargs) echo "Sourcing variables from .env..."
set -a
source .env
set +a
else else
echo "ERROR: .env file not found in $(pwd)" echo "ERROR: .env file not found in $(pwd)"
exit 1 exit 1
fi fi
# Define paths relative to the current folder # 2. QUOTE CLEANING
CONFIG_DIR="/docker/keepalived/config" # Remove literal double quotes from the start/end of the string if they exist
CHECKS_DIR="/docker/keepalived/checks" SLOT_DEFINITIONS=$(echo "$SLOT_DEFINITIONS" | sed 's/^"//;s/"$//')
HEADER_TPL="/docker/keepalived/keepalived_header.tpl"
SLOT_TPL="/docker/keepalived/keepalived_slot.tpl" # 3. PATHS
CONFIG_DIR="./config"
CHECKS_DIR="./checks"
HEADER_TPL="./keepalived_header.tpl"
SLOT_TPL="./keepalived_slot.tpl"
CONFIG_FILE="${CONFIG_DIR}/keepalived.conf" CONFIG_FILE="${CONFIG_DIR}/keepalived.conf"
mkdir -p "$CONFIG_DIR" "$CHECKS_DIR" mkdir -p "$CONFIG_DIR" "$CHECKS_DIR"
# Build Header # 4. BUILD HEADER
if [ -z "$LXC_NUM" ]; then echo "ERROR: LXC_NUM not found in env"; exit 1; fi
cp "$HEADER_TPL" "$CONFIG_FILE" 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"
# Parse Slots # 5. PARSE SLOTS
while IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk # Using echo | while to ensure the cleaned string is parsed line-by-line
echo "$SLOT_DEFINITIONS" | while IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk
do do
[[ -z "$s_num" ]] && continue [[ -z "$s_num" ]] && continue
[[ -z "$s_vip" ]] && continue [[ -z "$s_vip" ]] && continue
@ -33,6 +43,7 @@ do
TEMP_SLOT="/tmp/slot_${s_num}.conf" TEMP_SLOT="/tmp/slot_${s_num}.conf"
cp "$SLOT_TPL" "$TEMP_SLOT" cp "$SLOT_TPL" "$TEMP_SLOT"
# Password logic (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}"
@ -48,8 +59,10 @@ do
cat "$TEMP_SLOT" >> "$CONFIG_FILE" cat "$TEMP_SLOT" >> "$CONFIG_FILE"
rm "$TEMP_SLOT" rm "$TEMP_SLOT"
done <<< "$SLOT_DEFINITIONS" done
# Move check script to its final home # 6. PERMS
cp ./check_services.sh "${CHECKS_DIR}/check_services.sh" cp ./check_services.sh "${CHECKS_DIR}/check_services.sh"
chmod +x "${CHECKS_DIR}/check_services.sh" chmod +x "${CHECKS_DIR}/check_services.sh"
echo "Success: keepalived.conf generated."